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-2018 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 "pf.h"
46 #include "pool.h"
47 #include "plugin.h"
48 #include "manage.h"
49 
50 /*
51  * Our global key schedules, packaged thusly
52  * to facilitate --persist-key.
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 */
72 };
73 
74 /*
75  * struct packet_id_persist should be empty if we are not
76  * building with crypto.
77  */
78 #ifndef PACKET_ID_H
80 {
81  int dummy;
82 };
83 static inline void
85 {
86 }
87 #endif
88 
89 /*
90  * Packet processing buffers.
91  */
93 {
94  /* miscellaneous buffer, used by ping, occ, etc. */
95  struct buffer aux_buf;
96 
97  /* workspace buffers used by crypto routines */
98  struct buffer encrypt_buf;
99  struct buffer decrypt_buf;
100 
101  /* workspace buffers for compression */
102 #ifdef USE_COMP
103  struct buffer compress_buf;
104  struct buffer decompress_buf;
105 #endif
106 
107  /*
108  * Buffers used to read from TUN device
109  * and TCP/UDP port.
110  */
111  struct buffer read_link_buf;
112  struct buffer read_tun_buf;
113 };
114 
115 /*
116  * always-persistent context variables
117  */
119 {
121 };
122 
123 
124 /**************************************************************************/
133 struct context_0
134 {
135  /* workspace for --user/--group */
137  /* helper which tells us whether we should keep trying to drop privileges */
141 };
142 
143 
153 struct context_1
154 {
159  /* tunnel session keys */
160  struct key_schedule ks;
161 
162  /* preresolved and cached host names */
164 
165  /* persist crypto sequence number to/from file */
166  struct packet_id_persist pid_persist;
167 
168  struct tuntap *tuntap;
177  /* list of --route-ipv6 directives */
179 
180  /* --status file */
183 
184  /* HTTP proxy object */
187 
188  /* SOCKS proxy object */
191 
192  /* persist --ifconfig-pool db to file */
195 
196  /* if client mode, hash of option strings we pulled from server */
197  struct sha256_digest pulled_options_digest_save;
205 };
206 
207 
208 static inline bool
210 {
211  return cas == CAS_PENDING || cas == CAS_PENDING_DEFERRED
213 }
214 
224 struct context_2
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 
238  struct link_socket *link_socket; /* socket used for TCP/UDP connection to remote */
241  const struct link_socket *accept_from; /* possibly do accept() on a parent link_socket */
242 
243  struct link_socket_actual *to_link_addr; /* IP address of remote */
244  struct link_socket_actual from; /* address of incoming datagram */
245 
246  /* MTU frame parameters */
247  struct frame frame; /* Active frame parameters */
248  struct frame frame_initial; /* Restored on new session */
249 
250 #ifdef ENABLE_FRAGMENT
251  /* Object to handle advanced MTU negotiation and datagram fragmentation */
253  struct frame frame_fragment;
254  struct frame frame_fragment_initial;
255  struct frame frame_fragment_omit;
256 #endif
257 
258  /*
259  * Traffic shaper object.
260  */
261  struct shaper shaper;
262 
263  /*
264  * Statistics
265  */
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  */
282  struct event_timeout wait_for_connect;
283  struct event_timeout ping_send_interval;
284  struct event_timeout ping_rec_interval;
285 
286  /* --inactive */
287  struct event_timeout inactivity_interval;
289 
290  /* the option strings must match across peers */
293 
294  int occ_op; /* INIT to -1 */
296  struct event_timeout occ_interval;
297 
298  /*
299  * Keep track of maximum packet size received so far
300  * (of authenticated packets).
301  */
302  int original_recv_size; /* temporary */
303  int max_recv_size_local; /* max packet size received */
304  int max_recv_size_remote; /* max packet size received by remote */
305  int max_send_size_local; /* max packet size sent */
306  int max_send_size_remote; /* max packet size sent by remote */
307 
308 
309  /* remote wants us to send back a load test packet of this size */
311 
312  struct event_timeout occ_mtu_load_test_interval;
314 
315  /*
316  * TLS-mode crypto objects.
317  */
332  /* used to optimize calls to tls_multi_process */
333  struct interval tmp_int;
334 
335  /* throw this signal on TLS errors */
337 
344  struct event_timeout packet_id_persist_interval;
345 
346 #ifdef USE_COMP
347  struct compress_context *comp_context;
351 #endif
352 
353  /*
354  * Buffers used for packet processing.
355  */
357  bool buffers_owned; /* if true, we should free all buffers on close */
358 
359  /*
360  * These buffers don't actually allocate storage, they are used
361  * as pointers to the allocated buffers in
362  * struct context_buffers.
363  */
364  struct buffer buf;
365  struct buffer to_tun;
366  struct buffer to_link;
367 
368  /* should we print R|W|r|w to console on packet transfers? */
369  bool log_rw;
370 
371  /* route stuff */
372  struct event_timeout route_wakeup;
373  struct event_timeout route_wakeup_expire;
374 
375  /* did we open tun/tap dev during this cycle? */
377 
378  /*
379  * Event loop info
380  */
381 
382  /* how long to wait on link/tun read before we will need to be serviced */
383  struct timeval timeval;
384 
385  /* next wakeup for processing coarse timers (>1 sec resolution) */
387 
388  /* maintain a random delta to add to timeouts to avoid contexts
389  * waking up simultaneously */
391  struct timeval timeout_random_component;
392 
393  /* Timer for everything up to the first packet from the *OpenVPN* server
394  * socks, http proxy, and tcp packets do not count */
395  struct event_timeout server_poll_interval;
396 
397  /* indicates that the do_up_delay function has run */
398  bool do_up_ran;
399 
400  /* indicates that we have received a SIGTERM when
401  * options->explicit_exit_notification is enabled,
402  * but we have not exited yet */
404  struct event_timeout explicit_exit_notification_interval;
405 
406  /* environmental variables to pass to scripts */
407  struct env_set *es;
408  bool es_owned;
409 
410  /* don't wait for TUN/TAP/UDP to be ready to accept write */
411  bool fast_io;
412 
413  /* --ifconfig endpoints to be pushed to client */
420 
422  struct in6_addr push_ifconfig_ipv6_local;
424  struct in6_addr push_ifconfig_ipv6_remote;
425 
426  struct event_timeout push_request_interval;
428 
429  /* hash of pulled options, so we can compare when options change */
432  struct sha256_digest pulled_options_digest;
433 
434  struct event_timeout scheduled_exit;
436 
437  /* packet filter */
438 #ifdef ENABLE_PF
439  struct pf_context pf;
440 #endif
441 
442 #ifdef ENABLE_MANAGEMENT
443  struct man_def_auth_context mda_context;
444 #endif
445 
446 #ifdef ENABLE_ASYNC_PUSH
447  int inotify_fd; /* descriptor for monitoring file changes */
448 #endif
449 };
450 
451 
463 struct context
464 {
465  struct options options;
468  bool first_time;
471  /* context modes */
472 #define CM_P2P 0 /* standalone point-to-point session or client */
473 #define CM_TOP 1 /* top level of a multi-client or point-to-multipoint server */
474 #define CM_TOP_CLONE 2 /* clone of a CM_TOP context for one thread */
475 #define CM_CHILD_UDP 3 /* child context of a CM_TOP or CM_THREAD */
476 #define CM_CHILD_TCP 4 /* child context of a CM_TOP or CM_THREAD */
477  int mode;
482  struct gc_arena gc;
486  struct env_set *es;
490  struct signal_info *sig;
500  struct context_persist persist;
502  struct context_0 *c0;
503  struct context_1 c1;
504  struct context_2 c2;
505 };
506 
507 /*
508  * Check for a signal when inside an event loop
509  */
510 #define EVENT_LOOP_CHECK_SIGNAL(c, func, arg) \
511  if (IS_SIG(c)) \
512  { \
513  const int brk = func(arg); \
514  perf_pop(); \
515  if (brk) { \
516  break;} \
517  else { \
518  continue;} \
519  }
520 
521 /*
522  * Macros for referencing objects which may not
523  * have been compiled in.
524  */
525 
526 #define TLS_MODE(c) ((c)->c2.tls_multi != NULL)
527 #define PROTO_DUMP_FLAGS (check_debug_level(D_LINK_RW_VERBOSE) ? (PD_SHOW_DATA|PD_VERBOSE) : 0)
528 #define PROTO_DUMP(buf, gc) protocol_dump((buf), \
529  PROTO_DUMP_FLAGS \
530  |(c->c2.tls_multi ? PD_TLS : 0) \
531  |(c->options.tls_auth_file ? c->c1.ks.key_type.hmac_length : 0), \
532  gc)
533 
534 #define CIPHER_ENABLED(c) (c->c1.ks.key_type.cipher != NULL)
535 
536 /* this represents "disabled peer-id" */
537 #define MAX_PEER_ID 0xFFFFFF
538 
539 #endif /* ifndef OPENVPN_H */
struct env_set * es
Definition: openvpn.h:407
bool socks_proxy_owned
Definition: openvpn.h:190
counter_type tun_read_bytes
Definition: openvpn.h:266
Security parameter state for processing data channel packets.
Definition: crypto.h:232
bool log_rw
Definition: openvpn.h:369
Definition: tun.h:155
static bool is_cas_pending(enum client_connect_status cas)
Definition: openvpn.h:209
Level 1 context containing state that persists across SIGUSR1 restarts.
Definition: openvpn.h:153
Fragmentation and reassembly state for one VPN tunnel instance.
Definition: fragment.h:136
counter_type link_write_bytes
Definition: openvpn.h:270
bool did_open_tun
Definition: openvpn.h:376
struct cached_dns_entry * dns_cache
Definition: openvpn.h:163
Contains all state information for one tunnel.
Definition: openvpn.h:463
Packet geometry parameters.
Definition: mtu.h:93
struct env_set * es
Set of environment variables.
Definition: openvpn.h:486
bool push_ifconfig_defined
Definition: openvpn.h:415
int occ_mtu_load_size
Definition: openvpn.h:310
in_addr_t push_ifconfig_local
Definition: openvpn.h:417
static void packet_id_persist_init(struct packet_id_persist *p)
Definition: openvpn.h:84
Security parameter state for a single VPN tunnel.
Definition: ssl_common.h:533
struct tls_auth_standalone * tls_auth_standalone
TLS state structure required for the initial authentication of a client&#39;s connection attempt...
Definition: openvpn.h:321
int occ_mtu_load_n_tries
Definition: openvpn.h:313
bool pulled_options_digest_init_done
Definition: openvpn.h:430
struct context_buffers * buffers
Definition: openvpn.h:356
struct link_socket_info * link_socket_info
Definition: openvpn.h:240
const struct link_socket * accept_from
Definition: openvpn.h:241
struct socks_proxy_info * socks_proxy
Definition: openvpn.h:189
#define in_addr_t
Definition: config-msvc.h:103
struct signal_info * sig
Internal error signaling object.
Definition: openvpn.h:490
void * openvpn_net_ctx_t
Definition: networking.h:26
int max_recv_size_local
Definition: openvpn.h:303
int occ_n_tries
Definition: openvpn.h:295
struct tuntap * tuntap
Tun/tap virtual network interface.
Definition: openvpn.h:168
Level 2 context containing state that is reset on both SIGHUP and SIGUSR1 restarts.
Definition: openvpn.h:224
struct key_ctx_bi tls_wrap_key
Definition: openvpn.h:68
Definition: socket.h:75
bool tuntap_owned
Whether the tun/tap interface should be cleaned up when this context is cleaned up.
Definition: openvpn.h:169
bool push_request_received
Definition: openvpn.h:414
time_t explicit_exit_notification_time_wait
Definition: openvpn.h:403
bool uid_gid_chroot_set
Definition: openvpn.h:138
openvpn_net_ctx_t net_ctx
Networking API opaque context.
Definition: openvpn.h:488
md_ctx_t * pulled_options_state
Definition: openvpn.h:431
counter_type tun_write_bytes
Definition: openvpn.h:267
Definition: shaper.h:47
counter_type link_read_bytes
Definition: openvpn.h:268
struct link_socket_actual * to_link_addr
Definition: openvpn.h:243
int inactivity_bytes
Definition: openvpn.h:288
struct route_list * route_list
List of routing information.
Definition: openvpn.h:173
in_addr_t push_ifconfig_local_alias
Definition: openvpn.h:419
at least handler succeeded, no result yet
Definition: ssl_common.h:515
int push_ifconfig_ipv6_netbits
Definition: openvpn.h:423
uint64_t counter_type
Definition: common.h:30
Container for one set of cipher and/or HMAC contexts.
Definition: crypto.h:164
int event_set_max
Definition: openvpn.h:232
struct key_ctx auth_token_key
Definition: openvpn.h:71
int restart_sleep_seconds
Definition: openvpn.h:120
struct link_socket * link_socket
Definition: openvpn.h:238
struct buffer tls_crypt_v2_wkc
Wrapped client key.
Definition: openvpn.h:70
bool buffers_owned
Definition: openvpn.h:357
struct route_ipv6_list * route_ipv6_list
Definition: openvpn.h:178
time_t sent_push_reply_expiry
Definition: openvpn.h:416
time_t update_timeout_random_component
Definition: openvpn.h:390
bool uid_gid_specified
Definition: openvpn.h:136
struct ifconfig_pool_persist * ifconfig_pool_persist
Definition: openvpn.h:193
struct event_set * event_set
Definition: openvpn.h:231
int max_recv_size_remote
Definition: openvpn.h:304
struct key_type tls_auth_key_type
Definition: openvpn.h:67
int scheduled_exit_signal
Definition: openvpn.h:435
bool push_ifconfig_ipv6_defined
Definition: openvpn.h:421
Structure that wraps the TLS context.
Definition: ssl_mbedtls.h:104
struct context_0 * c0
Level 0 context.
Definition: openvpn.h:502
Level 0 context containing information related to the OpenVPN process.
Definition: openvpn.h:133
int max_send_size_remote
Definition: openvpn.h:306
time_t push_request_timeout
Definition: openvpn.h:427
struct tls_root_ctx ssl_ctx
Definition: openvpn.h:64
int mode
Role of this context within the OpenVPN process.
Definition: openvpn.h:477
char * options_string_remote
Definition: openvpn.h:292
Definition: misc.h:56
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
bool plugins_owned
Whether the plug-ins should be cleaned up when this context is cleaned up.
Definition: openvpn.h:493
struct fragment_master * fragment
Definition: openvpn.h:252
struct status_output * status_output
Definition: openvpn.h:181
struct plugin_list * plugins
List of plug-ins.
Definition: openvpn.h:492
struct http_proxy_info * http_proxy
Definition: openvpn.h:185
bool ifconfig_pool_persist_owned
Definition: openvpn.h:194
struct user_pass * auth_user_pass
Username and password for authentication.
Definition: openvpn.h:202
bool first_time
True on the first iteration of OpenVPN&#39;s main loop.
Definition: openvpn.h:468
bool do_up_ran
Definition: openvpn.h:398
bool fast_io
Definition: openvpn.h:411
char * options_string_local
Definition: openvpn.h:291
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
int original_recv_size
Definition: openvpn.h:302
client_connect_status
Definition: ssl_common.h:511
struct key_ctx tls_crypt_v2_server_key
Definition: openvpn.h:69
Data Channel Fragmentation module header file.
bool did_we_daemonize
Whether demonization has already taken place.
Definition: openvpn.h:497
bool status_output_owned
Definition: openvpn.h:182
Wrapper struct to pass around SHA256 digests.
Definition: crypto.h:132
bool link_socket_owned
Definition: openvpn.h:239
unsigned int event_set_status
Definition: openvpn.h:236
struct tls_multi * tls_multi
TLS state structure for this VPN tunnel.
Definition: openvpn.h:318
in_addr_t push_ifconfig_remote_netmask
Definition: openvpn.h:418
int max_send_size_local
Definition: openvpn.h:305
int occ_op
Definition: openvpn.h:294
bool http_proxy_owned
Definition: openvpn.h:186
bool es_owned
Definition: openvpn.h:408
bool event_set_owned
Definition: openvpn.h:233
mbedtls_md_context_t md_ctx_t
Generic message digest context.
int tls_exit_signal
Definition: openvpn.h:336
struct key_ctx_bi static_key
Definition: openvpn.h:61
Container for two sets of OpenSSL cipher and/or HMAC contexts for both sending and receiving directio...
Definition: crypto.h:219
time_t coarse_timer_wakeup
Definition: openvpn.h:386
counter_type link_read_bytes_auth
Definition: openvpn.h:269