OpenVPN
openvpn.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-2026 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, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifndef OPENVPN_H
24#define OPENVPN_H
25
26#include "buffer.h"
27#include "options.h"
28#include "socket.h"
29#include "crypto.h"
30#include "ssl.h"
31#include "packet_id.h"
32#include "comp.h"
33#include "tun.h"
34#include "interval.h"
35#include "status.h"
36#include "fragment.h"
37#include "shaper.h"
38#include "route.h"
39#include "proxy.h"
40#include "socks.h"
41#include "sig.h"
42#include "misc.h"
43#include "mbuf.h"
44#include "pool.h"
45#include "plugin.h"
46#include "manage.h"
47#include "dns.h"
48#include "siphash.h"
49
50/*
51 * Our global key schedules, packaged thusly
52 * to facilitate key persistence.
53 */
54
56{
57 /* which cipher, HMAC digest, and key sizes are we using? */
59
60 /* pre-shared static key, read from a file */
62
63 /* our global SSL context */
65
66 /* optional TLS control channel wrapping */
75};
76
77/*
78 * struct packet_id_persist should be empty if we are not
79 * building with crypto.
80 */
81#ifndef PACKET_ID_H
83{
84 int dummy;
85};
86static inline void
90#endif
91
92/*
93 * Packet processing buffers.
94 */
96{
97 /* miscellaneous buffer, used by ping, occ, etc. */
99
100 /* workspace buffers used by crypto routines */
103
104 /* workspace buffers for compression */
105#ifdef USE_COMP
106 struct buffer compress_buf;
107 struct buffer decompress_buf;
108#endif
109
110 /*
111 * Buffers used to read from TUN device
112 * and TCP/UDP port.
113 */
116};
117
118/*
119 * always-persistent context variables
120 */
126
127
128/**************************************************************************/
138{
139 /* workspace for --user/--group */
141 /* helper which tells us whether we should keep trying to drop privileges */
145};
146
147
158{
164 /* tunnel session keys */
166
167 /* preresolved and cached host names */
169
170 /* persist crypto sequence number to/from file */
172
173 struct tuntap *tuntap;
182 /* list of --route-ipv6 directives */
184
185 /* --status file */
188
189 /* HTTP proxy object */
192
193 /* SOCKS proxy object */
196
197 /* persist --ifconfig-pool db to file */
200
201 /* if client mode, hash of option strings we pulled from server */
206};
207
208
209static inline bool
211{
212 return cas == CAS_PENDING || cas == CAS_PENDING_DEFERRED || cas == CAS_PENDING_DEFERRED_PARTIAL;
213}
214
225{
226 struct gc_arena gc;
230 /* our global wait events */
234
235 /* bitmask for event status. Check event.h for possible values */
236 unsigned int event_set_status;
237
240
242
243 const struct link_socket *accept_from; /* possibly do accept() on a parent link_socket */
244
245 struct link_socket_actual *to_link_addr; /* IP address of remote */
246 struct link_socket_actual from; /* address of incoming datagram */
247
248 /* MTU frame parameters */
249 struct frame frame; /* Active frame parameters */
250
251#ifdef ENABLE_FRAGMENT
252 /* Object to handle advanced MTU negotiation and datagram fragmentation */
255#endif
256
257 /*
258 * Traffic shaper object.
259 */
261
262 /*
263 * Statistics
264 */
272#ifdef PACKET_TRUNCATION_CHECK
273 counter_type n_trunc_tun_read;
274 counter_type n_trunc_tun_write;
275 counter_type n_trunc_pre_encrypt;
276 counter_type n_trunc_post_decrypt;
277#endif
278
279 /*
280 * Timer objects for ping and inactivity
281 * timeout features.
282 */
286
287 /* --inactive */
290
292
293 /* auth token renewal timer */
295
296 /* the option strings must match across peers */
299
300 int occ_op; /* INIT to -1 */
303
304 /*
305 * Keep track of maximum packet size received so far
306 * (of authenticated packets).
307 */
308 int original_recv_size; /* temporary */
309 int max_recv_size_local; /* max packet size received */
310 int max_recv_size_remote; /* max packet size received by remote */
311 int max_send_size_local; /* max packet size sent */
312 int max_send_size_remote; /* max packet size sent by remote */
313
314
315 /* remote wants us to send back a load test packet of this size */
317
320
321 /*
322 * TLS-mode crypto objects.
323 */
343 /* used to optimize calls to tls_multi_process */
345
346 /* throw this signal on TLS errors */
348
356
357#ifdef USE_COMP
358 struct compress_context *comp_context;
362#endif
363
364 /*
365 * Buffers used for packet processing.
366 */
368 bool buffers_owned; /* if true, we should free all buffers on close */
369
370 /*
371 * These buffers don't actually allocate storage, they are used
372 * as pointers to the allocated buffers in
373 * struct context_buffers.
374 */
375 struct buffer buf;
378
379 /* should we print R|W|r|w to console on packet transfers? */
380 bool log_rw;
381
382 /* route stuff */
385
386 /* did we open tun/tap dev during this cycle? */
388
389 /*
390 * Event loop info
391 */
392
397
398 /* next wakeup for processing coarse timers (>1 sec resolution) */
400
401 /* maintain a random delta to add to timeouts to avoid contexts
402 * waking up simultaneously */
405
406 /* Timer for everything up to the first packet from the *OpenVPN* server
407 * socks, http proxy, and tcp packets do not count */
409
410 /* indicates that the do_up_delay function has run */
412
413 /* indicates that we have received a SIGTERM when
414 * options->explicit_exit_notification is enabled,
415 * but we have not exited yet */
418
419 /* environmental variables to pass to scripts */
420 struct env_set *es;
422
423 /* --ifconfig endpoints to be pushed to client */
430
435
438
439 /* hash of pulled options, so we can compare when options change */
443
446
447 /* packet filter */
448
449#ifdef ENABLE_MANAGEMENT
451#endif
452
453#ifdef ENABLE_ASYNC_PUSH
454 int inotify_fd; /* descriptor for monitoring file changes */
455#endif
456};
457
458
471{
478 /* context modes */
479#define CM_P2P 0 /* standalone point-to-point session or client */
480#define CM_TOP 1 /* top level of a multi-client or point-to-multipoint server */
481#define CM_TOP_CLONE 2 /* clone of a CM_TOP context for one thread */
482#define CM_CHILD_UDP 3 /* child context of a CM_TOP or CM_THREAD */
483#define CM_CHILD_TCP 4 /* child context of a CM_TOP or CM_THREAD */
484 int mode;
492 struct gc_arena gc;
496 struct env_set *es;
500 struct signal_info *sig;
512 struct context_0 *c0;
513 struct context_1 c1;
514 struct context_2 c2;
515};
516
517/*
518 * Check for a signal when inside an event loop
519 */
520#define EVENT_LOOP_CHECK_SIGNAL(c, func, arg) \
521 if (IS_SIG(c)) \
522 { \
523 const int brk = func(arg); \
524 if (brk) \
525 { \
526 break; \
527 } \
528 else \
529 { \
530 continue; \
531 } \
532 }
533
534/*
535 * Macros for referencing objects which may not
536 * have been compiled in.
537 */
538
539#define TLS_MODE(c) ((c)->c2.tls_multi != NULL)
540#define PROTO_DUMP_FLAGS (check_debug_level(D_LINK_RW_VERBOSE) ? (PD_SHOW_DATA | PD_VERBOSE) : 0)
541#define PROTO_DUMP(buf, gc) \
542 protocol_dump( \
543 (buf), \
544 PROTO_DUMP_FLAGS | (c->c2.tls_multi ? PD_TLS : 0) \
545 | (c->options.tls_auth_file ? md_kt_size(c->c1.ks.key_type.digest) : 0) \
546 | (c->options.tls_crypt_file || c->options.tls_crypt_v2_file ? PD_TLS_CRYPT : 0), \
547 gc)
548
549/* this represents "disabled peer-id" */
550#define MAX_PEER_ID 0xFFFFFF
551
552#endif /* ifndef OPENVPN_H */
Buffer management functions and garbage collection.
uint64_t counter_type
Definition common.h:31
Data Channel Cryptography Module.
Data Channel Fragmentation module header file.
void * openvpn_net_ctx_t
Definition networking.h:38
static void packet_id_persist_init(struct packet_id_persist *p)
Definition openvpn.h:87
static bool is_cas_pending(enum multi_status cas)
Definition openvpn.h:210
#define SIPHASH_KEY_SIZE
Definition siphash.h:35
Control Channel SSL/Data channel negotiation module.
multi_status
Definition ssl_common.h:579
@ CAS_PENDING_DEFERRED
Waiting on an async option import handler.
Definition ssl_common.h:583
@ CAS_PENDING_DEFERRED_PARTIAL
at least handler succeeded but another is still pending
Definition ssl_common.h:584
@ CAS_PENDING
Options import (Connect script/plugin, ccd,...)
Definition ssl_common.h:582
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
Definition socket.h:67
Level 0 context containing information related to the OpenVPN process.
Definition openvpn.h:138
bool uid_gid_chroot_set
Definition openvpn.h:142
bool uid_gid_specified
Definition openvpn.h:140
Level 1 context containing state that persists across SIGUSR1 restarts.
Definition openvpn.h:158
struct key_schedule ks
Definition openvpn.h:165
struct ifconfig_pool_persist * ifconfig_pool_persist
Definition openvpn.h:198
bool http_proxy_owned
Definition openvpn.h:191
struct status_output * status_output
Definition openvpn.h:186
struct route_list * route_list
List of routing information.
Definition openvpn.h:178
struct link_socket_addr * link_socket_addrs
Local and remote addresses on the external network.
Definition openvpn.h:160
struct sha256_digest pulled_options_digest_save
Hash of option strings received from the remote OpenVPN server.
Definition openvpn.h:202
int link_sockets_num
Definition openvpn.h:159
bool status_output_owned
Definition openvpn.h:187
struct route_ipv6_list * route_ipv6_list
Definition openvpn.h:183
struct packet_id_persist pid_persist
Definition openvpn.h:171
struct http_proxy_info * http_proxy
Definition openvpn.h:190
bool socks_proxy_owned
Definition openvpn.h:195
bool tuntap_owned
Whether the tun/tap interface should be cleaned up when this context is cleaned up.
Definition openvpn.h:174
bool ifconfig_pool_persist_owned
Definition openvpn.h:199
struct socks_proxy_info * socks_proxy
Definition openvpn.h:194
struct cached_dns_entry * dns_cache
Definition openvpn.h:168
struct tuntap * tuntap
Tun/tap virtual network interface.
Definition openvpn.h:173
Level 2 context containing state that is reset on both SIGHUP and SIGUSR1 restarts.
Definition openvpn.h:225
bool push_request_received
Definition openvpn.h:424
counter_type link_read_bytes
Definition openvpn.h:267
char * options_string_local
Definition openvpn.h:297
counter_type link_write_bytes
Definition openvpn.h:270
bool push_ifconfig_ipv6_defined
Definition openvpn.h:431
struct event_timeout occ_mtu_load_test_interval
Definition openvpn.h:318
struct event_timeout server_poll_interval
Definition openvpn.h:408
int max_recv_size_local
Definition openvpn.h:309
struct fragment_master * fragment
Definition openvpn.h:253
time_t update_timeout_random_component
Definition openvpn.h:403
unsigned int event_set_status
Definition openvpn.h:236
bool do_up_ran
Definition openvpn.h:411
int occ_mtu_load_size
Definition openvpn.h:316
int max_recv_size_remote
Definition openvpn.h:310
bool push_ifconfig_defined
Definition openvpn.h:425
char * options_string_remote
Definition openvpn.h:298
counter_type dco_read_bytes
Definition openvpn.h:268
int occ_mtu_load_n_tries
Definition openvpn.h:319
struct event_timeout route_wakeup_expire
Definition openvpn.h:384
struct event_timeout ping_send_interval
Definition openvpn.h:284
int max_send_size_local
Definition openvpn.h:311
bool did_open_tun
Definition openvpn.h:387
md_ctx_t * pulled_options_state
Definition openvpn.h:441
struct timeval timeout_random_component
Definition openvpn.h:404
counter_type tun_read_bytes
Definition openvpn.h:265
bool es_owned
Definition openvpn.h:421
struct man_def_auth_context mda_context
Definition openvpn.h:450
const struct link_socket * accept_from
Definition openvpn.h:243
bool pulled_options_digest_init_done
Definition openvpn.h:440
struct tls_auth_standalone * tls_auth_standalone
TLS state structure required for the initial authentication of a client's connection attempt.
Definition openvpn.h:327
counter_type dco_write_bytes
Definition openvpn.h:271
struct event_timeout scheduled_exit
Definition openvpn.h:444
int occ_op
Definition openvpn.h:300
struct env_set * es
Definition openvpn.h:420
time_t push_request_timeout
Definition openvpn.h:437
struct interval tmp_int
Definition openvpn.h:344
struct event_timeout auth_token_renewal_interval
Definition openvpn.h:294
bool link_socket_owned
Definition openvpn.h:241
struct event_timeout wait_for_connect
Definition openvpn.h:283
struct event_timeout push_request_interval
Definition openvpn.h:436
struct tls_multi * tls_multi
TLS state structure for this VPN tunnel.
Definition openvpn.h:324
time_t sent_push_reply_expiry
Definition openvpn.h:426
time_t coarse_timer_wakeup
Definition openvpn.h:399
int scheduled_exit_signal
Definition openvpn.h:445
struct in6_addr push_ifconfig_ipv6_remote
Definition openvpn.h:434
struct link_socket_actual from
Definition openvpn.h:246
struct frame frame_fragment
Definition openvpn.h:254
int push_ifconfig_ipv6_netbits
Definition openvpn.h:433
struct buffer to_link
Definition openvpn.h:377
int64_t inactivity_bytes
Definition openvpn.h:289
struct buffer to_tun
Definition openvpn.h:376
struct in6_addr push_ifconfig_ipv6_local
Definition openvpn.h:432
bool buffers_owned
Definition openvpn.h:368
counter_type tun_write_bytes
Definition openvpn.h:266
struct event_timeout occ_interval
Definition openvpn.h:302
struct link_socket ** link_sockets
Definition openvpn.h:238
in_addr_t push_ifconfig_local_alias
Definition openvpn.h:429
counter_type link_read_bytes_auth
Definition openvpn.h:269
struct link_socket_info ** link_socket_infos
Definition openvpn.h:239
uint8_t session_id_key[SIPHASH_KEY_SIZE]
the siphash secret we use to generate and verify our syn cookie like session ids from the server.
Definition openvpn.h:339
struct event_timeout packet_id_persist_interval
Definition openvpn.h:355
struct link_socket_actual * to_link_addr
Definition openvpn.h:245
struct event_timeout session_interval
Definition openvpn.h:291
in_addr_t push_ifconfig_remote_netmask
Definition openvpn.h:428
int original_recv_size
Definition openvpn.h:308
struct buffer buf
Definition openvpn.h:375
struct timeval timeval
Time to next event of timers and similar.
Definition openvpn.h:396
time_t explicit_exit_notification_time_wait
Definition openvpn.h:416
bool log_rw
Definition openvpn.h:380
int event_set_max
Definition openvpn.h:232
struct gc_arena gc
Garbage collection arena for allocations done in the level 2 scope of this context_2 structure.
Definition openvpn.h:226
struct sha256_digest pulled_options_digest
Definition openvpn.h:442
struct event_set * event_set
Definition openvpn.h:231
struct context_buffers * buffers
Definition openvpn.h:367
struct event_timeout explicit_exit_notification_interval
Definition openvpn.h:417
int max_send_size_remote
Definition openvpn.h:312
struct event_timeout route_wakeup
Definition openvpn.h:383
int tls_exit_signal
Definition openvpn.h:347
struct event_timeout inactivity_interval
Definition openvpn.h:288
int occ_n_tries
Definition openvpn.h:301
struct event_timeout ping_rec_interval
Definition openvpn.h:285
in_addr_t push_ifconfig_local
Definition openvpn.h:427
bool event_set_owned
Definition openvpn.h:233
struct buffer read_link_buf
Definition openvpn.h:114
struct buffer encrypt_buf
Definition openvpn.h:101
struct buffer read_tun_buf
Definition openvpn.h:115
struct buffer decrypt_buf
Definition openvpn.h:102
struct buffer aux_buf
Definition openvpn.h:98
int restart_sleep_seconds
Definition openvpn.h:123
struct dns_updown_runner_info duri
Definition openvpn.h:124
Contains all state information for one tunnel.
Definition openvpn.h:471
int mode
Role of this context within the OpenVPN process.
Definition openvpn.h:484
struct context_0 * c0
Level 0 context.
Definition openvpn.h:512
bool did_we_daemonize
Whether demonization has already taken place.
Definition openvpn.h:507
struct multi_context * multi
Pointer to the main P2MP context.
Definition openvpn.h:489
bool first_time
True on the first iteration of OpenVPN's main loop.
Definition openvpn.h:475
struct signal_info * sig
Internal error signaling object.
Definition openvpn.h:500
openvpn_net_ctx_t net_ctx
Networking API opaque context.
Definition openvpn.h:498
struct plugin_list * plugins
List of plug-ins.
Definition openvpn.h:502
struct context_2 c2
Level 2 context.
Definition openvpn.h:514
struct env_set * es
Set of environment variables.
Definition openvpn.h:496
bool plugins_owned
Whether the plug-ins should be cleaned up when this context is cleaned up.
Definition openvpn.h:503
struct gc_arena gc
Garbage collection arena for allocations done in the scope of this context structure.
Definition openvpn.h:492
struct context_1 c1
Level 1 context.
Definition openvpn.h:513
struct context_persist persist
Persistent context.
Definition openvpn.h:510
Security parameter state for processing data channel packets.
Definition crypto.h:293
Fragmentation and reassembly state for one VPN tunnel instance.
Definition fragment.h:140
Packet geometry parameters.
Definition mtu.h:113
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
Container for bidirectional cipher and HMAC key material.
Definition crypto.h:240
Container for two sets of OpenSSL cipher and/or HMAC contexts for both sending and receiving directio...
Definition crypto.h:280
Container for one set of cipher and/or HMAC contexts.
Definition crypto.h:202
struct buffer tls_crypt_v2_wkc
Wrapped client key.
Definition openvpn.h:73
struct key2 original_wrap_keydata
original tls-crypt key preserved to xored into the tls_crypt renegotiation key
Definition openvpn.h:71
struct key_ctx auth_token_key
Definition openvpn.h:74
struct key_type tls_auth_key_type
Definition openvpn.h:67
struct key_ctx_bi tls_wrap_key
Definition openvpn.h:68
struct key_ctx_bi static_key
Definition openvpn.h:61
struct tls_root_ctx * ssl_ctx
Definition openvpn.h:64
struct key_ctx tls_crypt_v2_server_key
Definition openvpn.h:72
Main OpenVPN server state structure.
Definition multi.h:162
Wrapper struct to pass around SHA256 digests.
Definition crypto.h:133
Security parameter state for a single VPN tunnel.
Definition ssl_common.h:611
Structure that wraps the TLS context.
Definition tun.h:181
uint32_t in_addr_t
Definition syshead.h:52