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-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 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
49/*
50 * Our global key schedules, packaged thusly
51 * to facilitate key persistence.
52 */
53
55{
56 /* which cipher, HMAC digest, and key sizes are we using? */
58
59 /* pre-shared static key, read from a file */
61
62 /* our global SSL context */
64
65 /* optional TLS control channel wrapping */
74};
75
76/*
77 * struct packet_id_persist should be empty if we are not
78 * building with crypto.
79 */
80#ifndef PACKET_ID_H
82{
83 int dummy;
84};
85static inline void
89#endif
90
91/*
92 * Packet processing buffers.
93 */
95{
96 /* miscellaneous buffer, used by ping, occ, etc. */
98
99 /* workspace buffers used by crypto routines */
102
103 /* workspace buffers for compression */
104#ifdef USE_COMP
105 struct buffer compress_buf;
106 struct buffer decompress_buf;
107#endif
108
109 /*
110 * Buffers used to read from TUN device
111 * and TCP/UDP port.
112 */
115};
116
117/*
118 * always-persistent context variables
119 */
124
125
126/**************************************************************************/
136{
137 /* workspace for --user/--group */
139 /* helper which tells us whether we should keep trying to drop privileges */
143};
144
145
156{
162 /* tunnel session keys */
164
165 /* preresolved and cached host names */
167
168 /* persist crypto sequence number to/from file */
170
171 struct tuntap *tuntap;
180 /* list of --route-ipv6 directives */
182
183 /* --status file */
186
187 /* HTTP proxy object */
190
191 /* SOCKS proxy object */
194
195 /* persist --ifconfig-pool db to file */
198
199 /* if client mode, hash of option strings we pulled from server */
204};
205
206
207static inline bool
209{
210 return cas == CAS_PENDING || cas == CAS_PENDING_DEFERRED
212}
213
224{
225 struct gc_arena gc;
229 /* our global wait events */
233
234 /* bitmask for event status. Check event.h for possible values */
235 unsigned int event_set_status;
236
239
241
242 const struct link_socket *accept_from; /* possibly do accept() on a parent link_socket */
243
244 struct link_socket_actual *to_link_addr; /* IP address of remote */
245 struct link_socket_actual from; /* address of incoming datagram */
246
247 /* MTU frame parameters */
248 struct frame frame; /* Active frame parameters */
249
250#ifdef ENABLE_FRAGMENT
251 /* Object to handle advanced MTU negotiation and datagram fragmentation */
254#endif
255
256 /*
257 * Traffic shaper object.
258 */
260
261 /*
262 * Statistics
263 */
271#ifdef PACKET_TRUNCATION_CHECK
272 counter_type n_trunc_tun_read;
273 counter_type n_trunc_tun_write;
274 counter_type n_trunc_pre_encrypt;
275 counter_type n_trunc_post_decrypt;
276#endif
277
278 /*
279 * Timer objects for ping and inactivity
280 * timeout features.
281 */
285
286 /* --inactive */
289
291
292 /* auth token renewal timer */
294
295 /* the option strings must match across peers */
298
299 int occ_op; /* INIT to -1 */
302
303 /*
304 * Keep track of maximum packet size received so far
305 * (of authenticated packets).
306 */
307 int original_recv_size; /* temporary */
308 int max_recv_size_local; /* max packet size received */
309 int max_recv_size_remote; /* max packet size received by remote */
310 int max_send_size_local; /* max packet size sent */
311 int max_send_size_remote; /* max packet size sent by remote */
312
313
314 /* remote wants us to send back a load test packet of this size */
316
319
320 /*
321 * TLS-mode crypto objects.
322 */
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 /* don't wait for TUN/TAP/UDP to be ready to accept write */
425
426 /* --ifconfig endpoints to be pushed to client */
433
438
441
442 /* hash of pulled options, so we can compare when options change */
446
449
450 /* packet filter */
451
452#ifdef ENABLE_MANAGEMENT
454#endif
455
456#ifdef ENABLE_ASYNC_PUSH
457 int inotify_fd; /* descriptor for monitoring file changes */
458#endif
459};
460
461
474{
481 /* context modes */
482#define CM_P2P 0 /* standalone point-to-point session or client */
483#define CM_TOP 1 /* top level of a multi-client or point-to-multipoint server */
484#define CM_TOP_CLONE 2 /* clone of a CM_TOP context for one thread */
485#define CM_CHILD_UDP 3 /* child context of a CM_TOP or CM_THREAD */
486#define CM_CHILD_TCP 4 /* child context of a CM_TOP or CM_THREAD */
487 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 perf_pop(); \
525 if (brk) { \
526 break;} \
527 else { \
528 continue;} \
529 }
530
531/*
532 * Macros for referencing objects which may not
533 * have been compiled in.
534 */
535
536#define TLS_MODE(c) ((c)->c2.tls_multi != NULL)
537#define PROTO_DUMP_FLAGS (check_debug_level(D_LINK_RW_VERBOSE) ? (PD_SHOW_DATA|PD_VERBOSE) : 0)
538#define PROTO_DUMP(buf, gc) protocol_dump((buf), \
539 PROTO_DUMP_FLAGS \
540 |(c->c2.tls_multi ? PD_TLS : 0) \
541 |(c->options.tls_auth_file ? md_kt_size(c->c1.ks.key_type.digest) : 0) \
542 |(c->options.tls_crypt_file || c->options.tls_crypt_v2_file ? PD_TLS_CRYPT : 0), \
543 gc)
544
545/* this represents "disabled peer-id" */
546#define MAX_PEER_ID 0xFFFFFF
547
548#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:86
static bool is_cas_pending(enum multi_status cas)
Definition openvpn.h:208
Control Channel SSL/Data channel negotiation module.
multi_status
Definition ssl_common.h:567
@ CAS_PENDING_DEFERRED
Waiting on an async option import handler.
Definition ssl_common.h:571
@ CAS_PENDING_DEFERRED_PARTIAL
at least handler succeeded but another is still pending
Definition ssl_common.h:572
@ CAS_PENDING
Options import (Connect script/plugin, ccd,...)
Definition ssl_common.h:570
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:136
bool uid_gid_chroot_set
Definition openvpn.h:140
bool uid_gid_specified
Definition openvpn.h:138
Level 1 context containing state that persists across SIGUSR1 restarts.
Definition openvpn.h:156
struct key_schedule ks
Definition openvpn.h:163
struct ifconfig_pool_persist * ifconfig_pool_persist
Definition openvpn.h:196
bool http_proxy_owned
Definition openvpn.h:189
struct status_output * status_output
Definition openvpn.h:184
struct route_list * route_list
List of routing information.
Definition openvpn.h:176
struct link_socket_addr * link_socket_addrs
Local and remote addresses on the external network.
Definition openvpn.h:158
struct sha256_digest pulled_options_digest_save
Hash of option strings received from the remote OpenVPN server.
Definition openvpn.h:200
int link_sockets_num
Definition openvpn.h:157
bool status_output_owned
Definition openvpn.h:185
struct route_ipv6_list * route_ipv6_list
Definition openvpn.h:181
struct packet_id_persist pid_persist
Definition openvpn.h:169
struct http_proxy_info * http_proxy
Definition openvpn.h:188
bool socks_proxy_owned
Definition openvpn.h:193
bool tuntap_owned
Whether the tun/tap interface should be cleaned up when this context is cleaned up.
Definition openvpn.h:172
bool ifconfig_pool_persist_owned
Definition openvpn.h:197
struct socks_proxy_info * socks_proxy
Definition openvpn.h:192
struct cached_dns_entry * dns_cache
Definition openvpn.h:166
struct tuntap * tuntap
Tun/tap virtual network interface.
Definition openvpn.h:171
Level 2 context containing state that is reset on both SIGHUP and SIGUSR1 restarts.
Definition openvpn.h:224
bool push_request_received
Definition openvpn.h:427
counter_type link_read_bytes
Definition openvpn.h:266
char * options_string_local
Definition openvpn.h:296
counter_type link_write_bytes
Definition openvpn.h:269
bool push_ifconfig_ipv6_defined
Definition openvpn.h:434
struct event_timeout occ_mtu_load_test_interval
Definition openvpn.h:317
struct event_timeout server_poll_interval
Definition openvpn.h:408
int max_recv_size_local
Definition openvpn.h:308
struct fragment_master * fragment
Definition openvpn.h:252
time_t update_timeout_random_component
Definition openvpn.h:403
unsigned int event_set_status
Definition openvpn.h:235
bool do_up_ran
Definition openvpn.h:411
int occ_mtu_load_size
Definition openvpn.h:315
int max_recv_size_remote
Definition openvpn.h:309
bool push_ifconfig_defined
Definition openvpn.h:428
char * options_string_remote
Definition openvpn.h:297
counter_type dco_read_bytes
Definition openvpn.h:267
int occ_mtu_load_n_tries
Definition openvpn.h:318
struct event_timeout route_wakeup_expire
Definition openvpn.h:384
struct event_timeout ping_send_interval
Definition openvpn.h:283
int max_send_size_local
Definition openvpn.h:310
bool did_open_tun
Definition openvpn.h:387
md_ctx_t * pulled_options_state
Definition openvpn.h:444
struct timeval timeout_random_component
Definition openvpn.h:404
counter_type tun_read_bytes
Definition openvpn.h:264
bool es_owned
Definition openvpn.h:421
struct man_def_auth_context mda_context
Definition openvpn.h:453
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:338
const struct link_socket * accept_from
Definition openvpn.h:242
bool pulled_options_digest_init_done
Definition openvpn.h:443
struct tls_auth_standalone * tls_auth_standalone
TLS state structure required for the initial authentication of a client's connection attempt.
Definition openvpn.h:326
counter_type dco_write_bytes
Definition openvpn.h:270
struct event_timeout scheduled_exit
Definition openvpn.h:447
int occ_op
Definition openvpn.h:299
struct env_set * es
Definition openvpn.h:420
time_t push_request_timeout
Definition openvpn.h:440
struct interval tmp_int
Definition openvpn.h:344
struct event_timeout auth_token_renewal_interval
Definition openvpn.h:293
bool link_socket_owned
Definition openvpn.h:240
struct event_timeout wait_for_connect
Definition openvpn.h:282
struct event_timeout push_request_interval
Definition openvpn.h:439
struct tls_multi * tls_multi
TLS state structure for this VPN tunnel.
Definition openvpn.h:323
time_t sent_push_reply_expiry
Definition openvpn.h:429
time_t coarse_timer_wakeup
Definition openvpn.h:399
int scheduled_exit_signal
Definition openvpn.h:448
struct in6_addr push_ifconfig_ipv6_remote
Definition openvpn.h:437
struct link_socket_actual from
Definition openvpn.h:245
struct frame frame_fragment
Definition openvpn.h:253
int push_ifconfig_ipv6_netbits
Definition openvpn.h:436
struct buffer to_link
Definition openvpn.h:377
int64_t inactivity_bytes
Definition openvpn.h:288
struct buffer to_tun
Definition openvpn.h:376
struct in6_addr push_ifconfig_ipv6_local
Definition openvpn.h:435
bool buffers_owned
Definition openvpn.h:368
counter_type tun_write_bytes
Definition openvpn.h:265
struct event_timeout occ_interval
Definition openvpn.h:301
struct link_socket ** link_sockets
Definition openvpn.h:237
in_addr_t push_ifconfig_local_alias
Definition openvpn.h:432
counter_type link_read_bytes_auth
Definition openvpn.h:268
struct link_socket_info ** link_socket_infos
Definition openvpn.h:238
struct event_timeout packet_id_persist_interval
Definition openvpn.h:355
struct link_socket_actual * to_link_addr
Definition openvpn.h:244
struct event_timeout session_interval
Definition openvpn.h:290
in_addr_t push_ifconfig_remote_netmask
Definition openvpn.h:431
int original_recv_size
Definition openvpn.h:307
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:231
struct gc_arena gc
Garbage collection arena for allocations done in the level 2 scope of this context_2 structure.
Definition openvpn.h:225
bool fast_io
Definition openvpn.h:424
struct sha256_digest pulled_options_digest
Definition openvpn.h:445
struct event_set * event_set
Definition openvpn.h:230
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:311
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:287
int occ_n_tries
Definition openvpn.h:300
struct event_timeout ping_rec_interval
Definition openvpn.h:284
in_addr_t push_ifconfig_local
Definition openvpn.h:430
bool event_set_owned
Definition openvpn.h:232
struct buffer read_link_buf
Definition openvpn.h:113
struct buffer encrypt_buf
Definition openvpn.h:100
struct buffer read_tun_buf
Definition openvpn.h:114
struct buffer decrypt_buf
Definition openvpn.h:101
struct buffer aux_buf
Definition openvpn.h:97
int restart_sleep_seconds
Definition openvpn.h:122
Contains all state information for one tunnel.
Definition openvpn.h:474
int mode
Role of this context within the OpenVPN process.
Definition openvpn.h:487
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
bool first_time
True on the first iteration of OpenVPN's main loop.
Definition openvpn.h:478
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:292
Fragmentation and reassembly state for one VPN tunnel instance.
Definition fragment.h:136
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:72
struct key2 original_wrap_keydata
original tls-crypt key preserved to xored into the tls_crypt renegotiation key
Definition openvpn.h:70
struct key_ctx auth_token_key
Definition openvpn.h:73
struct tls_root_ctx ssl_ctx
Definition openvpn.h:63
struct key_type tls_auth_key_type
Definition openvpn.h:66
struct key_ctx_bi tls_wrap_key
Definition openvpn.h:67
struct key_ctx_bi static_key
Definition openvpn.h:60
struct key_ctx tls_crypt_v2_server_key
Definition openvpn.h:71
Wrapper struct to pass around SHA256 digests.
Definition crypto.h:133
Security parameter state for a single VPN tunnel.
Definition ssl_common.h:597
Structure that wraps the TLS context.
Definition tun.h:181