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 * Packet processing buffers.
79 */
81{
82 /* miscellaneous buffer, used by ping, occ, etc. */
84
85 /* workspace buffers used by crypto routines */
88
89 /* workspace buffers for compression */
90#ifdef USE_COMP
91 struct buffer compress_buf;
92 struct buffer decompress_buf;
93#endif
94
95 /*
96 * Buffers used to read from TUN device
97 * and TCP/UDP port.
98 */
101};
102
103/*
104 * always-persistent context variables
105 */
111
112
113/**************************************************************************/
123{
124 /* workspace for --user/--group */
126 /* helper which tells us whether we should keep trying to drop privileges */
130};
131
132
143{
149 /* tunnel session keys */
151
152 /* preresolved and cached host names */
154
155 struct tuntap *tuntap;
164 /* list of --route-ipv6 directives */
166
167 /* --status file */
170
171 /* HTTP proxy object */
174
175 /* SOCKS proxy object */
178
179 /* persist --ifconfig-pool db to file */
182
183 /* if client mode, hash of option strings we pulled from server */
188};
189
190
191static inline bool
193{
194 return cas == CAS_PENDING || cas == CAS_PENDING_DEFERRED || cas == CAS_PENDING_DEFERRED_PARTIAL;
195}
196
207{
208 struct gc_arena gc;
212 /* our global wait events */
216
217 /* bitmask for event status. Check event.h for possible values */
218 unsigned int event_set_status;
219
222
224
225 const struct link_socket *accept_from; /* possibly do accept() on a parent link_socket */
226
227 struct link_socket_actual *to_link_addr; /* IP address of remote */
228 struct link_socket_actual from; /* address of incoming datagram */
229
230 /* MTU frame parameters */
231 struct frame frame; /* Active frame parameters */
232
233#ifdef ENABLE_FRAGMENT
234 /* Object to handle advanced MTU negotiation and datagram fragmentation */
237#endif
238
239 /*
240 * Traffic shaper object.
241 */
243
244 /*
245 * Statistics
246 */
254
255 /*
256 * Timer objects for ping and inactivity
257 * timeout features.
258 */
262
263 /* --inactive */
266
268
269 /* auth token renewal timer */
271
272 /* the option strings must match across peers */
275
276 int occ_op; /* INIT to -1 */
279
280 /*
281 * Keep track of maximum packet size received so far
282 * (of authenticated packets).
283 */
284 int original_recv_size; /* temporary */
285 int max_recv_size_local; /* max packet size received */
286 int max_recv_size_remote; /* max packet size received by remote */
287 int max_send_size_local; /* max packet size sent */
288 int max_send_size_remote; /* max packet size sent by remote */
289
290
291 /* remote wants us to send back a load test packet of this size */
293
296
297 /*
298 * TLS-mode crypto objects.
299 */
319 /* used to optimize calls to tls_multi_process */
321
322 /* throw this signal on TLS errors */
324
331#ifdef USE_COMP
332 struct compress_context *comp_context;
336#endif
337
338 /*
339 * Buffers used for packet processing.
340 */
342 bool buffers_owned; /* if true, we should free all buffers on close */
343
344 /*
345 * These buffers don't actually allocate storage, they are used
346 * as pointers to the allocated buffers in
347 * struct context_buffers.
348 */
349 struct buffer buf;
352
353 /* should we print R|W|r|w to console on packet transfers? */
354 bool log_rw;
355
356 /* route stuff */
359
360 /* did we open tun/tap dev during this cycle? */
362
363 /*
364 * Event loop info
365 */
366
371
372 /* next wakeup for processing coarse timers (>1 sec resolution) */
374
375 /* maintain a random delta to add to timeouts to avoid contexts
376 * waking up simultaneously */
379
380 /* Timer for everything up to the first packet from the *OpenVPN* server
381 * socks, http proxy, and tcp packets do not count */
383
384 /* indicates that the do_up_delay function has run */
386
387 /* indicates that we have received a SIGTERM when
388 * options->explicit_exit_notification is enabled,
389 * but we have not exited yet */
392
393 /* environmental variables to pass to scripts */
394 struct env_set *es;
396
397 /* --ifconfig endpoints to be pushed to client */
404
409
412
413 /* hash of pulled options, so we can compare when options change */
417
420
421 /* packet filter */
422
423#ifdef ENABLE_MANAGEMENT
425#endif
426
427#ifdef ENABLE_ASYNC_PUSH
428 int inotify_fd; /* descriptor for monitoring file changes */
429#endif
430};
431
432
445{
452 /* context modes */
453#define CM_P2P 0 /* standalone point-to-point session or client */
454#define CM_TOP 1 /* top level of a multi-client or point-to-multipoint server */
455#define CM_TOP_CLONE 2 /* clone of a CM_TOP context for one thread */
456#define CM_CHILD_UDP 3 /* child context of a CM_TOP or CM_THREAD */
457#define CM_CHILD_TCP 4 /* child context of a CM_TOP or CM_THREAD */
458 int mode;
466 struct gc_arena gc;
470 struct env_set *es;
474 struct signal_info *sig;
488 struct context_0 *c0;
489 struct context_1 c1;
490 struct context_2 c2;
491};
492
493/*
494 * Check for a signal when inside an event loop
495 */
496#define EVENT_LOOP_CHECK_SIGNAL(c, func, arg) \
497 if (IS_SIG(c)) \
498 { \
499 const int brk = func(arg); \
500 if (brk) \
501 { \
502 break; \
503 } \
504 else \
505 { \
506 continue; \
507 } \
508 }
509
510/*
511 * Macros for referencing objects which may not
512 * have been compiled in.
513 */
514
515#define TLS_MODE(c) ((c)->c2.tls_multi != NULL)
516#define PROTO_DUMP_FLAGS (check_debug_level(D_LINK_RW_VERBOSE) ? (PD_SHOW_DATA | PD_VERBOSE) : 0)
517#define PROTO_DUMP(buf, gc) \
518 protocol_dump( \
519 (buf), \
520 PROTO_DUMP_FLAGS | (c->c2.tls_multi ? PD_TLS : 0) \
521 | (c->options.tls_auth_file ? md_kt_size(c->c1.ks.key_type.digest) : 0) \
522 | (c->options.tls_crypt_file || c->options.tls_crypt_v2_file ? PD_TLS_CRYPT : 0), \
523 gc)
524
525/* this represents "disabled peer-id" */
526#define MAX_PEER_ID 0xFFFFFF
527
528#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 bool is_cas_pending(enum multi_status cas)
Definition openvpn.h:192
#define SIPHASH_KEY_SIZE
Definition siphash.h:35
Control Channel SSL/Data channel negotiation module.
multi_status
Definition ssl_common.h:587
@ CAS_PENDING_DEFERRED
Waiting on an async option import handler.
Definition ssl_common.h:591
@ CAS_PENDING_DEFERRED_PARTIAL
at least handler succeeded but another is still pending
Definition ssl_common.h:592
@ CAS_PENDING
Options import (Connect script/plugin, ccd,...)
Definition ssl_common.h:590
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
Definition resolve.h:89
Level 0 context containing information related to the OpenVPN process.
Definition openvpn.h:123
bool uid_gid_chroot_set
Definition openvpn.h:127
bool uid_gid_specified
Definition openvpn.h:125
Level 1 context containing state that persists across SIGUSR1 restarts.
Definition openvpn.h:143
struct key_schedule ks
Definition openvpn.h:150
struct ifconfig_pool_persist * ifconfig_pool_persist
Definition openvpn.h:180
bool http_proxy_owned
Definition openvpn.h:173
struct status_output * status_output
Definition openvpn.h:168
struct route_list * route_list
List of routing information.
Definition openvpn.h:160
struct link_socket_addr * link_socket_addrs
Local and remote addresses on the external network.
Definition openvpn.h:145
struct sha256_digest pulled_options_digest_save
Hash of option strings received from the remote OpenVPN server.
Definition openvpn.h:184
int link_sockets_num
Definition openvpn.h:144
bool status_output_owned
Definition openvpn.h:169
struct route_ipv6_list * route_ipv6_list
Definition openvpn.h:165
struct http_proxy_info * http_proxy
Definition openvpn.h:172
bool socks_proxy_owned
Definition openvpn.h:177
bool tuntap_owned
Whether the tun/tap interface should be cleaned up when this context is cleaned up.
Definition openvpn.h:156
bool ifconfig_pool_persist_owned
Definition openvpn.h:181
struct socks_proxy_info * socks_proxy
Definition openvpn.h:176
struct cached_dns_entry * dns_cache
Definition openvpn.h:153
struct tuntap * tuntap
Tun/tap virtual network interface.
Definition openvpn.h:155
Level 2 context containing state that is reset on both SIGHUP and SIGUSR1 restarts.
Definition openvpn.h:207
bool push_request_received
Definition openvpn.h:398
counter_type link_read_bytes
Definition openvpn.h:249
char * options_string_local
Definition openvpn.h:273
counter_type link_write_bytes
Definition openvpn.h:252
bool push_ifconfig_ipv6_defined
Definition openvpn.h:405
struct event_timeout occ_mtu_load_test_interval
Definition openvpn.h:294
struct event_timeout server_poll_interval
Definition openvpn.h:382
int max_recv_size_local
Definition openvpn.h:285
struct fragment_master * fragment
Definition openvpn.h:235
time_t update_timeout_random_component
Definition openvpn.h:377
unsigned int event_set_status
Definition openvpn.h:218
bool do_up_ran
Definition openvpn.h:385
int occ_mtu_load_size
Definition openvpn.h:292
int max_recv_size_remote
Definition openvpn.h:286
bool push_ifconfig_defined
Definition openvpn.h:399
char * options_string_remote
Definition openvpn.h:274
counter_type dco_read_bytes
Definition openvpn.h:250
int occ_mtu_load_n_tries
Definition openvpn.h:295
struct event_timeout route_wakeup_expire
Definition openvpn.h:358
struct event_timeout ping_send_interval
Definition openvpn.h:260
int max_send_size_local
Definition openvpn.h:287
bool did_open_tun
Definition openvpn.h:361
md_ctx_t * pulled_options_state
Definition openvpn.h:415
struct timeval timeout_random_component
Definition openvpn.h:378
counter_type tun_read_bytes
Definition openvpn.h:247
bool es_owned
Definition openvpn.h:395
struct man_def_auth_context mda_context
Definition openvpn.h:424
const struct link_socket * accept_from
Definition openvpn.h:225
bool pulled_options_digest_init_done
Definition openvpn.h:414
struct tls_auth_standalone * tls_auth_standalone
TLS state structure required for the initial authentication of a client's connection attempt.
Definition openvpn.h:303
counter_type dco_write_bytes
Definition openvpn.h:253
struct event_timeout scheduled_exit
Definition openvpn.h:418
int occ_op
Definition openvpn.h:276
struct env_set * es
Definition openvpn.h:394
time_t push_request_timeout
Definition openvpn.h:411
struct interval tmp_int
Definition openvpn.h:320
struct event_timeout auth_token_renewal_interval
Definition openvpn.h:270
bool link_socket_owned
Definition openvpn.h:223
struct event_timeout wait_for_connect
Definition openvpn.h:259
struct event_timeout push_request_interval
Definition openvpn.h:410
struct tls_multi * tls_multi
TLS state structure for this VPN tunnel.
Definition openvpn.h:300
time_t sent_push_reply_expiry
Definition openvpn.h:400
time_t coarse_timer_wakeup
Definition openvpn.h:373
int scheduled_exit_signal
Definition openvpn.h:419
struct in6_addr push_ifconfig_ipv6_remote
Definition openvpn.h:408
struct link_socket_actual from
Definition openvpn.h:228
struct frame frame_fragment
Definition openvpn.h:236
int push_ifconfig_ipv6_netbits
Definition openvpn.h:407
struct buffer to_link
Definition openvpn.h:351
int64_t inactivity_bytes
Definition openvpn.h:265
struct buffer to_tun
Definition openvpn.h:350
struct in6_addr push_ifconfig_ipv6_local
Definition openvpn.h:406
bool buffers_owned
Definition openvpn.h:342
counter_type tun_write_bytes
Definition openvpn.h:248
struct event_timeout occ_interval
Definition openvpn.h:278
struct link_socket ** link_sockets
Definition openvpn.h:220
in_addr_t push_ifconfig_local_alias
Definition openvpn.h:403
counter_type link_read_bytes_auth
Definition openvpn.h:251
struct link_socket_info ** link_socket_infos
Definition openvpn.h:221
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:315
struct link_socket_actual * to_link_addr
Definition openvpn.h:227
struct event_timeout session_interval
Definition openvpn.h:267
in_addr_t push_ifconfig_remote_netmask
Definition openvpn.h:402
int original_recv_size
Definition openvpn.h:284
struct buffer buf
Definition openvpn.h:349
struct timeval timeval
Time to next event of timers and similar.
Definition openvpn.h:370
time_t explicit_exit_notification_time_wait
Definition openvpn.h:390
bool log_rw
Definition openvpn.h:354
int event_set_max
Definition openvpn.h:214
struct gc_arena gc
Garbage collection arena for allocations done in the level 2 scope of this context_2 structure.
Definition openvpn.h:208
struct sha256_digest pulled_options_digest
Definition openvpn.h:416
struct event_set * event_set
Definition openvpn.h:213
struct context_buffers * buffers
Definition openvpn.h:341
struct event_timeout explicit_exit_notification_interval
Definition openvpn.h:391
int max_send_size_remote
Definition openvpn.h:288
struct event_timeout route_wakeup
Definition openvpn.h:357
int tls_exit_signal
Definition openvpn.h:323
struct event_timeout inactivity_interval
Definition openvpn.h:264
int occ_n_tries
Definition openvpn.h:277
struct event_timeout ping_rec_interval
Definition openvpn.h:261
in_addr_t push_ifconfig_local
Definition openvpn.h:401
bool event_set_owned
Definition openvpn.h:215
struct buffer read_link_buf
Definition openvpn.h:99
struct buffer encrypt_buf
Definition openvpn.h:86
struct buffer read_tun_buf
Definition openvpn.h:100
struct buffer decrypt_buf
Definition openvpn.h:87
struct buffer aux_buf
Definition openvpn.h:83
int restart_sleep_seconds
Definition openvpn.h:108
struct dns_updown_runner_info duri
Definition openvpn.h:109
Contains all state information for one tunnel.
Definition openvpn.h:445
int mode
Role of this context within the OpenVPN process.
Definition openvpn.h:458
struct context_0 * c0
Level 0 context.
Definition openvpn.h:488
bool did_we_daemonize
Whether demonization has already taken place.
Definition openvpn.h:481
struct multi_context * multi
Pointer to the main P2MP context.
Definition openvpn.h:463
bool first_time
True on the first iteration of OpenVPN's main loop.
Definition openvpn.h:449
bool did_dco_iroutes
Whether DCO iroutes have been installed.
Definition openvpn.h:484
struct signal_info * sig
Internal error signaling object.
Definition openvpn.h:474
openvpn_net_ctx_t net_ctx
Networking API opaque context.
Definition openvpn.h:472
struct plugin_list * plugins
List of plug-ins.
Definition openvpn.h:476
struct context_2 c2
Level 2 context.
Definition openvpn.h:490
struct env_set * es
Set of environment variables.
Definition openvpn.h:470
bool plugins_owned
Whether the plug-ins should be cleaned up when this context is cleaned up.
Definition openvpn.h:477
struct gc_arena gc
Garbage collection arena for allocations done in the scope of this context structure.
Definition openvpn.h:466
struct context_1 c1
Level 1 context.
Definition openvpn.h:489
struct context_persist persist
Persistent context.
Definition openvpn.h:486
Security parameter state for processing data channel packets.
Definition crypto.h:286
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:233
Container for two sets of OpenSSL cipher and/or HMAC contexts for both sending and receiving directio...
Definition crypto.h:273
Container for one set of cipher and/or HMAC contexts.
Definition crypto.h:195
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:170
Wrapper struct to pass around SHA256 digests.
Definition crypto.h:126
Security parameter state for a single VPN tunnel.
Definition ssl_common.h:619
Structure that wraps the TLS context.
Definition ssl_mbedtls.h:86
Definition tun.h:181
uint32_t in_addr_t
Definition syshead.h:52