OpenVPN
tun.h File Reference
#include <winioctl.h>
#include <tap-windows.h>
#include <setupapi.h>
#include <cfgmgr32.h>
#include "buffer.h"
#include "error.h"
#include "mtu.h"
#include "win32.h"
#include "event.h"
#include "proto.h"
#include "misc.h"
#include "networking.h"
#include "dco.h"
Include dependency graph for tun.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  tuntap_options
struct  afunix_context
struct  tuntap
struct  tap_reg
struct  panel_reg
struct  device_instance_id_interface

Macros

#define DCO_WIN_REFERENCE_STRING   "ovpn-dco"
#define IPW32_SET_ADAPTIVE_DELAY_WINDOW   300
#define IPW32_SET_ADAPTIVE_TRY_NETSH   20
#define DHCP_OPTIONS_DHCP_OPTIONAL   (1 << 0)
#define DHCP_OPTIONS_DHCP_REQUIRED   (1 << 1)
#define IPW32_SET_MANUAL   0 /* "--ip-win32 manual" */
#define IPW32_SET_NETSH   1 /* "--ip-win32 netsh" */
#define IPW32_SET_IPAPI   2 /* "--ip-win32 ipapi" */
#define IPW32_SET_DHCP_MASQ   3 /* "--ip-win32 dynamic" */
#define IPW32_SET_ADAPTIVE   4 /* "--ip-win32 adaptive" */
#define IPW32_SET_N   5
#define N_DHCP_ADDR   4u
#define N_SEARCH_LIST_LEN   10 /* Max # of entries in domin-search list */
#define TUNNEL_TYPE(tt)
#define TUNNEL_TOPOLOGY(tt)
#define IFCONFIG_BEFORE_TUN_OPEN   0
#define IFCONFIG_AFTER_TUN_OPEN   1
#define IFCONFIG_DEFAULT   IFCONFIG_AFTER_TUN_OPEN
#define ROUTE_BEFORE_TUN   0
#define ROUTE_AFTER_TUN   1
#define ROUTE_ORDER_DEFAULT   ROUTE_AFTER_TUN

Typedefs

typedef struct afunix_context afunix_context_t

Enumerations

enum  tun_driver_type {
  WINDOWS_DRIVER_UNSPECIFIED , WINDOWS_DRIVER_TAP_WINDOWS6 , DRIVER_GENERIC_TUNTAP , DRIVER_AFUNIX ,
  DRIVER_NULL , DRIVER_DCO , DRIVER_UTUN
}

Functions

static bool tuntap_defined (const struct tuntap *tt)
void open_tun (const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt, openvpn_net_ctx_t *ctx)
void close_tun (struct tuntap *tt, openvpn_net_ctx_t *ctx)
void tun_open_device (struct tuntap *tt, const char *dev_node, const char **device_guid, struct gc_arena *gc)
void close_tun_handle (struct tuntap *tt)
ssize_t write_tun (const struct tuntap *tt, const uint8_t *buf, int len)
ssize_t read_tun (const struct tuntap *tt, uint8_t *buf, int len)
const char * guess_tuntap_dev (const char *dev, const char *dev_type, const char *dev_node, struct gc_arena *gc)
struct tuntap * init_tun (const char *dev, const char *dev_type, int topology, const char *ifconfig_local_parm, const char *ifconfig_remote_netmask_parm, const char *ifconfig_ipv6_local_parm, int ifconfig_ipv6_netbits_parm, const char *ifconfig_ipv6_remote_parm, const struct addrinfo *local_public, const struct addrinfo *remote_public, const bool strict_warn, struct env_set *es, openvpn_net_ctx_t *ctx, struct tuntap *tt)
void init_tun_post (struct tuntap *tt, const struct frame *frame, const struct tuntap_options *options)
void do_ifconfig_setenv (const struct tuntap *tt, struct env_set *es)
void do_ifconfig (struct tuntap *tt, const char *ifname, int tun_mtu, const struct env_set *es, openvpn_net_ctx_t *ctx)
 do_ifconfig - configure the tunnel interface
void undo_ifconfig (const struct tuntap *tt, openvpn_net_ctx_t *ctx)
 undo_ifconfig - undo configuration of the tunnel interface
bool is_dev_type (const char *dev, const char *dev_type, const char *match_type)
int dev_type_enum (const char *dev, const char *dev_type)
const char * dev_type_string (const char *dev, const char *dev_type)
const char * ifconfig_options_string (const struct tuntap *tt, bool remote, bool disable, struct gc_arena *gc)
bool is_tun_p2p (const struct tuntap *tt)
void warn_on_use_of_common_subnets (openvpn_net_ctx_t *ctx)
const char * print_tun_backend_driver (enum tun_driver_type driver)
 Return a string representation of the tun backed driver type.
static int ifconfig_order (const struct tuntap *tt)
static int route_order (const struct tuntap *tt)
int ascii2ipset (const char *name)
const char * ipset2ascii (int index)
const char * ipset2ascii_all (struct gc_arena *gc)
void verify_255_255_255_252 (in_addr_t local, in_addr_t remote)
const IP_ADAPTER_INFO * get_adapter_info_list (struct gc_arena *gc)
const IP_ADAPTER_INFO * get_tun_adapter (const struct tuntap *tt, const IP_ADAPTER_INFO *list)
const IP_ADAPTER_INFO * get_adapter_info (DWORD index, struct gc_arena *gc)
const IP_PER_ADAPTER_INFO * get_per_adapter_info (const DWORD index, struct gc_arena *gc)
const IP_ADAPTER_INFO * get_adapter (const IP_ADAPTER_INFO *ai, DWORD index)
bool is_adapter_up (const struct tuntap *tt, const IP_ADAPTER_INFO *list)
bool is_ip_in_adapter_subnet (const IP_ADAPTER_INFO *ai, const in_addr_t ip, in_addr_t *highest_netmask)
DWORD adapter_index_of_ip (const IP_ADAPTER_INFO *list, const in_addr_t ip, int *count, in_addr_t *netmask)
void show_tap_win_adapters (msglvl_t msglevel, msglvl_t warnlevel)
void show_adapters (msglvl_t msglevel)
void tap_allow_nonadmin_access (const char *dev_node)
void show_valid_win32_tun_subnets (void)
const char * tap_win_getinfo (const struct tuntap *tt, struct gc_arena *gc)
void tun_show_debug (const struct tuntap *tt)
bool dhcp_release_by_adapter_index (const DWORD adapter_index)
bool dhcp_renew_by_adapter_index (const DWORD adapter_index)
void fork_register_dns_action (const struct tuntap *tt)
void ipconfig_register_dns (const struct env_set *es)
void tun_standby_init (struct tuntap *tt)
bool tun_standby (struct tuntap *tt)
int tun_read_queue (struct tuntap *tt, int maxsize)
int tun_write_queue (struct tuntap *tt, const struct buffer *buf)
static bool tuntap_stop (int status)
static bool tuntap_abort (int status)
int tun_write_win32 (struct tuntap *tt, const struct buffer *buf)
static bool is_ip_packet_valid (const struct buffer *buf)
static bool tuntap_is_dco_win (const struct tuntap *tt)
static bool tuntap_is_dco_win_timeout (const struct tuntap *tt, ssize_t status)
static event_t tun_event_handle (const struct tuntap *tt)
static void tun_set (struct tuntap *tt, struct event_set *es, unsigned int rwflags, void *arg, unsigned int *persistent)
const char * tun_stat (const struct tuntap *tt, unsigned int rwflags, struct gc_arena *gc)
bool tun_name_is_fixed (const char *dev)
static bool is_tun_type_set (const struct tuntap *tt)
static void open_tun_null (struct tuntap *tt)

Macro Definition Documentation

◆ DCO_WIN_REFERENCE_STRING

#define DCO_WIN_REFERENCE_STRING   "ovpn-dco"

Definition at line 59 of file tun.h.

Referenced by tun_try_open_device().

◆ DHCP_OPTIONS_DHCP_OPTIONAL

#define DHCP_OPTIONS_DHCP_OPTIONAL   (1 << 0)

Definition at line 69 of file tun.h.

Referenced by add_option(), and tuntap_options_postprocess_dns().

◆ DHCP_OPTIONS_DHCP_REQUIRED

#define DHCP_OPTIONS_DHCP_REQUIRED   (1 << 1)

Definition at line 70 of file tun.h.

Referenced by add_option(), open_tun(), and options_postprocess_verify_ce().

◆ IFCONFIG_AFTER_TUN_OPEN

#define IFCONFIG_AFTER_TUN_OPEN   1

Definition at line 349 of file tun.h.

Referenced by do_open_tun(), and ifconfig_order().

◆ IFCONFIG_BEFORE_TUN_OPEN

#define IFCONFIG_BEFORE_TUN_OPEN   0

Definition at line 348 of file tun.h.

Referenced by do_open_tun(), and ifconfig_order().

◆ IFCONFIG_DEFAULT

#define IFCONFIG_DEFAULT   IFCONFIG_AFTER_TUN_OPEN

Definition at line 351 of file tun.h.

Referenced by ifconfig_order().

◆ IPW32_SET_ADAPTIVE

◆ IPW32_SET_ADAPTIVE_DELAY_WINDOW

#define IPW32_SET_ADAPTIVE_DELAY_WINDOW   300

Definition at line 65 of file tun.h.

Referenced by add_option().

◆ IPW32_SET_ADAPTIVE_TRY_NETSH

#define IPW32_SET_ADAPTIVE_TRY_NETSH   20

Definition at line 66 of file tun.h.

Referenced by tun_standby().

◆ IPW32_SET_DHCP_MASQ

◆ IPW32_SET_IPAPI

#define IPW32_SET_IPAPI   2 /* "--ip-win32 ipapi" */

Definition at line 79 of file tun.h.

Referenced by tuntap_set_ip_addr().

◆ IPW32_SET_MANUAL

#define IPW32_SET_MANUAL   0 /* "--ip-win32 manual" */

◆ IPW32_SET_N

#define IPW32_SET_N   5

Definition at line 82 of file tun.h.

Referenced by ascii2ipset(), ipset2ascii(), and ipset2ascii_all().

◆ IPW32_SET_NETSH

#define IPW32_SET_NETSH   1 /* "--ip-win32 netsh" */

Definition at line 78 of file tun.h.

Referenced by close_tun(), do_ifconfig_ipv4(), and options_postprocess_mutate_invariant().

◆ N_DHCP_ADDR

#define N_DHCP_ADDR   4u

◆ N_SEARCH_LIST_LEN

#define N_SEARCH_LIST_LEN   10 /* Max # of entries in domin-search list */

Definition at line 126 of file tun.h.

Referenced by add_option(), do_dns_domain_service(), and tuntap_options_postprocess_dns().

◆ ROUTE_AFTER_TUN

#define ROUTE_AFTER_TUN   1

Definition at line 380 of file tun.h.

Referenced by do_open_tun(), do_up(), and do_update().

◆ ROUTE_BEFORE_TUN

#define ROUTE_BEFORE_TUN   0

Definition at line 379 of file tun.h.

Referenced by do_open_tun(), and route_order().

◆ ROUTE_ORDER_DEFAULT

#define ROUTE_ORDER_DEFAULT   ROUTE_AFTER_TUN

Definition at line 381 of file tun.h.

Referenced by route_order().

◆ TUNNEL_TOPOLOGY

#define TUNNEL_TOPOLOGY ( tt)
Value:
((tt) ? ((tt)->topology) : TOP_UNDEF)
#define TOP_UNDEF
Definition proto.h:40

Definition at line 185 of file tun.h.

Referenced by multi_select_virtual_addr(), and multi_set_virtual_addr_env().

◆ TUNNEL_TYPE

Typedef Documentation

◆ afunix_context_t

Enumeration Type Documentation

◆ tun_driver_type

Enumerator
WINDOWS_DRIVER_UNSPECIFIED 
WINDOWS_DRIVER_TAP_WINDOWS6 
DRIVER_GENERIC_TUNTAP 
DRIVER_AFUNIX 

using an AF_UNIX socket to pass packets from/to an external program.

This is always defined. We error out if a user tries to open this type of backend on unsupported platforms.

DRIVER_NULL 
DRIVER_DCO 
DRIVER_UTUN 

macOS internal tun driver

Definition at line 43 of file tun.h.

Function Documentation

◆ adapter_index_of_ip()

DWORD adapter_index_of_ip ( const IP_ADAPTER_INFO * list,
const in_addr_t ip,
int * count,
in_addr_t * netmask )

◆ ascii2ipset()

int ascii2ipset ( const char * name)

Definition at line 6237 of file tun.c.

References ASSERT, IPW32_SET_N, ipset_names::short_form, and SIZE.

Referenced by add_option().

◆ close_tun()

◆ close_tun_handle()

void close_tun_handle ( struct tuntap * tt)

◆ dev_type_enum()

◆ dev_type_string()

const char * dev_type_string ( const char * dev,
const char * dev_type )

Definition at line 537 of file tun.c.

References dev_type_enum(), DEV_TYPE_TAP, and DEV_TYPE_TUN.

Referenced by do_open_tun(), open_tun_afunix(), and options_string().

◆ dhcp_release_by_adapter_index()

bool dhcp_release_by_adapter_index ( const DWORD adapter_index)

Definition at line 4771 of file tun.c.

References D_TUNTAP_INFO, gc, gc_free(), gc_new(), get_interface_info(), M_WARN, msg, status, and strerror_win32().

Referenced by add_option(), and dhcp_release().

◆ dhcp_renew_by_adapter_index()

bool dhcp_renew_by_adapter_index ( const DWORD adapter_index)

Definition at line 4812 of file tun.c.

References D_TUNTAP_INFO, gc, gc_free(), gc_new(), get_interface_info(), M_WARN, msg, status, and strerror_win32().

Referenced by add_option(), and dhcp_renew().

◆ do_ifconfig()

void do_ifconfig ( struct tuntap * tt,
const char * ifname,
int tun_mtu,
const struct env_set * es,
openvpn_net_ctx_t * ctx )

do_ifconfig - configure the tunnel interface

Parameters
ttthe tuntap interface context
ifnamethe human readable interface name
tun_mtuthe MTU value to set the interface to
esthe environment to be used when executing the commands
ctxthe networking API opaque context

Definition at line 1563 of file tun.c.

References D_LOW, tuntap::did_ifconfig_ipv6_setup, tuntap::did_ifconfig_setup, do_ifconfig_ipv4(), do_ifconfig_ipv6(), tuntap::local, tuntap::local_ipv6, management_set_state(), msg, net_ctx_free(), and OPENVPN_STATE_ASSIGN_IP.

Referenced by do_open_tun().

◆ do_ifconfig_setenv()

◆ fork_register_dns_action()

◆ get_adapter()

const IP_ADAPTER_INFO * get_adapter ( const IP_ADAPTER_INFO * ai,
DWORD index )

◆ get_adapter_info()

const IP_ADAPTER_INFO * get_adapter_info ( DWORD index,
struct gc_arena * gc )

Definition at line 4210 of file tun.c.

References gc, get_adapter(), and get_adapter_info_list().

Referenced by delete_temp_addresses(), dhcp_status(), and get_bypass_addresses().

◆ get_adapter_info_list()

const IP_ADAPTER_INFO * get_adapter_info_list ( struct gc_arena * gc)

◆ get_per_adapter_info()

const IP_PER_ADAPTER_INFO * get_per_adapter_info ( const DWORD index,
struct gc_arena * gc )

◆ get_tun_adapter()

const IP_ADAPTER_INFO * get_tun_adapter ( const struct tuntap * tt,
const IP_ADAPTER_INFO * list )

Definition at line 4295 of file tun.c.

References tuntap::adapter_index, and get_adapter().

Referenced by is_adapter_up(), and windows_route_find_if_index().

◆ guess_tuntap_dev()

const char * guess_tuntap_dev ( const char * dev,
const char * dev_type,
const char * dev_node,
struct gc_arena * gc )

Definition at line 557 of file tun.c.

References dev_type_enum(), DEV_TYPE_TAP, DEV_TYPE_TUN, gc, and netsh_get_id().

Referenced by do_open_tun().

◆ ifconfig_options_string()

const char * ifconfig_options_string ( const struct tuntap * tt,
bool remote,
bool disable,
struct gc_arena * gc )

◆ ifconfig_order()

int ifconfig_order ( const struct tuntap * tt)
inlinestatic

◆ init_tun()

struct tuntap * init_tun ( const char * dev,
const char * dev_type,
int topology,
const char * ifconfig_local_parm,
const char * ifconfig_remote_netmask_parm,
const char * ifconfig_ipv6_local_parm,
int ifconfig_ipv6_netbits_parm,
const char * ifconfig_ipv6_remote_parm,
const struct addrinfo * local_public,
const struct addrinfo * remote_public,
const bool strict_warn,
struct env_set * es,
openvpn_net_ctx_t * ctx,
struct tuntap * tt )

◆ init_tun_post()

◆ ipconfig_register_dns()

◆ ipset2ascii()

const char * ipset2ascii ( int index)

Definition at line 6252 of file tun.c.

References ASSERT, IPW32_SET_N, ipset_names::short_form, and SIZE.

Referenced by ipset2ascii_all().

◆ ipset2ascii_all()

const char * ipset2ascii_all ( struct gc_arena * gc)

Definition at line 6266 of file tun.c.

References alloc_buf_gc(), ASSERT, BSTR, buf_printf(), gc, ipset2ascii(), IPW32_SET_N, and SIZE.

Referenced by add_option().

◆ is_adapter_up()

bool is_adapter_up ( const struct tuntap * tt,
const IP_ADAPTER_INFO * list )

◆ is_dev_type()

bool is_dev_type ( const char * dev,
const char * dev_type,
const char * match_type )

Definition at line 500 of file tun.c.

References ASSERT.

Referenced by dev_type_enum(), and do_init_tun().

◆ is_ip_in_adapter_subnet()

bool is_ip_in_adapter_subnet ( const IP_ADAPTER_INFO * ai,
const in_addr_t ip,
in_addr_t * highest_netmask )

◆ is_ip_packet_valid()

bool is_ip_packet_valid ( const struct buffer * buf)
inlinestatic

Definition at line 505 of file tun.h.

References BLENZ, CBPTR, OPENVPN_IPH_GET_VER, and openvpn_iphdr::version_len.

◆ is_tun_p2p()

bool is_tun_p2p ( const struct tuntap * tt)

◆ is_tun_type_set()

bool is_tun_type_set ( const struct tuntap * tt)
inlinestatic

Definition at line 631 of file tun.h.

References DEV_TYPE_UNDEF, and tuntap::type.

Referenced by can_preserve_tun(), and do_close_tun().

◆ open_tun()

◆ open_tun_null()

void open_tun_null ( struct tuntap * tt)
inlinestatic

Definition at line 637 of file tun.h.

References tuntap::actual_name, and string_alloc().

Referenced by open_tun_backend().

◆ print_tun_backend_driver()

const char * print_tun_backend_driver ( enum tun_driver_type driver)

◆ read_tun()

ssize_t read_tun ( const struct tuntap * tt,
uint8_t * buf,
int len )

References gc.

Referenced by read_incoming_tun().

◆ route_order()

int route_order ( const struct tuntap * tt)
inlinestatic

Definition at line 384 of file tun.h.

References tuntap::backend_driver, DRIVER_AFUNIX, ROUTE_BEFORE_TUN, and ROUTE_ORDER_DEFAULT.

Referenced by do_open_tun(), do_up(), and do_update().

◆ show_adapters()

◆ show_tap_win_adapters()

void show_tap_win_adapters ( msglvl_t msglevel,
msglvl_t warnlevel )

◆ show_valid_win32_tun_subnets()

void show_valid_win32_tun_subnets ( void )

Definition at line 3775 of file tun.c.

Referenced by add_option().

◆ tap_allow_nonadmin_access()

void tap_allow_nonadmin_access ( const char * dev_node)

◆ tap_win_getinfo()

const char * tap_win_getinfo ( const struct tuntap * tt,
struct gc_arena * gc )

◆ tun_event_handle()

event_t tun_event_handle ( const struct tuntap * tt)
inlinestatic

Definition at line 591 of file tun.h.

References tuntap::rw_handle.

Referenced by tun_set().

◆ tun_name_is_fixed()

bool tun_name_is_fixed ( const char * dev)

Definition at line 1794 of file tun.c.

References has_digit().

◆ tun_open_device()

◆ tun_read_queue()

◆ tun_set()

void tun_set ( struct tuntap * tt,
struct event_set * es,
unsigned int rwflags,
void * arg,
unsigned int * persistent )
inlinestatic

◆ tun_show_debug()

void tun_show_debug ( const struct tuntap * tt)

◆ tun_standby()

◆ tun_standby_init()

void tun_standby_init ( struct tuntap * tt)

Definition at line 5295 of file tun.c.

References tuntap::standby_iter.

Referenced by do_up(), and do_update().

◆ tun_stat()

const char * tun_stat ( const struct tuntap * tt,
unsigned int rwflags,
struct gc_arena * gc )

◆ tun_write_queue()

◆ tun_write_win32()

int tun_write_win32 ( struct tuntap * tt,
const struct buffer * buf )

◆ tuntap_abort()

bool tuntap_abort ( int status)
inlinestatic

Definition at line 490 of file tun.h.

References status.

Referenced by read_incoming_tun().

◆ tuntap_defined()

bool tuntap_defined ( const struct tuntap * tt)
inlinestatic

◆ tuntap_is_dco_win()

bool tuntap_is_dco_win ( const struct tuntap * tt)
inlinestatic

◆ tuntap_is_dco_win_timeout()

bool tuntap_is_dco_win_timeout ( const struct tuntap * tt,
ssize_t status )
inlinestatic

Definition at line 538 of file tun.h.

References openvpn_errno, status, and tuntap_is_dco_win().

Referenced by read_incoming_link().

◆ tuntap_stop()

bool tuntap_stop ( int status)
inlinestatic

Definition at line 476 of file tun.h.

References status.

Referenced by read_incoming_tun().

◆ undo_ifconfig()

void undo_ifconfig ( const struct tuntap * tt,
openvpn_net_ctx_t * ctx )

undo_ifconfig - undo configuration of the tunnel interface

Parameters
ttthe tuntap interface context
ctxthe networking API opaque context

Definition at line 1652 of file tun.c.

References tuntap::backend_driver, tuntap::did_ifconfig_ipv6_setup, tuntap::did_ifconfig_setup, DRIVER_AFUNIX, DRIVER_NULL, net_ctx_reset(), undo_ifconfig_ipv4(), and undo_ifconfig_ipv6().

Referenced by do_close_tun_simple().

◆ verify_255_255_255_252()

void verify_255_255_255_252 ( in_addr_t local,
in_addr_t remote )

Definition at line 3739 of file tun.c.

References gc, gc_free(), gc_new(), M_FATAL, msg, and print_in_addr_t().

Referenced by init_tun().

◆ warn_on_use_of_common_subnets()

◆ write_tun()

ssize_t write_tun ( const struct tuntap * tt,
const uint8_t * buf,
int len )

Referenced by process_outgoing_tun().