blob: 80eaa2c471c8539e4992a675394a0db3d52f1473 [file] [log] [blame]
/*
* OpenVPN -- An application to securely tunnel IP networks
* over a single TCP/UDP port, with support for SSL/TLS-based
* session authentication and key exchange,
* packet encryption, packet authentication, and
* packet compression.
*
* Copyright (C) 2002-2018 OpenVPN Inc <sales@openvpn.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/*
* Support routines for configuring and accessing TUN/TAP
* virtual network adapters.
*
* This file is based on the TUN/TAP driver interface routines
* from VTun by Maxim Krasnyansky <max_mk@yahoo.com>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#elif defined(_MSC_VER)
#include "config-msvc.h"
#endif
#include "syshead.h"
#include "tun.h"
#include "fdmisc.h"
#include "common.h"
#include "misc.h"
#include "socket.h"
#include "manage.h"
#include "route.h"
#include "win32.h"
#include "block_dns.h"
#include "memdbg.h"
#ifdef _WIN32
#include "openvpn-msg.h"
#endif
#include <string.h>
#ifdef _WIN32
/* #define SIMULATE_DHCP_FAILED */ /* simulate bad DHCP negotiation */
#define NI_TEST_FIRST (1<<0)
#define NI_IP_NETMASK (1<<1)
#define NI_OPTIONS (1<<2)
static void netsh_ifconfig(const struct tuntap_options *to,
const char *flex_name,
const in_addr_t ip,
const in_addr_t netmask,
const unsigned int flags);
static void netsh_set_dns6_servers(const struct in6_addr *addr_list,
const int addr_len,
const char *flex_name);
static void netsh_command(const struct argv *a, int n, int msglevel);
static const char *netsh_get_id(const char *dev_node, struct gc_arena *gc);
static DWORD get_adapter_index_flexible(const char *name);
static bool
do_address_service(const bool add, const short family, const struct tuntap *tt)
{
DWORD len;
bool ret = false;
ack_message_t ack;
struct gc_arena gc = gc_new();
HANDLE pipe = tt->options.msg_channel;
address_message_t addr = {
.header = {
(add ? msg_add_address : msg_del_address),
sizeof(address_message_t),
0
},
.family = family,
.iface = { .index = tt->adapter_index, .name = "" }
};
if (addr.iface.index == TUN_ADAPTER_INDEX_INVALID)
{
strncpy(addr.iface.name, tt->actual_name, sizeof(addr.iface.name));
addr.iface.name[sizeof(addr.iface.name) - 1] = '\0';
}
if (addr.family == AF_INET)
{
addr.address.ipv4.s_addr = tt->local;
addr.prefix_len = 32;
}
else
{
addr.address.ipv6 = tt->local_ipv6;
addr.prefix_len = tt->netbits_ipv6;
}
if (!WriteFile(pipe, &addr, sizeof(addr), &len, NULL)
|| !ReadFile(pipe, &ack, sizeof(ack), &len, NULL))
{
msg(M_WARN, "TUN: could not talk to service: %s [%lu]",
strerror_win32(GetLastError(), &gc), GetLastError());
goto out;
}
if (ack.error_number != NO_ERROR)
{
msg(M_WARN, "TUN: %s address failed using service: %s [status=%u if_index=%d]",
(add ? "adding" : "deleting"), strerror_win32(ack.error_number, &gc),
ack.error_number, addr.iface.index);
goto out;
}
ret = true;
out:
gc_free(&gc);
return ret;
}
static bool
do_dns6_service(bool add, const struct tuntap *tt)
{
DWORD len;
bool ret = false;
ack_message_t ack;
struct gc_arena gc = gc_new();
HANDLE pipe = tt->options.msg_channel;
int addr_len = add ? tt->options.dns6_len : 0;
if (addr_len == 0 && add) /* no addresses to add */
{
return true;
}
dns_cfg_message_t dns = {
.header = {
(add ? msg_add_dns_cfg : msg_del_dns_cfg),
sizeof(dns_cfg_message_t),
0
},
.iface = { .index = tt->adapter_index, .name = "" },
.domains = "",
.family = AF_INET6,
.addr_len = addr_len
};
/* interface name is required */
strncpy(dns.iface.name, tt->actual_name, sizeof(dns.iface.name));
dns.iface.name[sizeof(dns.iface.name) - 1] = '\0';
if (addr_len > _countof(dns.addr))
{
addr_len = _countof(dns.addr);
dns.addr_len = addr_len;
msg(M_WARN, "Number of IPv6 DNS addresses sent to service truncated to %d",
addr_len);
}
for (int i = 0; i < addr_len; ++i)
{
dns.addr[i].ipv6 = tt->options.dns6[i];
}
msg(D_LOW, "%s IPv6 dns servers on '%s' (if_index = %d) using service",
(add ? "Setting" : "Deleting"), dns.iface.name, dns.iface.index);
if (!WriteFile(pipe, &dns, sizeof(dns), &len, NULL)
|| !ReadFile(pipe, &ack, sizeof(ack), &len, NULL))
{
msg(M_WARN, "TUN: could not talk to service: %s [%lu]",
strerror_win32(GetLastError(), &gc), GetLastError());
goto out;
}
if (ack.error_number != NO_ERROR)
{
msg(M_WARN, "TUN: %s IPv6 dns failed using service: %s [status=%u if_name=%s]",
(add ? "adding" : "deleting"), strerror_win32(ack.error_number, &gc),
ack.error_number, dns.iface.name);
goto out;
}
msg(M_INFO, "IPv6 dns servers %s using service", (add ? "set" : "deleted"));
ret = true;
out:
gc_free(&gc);
return ret;
}
#endif /* ifdef _WIN32 */
#ifdef TARGET_SOLARIS
static void solaris_error_close(struct tuntap *tt, const struct env_set *es, const char *actual, bool unplumb_inet6);
#include <stropts.h>
#endif
#if defined(TARGET_DARWIN) && HAVE_NET_IF_UTUN_H
#include <sys/kern_control.h>
#include <net/if_utun.h>
#include <sys/sys_domain.h>
#endif
static void clear_tuntap(struct tuntap *tuntap);
bool
is_dev_type(const char *dev, const char *dev_type, const char *match_type)
{
ASSERT(match_type);
if (!dev)
{
return false;
}
if (dev_type)
{
return !strcmp(dev_type, match_type);
}
else
{
return !strncmp(dev, match_type, strlen(match_type));
}
}
int
dev_type_enum(const char *dev, const char *dev_type)
{
if (is_dev_type(dev, dev_type, "tun"))
{
return DEV_TYPE_TUN;
}
else if (is_dev_type(dev, dev_type, "tap"))
{
return DEV_TYPE_TAP;
}
else if (is_dev_type(dev, dev_type, "null"))
{
return DEV_TYPE_NULL;
}
else
{
return DEV_TYPE_UNDEF;
}
}
const char *
dev_type_string(const char *dev, const char *dev_type)
{
switch (dev_type_enum(dev, dev_type))
{
case DEV_TYPE_TUN:
return "tun";
case DEV_TYPE_TAP:
return "tap";
case DEV_TYPE_NULL:
return "null";
default:
return "[unknown-dev-type]";
}
}
/*
* Try to predict the actual TUN/TAP device instance name,
* before the device is actually opened.
*/
const char *
guess_tuntap_dev(const char *dev,
const char *dev_type,
const char *dev_node,
struct gc_arena *gc)
{
#ifdef _WIN32
const int dt = dev_type_enum(dev, dev_type);
if (dt == DEV_TYPE_TUN || dt == DEV_TYPE_TAP)
{
return netsh_get_id(dev_node, gc);
}
#endif
/* default case */
return dev;
}
/* --ifconfig-nowarn disables some options sanity checking */
static const char ifconfig_warn_how_to_silence[] = "(silence this warning with --ifconfig-nowarn)";
/*
* If !tun, make sure ifconfig_remote_netmask looks
* like a netmask.
*
* If tun, make sure ifconfig_remote_netmask looks
* like an IPv4 address.
*/
static void
ifconfig_sanity_check(bool tun, in_addr_t addr, int topology)
{
struct gc_arena gc = gc_new();
const bool looks_like_netmask = ((addr & 0xFF000000) == 0xFF000000);
if (tun)
{
if (looks_like_netmask && (topology == TOP_NET30 || topology == TOP_P2P))
{
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",
print_in_addr_t(addr, 0, &gc),
ifconfig_warn_how_to_silence);
}
}
else /* tap */
{
if (!looks_like_netmask)
{
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",
ifconfig_warn_how_to_silence);
}
}
gc_free(&gc);
}
/*
* For TAP-style devices, generate a broadcast address.
*/
static in_addr_t
generate_ifconfig_broadcast_addr(in_addr_t local,
in_addr_t netmask)
{
return local | ~netmask;
}
/*
* Check that --local and --remote addresses do not
* clash with ifconfig addresses or subnet.
*/
static void
check_addr_clash(const char *name,
int type,
in_addr_t public,
in_addr_t local,
in_addr_t remote_netmask)
{
struct gc_arena gc = gc_new();
#if 0
msg(M_INFO, "CHECK_ADDR_CLASH type=%d public=%s local=%s, remote_netmask=%s",
type,
print_in_addr_t(public, 0, &gc),
print_in_addr_t(local, 0, &gc),
print_in_addr_t(remote_netmask, 0, &gc));
#endif
if (public)
{
if (type == DEV_TYPE_TUN)
{
const in_addr_t test_netmask = 0xFFFFFF00;
const in_addr_t public_net = public & test_netmask;
const in_addr_t local_net = local & test_netmask;
const in_addr_t remote_net = remote_netmask & test_netmask;
if (public == local || public == remote_netmask)
{
msg(M_WARN,
"WARNING: --%s address [%s] conflicts with --ifconfig address pair [%s, %s]. %s",
name,
print_in_addr_t(public, 0, &gc),
print_in_addr_t(local, 0, &gc),
print_in_addr_t(remote_netmask, 0, &gc),
ifconfig_warn_how_to_silence);
}
if (public_net == local_net || public_net == remote_net)
{
msg(M_WARN,
"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",
name,
print_in_addr_t(public, 0, &gc),
print_in_addr_t(local, 0, &gc),
print_in_addr_t(remote_netmask, 0, &gc),
ifconfig_warn_how_to_silence);
}
}
else if (type == DEV_TYPE_TAP)
{
const in_addr_t public_network = public & remote_netmask;
const in_addr_t virtual_network = local & remote_netmask;
if (public_network == virtual_network)
{
msg(M_WARN,
"WARNING: --%s address [%s] conflicts with --ifconfig subnet [%s, %s] -- local and remote addresses cannot be inside of the --ifconfig subnet. %s",
name,
print_in_addr_t(public, 0, &gc),
print_in_addr_t(local, 0, &gc),
print_in_addr_t(remote_netmask, 0, &gc),
ifconfig_warn_how_to_silence);
}
}
}
gc_free(&gc);
}
/*
* Issue a warning if ip/netmask (on the virtual IP network) conflicts with
* the settings on the local LAN. This is designed to flag issues where
* (for example) the OpenVPN server LAN is running on 192.168.1.x, but then
* an OpenVPN client tries to connect from a public location that is also running
* off of a router set to 192.168.1.x.
*/
void
check_subnet_conflict(const in_addr_t ip,
const in_addr_t netmask,
const char *prefix)
{
#if 0 /* too many false positives */
struct gc_arena gc = gc_new();
in_addr_t lan_gw = 0;
in_addr_t lan_netmask = 0;
if (get_default_gateway(&lan_gw, &lan_netmask) && lan_netmask)
{
const in_addr_t lan_network = lan_gw & lan_netmask;
const in_addr_t network = ip & netmask;
/* do the two subnets defined by network/netmask and lan_network/lan_netmask intersect? */
if ((network & lan_netmask) == lan_network
|| (lan_network & netmask) == network)
{
msg(M_WARN, "WARNING: potential %s subnet conflict between local LAN [%s/%s] and remote VPN [%s/%s]",
prefix,
print_in_addr_t(lan_network, 0, &gc),
print_in_addr_t(lan_netmask, 0, &gc),
print_in_addr_t(network, 0, &gc),
print_in_addr_t(netmask, 0, &gc));
}
}
gc_free(&gc);
#endif /* if 0 */
}
void
warn_on_use_of_common_subnets(void)
{
struct gc_arena gc = gc_new();
struct route_gateway_info rgi;
const int needed = (RGI_ADDR_DEFINED|RGI_NETMASK_DEFINED);
get_default_gateway(&rgi);
if ((rgi.flags & needed) == needed)
{
const in_addr_t lan_network = rgi.gateway.addr & rgi.gateway.netmask;
if (lan_network == 0xC0A80000 || lan_network == 0xC0A80100)
{
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.");
}
}
gc_free(&gc);
}
/*
* Return a string to be used for options compatibility check
* between peers.
*/
const char *
ifconfig_options_string(const struct tuntap *tt, bool remote, bool disable, struct gc_arena *gc)
{
struct buffer out = alloc_buf_gc(256, gc);
if (tt->did_ifconfig_setup && !disable)
{
if (tt->type == DEV_TYPE_TAP || (tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET))
{
buf_printf(&out, "%s %s",
print_in_addr_t(tt->local & tt->remote_netmask, 0, gc),
print_in_addr_t(tt->remote_netmask, 0, gc));
}
else if (tt->type == DEV_TYPE_TUN)
{
const char *l, *r;
if (remote)
{
r = print_in_addr_t(tt->local, 0, gc);
l = print_in_addr_t(tt->remote_netmask, 0, gc);
}
else
{
l = print_in_addr_t(tt->local, 0, gc);
r = print_in_addr_t(tt->remote_netmask, 0, gc);
}
buf_printf(&out, "%s %s", r, l);
}
else
{
buf_printf(&out, "[undef]");
}
}
return BSTR(&out);
}
/*
* Return a status string describing wait state.
*/
const char *
tun_stat(const struct tuntap *tt, unsigned int rwflags, struct gc_arena *gc)
{
struct buffer out = alloc_buf_gc(64, gc);
if (tt)
{
if (rwflags & EVENT_READ)
{
buf_printf(&out, "T%s",
(tt->rwflags_debug & EVENT_READ) ? "R" : "r");
#ifdef _WIN32
buf_printf(&out, "%s",
overlapped_io_state_ascii(&tt->reads));
#endif
}
if (rwflags & EVENT_WRITE)
{
buf_printf(&out, "T%s",
(tt->rwflags_debug & EVENT_WRITE) ? "W" : "w");
#ifdef _WIN32
buf_printf(&out, "%s",
overlapped_io_state_ascii(&tt->writes));
#endif
}
}
else
{
buf_printf(&out, "T?");
}
return BSTR(&out);
}
/*
* Return true for point-to-point topology, false for subnet topology
*/
bool
is_tun_p2p(const struct tuntap *tt)
{
bool tun = false;
if (tt->type == DEV_TYPE_TAP
|| (tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET)
|| tt->type == DEV_TYPE_NULL )
{
tun = false;
}
else if (tt->type == DEV_TYPE_TUN)
{
tun = true;
}
else
{
msg(M_FATAL, "Error: problem with tun vs. tap setting"); /* JYFIXME -- needs to be caught earlier, in init_tun? */
}
return tun;
}
/*
* Set the ifconfig_* environment variables, both for IPv4 and IPv6
*/
void
do_ifconfig_setenv(const struct tuntap *tt, struct env_set *es)
{
struct gc_arena gc = gc_new();
const char *ifconfig_local = print_in_addr_t(tt->local, 0, &gc);
const char *ifconfig_remote_netmask = print_in_addr_t(tt->remote_netmask, 0, &gc);
/*
* Set environmental variables with ifconfig parameters.
*/
if (tt->did_ifconfig_setup)
{
bool tun = is_tun_p2p(tt);
setenv_str(es, "ifconfig_local", ifconfig_local);
if (tun)
{
setenv_str(es, "ifconfig_remote", ifconfig_remote_netmask);
}
else
{
const char *ifconfig_broadcast = print_in_addr_t(tt->broadcast, 0, &gc);
setenv_str(es, "ifconfig_netmask", ifconfig_remote_netmask);
setenv_str(es, "ifconfig_broadcast", ifconfig_broadcast);
}
}
if (tt->did_ifconfig_ipv6_setup)
{
const char *ifconfig_ipv6_local = print_in6_addr(tt->local_ipv6, 0, &gc);
const char *ifconfig_ipv6_remote = print_in6_addr(tt->remote_ipv6, 0, &gc);
setenv_str(es, "ifconfig_ipv6_local", ifconfig_ipv6_local);
setenv_int(es, "ifconfig_ipv6_netbits", tt->netbits_ipv6);
setenv_str(es, "ifconfig_ipv6_remote", ifconfig_ipv6_remote);
}
gc_free(&gc);
}
/*
* Init tun/tap object.
*
* Set up tuntap structure for ifconfig,
* but don't execute yet.
*/
struct tuntap *
init_tun(const char *dev, /* --dev option */
const char *dev_type, /* --dev-type option */
int topology, /* one of the TOP_x values */
const char *ifconfig_local_parm, /* --ifconfig parm 1 */
const char *ifconfig_remote_netmask_parm, /* --ifconfig parm 2 */
const char *ifconfig_ipv6_local_parm, /* --ifconfig parm 1 IPv6 */
int ifconfig_ipv6_netbits_parm,
const char *ifconfig_ipv6_remote_parm, /* --ifconfig parm 2 IPv6 */
struct addrinfo *local_public,
struct addrinfo *remote_public,
const bool strict_warn,
struct env_set *es)
{
struct gc_arena gc = gc_new();
struct tuntap *tt;
ALLOC_OBJ(tt, struct tuntap);
clear_tuntap(tt);
tt->type = dev_type_enum(dev, dev_type);
tt->topology = topology;
if (ifconfig_local_parm && ifconfig_remote_netmask_parm)
{
bool tun = false;
/*
* We only handle TUN/TAP devices here, not --dev null devices.
*/
tun = is_tun_p2p(tt);
/*
* Convert arguments to binary IPv4 addresses.
*/
tt->local = getaddr(
GETADDR_RESOLVE
| GETADDR_HOST_ORDER
| GETADDR_FATAL_ON_SIGNAL
| GETADDR_FATAL,
ifconfig_local_parm,
0,
NULL,
NULL);
tt->remote_netmask = getaddr(
(tun ? GETADDR_RESOLVE : 0)
| GETADDR_HOST_ORDER
| GETADDR_FATAL_ON_SIGNAL
| GETADDR_FATAL,
ifconfig_remote_netmask_parm,
0,
NULL,
NULL);
/*
* Look for common errors in --ifconfig parms
*/
if (strict_warn)
{
struct addrinfo *curele;
ifconfig_sanity_check(tt->type == DEV_TYPE_TUN, tt->remote_netmask, tt->topology);
/*
* If local_public or remote_public addresses are defined,
* make sure they do not clash with our virtual subnet.
*/
for (curele = local_public; curele; curele = curele->ai_next)
{
if (curele->ai_family == AF_INET)
{
check_addr_clash("local",
tt->type,
((struct sockaddr_in *)curele->ai_addr)->sin_addr.s_addr,
tt->local,
tt->remote_netmask);
}
}
for (curele = remote_public; curele; curele = curele->ai_next)
{
if (curele->ai_family == AF_INET)
{
check_addr_clash("remote",
tt->type,
((struct sockaddr_in *)curele->ai_addr)->sin_addr.s_addr,
tt->local,
tt->remote_netmask);
}
}
if (tt->type == DEV_TYPE_TAP || (tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET))
{
check_subnet_conflict(tt->local, tt->remote_netmask, "TUN/TAP adapter");
}
else if (tt->type == DEV_TYPE_TUN)
{
check_subnet_conflict(tt->local, IPV4_NETMASK_HOST, "TUN/TAP adapter");
}
}
/*
* If TAP-style interface, generate broadcast address.
*/
if (!tun)
{
tt->broadcast = generate_ifconfig_broadcast_addr(tt->local, tt->remote_netmask);
}
#ifdef _WIN32
/*
* Make sure that both ifconfig addresses are part of the
* same .252 subnet.
*/
if (tun)
{
verify_255_255_255_252(tt->local, tt->remote_netmask);
tt->adapter_netmask = ~3;
}
else
{
tt->adapter_netmask = tt->remote_netmask;
}
#endif
tt->did_ifconfig_setup = true;
}
if (ifconfig_ipv6_local_parm && ifconfig_ipv6_remote_parm)
{
/*
* Convert arguments to binary IPv6 addresses.
*/
if (inet_pton( AF_INET6, ifconfig_ipv6_local_parm, &tt->local_ipv6 ) != 1
|| inet_pton( AF_INET6, ifconfig_ipv6_remote_parm, &tt->remote_ipv6 ) != 1)
{
msg( M_FATAL, "init_tun: problem converting IPv6 ifconfig addresses %s and %s to binary", ifconfig_ipv6_local_parm, ifconfig_ipv6_remote_parm );
}
tt->netbits_ipv6 = ifconfig_ipv6_netbits_parm;
tt->did_ifconfig_ipv6_setup = true;
}
/*
* Set environmental variables with ifconfig parameters.
*/
if (es)
{
do_ifconfig_setenv(tt, es);
}
gc_free(&gc);
return tt;
}
/*
* Platform specific tun initializations
*/
void
init_tun_post(struct tuntap *tt,
const struct frame *frame,
const struct tuntap_options *options)
{
tt->options = *options;
#ifdef _WIN32
overlapped_io_init(&tt->reads, frame, FALSE, true);
overlapped_io_init(&tt->writes, frame, TRUE, true);
tt->rw_handle.read = tt->reads.overlapped.hEvent;
tt->rw_handle.write = tt->writes.overlapped.hEvent;
tt->adapter_index = TUN_ADAPTER_INDEX_INVALID;
#endif
}
#if defined(_WIN32) \
|| defined(TARGET_DARWIN) || defined(TARGET_NETBSD) || defined(TARGET_OPENBSD)
/* some of the platforms will auto-add a "network route" pointing
* to the interface on "ifconfig tunX 2001:db8::1/64", others need
* an extra call to "route add..."
* -> helper function to simplify code below
*/
void
add_route_connected_v6_net(struct tuntap *tt,
const struct env_set *es)
{
struct route_ipv6 r6;
CLEAR(r6);
r6.network = tt->local_ipv6;
r6.netbits = tt->netbits_ipv6;
r6.gateway = tt->local_ipv6;
r6.metric = 0; /* connected route */
r6.flags = RT_DEFINED | RT_METRIC_DEFINED;
add_route_ipv6(&r6, tt, 0, es);
}
void
delete_route_connected_v6_net(struct tuntap *tt,
const struct env_set *es)
{
struct route_ipv6 r6;
CLEAR(r6);
r6.network = tt->local_ipv6;
r6.netbits = tt->netbits_ipv6;
r6.gateway = tt->local_ipv6;
r6.metric = 0; /* connected route */
r6.flags = RT_DEFINED | RT_ADDED | RT_METRIC_DEFINED;
route_ipv6_clear_host_bits(&r6);
delete_route_ipv6(&r6, tt, 0, es);
}
#endif /* if defined(_WIN32) || defined(TARGET_DARWIN) || defined(TARGET_NETBSD) || defined(TARGET_OPENBSD) */
#if defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) \
|| defined(TARGET_NETBSD) || defined(TARGET_OPENBSD)
/* we can't use true subnet mode on tun on all platforms, as that
* conflicts with IPv6 (wants to use ND then, which we don't do),
* but the OSes want "a remote address that is different from ours"
* - so we construct one, normally the first in the subnet, but if
* this is the same as ours, use the second one.
* The actual address does not matter at all, as the tun interface
* is still point to point and no layer 2 resolution is done...
*/
in_addr_t
create_arbitrary_remote( struct tuntap *tt )
{
in_addr_t remote;
remote = (tt->local & tt->remote_netmask) +1;
if (remote == tt->local)
{
remote++;
}
return remote;
}
#endif
/* execute the ifconfig command through the shell */
void
do_ifconfig(struct tuntap *tt,
const char *actual, /* actual device name */
int tun_mtu,
const struct env_set *es)
{
struct gc_arena gc = gc_new();
if (tt->did_ifconfig_setup)
{
bool tun = false;
const char *ifconfig_local = NULL;
const char *ifconfig_remote_netmask = NULL;
const char *ifconfig_broadcast = NULL;
const char *ifconfig_ipv6_local = NULL;
bool do_ipv6 = false;
struct argv argv = argv_new();
msg( D_LOW, "do_ifconfig, tt->did_ifconfig_ipv6_setup=%d",
tt->did_ifconfig_ipv6_setup );
/*
* We only handle TUN/TAP devices here, not --dev null devices.
*/
tun = is_tun_p2p(tt);
/*
* Set ifconfig parameters
*/
ifconfig_local = print_in_addr_t(tt->local, 0, &gc);
ifconfig_remote_netmask = print_in_addr_t(tt->remote_netmask, 0, &gc);
if (tt->did_ifconfig_ipv6_setup)
{
ifconfig_ipv6_local = print_in6_addr(tt->local_ipv6, 0, &gc);
do_ipv6 = true;
}
/*
* If TAP-style device, generate broadcast address.
*/
if (!tun)
{
ifconfig_broadcast = print_in_addr_t(tt->broadcast, 0, &gc);
}
#ifdef ENABLE_MANAGEMENT
if (management)
{
management_set_state(management,
OPENVPN_STATE_ASSIGN_IP,
NULL,
&tt->local,
&tt->local_ipv6,
NULL,
NULL);
}
#endif
#if defined(TARGET_LINUX)
#ifdef ENABLE_IPROUTE
/*
* Set the MTU for the device
*/
argv_printf(&argv,
"%s link set dev %s up mtu %d",
iproute_path,
actual,
tun_mtu
);
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, es, S_FATAL, "Linux ip link set failed");
if (tun)
{
/*
* Set the address for the device
*/
argv_printf(&argv,
"%s addr add dev %s local %s peer %s",
iproute_path,
actual,
ifconfig_local,
ifconfig_remote_netmask
);
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, es, S_FATAL, "Linux ip addr add failed");
}
else
{
argv_printf(&argv,
"%s addr add dev %s %s/%d broadcast %s",
iproute_path,
actual,
ifconfig_local,
netmask_to_netbits2(tt->remote_netmask),
ifconfig_broadcast
);
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, es, S_FATAL, "Linux ip addr add failed");
}
if (do_ipv6)
{
argv_printf( &argv,
"%s -6 addr add %s/%d dev %s",
iproute_path,
ifconfig_ipv6_local,
tt->netbits_ipv6,
actual
);
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, es, S_FATAL, "Linux ip -6 addr add failed");
}
tt->did_ifconfig = true;
#else /* ifdef ENABLE_IPROUTE */
if (tun)
{
argv_printf(&argv,
"%s %s %s pointopoint %s mtu %d",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu
);
}
else
{
argv_printf(&argv,
"%s %s %s netmask %s mtu %d broadcast %s",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu,
ifconfig_broadcast
);
}
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, es, S_FATAL, "Linux ifconfig failed");
if (do_ipv6)
{
argv_printf(&argv,
"%s %s add %s/%d",
IFCONFIG_PATH,
actual,
ifconfig_ipv6_local,
tt->netbits_ipv6
);
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, es, S_FATAL, "Linux ifconfig inet6 failed");
}
tt->did_ifconfig = true;
#endif /*ENABLE_IPROUTE*/
#elif defined(TARGET_ANDROID)
if (do_ipv6)
{
struct buffer out6 = alloc_buf_gc(64, &gc);
buf_printf(&out6, "%s/%d", ifconfig_ipv6_local,tt->netbits_ipv6);
management_android_control(management, "IFCONFIG6",buf_bptr(&out6));
}
struct buffer out = alloc_buf_gc(64, &gc);
char *top;
switch (tt->topology)
{
case TOP_NET30:
top = "net30";
break;
case TOP_P2P:
top = "p2p";
break;
case TOP_SUBNET:
top = "subnet";
break;
default:
top = "undef";
}
buf_printf(&out, "%s %s %d %s", ifconfig_local, ifconfig_remote_netmask, tun_mtu, top);
management_android_control(management, "IFCONFIG", buf_bptr(&out));
#elif defined(TARGET_SOLARIS)
/* Solaris 2.6 (and 7?) cannot set all parameters in one go...
* example:
* ifconfig tun2 10.2.0.2 10.2.0.1 mtu 1450 up
* ifconfig tun2 netmask 255.255.255.255
*/
if (tun)
{
argv_printf(&argv,
"%s %s %s %s mtu %d up",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu
);
argv_msg(M_INFO, &argv);
if (!openvpn_execve_check(&argv, es, 0, "Solaris ifconfig phase-1 failed"))
{
solaris_error_close(tt, es, actual, false);
}
argv_printf(&argv,
"%s %s netmask 255.255.255.255",
IFCONFIG_PATH,
actual
);
}
else if (tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET)
{
argv_printf(&argv,
"%s %s %s %s netmask %s mtu %d up",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu
);
}
else
{
argv_printf(&argv,
" %s %s %s netmask %s broadcast + up",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask
);
}
argv_msg(M_INFO, &argv);
if (!openvpn_execve_check(&argv, es, 0, "Solaris ifconfig phase-2 failed"))
{
solaris_error_close(tt, es, actual, false);
}
if (do_ipv6)
{
argv_printf(&argv, "%s %s inet6 unplumb",
IFCONFIG_PATH, actual );
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, es, 0, NULL);
if (tt->type == DEV_TYPE_TUN)
{
const char *ifconfig_ipv6_remote =
print_in6_addr(tt->remote_ipv6, 0, &gc);
argv_printf(&argv,
"%s %s inet6 plumb %s/%d %s up",
IFCONFIG_PATH,
actual,
ifconfig_ipv6_local,
tt->netbits_ipv6,
ifconfig_ipv6_remote
);
}
else /* tap mode */
{
/* base IPv6 tap interface needs to be brought up first
*/
argv_printf(&argv, "%s %s inet6 plumb up",
IFCONFIG_PATH, actual );
argv_msg(M_INFO, &argv);
if (!openvpn_execve_check(&argv, es, 0, "Solaris ifconfig IPv6 (prepare) failed"))
{
solaris_error_close(tt, es, actual, true);
}
/* we might need to do "ifconfig %s inet6 auto-dhcp drop"
* after the system has noticed the interface and fired up
* the DHCPv6 client - but this takes quite a while, and the
* server will ignore the DHCPv6 packets anyway. So we don't.
*/
/* static IPv6 addresses need to go to a subinterface (tap0:1)
*/
argv_printf(&argv,
"%s %s inet6 addif %s/%d up",
IFCONFIG_PATH, actual,
ifconfig_ipv6_local, tt->netbits_ipv6 );
}
argv_msg(M_INFO, &argv);
if (!openvpn_execve_check(&argv, es, 0, "Solaris ifconfig IPv6 failed"))
{
solaris_error_close(tt, es, actual, true);
}
}
if (!tun && tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET)
{
/* Add a network route for the local tun interface */
struct route_ipv4 r;
CLEAR(r);
r.flags = RT_DEFINED | RT_METRIC_DEFINED;
r.network = tt->local & tt->remote_netmask;
r.netmask = tt->remote_netmask;
r.gateway = tt->local;
r.metric = 0;
add_route(&r, tt, 0, NULL, es);
}
tt->did_ifconfig = true;
#elif defined(TARGET_OPENBSD)
in_addr_t remote_end; /* for "virtual" subnet topology */
/*
* On OpenBSD, tun interfaces are persistent if created with
* "ifconfig tunX create", and auto-destroyed if created by
* opening "/dev/tunX" (so we just use the /dev/tunX)
*/
/* example: ifconfig tun2 10.2.0.2 10.2.0.1 mtu 1450 netmask 255.255.255.255 up */
if (tun)
{
argv_printf(&argv,
"%s %s %s %s mtu %d netmask 255.255.255.255 up -link0",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu
);
}
else if (tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET)
{
remote_end = create_arbitrary_remote( tt );
argv_printf(&argv,
"%s %s %s %s mtu %d netmask %s up -link0",
IFCONFIG_PATH,
actual,
ifconfig_local,
print_in_addr_t(remote_end, 0, &gc),
tun_mtu,
ifconfig_remote_netmask
);
}
else
{
argv_printf(&argv,
"%s %s %s netmask %s mtu %d broadcast %s link0",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu,
ifconfig_broadcast
);
}
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, es, S_FATAL, "OpenBSD ifconfig failed");
/* Add a network route for the local tun interface */
if (!tun && tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET)
{
struct route_ipv4 r;
CLEAR(r);
r.flags = RT_DEFINED;
r.network = tt->local & tt->remote_netmask;
r.netmask = tt->remote_netmask;
r.gateway = remote_end;
add_route(&r, tt, 0, NULL, es);
}
if (do_ipv6)
{
argv_printf(&argv,
"%s %s inet6 %s/%d",
IFCONFIG_PATH,
actual,
ifconfig_ipv6_local,
tt->netbits_ipv6
);
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, es, S_FATAL, "OpenBSD ifconfig inet6 failed");
/* and, hooray, we explicitely need to add a route... */
add_route_connected_v6_net(tt, es);
}
tt->did_ifconfig = true;
#elif defined(TARGET_NETBSD)
in_addr_t remote_end; /* for "virtual" subnet topology */
if (tun)
{
argv_printf(&argv,
"%s %s %s %s mtu %d netmask 255.255.255.255 up",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu
);
}
else if (tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET)
{
remote_end = create_arbitrary_remote( tt );
argv_printf(&argv,
"%s %s %s %s mtu %d netmask %s up",
IFCONFIG_PATH,
actual,
ifconfig_local,
print_in_addr_t(remote_end, 0, &gc),
tun_mtu,
ifconfig_remote_netmask
);
}
else
{
/*
* NetBSD has distinct tun and tap devices
* so we don't need the "link0" extra parameter to specify we want to do
* tunneling at the ethernet level
*/
argv_printf(&argv,
"%s %s %s netmask %s mtu %d broadcast %s",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu,
ifconfig_broadcast
);
}
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, es, S_FATAL, "NetBSD ifconfig failed");
/* Add a network route for the local tun interface */
if (!tun && tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET)
{
struct route_ipv4 r;
CLEAR(r);
r.flags = RT_DEFINED;
r.network = tt->local & tt->remote_netmask;
r.netmask = tt->remote_netmask;
r.gateway = remote_end;
add_route(&r, tt, 0, NULL, es);
}
if (do_ipv6)
{
argv_printf(&argv,
"%s %s inet6 %s/%d",
IFCONFIG_PATH,
actual,
ifconfig_ipv6_local,
tt->netbits_ipv6
);
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, es, S_FATAL, "NetBSD ifconfig inet6 failed");
/* and, hooray, we explicitely need to add a route... */
add_route_connected_v6_net(tt, es);
}
tt->did_ifconfig = true;
#elif defined(TARGET_DARWIN)
/*
* Darwin (i.e. Mac OS X) seems to exhibit similar behaviour to OpenBSD...
*/
argv_printf(&argv,
"%s %s delete",
IFCONFIG_PATH,
actual);
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, es, 0, NULL);
msg(M_INFO, "NOTE: Tried to delete pre-existing tun/tap instance -- No Problem if failure");
/* example: ifconfig tun2 10.2.0.2 10.2.0.1 mtu 1450 netmask 255.255.255.255 up */
if (tun)
{
argv_printf(&argv,
"%s %s %s %s mtu %d netmask 255.255.255.255 up",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu
);
}
else
{
if (tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET)
{
argv_printf(&argv,
"%s %s %s %s netmask %s mtu %d up",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu
);
}
else
{
argv_printf(&argv,
"%s %s %s netmask %s mtu %d up",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu
);
}
}
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, es, S_FATAL, "Mac OS X ifconfig failed");
tt->did_ifconfig = true;
/* Add a network route for the local tun interface */
if (!tun && tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET)
{
struct route_ipv4 r;
CLEAR(r);
r.flags = RT_DEFINED;
r.network = tt->local & tt->remote_netmask;
r.netmask = tt->remote_netmask;
r.gateway = tt->local;
add_route(&r, tt, 0, NULL, es);
}
if (do_ipv6)
{
argv_printf(&argv,
"%s %s inet6 %s/%d",
IFCONFIG_PATH,
actual,
ifconfig_ipv6_local,
tt->netbits_ipv6
);
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, es, S_FATAL, "MacOS X ifconfig inet6 failed");
/* and, hooray, we explicitely need to add a route... */
add_route_connected_v6_net(tt, es);
}
#elif defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY)
in_addr_t remote_end; /* for "virtual" subnet topology */
/* example: ifconfig tun2 10.2.0.2 10.2.0.1 mtu 1450 netmask 255.255.255.255 up */
if (tun)
{
argv_printf(&argv,
"%s %s %s %s mtu %d netmask 255.255.255.255 up",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu
);
}
else if (tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET)
{
remote_end = create_arbitrary_remote( tt );
argv_printf(&argv,
"%s %s %s %s mtu %d netmask %s up",
IFCONFIG_PATH,
actual,
ifconfig_local,
print_in_addr_t(remote_end, 0, &gc),
tun_mtu,
ifconfig_remote_netmask
);
}
else
{
argv_printf(&argv,
"%s %s %s netmask %s mtu %d up",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu
);
}
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, es, S_FATAL, "FreeBSD ifconfig failed");
tt->did_ifconfig = true;
/* Add a network route for the local tun interface */
if (!tun && tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET)
{
struct route_ipv4 r;
CLEAR(r);
r.flags = RT_DEFINED;
r.network = tt->local & tt->remote_netmask;
r.netmask = tt->remote_netmask;
r.gateway = remote_end;
add_route(&r, tt, 0, NULL, es);
}
if (do_ipv6)
{
argv_printf(&argv,
"%s %s inet6 %s/%d",
IFCONFIG_PATH,
actual,
ifconfig_ipv6_local,
tt->netbits_ipv6
);
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, es, S_FATAL, "FreeBSD ifconfig inet6 failed");
}
#elif defined(TARGET_AIX)
{
/* AIX ifconfig will complain if it can't find ODM path in env */
struct env_set *aix_es = env_set_create(NULL);
env_set_add( aix_es, "ODMDIR=/etc/objrepos" );
if (tun)
{
msg(M_FATAL, "no tun support on AIX (canthappen)");
}
/* example: ifconfig tap0 172.30.1.1 netmask 255.255.254.0 up */
argv_printf(&argv,
"%s %s %s netmask %s mtu %d up",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu
);
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, aix_es, S_FATAL, "AIX ifconfig failed");
tt->did_ifconfig = true;
if (do_ipv6)
{
argv_printf(&argv,
"%s %s inet6 %s/%d",
IFCONFIG_PATH,
actual,
ifconfig_ipv6_local,
tt->netbits_ipv6
);
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, aix_es, S_FATAL, "AIX ifconfig inet6 failed");
}
env_set_destroy(aix_es);
}
#elif defined (_WIN32)
{
ASSERT(actual != NULL);
switch (tt->options.ip_win32_type)
{
case IPW32_SET_MANUAL:
msg(M_INFO, "******** NOTE: Please manually set the IP/netmask of '%s' to %s/%s (if it is not already set)",
actual,
ifconfig_local,
print_in_addr_t(tt->adapter_netmask, 0, &gc));
break;
case IPW32_SET_NETSH:
netsh_ifconfig(&tt->options,
actual,
tt->local,
tt->adapter_netmask,
NI_IP_NETMASK|NI_OPTIONS);
break;
}
tt->did_ifconfig = true;
}
if (do_ipv6)
{
if (tt->options.ip_win32_type == IPW32_SET_MANUAL)
{
msg(M_INFO, "******** NOTE: Please manually set the v6 IP of '%s' to %s (if it is not already set)",
actual,
ifconfig_ipv6_local);
}
else if (tt->options.msg_channel)
{
do_address_service(true, AF_INET6, tt);
do_dns6_service(true, tt);
}
else
{
/* example: netsh interface ipv6 set address interface=42 2001:608:8003::d store=active */
char iface[64];
openvpn_snprintf(iface, sizeof(iface), "interface=%lu", tt->adapter_index );
argv_printf(&argv,
"%s%sc interface ipv6 set address %s %s store=active",
get_win_sys_path(),
NETSH_PATH_SUFFIX,
iface,
ifconfig_ipv6_local );
netsh_command(&argv, 4, M_FATAL);
/* set ipv6 dns servers if any are specified */
netsh_set_dns6_servers(tt->options.dns6, tt->options.dns6_len, actual);
}
/* explicit route needed */
if (tt->options.ip_win32_type != IPW32_SET_MANUAL)
{
add_route_connected_v6_net(tt, es);
}
}
#else /* if defined(TARGET_LINUX) */
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.");
#endif /* if defined(TARGET_LINUX) */
argv_reset(&argv);
}
gc_free(&gc);
}
static void
clear_tuntap(struct tuntap *tuntap)
{
CLEAR(*tuntap);
#ifdef _WIN32
tuntap->hand = NULL;
#else
tuntap->fd = -1;
#endif
#ifdef TARGET_SOLARIS
tuntap->ip_fd = -1;
#endif
}
static void
open_null(struct tuntap *tt)
{
tt->actual_name = string_alloc("null", NULL);
}
#if defined (TARGET_OPENBSD) || (defined(TARGET_DARWIN) && HAVE_NET_IF_UTUN_H)
/*
* OpenBSD and Mac OS X when using utun
* have a slightly incompatible TUN device from
* the rest of the world, in that it prepends a
* uint32 to the beginning of the IP header
* to designate the protocol (why not just
* look at the version field in the IP header to
* determine v4 or v6?).
*
* We strip off this field on reads and
* put it back on writes.
*
* I have not tested TAP devices on OpenBSD,
* but I have conditionalized the special
* TUN handling code described above to
* go away for TAP devices.
*/
#include <netinet/ip.h>
#include <sys/uio.h>
static inline int
header_modify_read_write_return(int len)
{
if (len > 0)
{
return len > sizeof(u_int32_t) ? len - sizeof(u_int32_t) : 0;
}
else
{
return len;
}
}
int
write_tun_header(struct tuntap *tt, uint8_t *buf, int len)
{
if (tt->type == DEV_TYPE_TUN)
{
u_int32_t type;
struct iovec iv[2];
struct openvpn_iphdr *iph;
iph = (struct openvpn_iphdr *) buf;
if (OPENVPN_IPH_GET_VER(iph->version_len) == 6)
{
type = htonl(AF_INET6);
}
else
{
type = htonl(AF_INET);
}
iv[0].iov_base = &type;
iv[0].iov_len = sizeof(type);
iv[1].iov_base = buf;
iv[1].iov_len = len;
return header_modify_read_write_return(writev(tt->fd, iv, 2));
}
else
{
return write(tt->fd, buf, len);
}
}
int
read_tun_header(struct tuntap *tt, uint8_t *buf, int len)
{
if (tt->type == DEV_TYPE_TUN)
{
u_int32_t type;
struct iovec iv[2];
iv[0].iov_base = &type;
iv[0].iov_len = sizeof(type);
iv[1].iov_base = buf;
iv[1].iov_len = len;
return header_modify_read_write_return(readv(tt->fd, iv, 2));
}
else
{
return read(tt->fd, buf, len);
}
}
#endif /* if defined (TARGET_OPENBSD) || (defined(TARGET_DARWIN) && HAVE_NET_IF_UTUN_H) */
#if !(defined(_WIN32) || defined(TARGET_LINUX))
static void
open_tun_generic(const char *dev, const char *dev_type, const char *dev_node,
bool dynamic, struct tuntap *tt)
{
char tunname[256];
char dynamic_name[256];
bool dynamic_opened = false;
if (tt->type == DEV_TYPE_NULL)
{
open_null(tt);
}
else
{
/*
* --dev-node specified, so open an explicit device node
*/
if (dev_node)
{
openvpn_snprintf(tunname, sizeof(tunname), "%s", dev_node);
}
else
{
/*
* dynamic open is indicated by --dev specified without
* explicit unit number. Try opening /dev/[dev]n
* where n = [0, 255].
*/
#ifdef TARGET_NETBSD
/* on NetBSD, tap (but not tun) devices are opened by
* opening /dev/tap and then querying the system about the
* actual device name (tap0, tap1, ...) assigned
*/
if (dynamic && strcmp( dev, "tap" ) == 0)
{
struct ifreq ifr;
if ((tt->fd = open( "/dev/tap", O_RDWR)) < 0)
{
msg(M_FATAL, "Cannot allocate NetBSD TAP dev dynamically");
}
if (ioctl( tt->fd, TAPGIFNAME, (void *)&ifr ) < 0)
{
msg(M_FATAL, "Cannot query NetBSD TAP device name");
}
CLEAR(dynamic_name);
strncpy( dynamic_name, ifr.ifr_name, sizeof(dynamic_name)-1 );
dynamic_opened = true;
openvpn_snprintf(tunname, sizeof(tunname), "/dev/%s", dynamic_name );
}
else
#endif
if (dynamic && !has_digit((unsigned char *)dev))
{
int i;
for (i = 0; i < 256; ++i)
{
openvpn_snprintf(tunname, sizeof(tunname),
"/dev/%s%d", dev, i);
openvpn_snprintf(dynamic_name, sizeof(dynamic_name),
"%s%d", dev, i);
if ((tt->fd = open(tunname, O_RDWR)) > 0)
{
dynamic_opened = true;
break;
}
msg(D_READ_WRITE | M_ERRNO, "Tried opening %s (failed)", tunname);
}
if (!dynamic_opened)
{
msg(M_FATAL, "Cannot allocate TUN/TAP dev dynamically");
}
}
/*
* explicit unit number specified
*/
else
{
openvpn_snprintf(tunname, sizeof(tunname), "/dev/%s", dev);
}
}
if (!dynamic_opened)
{
/* has named device existed before? if so, don't destroy at end */
if (if_nametoindex( dev ) > 0)
{
msg(M_INFO, "TUN/TAP device %s exists previously, keep at program end", dev );
tt->persistent_if = true;
}
if ((tt->fd = open(tunname, O_RDWR)) < 0)
{
msg(M_ERR, "Cannot open TUN/TAP dev %s", tunname);
}
}
set_nonblock(tt->fd);
set_cloexec(tt->fd); /* don't pass fd to scripts */
msg(M_INFO, "TUN/TAP device %s opened", tunname);
/* tt->actual_name is passed to up and down scripts and used as the ifconfig dev name */
tt->actual_name = string_alloc(dynamic_opened ? dynamic_name : dev, NULL);
}
}
#endif /* !_WIN32 && !TARGET_LINUX */
#if !defined(_WIN32)
static void
close_tun_generic(struct tuntap *tt)
{
if (tt->fd >= 0)
{
close(tt->fd);
}
if (tt->actual_name)
{
free(tt->actual_name);
}
clear_tuntap(tt);
}
#endif /* !_WIN32 */
#if defined (TARGET_ANDROID)
void
open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt)
{
#define ANDROID_TUNNAME "vpnservice-tun"
struct user_pass up;
struct gc_arena gc = gc_new();
bool opentun;
int oldtunfd = tt->fd;
/* Prefer IPv6 DNS servers,
* Android will use the DNS server in the order we specify*/
for (int i = 0; i < tt->options.dns6_len; i++)
{
management_android_control(management, "DNS6SERVER",
print_in6_addr(tt->options.dns6[i], 0, &gc));
}
for (int i = 0; i < tt->options.dns_len; i++)
{
management_android_control(management, "DNSSERVER",
print_in_addr_t(tt->options.dns[i], 0, &gc));
}
if (tt->options.domain)
{
management_android_control(management, "DNSDOMAIN", tt->options.domain);
}
int android_method = managment_android_persisttun_action(management);
/* Android 4.4 workaround */
if (oldtunfd >=0 && android_method == ANDROID_OPEN_AFTER_CLOSE)
{
close(oldtunfd);
management_sleep(2);
}
if (oldtunfd >=0 && android_method == ANDROID_KEEP_OLD_TUN)
{
/* keep the old fd */
opentun = true;
}
else
{
opentun = management_android_control(management, "OPENTUN", dev);
/* Pick up the fd from management interface after calling the
* OPENTUN command */
tt->fd = management->connection.lastfdreceived;
management->connection.lastfdreceived = -1;
}
if (oldtunfd>=0 && android_method == ANDROID_OPEN_BEFORE_CLOSE)
{
close(oldtunfd);
}
/* Set the actual name to a dummy name */
tt->actual_name = string_alloc(ANDROID_TUNNAME, NULL);
if ((tt->fd < 0) || !opentun)
{
msg(M_ERR, "ERROR: Cannot open TUN");
}
gc_free(&gc);
}
void
close_tun(struct tuntap *tt)
{
if (tt)
{
close_tun_generic(tt);
free(tt);
}
}
int
write_tun(struct tuntap *tt, uint8_t *buf, int len)
{
return write(tt->fd, buf, len);
}
int
read_tun(struct tuntap *tt, uint8_t *buf, int len)
{
return read(tt->fd, buf, len);
}
#elif defined(TARGET_LINUX)
#ifndef HAVE_LINUX_SOCKIOS_H
#error header file linux/sockios.h required
#endif
#if !PEDANTIC
void
open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt)
{
struct ifreq ifr;
/*
* We handle --dev null specially, we do not open /dev/null for this.
*/
if (tt->type == DEV_TYPE_NULL)
{
open_null(tt);
}
else
{
/*
* Process --dev-node
*/
const char *node = dev_node;
if (!node)
{
node = "/dev/net/tun";
}
/*
* Open the interface
*/
if ((tt->fd = open(node, O_RDWR)) < 0)
{
msg(M_ERR, "ERROR: Cannot open TUN/TAP dev %s", node);
}
/*
* Process --tun-ipv6
*/
CLEAR(ifr);
ifr.ifr_flags = IFF_NO_PI;
#if defined(IFF_ONE_QUEUE) && defined(SIOCSIFTXQLEN)
ifr.ifr_flags |= IFF_ONE_QUEUE;
#endif
/*
* Figure out if tun or tap device
*/
if (tt->type == DEV_TYPE_TUN)
{
ifr.ifr_flags |= IFF_TUN;
}
else if (tt->type == DEV_TYPE_TAP)
{
ifr.ifr_flags |= IFF_TAP;
}
else
{
msg(M_FATAL, "I don't recognize device %s as a tun or tap device",
dev);
}
/*
* Set an explicit name, if --dev is not tun or tap
*/
if (strcmp(dev, "tun") && strcmp(dev, "tap"))
{
strncpynt(ifr.ifr_name, dev, IFNAMSIZ);
}
/*
* Use special ioctl that configures tun/tap device with the parms
* we set in ifr
*/
if (ioctl(tt->fd, TUNSETIFF, (void *) &ifr) < 0)
{
msg(M_ERR, "ERROR: Cannot ioctl TUNSETIFF %s", dev);
}
msg(M_INFO, "TUN/TAP device %s opened", ifr.ifr_name);
/*
* Try making the TX send queue bigger
*/
#if defined(IFF_ONE_QUEUE) && defined(SIOCSIFTXQLEN)
if (tt->options.txqueuelen)
{
struct ifreq netifr;
int ctl_fd;
if ((ctl_fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0)
{
CLEAR(netifr);
strncpynt(netifr.ifr_name, ifr.ifr_name, IFNAMSIZ);
netifr.ifr_qlen = tt->options.txqueuelen;
if (ioctl(ctl_fd, SIOCSIFTXQLEN, (void *) &netifr) >= 0)
{
msg(D_OSBUF, "TUN/TAP TX queue length set to %d", tt->options.txqueuelen);
}
else
{
msg(M_WARN | M_ERRNO, "Note: Cannot set tx queue length on %s", ifr.ifr_name);
}
close(ctl_fd);
}
else
{
msg(M_WARN | M_ERRNO, "Note: Cannot open control socket on %s", ifr.ifr_name);
}
}
#endif /* if defined(IFF_ONE_QUEUE) && defined(SIOCSIFTXQLEN) */
set_nonblock(tt->fd);
set_cloexec(tt->fd);
tt->actual_name = string_alloc(ifr.ifr_name, NULL);
}
return;
}
#else /* if !PEDANTIC */
void
open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt)
{
ASSERT(0);
}
#endif /* !PENDANTIC */
#ifdef ENABLE_FEATURE_TUN_PERSIST
void
tuncfg(const char *dev, const char *dev_type, const char *dev_node, int persist_mode, const char *username, const char *groupname, const struct tuntap_options *options)
{
struct tuntap *tt;
ALLOC_OBJ(tt, struct tuntap);
clear_tuntap(tt);
tt->type = dev_type_enum(dev, dev_type);
tt->options = *options;
open_tun(dev, dev_type, dev_node, tt);
if (ioctl(tt->fd, TUNSETPERSIST, persist_mode) < 0)
{
msg(M_ERR, "Cannot ioctl TUNSETPERSIST(%d) %s", persist_mode, dev);
}
if (username != NULL)
{
struct platform_state_user platform_state_user;
if (!platform_user_get(username, &platform_state_user))
{
msg(M_ERR, "Cannot get user entry for %s", username);
}
else if (ioctl(tt->fd, TUNSETOWNER, platform_state_user.pw->pw_uid) < 0)
{
msg(M_ERR, "Cannot ioctl TUNSETOWNER(%s) %s", username, dev);
}
}
if (groupname != NULL)
{
struct platform_state_group platform_state_group;
if (!platform_group_get(groupname, &platform_state_group))
{
msg(M_ERR, "Cannot get group entry for %s", groupname);
}
else if (ioctl(tt->fd, TUNSETGROUP, platform_state_group.gr->gr_gid) < 0)
{
msg(M_ERR, "Cannot ioctl TUNSETOWNER(%s) %s", groupname, dev);
}
}
close_tun(tt);
msg(M_INFO, "Persist state set to: %s", (persist_mode ? "ON" : "OFF"));
}
#endif /* ENABLE_FEATURE_TUN_PERSIST */
void
close_tun(struct tuntap *tt)
{
if (tt)
{
if (tt->type != DEV_TYPE_NULL && tt->did_ifconfig)
{
struct argv argv = argv_new();
struct gc_arena gc = gc_new();
#ifdef ENABLE_IPROUTE
if (is_tun_p2p(tt))
{
argv_printf(&argv,
"%s addr del dev %s local %s peer %s",
iproute_path,
tt->actual_name,
print_in_addr_t(tt->local, 0, &gc),
print_in_addr_t(tt->remote_netmask, 0, &gc)
);
}
else
{
argv_printf(&argv,
"%s addr del dev %s %s/%d",
iproute_path,
tt->actual_name,
print_in_addr_t(tt->local, 0, &gc),
netmask_to_netbits2(tt->remote_netmask)
);
}
#else /* ifdef ENABLE_IPROUTE */
argv_printf(&argv,
"%s %s 0.0.0.0",
IFCONFIG_PATH,
tt->actual_name
);
#endif /* ifdef ENABLE_IPROUTE */
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, NULL, 0, "Linux ip addr del failed");
if (tt->did_ifconfig_ipv6_setup)
{
const char *ifconfig_ipv6_local = print_in6_addr(tt->local_ipv6, 0, &gc);
#ifdef ENABLE_IPROUTE
argv_printf(&argv, "%s -6 addr del %s/%d dev %s",
iproute_path,
ifconfig_ipv6_local,
tt->netbits_ipv6,
tt->actual_name
);
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, NULL, 0, "Linux ip -6 addr del failed");
#else /* ifdef ENABLE_IPROUTE */
argv_printf(&argv,
"%s %s del %s/%d",
IFCONFIG_PATH,
tt->actual_name,
ifconfig_ipv6_local,
tt->netbits_ipv6
);
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, NULL, 0, "Linux ifconfig inet6 del failed");
#endif
}
argv_reset(&argv);
gc_free(&gc);
}
close_tun_generic(tt);
free(tt);
}
}
int
write_tun(struct tuntap *tt, uint8_t *buf, int len)
{
return write(tt->fd, buf, len);
}
int
read_tun(struct tuntap *tt, uint8_t *buf, int len)
{
return read(tt->fd, buf, len);
}
#elif defined(TARGET_SOLARIS)
#ifndef TUNNEWPPA
#error I need the symbol TUNNEWPPA from net/if_tun.h
#endif
void
open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt)
{
int if_fd, ip_muxid, arp_muxid, arp_fd, ppa = -1;
struct lifreq ifr;
const char *ptr;
const char *ip_node, *arp_node;
const char *dev_tuntap_type;
int link_type;
bool is_tun;
struct strioctl strioc_if, strioc_ppa;
/* improved generic TUN/TAP driver from
* http://www.whiteboard.ne.jp/~admin2/tuntap/
* has IPv6 support
*/
CLEAR(ifr);
if (tt->type == DEV_TYPE_NULL)
{
open_null(tt);
return;
}
if (tt->type == DEV_TYPE_TUN)
{
ip_node = "/dev/udp";
if (!dev_node)
{
dev_node = "/dev/tun";
}
dev_tuntap_type = "tun";
link_type = I_PLINK;
is_tun = true;
}
else if (tt->type == DEV_TYPE_TAP)
{
ip_node = "/dev/udp";
if (!dev_node)
{
dev_node = "/dev/tap";
}
arp_node = dev_node;
dev_tuntap_type = "tap";
link_type = I_PLINK; /* was: I_LINK */
is_tun = false;
}
else
{
msg(M_FATAL, "I don't recognize device %s as a tun or tap device",
dev);
}
if ((tt->ip_fd = open(ip_node, O_RDWR, 0)) < 0)
{
msg(M_ERR, "Can't open %s", ip_node);
}
if ((tt->fd = open(dev_node, O_RDWR, 0)) < 0)
{
msg(M_ERR, "Can't open %s", dev_node);
}
/* get unit number */
if (*dev)
{
ptr = dev;
while (*ptr && !isdigit((int) *ptr))
{
ptr++;
}
ppa = atoi(ptr);
}
/* Assign a new PPA and get its unit number. */
strioc_ppa.ic_cmd = TUNNEWPPA;
strioc_ppa.ic_timout = 0;
strioc_ppa.ic_len = sizeof(ppa);
strioc_ppa.ic_dp = (char *)&ppa;
if (*ptr == '\0') /* no number given, try dynamic */
{
bool found_one = false;
while (!found_one && ppa < 64)
{
int new_ppa = ioctl(tt->fd, I_STR, &strioc_ppa);
if (new_ppa >= 0)
{
msg( M_INFO, "open_tun: got dynamic interface '%s%d'", dev_tuntap_type, new_ppa );
ppa = new_ppa;
found_one = true;
break;
}
if (errno != EEXIST)
{
msg(M_ERR, "open_tun: unexpected error trying to find free %s interface", dev_tuntap_type );
}
ppa++;
}
if (!found_one)
{
msg(M_ERR, "open_tun: could not find free %s interface, give up.", dev_tuntap_type );
}
}
else /* try this particular one */
{
if ((ppa = ioctl(tt->fd, I_STR, &strioc_ppa)) < 0)
{
msg(M_ERR, "Can't assign PPA for new interface (%s%d)", dev_tuntap_type, ppa );
}
}
if ((if_fd = open(dev_node, O_RDWR, 0)) < 0)
{
msg(M_ERR, "Can't open %s (2)", dev_node);
}
if (ioctl(if_fd, I_PUSH, "ip") < 0)
{
msg(M_ERR, "Can't push IP module");
}
if (tt->type == DEV_TYPE_TUN)
{
/* Assign ppa according to the unit number returned by tun device */
if (ioctl(if_fd, IF_UNITSEL, (char *) &ppa) < 0)
{
msg(M_ERR, "Can't set PPA %d", ppa);
}
}
tt->actual_name = (char *) malloc(32);
check_malloc_return(tt->actual_name);
openvpn_snprintf(tt->actual_name, 32, "%s%d", dev_tuntap_type, ppa);
if (tt->type == DEV_TYPE_TAP)
{
if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
{
msg(M_ERR, "Can't get flags\n");
}
strncpynt(ifr.lifr_name, tt->actual_name, sizeof(ifr.lifr_name));
ifr.lifr_ppa = ppa;
/* Assign ppa according to the unit number returned by tun device */
if (ioctl(if_fd, SIOCSLIFNAME, &ifr) < 0)
{
msg(M_ERR, "Can't set PPA %d", ppa);
}
if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
{
msg(M_ERR, "Can't get flags\n");
}
/* Push arp module to if_fd */
if (ioctl(if_fd, I_PUSH, "arp") < 0)
{
msg(M_ERR, "Can't push ARP module");
}
/* Pop any modules on the stream */
while (true)
{
if (ioctl(tt->ip_fd, I_POP, NULL) < 0)
{
break;
}
}
/* Push arp module to ip_fd */
if (ioctl(tt->ip_fd, I_PUSH, "arp") < 0)
{
msg(M_ERR, "Can't push ARP module\n");
}
/* Open arp_fd */
if ((arp_fd = open(arp_node, O_RDWR, 0)) < 0)
{
msg(M_ERR, "Can't open %s\n", arp_node);
}
/* Push arp module to arp_fd */
if (ioctl(arp_fd, I_PUSH, "arp") < 0)
{
msg(M_ERR, "Can't push ARP module\n");
}
/* Set ifname to arp */
strioc_if.ic_cmd = SIOCSLIFNAME;
strioc_if.ic_timout = 0;
strioc_if.ic_len = sizeof(ifr);
strioc_if.ic_dp = (char *)&ifr;
if (ioctl(arp_fd, I_STR, &strioc_if) < 0)
{
msg(M_ERR, "Can't set ifname to arp\n");
}
}
if ((ip_muxid = ioctl(tt->ip_fd, link_type, if_fd)) < 0)
{
msg(M_ERR, "Can't link %s device to IP", dev_tuntap_type);
}
if (tt->type == DEV_TYPE_TAP)
{
if ((arp_muxid = ioctl(tt->ip_fd, link_type, arp_fd)) < 0)
{
msg(M_ERR, "Can't link %s device to ARP", dev_tuntap_type);
}
close(arp_fd);
}
CLEAR(ifr);
strncpynt(ifr.lifr_name, tt->actual_name, sizeof(ifr.lifr_name));
ifr.lifr_ip_muxid = ip_muxid;
if (tt->type == DEV_TYPE_TAP)
{
ifr.lifr_arp_muxid = arp_muxid;
}
if (ioctl(tt->ip_fd, SIOCSLIFMUXID, &ifr) < 0)
{
if (tt->type == DEV_TYPE_TAP)
{
ioctl(tt->ip_fd, I_PUNLINK, arp_muxid);
}
ioctl(tt->ip_fd, I_PUNLINK, ip_muxid);
msg(M_ERR, "Can't set multiplexor id");
}
set_nonblock(tt->fd);
set_cloexec(tt->fd);
set_cloexec(tt->ip_fd);
msg(M_INFO, "TUN/TAP device %s opened", tt->actual_name);
}
static void
solaris_close_tun(struct tuntap *tt)
{
if (tt)
{
/* IPv6 interfaces need to be 'manually' de-configured */
if (tt->did_ifconfig_ipv6_setup)
{
struct argv argv = argv_new();
argv_printf( &argv, "%s %s inet6 unplumb",
IFCONFIG_PATH, tt->actual_name );
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, NULL, 0, "Solaris ifconfig inet6 unplumb failed");
argv_reset(&argv);
}
if (tt->ip_fd >= 0)
{
struct lifreq ifr;
CLEAR(ifr);
strncpynt(ifr.lifr_name, tt->actual_name, sizeof(ifr.lifr_name));
if (ioctl(tt->ip_fd, SIOCGLIFFLAGS, &ifr) < 0)
{
msg(M_WARN | M_ERRNO, "Can't get iface flags");
}
if (ioctl(tt->ip_fd, SIOCGLIFMUXID, &ifr) < 0)
{
msg(M_WARN | M_ERRNO, "Can't get multiplexor id");
}
if (tt->type == DEV_TYPE_TAP)
{
if (ioctl(tt->ip_fd, I_PUNLINK, ifr.lifr_arp_muxid) < 0)
{
msg(M_WARN | M_ERRNO, "Can't unlink interface(arp)");
}
}
if (ioctl(tt->ip_fd, I_PUNLINK, ifr.lifr_ip_muxid) < 0)
{
msg(M_WARN | M_ERRNO, "Can't unlink interface(ip)");
}
close(tt->ip_fd);
tt->ip_fd = -1;
}
if (tt->fd >= 0)
{
close(tt->fd);
tt->fd = -1;
}
}
}
/*
* Close TUN device.
*/
void
close_tun(struct tuntap *tt)
{
if (tt)
{
solaris_close_tun(tt);
if (tt->actual_name)
{
free(tt->actual_name);
}
clear_tuntap(tt);
free(tt);
}
}
static void
solaris_error_close(struct tuntap *tt, const struct env_set *es,
const char *actual, bool unplumb_inet6 )
{
struct argv argv = argv_new();
if (unplumb_inet6)
{
argv_printf( &argv, "%s %s inet6 unplumb",
IFCONFIG_PATH, actual );
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, es, 0, "Solaris ifconfig inet6 unplumb failed");
}
argv_printf(&argv,
"%s %s unplumb",
IFCONFIG_PATH,
actual);
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, es, 0, "Solaris ifconfig unplumb failed");
close_tun(tt);
msg(M_FATAL, "Solaris ifconfig failed");
argv_reset(&argv);
}
int
write_tun(struct tuntap *tt, uint8_t *buf, int len)
{
struct strbuf sbuf;
sbuf.len = len;
sbuf.buf = (char *)buf;
return putmsg(tt->fd, NULL, &sbuf, 0) >= 0 ? sbuf.len : -1;
}
int
read_tun(struct tuntap *tt, uint8_t *buf, int len)
{
struct strbuf sbuf;
int f = 0;
sbuf.maxlen = len;
sbuf.buf = (char *)buf;
return getmsg(tt->fd, NULL, &sbuf, &f) >= 0 ? sbuf.len : -1;
}
#elif defined(TARGET_OPENBSD)
void
open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt)
{
open_tun_generic(dev, dev_type, dev_node, true, tt);
/* Enable multicast on the interface */
if (tt->fd >= 0)
{
struct tuninfo info;
if (ioctl(tt->fd, TUNGIFINFO, &info) < 0)
{
msg(M_WARN | M_ERRNO, "Can't get interface info");
}
#ifdef IFF_MULTICAST /* openbsd 4.x doesn't have this */
info.flags |= IFF_MULTICAST;
#endif
if (ioctl(tt->fd, TUNSIFINFO, &info) < 0)
{
msg(M_WARN | M_ERRNO, "Can't set interface info");
}
}
}
/* tun(4): "If the device was created by opening /dev/tunN, it will be
* automatically destroyed. Devices created via ifconfig(8) are
* only marked as not running and traffic will be dropped
* returning EHOSTDOWN."
* --> no special handling should be needed - *but* OpenBSD is misbehaving
* here: if the interface was put in tap mode ("ifconfig tunN link0"), it
* *will* stay around, and needs to be cleaned up manually
*/
void
close_tun(struct tuntap *tt)
{
/* only *TAP* devices need destroying, tun devices auto-self-destruct
*/
if (tt && (tt->type == DEV_TYPE_TUN || tt->persistent_if ) )
{
close_tun_generic(tt);
free(tt);
}
else if (tt)
{
struct gc_arena gc = gc_new();
struct argv argv = argv_new();
/* setup command, close tun dev (clears tt->actual_name!), run command
*/
argv_printf(&argv, "%s %s destroy",
IFCONFIG_PATH, tt->actual_name);
close_tun_generic(tt);
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, NULL, 0, "OpenBSD 'destroy tun interface' failed (non-critical)");
free(tt);
}
}
int
write_tun(struct tuntap *tt, uint8_t *buf, int len)
{
return write_tun_header(tt, buf, len);
}
int
read_tun(struct tuntap *tt, uint8_t *buf, int len)
{
return read_tun_header(tt, buf, len);
}
#elif defined(TARGET_NETBSD)
/*
* NetBSD before 4.0 does not support IPv6 on tun out of the box,
* but there exists a patch (sys/net/if_tun.c, 1.79->1.80, see PR 32944).
*
* NetBSD 4.0 and up do, but we need to put the tun interface into
* "multi_af" mode, which will prepend the address family to all packets
* (same as OpenBSD and FreeBSD). If this is not enabled, the kernel
* silently drops all IPv6 packets on output and gets confused on input.
*
* On earlier versions, multi_af is not available at all, so we have
* two different NetBSD code variants here :-(
*
*/
void
open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt)
{
open_tun_generic(dev, dev_type, dev_node, true, tt);
if (tt->fd >= 0)
{
int i = IFF_POINTOPOINT|IFF_MULTICAST;
ioctl(tt->fd, TUNSIFMODE, &i); /* multicast on */
i = 0;
ioctl(tt->fd, TUNSLMODE, &i); /* link layer mode off */
if (tt->type == DEV_TYPE_TUN)
{
i = 1;
if (ioctl(tt->fd, TUNSIFHEAD, &i) < 0) /* multi-af mode on */
{
msg(M_WARN | M_ERRNO, "ioctl(TUNSIFHEAD)");
}
}
}
}
/* the current way OpenVPN handles tun devices on NetBSD leads to
* lingering tunX interfaces after close -> for a full cleanup, they
* need to be explicitely destroyed
*/
void
close_tun(struct tuntap *tt)
{
/* only tun devices need destroying, tap devices auto-self-destruct
*/
if (tt && ( tt->type != DEV_TYPE_TUN || tt->persistent_if ) )
{
close_tun_generic(tt);
free(tt);
}
else if (tt)
{
struct gc_arena gc = gc_new();
struct argv argv = argv_new();
/* setup command, close tun dev (clears tt->actual_name!), run command
*/
argv_printf(&argv, "%s %s destroy",
IFCONFIG_PATH, tt->actual_name);
close_tun_generic(tt);
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, NULL, 0, "NetBSD 'destroy tun interface' failed (non-critical)");
free(tt);
}
}
static inline int
netbsd_modify_read_write_return(int len)
{
if (len > 0)
{
return len > sizeof(u_int32_t) ? len - sizeof(u_int32_t) : 0;
}
else
{
return len;
}
}
int
write_tun(struct tuntap *tt, uint8_t *buf, int len)
{
if (tt->type == DEV_TYPE_TUN)
{
u_int32_t type;
struct iovec iv[2];
struct openvpn_iphdr *iph;
iph = (struct openvpn_iphdr *) buf;
if (OPENVPN_IPH_GET_VER(iph->version_len) == 6)
{
type = htonl(AF_INET6);
}
else
{
type = htonl(AF_INET);
}
iv[0].iov_base = (char *)&type;
iv[0].iov_len = sizeof(type);
iv[1].iov_base = buf;
iv[1].iov_len = len;
return netbsd_modify_read_write_return(writev(tt->fd, iv, 2));
}
else
{
return write(tt->fd, buf, len);
}
}
int
read_tun(struct tuntap *tt, uint8_t *buf, int len)
{
if (tt->type == DEV_TYPE_TUN)
{
u_int32_t type;
struct iovec iv[2];
iv[0].iov_base = (char *)&type;
iv[0].iov_len = sizeof(type);
iv[1].iov_base = buf;
iv[1].iov_len = len;
return netbsd_modify_read_write_return(readv(tt->fd, iv, 2));
}
else
{
return read(tt->fd, buf, len);
}
}
#elif defined(TARGET_FREEBSD)
static inline int
freebsd_modify_read_write_return(int len)
{
if (len > 0)
{
return len > sizeof(u_int32_t) ? len - sizeof(u_int32_t) : 0;
}
else
{
return len;
}
}
void
open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt)
{
open_tun_generic(dev, dev_type, dev_node, true, tt);
if (tt->fd >= 0 && tt->type == DEV_TYPE_TUN)
{
int i = IFF_POINTOPOINT | IFF_MULTICAST;
if (ioctl(tt->fd, TUNSIFMODE, &i) < 0)
{
msg(M_WARN | M_ERRNO, "ioctl(TUNSIFMODE)");
}
i = 1;
if (ioctl(tt->fd, TUNSIFHEAD, &i) < 0)
{
msg(M_WARN | M_ERRNO, "ioctl(TUNSIFHEAD)");
}
}
}
/* tun(4): "These network interfaces persist until the if_tun.ko module is
* unloaded, or until removed with the ifconfig(8) command."
* (verified for FreeBSD 6.3, 7.4, 8.2 and 9, same for tap(4))
*
* so, to avoid lingering tun/tap interfaces after OpenVPN quits,
* we need to call "ifconfig ... destroy" for cleanup
*/
void
close_tun(struct tuntap *tt)
{
if (tt && tt->persistent_if) /* keep pre-existing if around */
{
close_tun_generic(tt);
free(tt);
}
else if (tt) /* close and destroy */
{
struct argv argv = argv_new();
/* setup command, close tun dev (clears tt->actual_name!), run command
*/
argv_printf(&argv, "%s %s destroy",
IFCONFIG_PATH, tt->actual_name);
close_tun_generic(tt);
argv_msg(M_INFO, &argv);
openvpn_execve_check(&argv, NULL, 0, "FreeBSD 'destroy tun interface' failed (non-critical)");
free(tt);
}
}
int
write_tun(struct tuntap *tt, uint8_t *buf, int len)
{
if (tt->type == DEV_TYPE_TUN)
{
u_int32_t type;
struct iovec iv[2];
struct ip *iph;
iph = (struct ip *) buf;
if (iph->ip_v == 6)
{
type = htonl(AF_INET6);
}
else
{
type = htonl(AF_INET);
}
iv[0].iov_base = (char *)&type;
iv[0].iov_len = sizeof(type);
iv[1].iov_base = buf;
iv[1].iov_len = len;
return freebsd_modify_read_write_return(writev(tt->fd, iv, 2));
}
else
{
return write(tt->fd, buf, len);
}
}
int
read_tun(struct tuntap *tt, uint8_t *buf, int len)
{
if (tt->type == DEV_TYPE_TUN)
{
u_int32_t type;
struct iovec iv[2];
iv[0].iov_base = (char *)&type;
iv[0].iov_len = sizeof(type);
iv[1].iov_base = buf;
iv[1].iov_len = len;
return freebsd_modify_read_write_return(readv(tt->fd, iv, 2));
}
else
{
return read(tt->fd, buf, len);
}
}
#elif defined(TARGET_DRAGONFLY)
static inline int
dragonfly_modify_read_write_return(int len)
{
if (len > 0)
{
return len > sizeof(u_int32_t) ? len - sizeof(u_int32_t) : 0;
}
else
{
return len;
}
}
void
open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt)
{
open_tun_generic(dev, dev_type, dev_node, true, tt);
if (tt->fd >= 0)
{
int i = 0;
/* Disable extended modes */
ioctl(tt->fd, TUNSLMODE, &i);
i = 1;
ioctl(tt->fd, TUNSIFHEAD, &i);
}
}
void
close_tun(struct tuntap *tt)
{
if (tt)
{
close_tun_generic(tt);
free(tt);
}
}
int
write_tun(struct tuntap *tt, uint8_t *buf, int len)
{
if (tt->type == DEV_TYPE_TUN)
{
u_int32_t type;
struct iovec iv[2];
struct ip *iph;
iph = (struct ip *) buf;
if (iph->ip_v == 6)
{
type = htonl(AF_INET6);
}
else
{
type = htonl(AF_INET);
}
iv[0].iov_base = (char *)&type;
iv[0].iov_len = sizeof(type);
iv[1].iov_base = buf;
iv[1].iov_len = len;
return dragonfly_modify_read_write_return(writev(tt->fd, iv, 2));
}
else
{
return write(tt->fd, buf, len);
}
}
int
read_tun(struct tuntap *tt, uint8_t *buf, int len)
{
if (tt->type == DEV_TYPE_TUN)
{
u_int32_t type;
struct iovec iv[2];
iv[0].iov_base = (char *)&type;
iv[0].iov_len = sizeof(type);
iv[1].iov_base = buf;
iv[1].iov_len = len;
return dragonfly_modify_read_write_return(readv(tt->fd, iv, 2));
}
else
{
return read(tt->fd, buf, len);
}
}
#elif defined(TARGET_DARWIN)
/* Darwin (MacOS X) is mostly "just use the generic stuff", but there
* is always one caveat...:
*
* If IPv6 is configured, and the tun device is closed, the IPv6 address
* configured to the tun interface changes to a lingering /128 route
* pointing to lo0. Need to unconfigure... (observed on 10.5)
*/
/*
* utun is the native Darwin tun driver present since at least 10.7
* Thanks goes to Jonathan Levin for providing an example how to utun
* (http://newosxbook.com/src.jl?tree=listings&file=17-15-utun.c)
*/
#ifdef HAVE_NET_IF_UTUN_H
/* Helper functions that tries to open utun device
* return -2 on early initialization failures (utun not supported
* at all (old OS X) and -1 on initlization failure of utun
* device (utun works but utunX is already used */
static
int
utun_open_helper(struct ctl_info ctlInfo, int utunnum)
{
struct sockaddr_ctl sc;
int fd;
fd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);
if (fd < 0)
{
msg(M_INFO | M_ERRNO, "Opening utun (socket(SYSPROTO_CONTROL))");
return -2;
}
if (ioctl(fd, CTLIOCGINFO, &ctlInfo) == -1)
{
close(fd);
msg(M_INFO | M_ERRNO, "Opening utun (ioctl(CTLIOCGINFO))");
return -2;
}
sc.sc_id = ctlInfo.ctl_id;
sc.sc_len = sizeof(sc);
sc.sc_family = AF_SYSTEM;
sc.ss_sysaddr = AF_SYS_CONTROL;
sc.sc_unit = utunnum+1;
/* If the connect is successful, a utun%d device will be created, where "%d"
* is (sc.sc_unit - 1) */
if (connect(fd, (struct sockaddr *)&sc, sizeof(sc)) < 0)
{
msg(M_INFO | M_ERRNO, "Opening utun (connect(AF_SYS_CONTROL))");
close(fd);
return -1;
}
set_nonblock(fd);
set_cloexec(fd); /* don't pass fd to scripts */
return fd;
}
void
open_darwin_utun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt)
{
struct ctl_info ctlInfo;
int fd;
char utunname[20];
int utunnum = -1;
socklen_t utunname_len = sizeof(utunname);
/* dev_node is simply utun, do the normal dynamic utun
* otherwise try to parse the utun number */
if (dev_node && (strcmp("utun", dev_node) != 0 ))
{
if (sscanf(dev_node, "utun%d", &utunnum) != 1)
{
msg(M_FATAL, "Cannot parse 'dev-node %s' please use 'dev-node utunX'"
"to use a utun device number X", dev_node);
}
}
CLEAR(ctlInfo);
if (strlcpy(ctlInfo.ctl_name, UTUN_CONTROL_NAME, sizeof(ctlInfo.ctl_name)) >=
sizeof(ctlInfo.ctl_name))
{
msg(M_ERR, "Opening utun: UTUN_CONTROL_NAME too long");
}
/* try to open first available utun device if no specific utun is requested */
if (utunnum == -1)
{
for (utunnum = 0; utunnum<255; utunnum++)
{
fd = utun_open_helper(ctlInfo, utunnum);
/* Break if the fd is valid,
* or if early initalization failed (-2) */
if (fd !=-1)
{
break;
}
}
}
else
{
fd = utun_open_helper(ctlInfo, utunnum);
}
/* opening an utun device failed */
tt->fd = fd;
if (fd < 0)
{
return;
}
/* Retrieve the assigned interface name. */
if (getsockopt(fd, SYSPROTO_CONTROL, UTUN_OPT_IFNAME, utunname, &utunname_len))
{
msg(M_ERR | M_ERRNO, "Error retrieving utun interface name");
}
tt->actual_name = string_alloc(utunname, NULL);
msg(M_INFO, "Opened utun device %s", utunname);
tt->is_utun = true;
}
#endif /* ifdef HAVE_NET_IF_UTUN_H */
void
open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt)
{
#ifdef HAVE_NET_IF_UTUN_H
/* If dev_node does not start start with utun assume regular tun/tap */
if ((!dev_node && tt->type==DEV_TYPE_TUN)
|| (dev_node && !strncmp(dev_node, "utun", 4)))
{
/* Check if user has specific dev_type tap and forced utun with
* dev-nod