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-2025 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 OPENVPN_H
25#define OPENVPN_H
26
27#include "buffer.h"
28#include "options.h"
29#include "socket.h"
30#include "crypto.h"
31#include "ssl.h"
32#include "packet_id.h"
33#include "comp.h"
34#include "tun.h"
35#include "interval.h"
36#include "status.h"
37#include "fragment.h"
38#include "shaper.h"
39#include "route.h"
40#include "proxy.h"
41#include "socks.h"
42#include "sig.h"
43#include "misc.h"
44#include "mbuf.h"
45#include "pool.h"
46#include "plugin.h"
47#include "manage.h"
48#include "dns.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
214}
215
226{
227 struct gc_arena gc;
231 /* our global wait events */
235
236 /* bitmask for event status. Check event.h for possible values */
237 unsigned int event_set_status;
238
241
243
244 const struct link_socket *accept_from; /* possibly do accept() on a parent link_socket */
245
246 struct link_socket_actual *to_link_addr; /* IP address of remote */
247 struct link_socket_actual from; /* address of incoming datagram */
248
249 /* MTU frame parameters */
250 struct frame frame; /* Active frame parameters */
251
252#ifdef ENABLE_FRAGMENT
253 /* Object to handle advanced MTU negotiation and datagram fragmentation */
256#endif
257
258 /*
259 * Traffic shaper object.
260 */
262
263 /*
264 * Statistics
265 */
273#ifdef PACKET_TRUNCATION_CHECK
274 counter_type n_trunc_tun_read;
275 counter_type n_trunc_tun_write;
276 counter_type n_trunc_pre_encrypt;
277 counter_type n_trunc_post_decrypt;
278#endif
279
280 /*
281 * Timer objects for ping and inactivity
282 * timeout features.
283 */
287
288 /* --inactive */
291
293
294 /* auth token renewal timer */
296
297 /* the option strings must match across peers */
300
301 int occ_op; /* INIT to -1 */
304
305 /*
306 * Keep track of maximum packet size received so far
307 * (of authenticated packets).
308 */
309 int original_recv_size; /* temporary */
310 int max_recv_size_local; /* max packet size received */
311 int max_recv_size_remote; /* max packet size received by remote */
312 int max_send_size_local; /* max packet size sent */
313 int max_send_size_remote; /* max packet size sent by remote */
314
315
316 /* remote wants us to send back a load test packet of this size */
318
321
322 /*
323 * TLS-mode crypto objects.
324 */
345 /* used to optimize calls to tls_multi_process */
347
348 /* throw this signal on TLS errors */
350
358
359#ifdef USE_COMP
360 struct compress_context *comp_context;
364#endif
365
366 /*
367 * Buffers used for packet processing.
368 */
370 bool buffers_owned; /* if true, we should free all buffers on close */
371
372 /*
373 * These buffers don't actually allocate storage, they are used
374 * as pointers to the allocated buffers in
375 * struct context_buffers.
376 */
377 struct buffer buf;
380
381 /* should we print R|W|r|w to console on packet transfers? */
382 bool log_rw;
383
384 /* route stuff */
387
388 /* did we open tun/tap dev during this cycle? */
390
391 /*
392 * Event loop info
393 */
394
399
400 /* next wakeup for processing coarse timers (>1 sec resolution) */
402
403 /* maintain a random delta to add to timeouts to avoid contexts
404 * waking up simultaneously */
407
408 /* Timer for everything up to the first packet from the *OpenVPN* server
409 * socks, http proxy, and tcp packets do not count */
411
412 /* indicates that the do_up_delay function has run */
414
415 /* indicates that we have received a SIGTERM when
416 * options->explicit_exit_notification is enabled,
417 * but we have not exited yet */
420
421 /* environmental variables to pass to scripts */
422 struct env_set *es;
424
425 /* don't wait for TUN/TAP/UDP to be ready to accept write */
427
428 /* --ifconfig endpoints to be pushed to client */
435
440
443
444 /* hash of pulled options, so we can compare when options change */
448
451
452 /* packet filter */
453
454#ifdef ENABLE_MANAGEMENT
456#endif
457
458#ifdef ENABLE_ASYNC_PUSH
459 int inotify_fd; /* descriptor for monitoring file changes */
460#endif
461};
462
463
476{
483 /* context modes */
484#define CM_P2P 0 /* standalone point-to-point session or client */
485#define CM_TOP 1 /* top level of a multi-client or point-to-multipoint server */
486#define CM_TOP_CLONE 2 /* clone of a CM_TOP context for one thread */
487#define CM_CHILD_UDP 3 /* child context of a CM_TOP or CM_THREAD */
488#define CM_CHILD_TCP 4 /* child context of a CM_TOP or CM_THREAD */
489 int mode;
494 struct gc_arena gc;
498 struct env_set *es;
502 struct signal_info *sig;
514 struct context_0 *c0;
515 struct context_1 c1;
516 struct context_2 c2;
517};
518
519/*
520 * Check for a signal when inside an event loop
521 */
522#define EVENT_LOOP_CHECK_SIGNAL(c, func, arg) \
523 if (IS_SIG(c)) \
524 { \
525 const int brk = func(arg); \
526 perf_pop(); \
527 if (brk) { \
528 break;} \
529 else { \
530 continue;} \
531 }
532
533/*
534 * Macros for referencing objects which may not
535 * have been compiled in.
536 */
537
538#define TLS_MODE(c) ((c)->c2.tls_multi != NULL)
539#define PROTO_DUMP_FLAGS (check_debug_level(D_LINK_RW_VERBOSE) ? (PD_SHOW_DATA|PD_VERBOSE) : 0)
540#define PROTO_DUMP(buf, gc) protocol_dump((buf), \
541 PROTO_DUMP_FLAGS \
542 |(c->c2.tls_multi ? PD_TLS : 0) \
543 |(c->options.tls_auth_file ? md_kt_size(c->c1.ks.key_type.digest) : 0) \
544 |(c->options.tls_crypt_file || c->options.tls_crypt_v2_file ? PD_TLS_CRYPT : 0), \
545 gc)
546
547/* this represents "disabled peer-id" */
548#define MAX_PEER_ID 0xFFFFFF
549
550#endif /* ifndef OPENVPN_H */
uint64_t counter_type
Definition common.h:30
Data Channel Cryptography Module.
mbedtls_md_context_t hmac_ctx_t
Generic HMAC context.
mbedtls_md_context_t md_ctx_t
Generic message digest context.
Data Channel Fragmentation module header file.
void * openvpn_net_ctx_t
Definition networking.h:39
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
Control Channel SSL/Data channel negotiation module.
multi_status
Definition ssl_common.h:571
@ CAS_PENDING_DEFERRED
Waiting on an async option import handler.
Definition ssl_common.h:576
@ CAS_PENDING_DEFERRED_PARTIAL
at least handler succeeded but another is still pending
Definition ssl_common.h:577
@ CAS_PENDING
Options import (Connect script/plugin, ccd,...)
Definition ssl_common.h:575
Wrapper structure for dynamically allocated memory.
Definition buffer.h:61
Definition socket.h:76
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:226
bool push_request_received
Definition openvpn.h:429
counter_type link_read_bytes
Definition openvpn.h:268
char * options_string_local
Definition openvpn.h:298
counter_type link_write_bytes
Definition openvpn.h:271
bool push_ifconfig_ipv6_defined
Definition openvpn.h:436
struct event_timeout occ_mtu_load_test_interval
Definition openvpn.h:319
struct event_timeout server_poll_interval
Definition openvpn.h:410
int max_recv_size_local
Definition openvpn.h:310
struct fragment_master * fragment
Definition openvpn.h:254
time_t update_timeout_random_component
Definition openvpn.h:405
unsigned int event_set_status
Definition openvpn.h:237
bool do_up_ran
Definition openvpn.h:413
int occ_mtu_load_size
Definition openvpn.h:317
int max_recv_size_remote
Definition openvpn.h:311
bool push_ifconfig_defined
Definition openvpn.h:430
char * options_string_remote
Definition openvpn.h:299
counter_type dco_read_bytes
Definition openvpn.h:269
int occ_mtu_load_n_tries
Definition openvpn.h:320
struct event_timeout route_wakeup_expire
Definition openvpn.h:386
struct event_timeout ping_send_interval
Definition openvpn.h:285
int max_send_size_local
Definition openvpn.h:312
bool did_open_tun
Definition openvpn.h:389
md_ctx_t * pulled_options_state
Definition openvpn.h:446
struct timeval timeout_random_component
Definition openvpn.h:406
counter_type tun_read_bytes
Definition openvpn.h:266
bool es_owned
Definition openvpn.h:423
struct man_def_auth_context mda_context
Definition openvpn.h:455
hmac_ctx_t * session_id_hmac
the HMAC we use to generate and verify our syn cookie like session ids from the server.
Definition openvpn.h:340
const struct link_socket * accept_from
Definition openvpn.h:244
bool pulled_options_digest_init_done
Definition openvpn.h:445
struct tls_auth_standalone * tls_auth_standalone
TLS state structure required for the initial authentication of a client's connection attempt.
Definition openvpn.h:328
counter_type dco_write_bytes
Definition openvpn.h:272
struct event_timeout scheduled_exit
Definition openvpn.h:449
int occ_op
Definition openvpn.h:301
struct env_set * es
Definition openvpn.h:422
time_t push_request_timeout
Definition openvpn.h:442
struct interval tmp_int
Definition openvpn.h:346
struct event_timeout auth_token_renewal_interval
Definition openvpn.h:295
bool link_socket_owned
Definition openvpn.h:242
struct event_timeout wait_for_connect
Definition openvpn.h:284
struct event_timeout push_request_interval
Definition openvpn.h:441
struct tls_multi * tls_multi
TLS state structure for this VPN tunnel.
Definition openvpn.h:325
time_t sent_push_reply_expiry
Definition openvpn.h:431
time_t coarse_timer_wakeup
Definition openvpn.h:401
int scheduled_exit_signal
Definition openvpn.h:450
struct in6_addr push_ifconfig_ipv6_remote
Definition openvpn.h:439
struct link_socket_actual from
Definition openvpn.h:247
struct frame frame_fragment
Definition openvpn.h:255
int push_ifconfig_ipv6_netbits
Definition openvpn.h:438
struct buffer to_link
Definition openvpn.h:379
int64_t inactivity_bytes
Definition openvpn.h:290
struct buffer to_tun
Definition openvpn.h:378
struct in6_addr push_ifconfig_ipv6_local
Definition openvpn.h:437
bool buffers_owned
Definition openvpn.h:370
counter_type tun_write_bytes
Definition openvpn.h:267
struct event_timeout occ_interval
Definition openvpn.h:303
struct link_socket ** link_sockets
Definition openvpn.h:239
in_addr_t push_ifconfig_local_alias
Definition openvpn.h:434
counter_type link_read_bytes_auth
Definition openvpn.h:270
struct link_socket_info ** link_socket_infos
Definition openvpn.h:240
struct event_timeout packet_id_persist_interval
Definition openvpn.h:357
struct link_socket_actual * to_link_addr
Definition openvpn.h:246
struct event_timeout session_interval
Definition openvpn.h:292
in_addr_t push_ifconfig_remote_netmask
Definition openvpn.h:433
int original_recv_size
Definition openvpn.h:309
struct buffer buf
Definition openvpn.h:377
struct timeval timeval
Time to next event of timers and similar.
Definition openvpn.h:398
time_t explicit_exit_notification_time_wait
Definition openvpn.h:418
bool log_rw
Definition openvpn.h:382
int event_set_max
Definition openvpn.h:233
struct gc_arena gc
Garbage collection arena for allocations done in the level 2 scope of this context_2 structure.
Definition openvpn.h:227
bool fast_io
Definition openvpn.h:426
struct sha256_digest pulled_options_digest
Definition openvpn.h:447
struct event_set * event_set
Definition openvpn.h:232
struct context_buffers * buffers
Definition openvpn.h:369
struct event_timeout explicit_exit_notification_interval
Definition openvpn.h:419
int max_send_size_remote
Definition openvpn.h:313
struct event_timeout route_wakeup
Definition openvpn.h:385
int tls_exit_signal
Definition openvpn.h:349
struct event_timeout inactivity_interval
Definition openvpn.h:289
int occ_n_tries
Definition openvpn.h:302
struct event_timeout ping_rec_interval
Definition openvpn.h:286
in_addr_t push_ifconfig_local
Definition openvpn.h:432
bool event_set_owned
Definition openvpn.h:234
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:476
int mode
Role of this context within the OpenVPN process.
Definition openvpn.h:489
struct context_0 * c0
Level 0 context.
Definition openvpn.h:514
bool did_we_daemonize
Whether demonization has already taken place.
Definition openvpn.h:509
bool first_time
True on the first iteration of OpenVPN's main loop.
Definition openvpn.h:480
struct signal_info * sig
Internal error signaling object.
Definition openvpn.h:502
openvpn_net_ctx_t net_ctx
Networking API opaque context.
Definition openvpn.h:500
struct plugin_list * plugins
List of plug-ins.
Definition openvpn.h:504
struct context_2 c2
Level 2 context.
Definition openvpn.h:516
struct env_set * es
Set of environment variables.
Definition openvpn.h:498
bool plugins_owned
Whether the plug-ins should be cleaned up when this context is cleaned up.
Definition openvpn.h:505
struct gc_arena gc
Garbage collection arena for allocations done in the scope of this context structure.
Definition openvpn.h:494
struct context_1 c1
Level 1 context.
Definition openvpn.h:515
struct context_persist persist
Persistent context.
Definition openvpn.h:512
Security parameter state for processing data channel packets.
Definition crypto.h:292
Fragmentation and reassembly state for one VPN tunnel instance.
Definition fragment.h:139
Packet geometry parameters.
Definition mtu.h:98
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:117
Container for bidirectional cipher and HMAC key material.
Definition crypto.h:239
Container for two sets of OpenSSL cipher and/or HMAC contexts for both sending and receiving directio...
Definition crypto.h:279
Container for one set of cipher and/or HMAC contexts.
Definition crypto.h:201
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 tls_root_ctx ssl_ctx
Definition openvpn.h:64
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 key_ctx tls_crypt_v2_server_key
Definition openvpn.h:72
Wrapper struct to pass around SHA256 digests.
Definition crypto.h:133
Security parameter state for a single VPN tunnel.
Definition ssl_common.h:604
Structure that wraps the TLS context.
Definition tun.h:178