OpenVPN
tun.h
Go to the documentation of this file.
1 /*
2  * OpenVPN -- An application to securely tunnel IP networks
3  * over a single TCP/UDP port, with support for SSL/TLS-based
4  * session authentication and key exchange,
5  * packet encryption, packet authentication, and
6  * packet compression.
7  *
8  * Copyright (C) 2002-2024 OpenVPN Inc <sales@openvpn.net>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2
12  * as published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 #ifndef TUN_H
25 #define TUN_H
26 
27 #ifdef _WIN32
28 #include <winioctl.h>
29 #include <tap-windows.h>
30 #include <setupapi.h>
31 #include <cfgmgr32.h>
32 #endif
33 
34 #include "buffer.h"
35 #include "error.h"
36 #include "mtu.h"
37 #include "win32.h"
38 #include "event.h"
39 #include "proto.h"
40 #include "misc.h"
41 #include "networking.h"
42 #include "ring_buffer.h"
43 #include "dco.h"
44 
45 #ifdef _WIN32
46 #define WINTUN_COMPONENT_ID "wintun"
47 #define DCO_WIN_REFERENCE_STRING "ovpn-dco"
48 
54 };
55 #endif
56 
57 #if defined(_WIN32) || defined(TARGET_ANDROID)
58 
59 #define TUN_ADAPTER_INDEX_INVALID ((DWORD)-1)
60 
61 /* time constants for --ip-win32 adaptive */
62 #define IPW32_SET_ADAPTIVE_DELAY_WINDOW 300
63 #define IPW32_SET_ADAPTIVE_TRY_NETSH 20
64 
65 /* bit flags for DHCP options */
66 #define DHCP_OPTIONS_DHCP_OPTIONAL (1<<0)
67 #define DHCP_OPTIONS_DHCP_REQUIRED (1<<1)
68 
70  /* --ip-win32 options */
72 
74 
75 #define IPW32_SET_MANUAL 0 /* "--ip-win32 manual" */
76 #define IPW32_SET_NETSH 1 /* "--ip-win32 netsh" */
77 #define IPW32_SET_IPAPI 2 /* "--ip-win32 ipapi" */
78 #define IPW32_SET_DHCP_MASQ 3 /* "--ip-win32 dynamic" */
79 #define IPW32_SET_ADAPTIVE 4 /* "--ip-win32 adaptive" */
80 #define IPW32_SET_N 5
82 
83 #ifdef _WIN32
84  HANDLE msg_channel;
85 #endif
86 
87  /* --ip-win32 dynamic options */
91 
92  /* --tap-sleep option */
93  int tap_sleep;
94 
95  /* --dhcp-option options */
96 
98 
99  const char *domain; /* DOMAIN (15) */
100 
101  const char *netbios_scope; /* NBS (47) */
102 
103  int netbios_node_type; /* NBT 1,2,4,8 (46) */
104 
105 #define N_DHCP_ADDR 4 /* Max # of addresses allowed for
106  * DNS, WINS, etc. */
107 
108  /* DNS (6) */
109  in_addr_t dns[N_DHCP_ADDR];
110  int dns_len;
111 
112  /* WINS (44) */
113  in_addr_t wins[N_DHCP_ADDR];
114  int wins_len;
115 
116  /* NTP (42) */
117  in_addr_t ntp[N_DHCP_ADDR];
118  int ntp_len;
119 
120  /* NBDD (45) */
121  in_addr_t nbdd[N_DHCP_ADDR];
122  int nbdd_len;
123 
124 #define N_SEARCH_LIST_LEN 10 /* Max # of entries in domin-search list */
125 
126  /* SEARCH (119), MacOS, Linux, Win10 1809+ */
129 
130  /* DISABLE_NBT (43, Vendor option 001) */
131  bool disable_nbt;
132 
134  bool dhcp_pre_release;
135 
136  bool register_dns;
137 
138  struct in6_addr dns6[N_DHCP_ADDR];
139  int dns6_len;
140 #if defined(TARGET_ANDROID)
141  const char *http_proxy;
142  int http_proxy_port;
143 #endif
144 };
145 
146 #elif defined(TARGET_LINUX)
147 
148 struct tuntap_options {
149  int txqueuelen;
150  bool disable_dco;
151 };
152 
153 #elif defined(TARGET_FREEBSD)
154 
155 struct tuntap_options {
156  bool disable_dco;
157 };
158 
159 #else /* if defined(_WIN32) || defined(TARGET_ANDROID) */
160 
161 struct tuntap_options {
162  int dummy; /* not used */
163  bool disable_dco; /* not used, but removes the need in #ifdefs */
164 };
165 
166 #endif /* if defined(_WIN32) || defined(TARGET_ANDROID) */
167 
168 /*
169  * Define a TUN/TAP dev.
170  */
171 
172 struct tuntap
173 {
174 #define TUNNEL_TYPE(tt) ((tt) ? ((tt)->type) : DEV_TYPE_UNDEF)
175  int type; /* DEV_TYPE_x as defined in proto.h */
176 
177 #define TUNNEL_TOPOLOGY(tt) ((tt) ? ((tt)->topology) : TOP_UNDEF)
178  int topology; /* one of the TOP_x values */
179 
182 
183  bool persistent_if; /* if existed before, keep on program end */
184 
185  struct tuntap_options options; /* options set on command line */
186 
187  char *actual_name; /* actual name of TUN/TAP dev, usually including unit number */
188 
189  /* ifconfig parameters */
190  in_addr_t local;
191  in_addr_t remote_netmask;
192 
193  struct in6_addr local_ipv6;
194  struct in6_addr remote_ipv6;
195  int netbits_ipv6;
196 
197 #ifdef _WIN32
198  HANDLE hand;
201  struct rw_handle rw_handle;
202 
203  /* used for setting interface address via IP Helper API
204  * or DHCP masquerade */
208  in_addr_t adapter_netmask;
209 
210  /* Windows adapter index for TAP-Windows adapter,
211  * ~0 if undefined */
212  DWORD adapter_index;
213 
215  int standby_iter;
216 
221 #else /* ifdef _WIN32 */
222  int fd; /* file descriptor for TUN/TAP dev */
223 #endif /* ifdef _WIN32 */
224 
225 #ifdef TARGET_SOLARIS
226  int ip_fd;
227 #endif
228 
229 #ifdef HAVE_NET_IF_UTUN_H
230  bool is_utun;
231 #endif
232  /* used for printing status info only */
233  unsigned int rwflags_debug;
234 
236 };
237 
238 static inline bool
239 tuntap_defined(const struct tuntap *tt)
240 {
241 #ifdef _WIN32
242  return tt && tt->hand != NULL;
243 #else
244  return tt && tt->fd >= 0;
245 #endif
246 }
247 
248 #ifdef _WIN32
249 static inline bool
250 tuntap_is_wintun(struct tuntap *tt)
251 {
252  return tt && tt->windows_driver == WINDOWS_DRIVER_WINTUN;
253 }
254 
255 static inline bool
256 tuntap_ring_empty(struct tuntap *tt)
257 {
258  return tuntap_is_wintun(tt) && (tt->wintun_send_ring->head == tt->wintun_send_ring->tail);
259 }
260 #endif
261 
262 /*
263  * Function prototypes
264  */
265 
266 void open_tun(const char *dev, const char *dev_type, const char *dev_node,
267  struct tuntap *tt, openvpn_net_ctx_t *ctx);
268 
269 void close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx);
270 
271 void tun_open_device(struct tuntap *tt, const char *dev_node,
272  const char **device_guid, struct gc_arena *gc);
273 
274 void close_tun_handle(struct tuntap *tt);
275 
276 int write_tun(struct tuntap *tt, uint8_t *buf, int len);
277 
278 int read_tun(struct tuntap *tt, uint8_t *buf, int len);
279 
280 void tuncfg(const char *dev, const char *dev_type, const char *dev_node,
281  int persist_mode, const char *username,
282  const char *groupname, const struct tuntap_options *options,
283  openvpn_net_ctx_t *ctx);
284 
285 const char *guess_tuntap_dev(const char *dev,
286  const char *dev_type,
287  const char *dev_node,
288  struct gc_arena *gc);
289 
290 struct tuntap *init_tun(const char *dev, /* --dev option */
291  const char *dev_type, /* --dev-type option */
292  int topology, /* one of the TOP_x values */
293  const char *ifconfig_local_parm, /* --ifconfig parm 1 */
294  const char *ifconfig_remote_netmask_parm, /* --ifconfig parm 2 */
295  const char *ifconfig_ipv6_local_parm, /* --ifconfig parm 1 / IPv6 */
296  int ifconfig_ipv6_netbits_parm, /* --ifconfig parm 1 / bits */
297  const char *ifconfig_ipv6_remote_parm, /* --ifconfig parm 2 / IPv6 */
298  struct addrinfo *local_public,
299  struct addrinfo *remote_public,
300  const bool strict_warn,
301  struct env_set *es,
302  openvpn_net_ctx_t *ctx,
303  struct tuntap *tt);
304 
305 void init_tun_post(struct tuntap *tt,
306  const struct frame *frame,
307  const struct tuntap_options *options);
308 
309 void do_ifconfig_setenv(const struct tuntap *tt,
310  struct env_set *es);
311 
321 void do_ifconfig(struct tuntap *tt, const char *ifname, int tun_mtu,
322  const struct env_set *es, openvpn_net_ctx_t *ctx);
323 
330 void undo_ifconfig(struct tuntap *tt, openvpn_net_ctx_t *ctx);
331 
332 bool is_dev_type(const char *dev, const char *dev_type, const char *match_type);
333 
334 int dev_type_enum(const char *dev, const char *dev_type);
335 
336 const char *dev_type_string(const char *dev, const char *dev_type);
337 
338 const char *ifconfig_options_string(const struct tuntap *tt, bool remote, bool disable, struct gc_arena *gc);
339 
340 bool is_tun_p2p(const struct tuntap *tt);
341 
342 void check_subnet_conflict(const in_addr_t ip,
343  const in_addr_t netmask,
344  const char *prefix);
345 
347 
348 /*
349  * Should ifconfig be called before or after
350  * tun dev open?
351  */
352 
353 #define IFCONFIG_BEFORE_TUN_OPEN 0
354 #define IFCONFIG_AFTER_TUN_OPEN 1
355 
356 #define IFCONFIG_DEFAULT IFCONFIG_AFTER_TUN_OPEN
357 
358 static inline int
359 ifconfig_order(void)
360 {
361 #if defined(TARGET_LINUX)
363 #elif defined(TARGET_SOLARIS)
365 #elif defined(TARGET_OPENBSD)
367 #elif defined(TARGET_DARWIN)
369 #elif defined(TARGET_NETBSD)
371 #elif defined(_WIN32)
373 #elif defined(TARGET_ANDROID)
375 #else /* if defined(TARGET_LINUX) */
376  return IFCONFIG_DEFAULT;
377 #endif
378 }
379 
380 #define ROUTE_BEFORE_TUN 0
381 #define ROUTE_AFTER_TUN 1
382 #define ROUTE_ORDER_DEFAULT ROUTE_AFTER_TUN
383 
384 static inline int
385 route_order(void)
386 {
387 #if defined(TARGET_ANDROID)
388  return ROUTE_BEFORE_TUN;
389 #else
390  return ROUTE_ORDER_DEFAULT;
391 #endif
392 }
393 
394 
395 #ifdef _WIN32
396 
397 struct tap_reg
398 {
399  const char *guid;
401  struct tap_reg *next;
402 };
403 
404 struct panel_reg
405 {
406  const char *name;
407  const char *guid;
408  struct panel_reg *next;
409 };
410 
412 {
414  const char *device_interface;
416 };
417 
418 int ascii2ipset(const char *name);
419 
420 const char *ipset2ascii(int index);
421 
422 const char *ipset2ascii_all(struct gc_arena *gc);
423 
424 void verify_255_255_255_252(in_addr_t local, in_addr_t remote);
425 
426 const IP_ADAPTER_INFO *get_adapter_info_list(struct gc_arena *gc);
427 
428 const IP_ADAPTER_INFO *get_tun_adapter(const struct tuntap *tt, const IP_ADAPTER_INFO *list);
429 
430 const IP_ADAPTER_INFO *get_adapter_info(DWORD index, struct gc_arena *gc);
431 
432 const IP_PER_ADAPTER_INFO *get_per_adapter_info(const DWORD index, struct gc_arena *gc);
433 
434 const IP_ADAPTER_INFO *get_adapter(const IP_ADAPTER_INFO *ai, DWORD index);
435 
436 bool is_adapter_up(const struct tuntap *tt, const IP_ADAPTER_INFO *list);
437 
438 bool is_ip_in_adapter_subnet(const IP_ADAPTER_INFO *ai, const in_addr_t ip, in_addr_t *highest_netmask);
439 
440 DWORD adapter_index_of_ip(const IP_ADAPTER_INFO *list,
441  const in_addr_t ip,
442  int *count,
443  in_addr_t *netmask);
444 
445 void show_tap_win_adapters(int msglev, int warnlev);
446 
447 void show_adapters(int msglev);
448 
449 void tap_allow_nonadmin_access(const char *dev_node);
450 
452 
453 const char *tap_win_getinfo(const struct tuntap *tt, struct gc_arena *gc);
454 
455 void tun_show_debug(struct tuntap *tt);
456 
457 bool dhcp_release_by_adapter_index(const DWORD adapter_index);
458 
459 bool dhcp_renew_by_adapter_index(const DWORD adapter_index);
460 
461 void fork_register_dns_action(struct tuntap *tt);
462 
463 void ipconfig_register_dns(const struct env_set *es);
464 
465 void tun_standby_init(struct tuntap *tt);
466 
467 bool tun_standby(struct tuntap *tt);
468 
469 int tun_read_queue(struct tuntap *tt, int maxsize);
470 
471 int tun_write_queue(struct tuntap *tt, struct buffer *buf);
472 
473 static inline bool
474 tuntap_stop(int status)
475 {
476  /*
477  * This corresponds to the STATUS_NO_SUCH_DEVICE
478  * error in tapdrvr.c.
479  */
480  if (status < 0)
481  {
482  return GetLastError() == ERROR_FILE_NOT_FOUND;
483  }
484  return false;
485 }
486 
487 static inline bool
488 tuntap_abort(int status)
489 {
490  /*
491  * Typically generated when driver is halted.
492  */
493  if (status < 0)
494  {
495  return GetLastError() == ERROR_OPERATION_ABORTED;
496  }
497  return false;
498 }
499 
500 int tun_write_win32(struct tuntap *tt, struct buffer *buf);
501 
502 static inline ULONG
503 wintun_ring_packet_align(ULONG size)
504 {
505  return (size + (WINTUN_PACKET_ALIGN - 1)) & ~(WINTUN_PACKET_ALIGN - 1);
506 }
507 
508 static inline ULONG
509 wintun_ring_wrap(ULONG value)
510 {
511  return value & (WINTUN_RING_CAPACITY - 1);
512 }
513 
514 static inline void
515 read_wintun(struct tuntap *tt, struct buffer *buf)
516 {
517  struct tun_ring *ring = tt->wintun_send_ring;
518  ULONG head = ring->head;
519  ULONG tail = ring->tail;
520  ULONG content_len;
521  struct TUN_PACKET *packet;
522  ULONG aligned_packet_size;
523 
524  *buf = tt->reads.buf_init;
525  buf->len = 0;
526 
527  if ((head >= WINTUN_RING_CAPACITY) || (tail >= WINTUN_RING_CAPACITY))
528  {
529  msg(M_INFO, "Wintun: ring capacity exceeded");
530  buf->len = -1;
531  return;
532  }
533 
534  if (head == tail)
535  {
536  /* nothing to read */
537  return;
538  }
539 
540  content_len = wintun_ring_wrap(tail - head);
541  if (content_len < sizeof(struct TUN_PACKET_HEADER))
542  {
543  msg(M_INFO, "Wintun: incomplete packet header in send ring");
544  buf->len = -1;
545  return;
546  }
547 
548  packet = (struct TUN_PACKET *) &ring->data[head];
549  if (packet->size > WINTUN_MAX_PACKET_SIZE)
550  {
551  msg(M_INFO, "Wintun: packet too big in send ring");
552  buf->len = -1;
553  return;
554  }
555 
556  aligned_packet_size = wintun_ring_packet_align(sizeof(struct TUN_PACKET_HEADER) + packet->size);
557  if (aligned_packet_size > content_len)
558  {
559  msg(M_INFO, "Wintun: incomplete packet in send ring");
560  buf->len = -1;
561  return;
562  }
563 
564  buf_write(buf, packet->data, packet->size);
565 
566  head = wintun_ring_wrap(head + aligned_packet_size);
567  ring->head = head;
568 }
569 
570 static inline bool
571 is_ip_packet_valid(const struct buffer *buf)
572 {
573  const struct openvpn_iphdr *ih = (const struct openvpn_iphdr *)BPTR(buf);
574 
575  if (OPENVPN_IPH_GET_VER(ih->version_len) == 4)
576  {
577  if (BLEN(buf) < sizeof(struct openvpn_iphdr))
578  {
579  return false;
580  }
581  }
582  else if (OPENVPN_IPH_GET_VER(ih->version_len) == 6)
583  {
584  if (BLEN(buf) < sizeof(struct openvpn_ipv6hdr))
585  {
586  return false;
587  }
588  }
589  else
590  {
591  return false;
592  }
593 
594  return true;
595 }
596 
597 static inline int
598 write_wintun(struct tuntap *tt, struct buffer *buf)
599 {
600  struct tun_ring *ring = tt->wintun_receive_ring;
601  ULONG head = ring->head;
602  ULONG tail = ring->tail;
603  ULONG aligned_packet_size;
604  ULONG buf_space;
605  struct TUN_PACKET *packet;
606 
607  /* wintun marks ring as corrupted (overcapacity) if it receives invalid IP packet */
608  if (!is_ip_packet_valid(buf))
609  {
610  msg(D_LOW, "write_wintun(): drop invalid IP packet");
611  return 0;
612  }
613 
614  if ((head >= WINTUN_RING_CAPACITY) || (tail >= WINTUN_RING_CAPACITY))
615  {
616  msg(M_INFO, "write_wintun(): head/tail value is over capacity");
617  return -1;
618  }
619 
620  aligned_packet_size = wintun_ring_packet_align(sizeof(struct TUN_PACKET_HEADER) + BLEN(buf));
621  buf_space = wintun_ring_wrap(head - tail - WINTUN_PACKET_ALIGN);
622  if (aligned_packet_size > buf_space)
623  {
624  msg(M_INFO, "write_wintun(): ring is full");
625  return 0;
626  }
627 
628  /* copy packet size and data into ring */
629  packet = (struct TUN_PACKET * )&ring->data[tail];
630  packet->size = BLEN(buf);
631  memcpy(packet->data, BPTR(buf), BLEN(buf));
632 
633  /* move ring tail */
634  ring->tail = wintun_ring_wrap(tail + aligned_packet_size);
635  if (ring->alertable != 0)
636  {
637  SetEvent(tt->rw_handle.write);
638  }
639 
640  return BLEN(buf);
641 }
642 
643 static inline int
644 write_tun_buffered(struct tuntap *tt, struct buffer *buf)
645 {
647  {
648  return write_wintun(tt, buf);
649  }
650  else
651  {
652  return tun_write_win32(tt, buf);
653  }
654 }
655 
656 static inline bool
657 tuntap_is_dco_win(struct tuntap *tt)
658 {
659  return tt && tt->windows_driver == WINDOWS_DRIVER_DCO;
660 }
661 
662 static inline bool
663 tuntap_is_dco_win_timeout(struct tuntap *tt, int status)
664 {
665  return tuntap_is_dco_win(tt) && (status < 0) && (openvpn_errno() == ERROR_NETNAME_DELETED);
666 }
667 
668 const char *
669 print_windows_driver(enum windows_driver_type windows_driver);
670 
671 #else /* ifdef _WIN32 */
672 
673 static inline bool
674 tuntap_stop(int status)
675 {
676  return false;
677 }
678 
679 static inline bool
680 tuntap_abort(int status)
681 {
682  return false;
683 }
684 
685 static inline void
686 tun_standby_init(struct tuntap *tt)
687 {
688 }
689 
690 static inline bool
691 tun_standby(struct tuntap *tt)
692 {
693  return true;
694 }
695 
696 
697 static inline bool
698 tuntap_is_dco_win(struct tuntap *tt)
699 {
700  return false;
701 }
702 
703 static inline bool
704 tuntap_is_dco_win_timeout(struct tuntap *tt, int status)
705 {
706  return false;
707 }
708 
709 #endif /* ifdef _WIN32 */
710 
711 /*
712  * TUN/TAP I/O wait functions
713  */
714 
715 static inline event_t
716 tun_event_handle(const struct tuntap *tt)
717 {
718 #ifdef _WIN32
719  return &tt->rw_handle;
720 #else
721  return tt->fd;
722 #endif
723 }
724 
725 static inline void
726 tun_set(struct tuntap *tt,
727  struct event_set *es,
728  unsigned int rwflags,
729  void *arg,
730  unsigned int *persistent)
731 {
732  if (!tuntap_defined(tt) || tuntap_is_dco_win(tt))
733  {
734  return;
735  }
736 
737  /* if persistent is defined, call event_ctl only if rwflags has changed since last call */
738  if (!persistent || *persistent != rwflags)
739  {
740  event_ctl(es, tun_event_handle(tt), rwflags, arg);
741  if (persistent)
742  {
743  *persistent = rwflags;
744  }
745  }
746 #ifdef _WIN32
747  if (tt->windows_driver == WINDOWS_DRIVER_TAP_WINDOWS6 && (rwflags & EVENT_READ))
748  {
749  tun_read_queue(tt, 0);
750  }
751 #endif
752  tt->rwflags_debug = rwflags;
753 
754 }
755 
756 const char *tun_stat(const struct tuntap *tt, unsigned int rwflags, struct gc_arena *gc);
757 bool tun_name_is_fixed(const char *dev);
758 
759 static inline bool
760 is_tun_type_set(const struct tuntap *tt)
761 {
762  return tt && tt->type != DEV_TYPE_UNDEF;
763 }
764 
765 #endif /* TUN_H */
tun_show_debug
void tun_show_debug(struct tuntap *tt)
Definition: tun.c:6877
tuntap::reads
struct overlapped_io reads
Definition: tun.h:198
read_tun
int read_tun(struct tuntap *tt, uint8_t *buf, int len)
tuntap_options::dhcp_masq_custom_offset
bool dhcp_masq_custom_offset
Definition: tun.h:88
WINTUN_RING_CAPACITY
#define WINTUN_RING_CAPACITY
Definition: ring_buffer.h:39
tuntap_options::dns
in_addr_t dns[N_DHCP_ADDR]
Definition: tun.h:108
tuntap_options::domain_search_list
const char * domain_search_list[N_SEARCH_LIST_LEN]
Definition: tun.h:126
M_INFO
#define M_INFO
Definition: errlevel.h:55
WINTUN_PACKET_ALIGN
#define WINTUN_PACKET_ALIGN
Definition: ring_buffer.h:42
tun_write_queue
int tun_write_queue(struct tuntap *tt, struct buffer *buf)
Definition: tun.c:3689
tuntap::did_ifconfig_setup
bool did_ifconfig_setup
Definition: tun.h:179
tuntap_options::dhcp_renew
bool dhcp_renew
Definition: tun.h:132
WINDOWS_DRIVER_UNSPECIFIED
@ WINDOWS_DRIVER_UNSPECIFIED
Definition: tun.h:50
open_tun
void open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt, openvpn_net_ctx_t *ctx)
Definition: tun.c:6815
error.h
device_instance_id_interface::next
struct device_instance_id_interface * next
Definition: tun.h:414
tun_event_handle
static event_t tun_event_handle(const struct tuntap *tt)
Definition: tun.h:715
ROUTE_ORDER_DEFAULT
#define ROUTE_ORDER_DEFAULT
Definition: tun.h:381
ROUTE_BEFORE_TUN
#define ROUTE_BEFORE_TUN
Definition: tun.h:379
tuntap_options::register_dns
bool register_dns
Definition: tun.h:135
networking.h
buffer::len
int len
Length in bytes of the actual content within the allocated memory.
Definition: buffer.h:66
tuntap_options::nbdd
in_addr_t nbdd[N_DHCP_ADDR]
Definition: tun.h:120
is_tun_p2p
bool is_tun_p2p(const struct tuntap *tt)
Definition: tun.c:738
tuntap_options::tap_sleep
int tap_sleep
Definition: tun.h:93
win32.h
tuntap::windows_driver
enum windows_driver_type windows_driver
Definition: tun.h:213
tuntap_options::domain_search_list_len
int domain_search_list_len
Definition: tun.h:127
tuntap_is_wintun
static bool tuntap_is_wintun(struct tuntap *tt)
Definition: tun.h:249
es
struct env_set * es
Definition: test_pkcs11.c:133
tuntap_options::netbios_node_type
int netbios_node_type
Definition: tun.h:103
print_windows_driver
const char * print_windows_driver(enum windows_driver_type windows_driver)
Definition: tun.c:7145
tuntap::type
int type
Definition: tun.h:174
TUN_PACKET::data
UCHAR data[WINTUN_MAX_PACKET_SIZE]
Definition: ring_buffer.h:80
close_tun
void close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx)
Definition: tun.c:6995
tuntap::wintun_receive_ring
struct tun_ring * wintun_receive_ring
Definition: tun.h:219
wintun_ring_wrap
static ULONG wintun_ring_wrap(ULONG value)
Definition: tun.h:508
tuntap::wintun_send_ring
struct tun_ring * wintun_send_ring
Definition: tun.h:218
tuntap_abort
static bool tuntap_abort(int status)
Definition: tun.h:487
windows_driver_type
windows_driver_type
Definition: tun.h:49
close_tun_handle
void close_tun_handle(struct tuntap *tt)
Definition: tun.c:6954
panel_reg
Definition: tun.h:403
tuntap_options::netbios_scope
const char * netbios_scope
Definition: tun.h:101
warn_on_use_of_common_subnets
void warn_on_use_of_common_subnets(openvpn_net_ctx_t *ctx)
Definition: tun.c:642
tuntap_options::ntp_len
int ntp_len
Definition: tun.h:117
openvpn_net_ctx_t
void * openvpn_net_ctx_t
Definition: networking.h:28
D_LOW
#define D_LOW
Definition: errlevel.h:97
tun_standby_init
void tun_standby_init(struct tuntap *tt)
Definition: tun.c:5817
is_ip_packet_valid
static bool is_ip_packet_valid(const struct buffer *buf)
Definition: tun.h:570
EVENT_READ
#define EVENT_READ
Definition: event.h:39
dhcp_renew_by_adapter_index
bool dhcp_renew_by_adapter_index(const DWORD adapter_index)
Definition: tun.c:5286
tuntap::wintun_send_ring_handle
HANDLE wintun_send_ring_handle
Definition: tun.h:216
get_tun_adapter
const IP_ADAPTER_INFO * get_tun_adapter(const struct tuntap *tt, const IP_ADAPTER_INFO *list)
Definition: tun.c:4737
tun_write_win32
int tun_write_win32(struct tuntap *tt, struct buffer *buf)
Definition: tun.c:3752
init_tun_post
void init_tun_post(struct tuntap *tt, const struct frame *frame, const struct tuntap_options *options)
Definition: tun.c:963
write_wintun
static int write_wintun(struct tuntap *tt, struct buffer *buf)
Definition: tun.h:597
frame
Packet geometry parameters.
Definition: mtu.h:98
IFCONFIG_AFTER_TUN_OPEN
#define IFCONFIG_AFTER_TUN_OPEN
Definition: tun.h:353
N_SEARCH_LIST_LEN
#define N_SEARCH_LIST_LEN
Definition: tun.h:123
TUN_PACKET_HEADER
Definition: ring_buffer.h:72
route_order
static int route_order(void)
Definition: tun.h:384
get_adapter
const IP_ADAPTER_INFO * get_adapter(const IP_ADAPTER_INFO *ai, DWORD index)
Definition: tun.c:4633
tuntap::actual_name
char * actual_name
Definition: tun.h:186
is_tun_type_set
static bool is_tun_type_set(const struct tuntap *tt)
Definition: tun.h:759
tun_read_queue
int tun_read_queue(struct tuntap *tt, int maxsize)
Definition: tun.c:3625
mtu.h
event_ctl
static void event_ctl(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
Definition: event.h:160
N_DHCP_ADDR
#define N_DHCP_ADDR
Definition: tun.h:105
tun_ring::tail
volatile ULONG tail
Definition: ring_buffer.h:53
get_adapter_info
const IP_ADAPTER_INFO * get_adapter_info(DWORD index, struct gc_arena *gc)
Definition: tun.c:4652
tuntap_defined
static bool tuntap_defined(const struct tuntap *tt)
Definition: tun.h:238
tuntap_options::ip_win32_defined
bool ip_win32_defined
Definition: tun.h:71
tuntap::did_ifconfig_ipv6_setup
bool did_ifconfig_ipv6_setup
Definition: tun.h:180
tap_reg::guid
const char * guid
Definition: tun.h:398
tuntap::hand
HANDLE hand
Definition: tun.h:197
tuntap::local_ipv6
struct in6_addr local_ipv6
Definition: tun.h:192
tuntap::rwflags_debug
unsigned int rwflags_debug
Definition: tun.h:232
is_dev_type
bool is_dev_type(const char *dev, const char *dev_type, const char *match_type)
Definition: tun.c:418
panel_reg::next
struct panel_reg * next
Definition: tun.h:407
wintun_ring_packet_align
static ULONG wintun_ring_packet_align(ULONG size)
Definition: tun.h:502
tun_ring
Wintun ring buffer See https://github.com/WireGuard/wintun#ring-layout.
Definition: ring_buffer.h:50
WINDOWS_DRIVER_DCO
@ WINDOWS_DRIVER_DCO
Definition: tun.h:53
tuntap_options::ip_win32_type
int ip_win32_type
Definition: tun.h:81
guess_tuntap_dev
const char * guess_tuntap_dev(const char *dev, const char *dev_type, const char *dev_node, struct gc_arena *gc)
Definition: tun.c:480
WINTUN_MAX_PACKET_SIZE
#define WINTUN_MAX_PACKET_SIZE
Definition: ring_buffer.h:41
BLEN
#define BLEN(buf)
Definition: buffer.h:127
proto.h
dev_type_string
const char * dev_type_string(const char *dev, const char *dev_type)
Definition: tun.c:457
tun_standby
bool tun_standby(struct tuntap *tt)
Definition: tun.c:5823
tuntap::rw_handle
struct rw_handle rw_handle
Definition: tun.h:200
ipset2ascii
const char * ipset2ascii(int index)
Definition: tun.c:7113
check_subnet_conflict
void check_subnet_conflict(const in_addr_t ip, const in_addr_t netmask, const char *prefix)
Definition: tun.c:611
tuntap::ipapi_instance
ULONG ipapi_instance
Definition: tun.h:206
TUN_PACKET
Definition: ring_buffer.h:77
misc.h
show_valid_win32_tun_subnets
void show_valid_win32_tun_subnets(void)
Definition: tun.c:4207
TUN_PACKET::size
uint32_t size
Definition: ring_buffer.h:79
WINDOWS_DRIVER_WINTUN
@ WINDOWS_DRIVER_WINTUN
Definition: tun.h:52
ipconfig_register_dns
void ipconfig_register_dns(const struct env_set *es)
Definition: tun.c:5352
tuntap::adapter_index
DWORD adapter_index
Definition: tun.h:211
IFCONFIG_BEFORE_TUN_OPEN
#define IFCONFIG_BEFORE_TUN_OPEN
Definition: tun.h:352
options
Definition: options.h:236
tuntap::adapter_netmask
in_addr_t adapter_netmask
Definition: tun.h:207
dco_context_t
void * dco_context_t
Definition: dco.h:254
tuntap_options
Definition: tun.h:69
tuntap_ring_empty
static bool tuntap_ring_empty(struct tuntap *tt)
Definition: tun.h:255
buffer
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
panel_reg::name
const char * name
Definition: tun.h:405
show_adapters
void show_adapters(int msglev)
Definition: tun.c:5095
event.h
tuntap_options::msg_channel
HANDLE msg_channel
Definition: tun.h:84
buf_write
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Definition: buffer.h:673
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)
Definition: tun.c:4795
tap_reg::next
struct tap_reg * next
Definition: tun.h:400
buffer.h
is_adapter_up
bool is_adapter_up(const struct tuntap *tt, const IP_ADAPTER_INFO *list)
Definition: tun.c:4750
get_adapter_info_list
const IP_ADAPTER_INFO * get_adapter_info_list(struct gc_arena *gc)
Definition: tun.c:4519
tuntap_options::wins_len
int wins_len
Definition: tun.h:113
BPTR
#define BPTR(buf)
Definition: buffer.h:124
gc_arena
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
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)
Definition: tun.c:4828
tuntap_options::nbdd_len
int nbdd_len
Definition: tun.h:121
show_tap_win_adapters
void show_tap_win_adapters(int msglev, int warnlev)
Definition: tun.c:4240
tuntap::writes
struct overlapped_io writes
Definition: tun.h:199
openvpn_ipv6hdr
Definition: proto.h:135
ifconfig_options_string
const char * ifconfig_options_string(const struct tuntap *tt, bool remote, bool disable, struct gc_arena *gc)
Definition: tun.c:665
tuntap_options::dhcp_masq_offset
int dhcp_masq_offset
Definition: tun.h:89
env_set
Definition: env_set.h:42
ring_buffer.h
tuntap_options::disable_nbt
bool disable_nbt
Definition: tun.h:130
tuntap_options::dns6_len
int dns6_len
Definition: tun.h:138
dco.h
fork_register_dns_action
void fork_register_dns_action(struct tuntap *tt)
Definition: tun.c:6130
tuntap_options::disable_dco
bool disable_dco
Definition: tun.h:73
tuntap_is_dco_win_timeout
static bool tuntap_is_dco_win_timeout(struct tuntap *tt, int status)
Definition: tun.h:662
ascii2ipset
int ascii2ipset(const char *name)
Definition: tun.c:7098
tuntap::ipapi_context
ULONG ipapi_context
Definition: tun.h:205
event_set
Definition: event.h:124
tun_ring::alertable
volatile LONG alertable
Definition: ring_buffer.h:54
tuntap::local
in_addr_t local
Definition: tun.h:189
tuntap_options::dhcp_pre_release
bool dhcp_pre_release
Definition: tun.h:133
overlapped_io::buf_init
struct buffer buf_init
Definition: win32.h:217
tuntap_options::wins
in_addr_t wins[N_DHCP_ADDR]
Definition: tun.h:112
tuntap::dco
dco_context_t dco
Definition: tun.h:234
read_wintun
static void read_wintun(struct tuntap *tt, struct buffer *buf)
Definition: tun.h:514
dhcp_release_by_adapter_index
bool dhcp_release_by_adapter_index(const DWORD adapter_index)
Definition: tun.c:5246
panel_reg::guid
const char * guid
Definition: tun.h:406
get_per_adapter_info
const IP_PER_ADAPTER_INFO * get_per_adapter_info(const DWORD index, struct gc_arena *gc)
Definition: tun.c:4546
OPENVPN_IPH_GET_VER
#define OPENVPN_IPH_GET_VER(v)
Definition: proto.h:107
openvpn_iphdr::version_len
uint8_t version_len
Definition: proto.h:109
status
static SERVICE_STATUS status
Definition: interactive.c:53
rw_handle::write
HANDLE write
Definition: win32.h:81
tuntap_options::dns6
struct in6_addr dns6[N_DHCP_ADDR]
Definition: tun.h:137
tuntap_options::domain
const char * domain
Definition: tun.h:99
tuntap::remote_netmask
in_addr_t remote_netmask
Definition: tun.h:190
tuntap
Definition: tun.h:171
rw_handle
Definition: win32.h:79
tap_reg
Definition: tun.h:396
tuntap::remote_ipv6
struct in6_addr remote_ipv6
Definition: tun.h:193
tun_open_device
void tun_open_device(struct tuntap *tt, const char *dev_node, const char **device_guid, struct gc_arena *gc)
Definition: tun.c:6648
tuntap_options::dhcp_lease_time
int dhcp_lease_time
Definition: tun.h:90
device_instance_id_interface
Definition: tun.h:410
WINDOWS_DRIVER_TAP_WINDOWS6
@ WINDOWS_DRIVER_TAP_WINDOWS6
Definition: tun.h:51
tuntap::netbits_ipv6
int netbits_ipv6
Definition: tun.h:194
tap_win_getinfo
const char * tap_win_getinfo(const struct tuntap *tt, struct gc_arena *gc)
Definition: tun.c:6859
tuntap_options::dns_len
int dns_len
Definition: tun.h:109
ifconfig_order
static int ifconfig_order(void)
Definition: tun.h:358
device_instance_id_interface::device_interface
const char * device_interface
Definition: tun.h:413
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
Definition: tun.c:1650
tuntap_options::dhcp_options
int dhcp_options
Definition: tun.h:97
tap_reg::windows_driver
enum windows_driver_type windows_driver
Definition: tun.h:399
dev_type_enum
int dev_type_enum(const char *dev, const char *dev_type)
Definition: tun.c:436
verify_255_255_255_252
void verify_255_255_255_252(in_addr_t local, in_addr_t remote)
Definition: tun.c:4170
tun_stat
const char * tun_stat(const struct tuntap *tt, unsigned int rwflags, struct gc_arena *gc)
Definition: tun.c:703
openvpn_errno
#define openvpn_errno()
Definition: error.h:72
tuncfg
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, openvpn_net_ctx_t *ctx)
openvpn_iphdr
Definition: proto.h:106
tuntap::persistent_if
bool persistent_if
Definition: tun.h:182
DEV_TYPE_UNDEF
#define DEV_TYPE_UNDEF
Definition: proto.h:35
tun_set
static void tun_set(struct tuntap *tt, struct event_set *es, unsigned int rwflags, void *arg, unsigned int *persistent)
Definition: tun.h:725
tun_name_is_fixed
bool tun_name_is_fixed(const char *dev)
Definition: tun.c:1881
ipset2ascii_all
const char * ipset2ascii_all(struct gc_arena *gc)
Definition: tun.c:7127
msg
#define msg(flags,...)
Definition: error.h:144
device_instance_id_interface::net_cfg_instance_id
LPBYTE net_cfg_instance_id
Definition: tun.h:412
tuntap_stop
static bool tuntap_stop(int status)
Definition: tun.h:473
tun_ring::data
UCHAR data[WINTUN_RING_CAPACITY+WINTUN_RING_TRAILING_BYTES]
Definition: ring_buffer.h:55
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, struct addrinfo *local_public, struct addrinfo *remote_public, const bool strict_warn, struct env_set *es, openvpn_net_ctx_t *ctx, struct tuntap *tt)
Definition: tun.c:808
tuntap::topology
int topology
Definition: tun.h:177
IFCONFIG_DEFAULT
#define IFCONFIG_DEFAULT
Definition: tun.h:355
tap_allow_nonadmin_access
void tap_allow_nonadmin_access(const char *dev_node)
Definition: tun.c:5144
overlapped_io
Definition: win32.h:202
tuntap_is_dco_win
static bool tuntap_is_dco_win(struct tuntap *tt)
Definition: tun.h:656
tuntap::ipapi_context_defined
bool ipapi_context_defined
Definition: tun.h:204
do_ifconfig_setenv
void do_ifconfig_setenv(const struct tuntap *tt, struct env_set *es)
Definition: tun.c:764
write_tun
int write_tun(struct tuntap *tt, uint8_t *buf, int len)
tuntap_options::ntp
in_addr_t ntp[N_DHCP_ADDR]
Definition: tun.h:116
tun_ring::head
volatile ULONG head
Definition: ring_buffer.h:52
tuntap::standby_iter
int standby_iter
Definition: tun.h:214
write_tun_buffered
static int write_tun_buffered(struct tuntap *tt, struct buffer *buf)
Definition: tun.h:643
undo_ifconfig
void undo_ifconfig(struct tuntap *tt, openvpn_net_ctx_t *ctx)
undo_ifconfig - undo configuration of the tunnel interface
Definition: tun.c:1749
tuntap::wintun_receive_ring_handle
HANDLE wintun_receive_ring_handle
Definition: tun.h:217