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 #if P2MP
193  /* persist --ifconfig-pool db to file */
196 
197  /* if client mode, hash of option strings we pulled from server */
198  struct sha256_digest pulled_options_digest_save;
207  const char *ciphername;
208  const char *authname;
209  int keysize;
210 #endif
211 };
212 
213 
214 /* client authentication state, CAS_SUCCEEDED must be 0 since
215  * non multi code path still checks this variable but does not initialise it
216  * so the code depends on zero initialisation */
223 };
224 
225 static inline bool
227 {
228  return cas == CAS_PENDING || cas == CAS_PENDING_DEFERRED
230 }
231 
241 struct context_2
242 {
243  struct gc_arena gc;
247  /* our global wait events */
251 
252  /* event flags returned by io_wait */
253 #define SOCKET_READ (1<<0)
254 #define SOCKET_WRITE (1<<1)
255 #define TUN_READ (1<<2)
256 #define TUN_WRITE (1<<3)
257 #define ES_ERROR (1<<4)
258 #define ES_TIMEOUT (1<<5)
259 #ifdef ENABLE_MANAGEMENT
260 #define MANAGEMENT_READ (1<<6)
261 #define MANAGEMENT_WRITE (1<<7)
262 #endif
263 #ifdef ENABLE_ASYNC_PUSH
264 #define FILE_CLOSED (1<<8)
265 #endif
266 
267  unsigned int event_set_status;
268 
269  struct link_socket *link_socket; /* socket used for TCP/UDP connection to remote */
272  const struct link_socket *accept_from; /* possibly do accept() on a parent link_socket */
273 
274  struct link_socket_actual *to_link_addr; /* IP address of remote */
275  struct link_socket_actual from; /* address of incoming datagram */
276 
277  /* MTU frame parameters */
278  struct frame frame; /* Active frame parameters */
279  struct frame frame_initial; /* Restored on new session */
280 
281 #ifdef ENABLE_FRAGMENT
282  /* Object to handle advanced MTU negotiation and datagram fragmentation */
284  struct frame frame_fragment;
285  struct frame frame_fragment_initial;
286  struct frame frame_fragment_omit;
287 #endif
288 
289 #ifdef ENABLE_FEATURE_SHAPER
290  /*
291  * Traffic shaper object.
292  */
293  struct shaper shaper;
294 #endif
295 
296  /*
297  * Statistics
298  */
304 #ifdef PACKET_TRUNCATION_CHECK
305  counter_type n_trunc_tun_read;
306  counter_type n_trunc_tun_write;
307  counter_type n_trunc_pre_encrypt;
308  counter_type n_trunc_post_decrypt;
309 #endif
310 
311  /*
312  * Timer objects for ping and inactivity
313  * timeout features.
314  */
315  struct event_timeout wait_for_connect;
316  struct event_timeout ping_send_interval;
317  struct event_timeout ping_rec_interval;
318 
319  /* --inactive */
320  struct event_timeout inactivity_interval;
322 
323  /* the option strings must match across peers */
326 
327  int occ_op; /* INIT to -1 */
329  struct event_timeout occ_interval;
330 
331  /*
332  * Keep track of maximum packet size received so far
333  * (of authenticated packets).
334  */
335  int original_recv_size; /* temporary */
336  int max_recv_size_local; /* max packet size received */
337  int max_recv_size_remote; /* max packet size received by remote */
338  int max_send_size_local; /* max packet size sent */
339  int max_send_size_remote; /* max packet size sent by remote */
340 
341 
342  /* remote wants us to send back a load test packet of this size */
344 
345  struct event_timeout occ_mtu_load_test_interval;
347 
348  /*
349  * TLS-mode crypto objects.
350  */
365  /* used to optimize calls to tls_multi_process */
366  struct interval tmp_int;
367 
368  /* throw this signal on TLS errors */
370 
377  struct event_timeout packet_id_persist_interval;
378 
379 #ifdef USE_COMP
380  struct compress_context *comp_context;
384 #endif
385 
386  /*
387  * Buffers used for packet processing.
388  */
390  bool buffers_owned; /* if true, we should free all buffers on close */
391 
392  /*
393  * These buffers don't actually allocate storage, they are used
394  * as pointers to the allocated buffers in
395  * struct context_buffers.
396  */
397  struct buffer buf;
398  struct buffer to_tun;
399  struct buffer to_link;
400 
401  /* should we print R|W|r|w to console on packet transfers? */
402  bool log_rw;
403 
404  /* route stuff */
405  struct event_timeout route_wakeup;
406  struct event_timeout route_wakeup_expire;
407 
408  /* did we open tun/tap dev during this cycle? */
410 
411  /*
412  * Event loop info
413  */
414 
415  /* how long to wait on link/tun read before we will need to be serviced */
416  struct timeval timeval;
417 
418  /* next wakeup for processing coarse timers (>1 sec resolution) */
420 
421  /* maintain a random delta to add to timeouts to avoid contexts
422  * waking up simultaneously */
424  struct timeval timeout_random_component;
425 
426  /* Timer for everything up to the first packet from the *OpenVPN* server
427  * socks, http proxy, and tcp packets do not count */
428  struct event_timeout server_poll_interval;
429 
430  /* indicates that the do_up_delay function has run */
431  bool do_up_ran;
432 
433  /* indicates that we have received a SIGTERM when
434  * options->explicit_exit_notification is enabled,
435  * but we have not exited yet */
437  struct event_timeout explicit_exit_notification_interval;
438 
439  /* environmental variables to pass to scripts */
440  struct env_set *es;
441  bool es_owned;
442 
443  /* don't wait for TUN/TAP/UDP to be ready to accept write */
444  bool fast_io;
445 
446 #if P2MP
447 
448  /* --ifconfig endpoints to be pushed to client */
455 
457  struct in6_addr push_ifconfig_ipv6_local;
459  struct in6_addr push_ifconfig_ipv6_remote;
460 
461 
462  enum client_connect_status context_auth;
463 
464  struct event_timeout push_request_interval;
467 
468  /* hash of pulled options, so we can compare when options change */
471  struct sha256_digest pulled_options_digest;
472 
473  struct event_timeout scheduled_exit;
475 #endif /* if P2MP */
476 
477  /* packet filter */
478 #ifdef ENABLE_PF
479  struct pf_context pf;
480 #endif
481 
482 #ifdef ENABLE_MANAGEMENT
483  struct man_def_auth_context mda_context;
484 #endif
485 
486 #ifdef ENABLE_ASYNC_PUSH
487  int inotify_fd; /* descriptor for monitoring file changes */
488 #endif
489 };
490 
491 
503 struct context
504 {
505  struct options options;
508  bool first_time;
511  /* context modes */
512 #define CM_P2P 0 /* standalone point-to-point session or client */
513 #define CM_TOP 1 /* top level of a multi-client or point-to-multipoint server */
514 #define CM_TOP_CLONE 2 /* clone of a CM_TOP context for one thread */
515 #define CM_CHILD_UDP 3 /* child context of a CM_TOP or CM_THREAD */
516 #define CM_CHILD_TCP 4 /* child context of a CM_TOP or CM_THREAD */
517  int mode;
522  struct gc_arena gc;
526  struct env_set *es;
530  struct signal_info *sig;
540  struct context_persist persist;
542  struct context_0 *c0;
543  struct context_1 c1;
544  struct context_2 c2;
545 };
546 
547 /*
548  * Check for a signal when inside an event loop
549  */
550 #define EVENT_LOOP_CHECK_SIGNAL(c, func, arg) \
551  if (IS_SIG(c)) \
552  { \
553  const int brk = func(arg); \
554  perf_pop(); \
555  if (brk) { \
556  break;} \
557  else { \
558  continue;} \
559  }
560 
561 /*
562  * Macros for referencing objects which may not
563  * have been compiled in.
564  */
565 
566 #define TLS_MODE(c) ((c)->c2.tls_multi != NULL)
567 #define PROTO_DUMP_FLAGS (check_debug_level(D_LINK_RW_VERBOSE) ? (PD_SHOW_DATA|PD_VERBOSE) : 0)
568 #define PROTO_DUMP(buf, gc) protocol_dump((buf), \
569  PROTO_DUMP_FLAGS \
570  |(c->c2.tls_multi ? PD_TLS : 0) \
571  |(c->options.tls_auth_file ? c->c1.ks.key_type.hmac_length : 0), \
572  gc)
573 
574 #define CIPHER_ENABLED(c) (c->c1.ks.key_type.cipher != NULL)
575 
576 /* this represents "disabled peer-id" */
577 #define MAX_PEER_ID 0xFFFFFF
578 
579 #endif /* ifndef OPENVPN_H */
struct env_set * es
Definition: openvpn.h:440
const char * ciphername
Data channel cipher from config file.
Definition: openvpn.h:207
bool socks_proxy_owned
Definition: openvpn.h:190
counter_type tun_read_bytes
Definition: openvpn.h:299
Security parameter state for processing data channel packets.
Definition: crypto.h:232
bool log_rw
Definition: openvpn.h:402
Definition: tun.h:151
static bool is_cas_pending(enum client_connect_status cas)
Definition: openvpn.h:226
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:303
client_connect_status
Definition: openvpn.h:217
bool did_open_tun
Definition: openvpn.h:409
struct cached_dns_entry * dns_cache
Definition: openvpn.h:163
Contains all state information for one tunnel.
Definition: openvpn.h:503
Packet geometry parameters.
Definition: mtu.h:93
struct env_set * es
Set of environment variables.
Definition: openvpn.h:526
bool push_ifconfig_defined
Definition: openvpn.h:450
int occ_mtu_load_size
Definition: openvpn.h:343
in_addr_t push_ifconfig_local
Definition: openvpn.h:452
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:512
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:354
int keysize
Data channel keysize from config file.
Definition: openvpn.h:209
int occ_mtu_load_n_tries
Definition: openvpn.h:346
bool pulled_options_digest_init_done
Definition: openvpn.h:469
struct context_buffers * buffers
Definition: openvpn.h:389
struct link_socket_info * link_socket_info
Definition: openvpn.h:271
const struct link_socket * accept_from
Definition: openvpn.h:272
struct socks_proxy_info * socks_proxy
Definition: openvpn.h:189
#define in_addr_t
Definition: config-msvc.h:140
struct signal_info * sig
Internal error signaling object.
Definition: openvpn.h:530
void * openvpn_net_ctx_t
Definition: networking.h:26
bool did_pre_pull_restore
Definition: openvpn.h:466
int max_recv_size_local
Definition: openvpn.h:336
int occ_n_tries
Definition: openvpn.h:328
at least handler succeeded, no result yet
Definition: openvpn.h:221
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:241
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:449
time_t explicit_exit_notification_time_wait
Definition: openvpn.h:436
bool uid_gid_chroot_set
Definition: openvpn.h:138
openvpn_net_ctx_t net_ctx
Networking API opaque context.
Definition: openvpn.h:528
md_ctx_t * pulled_options_state
Definition: openvpn.h:470
counter_type tun_write_bytes
Definition: openvpn.h:300
counter_type link_read_bytes
Definition: openvpn.h:301
struct link_socket_actual * to_link_addr
Definition: openvpn.h:274
int inactivity_bytes
Definition: openvpn.h:321
struct route_list * route_list
List of routing information.
Definition: openvpn.h:173
in_addr_t push_ifconfig_local_alias
Definition: openvpn.h:454
int push_ifconfig_ipv6_netbits
Definition: openvpn.h:458
Container for one set of cipher and/or HMAC contexts.
Definition: crypto.h:164
int event_set_max
Definition: openvpn.h:249
struct key_ctx auth_token_key
Definition: openvpn.h:71
int restart_sleep_seconds
Definition: openvpn.h:120
unsigned int counter_type
Definition: common.h:38
struct link_socket * link_socket
Definition: openvpn.h:269
struct buffer tls_crypt_v2_wkc
Wrapped client key.
Definition: openvpn.h:70
const char * authname
Data channel auth from config file.
Definition: openvpn.h:208
bool buffers_owned
Definition: openvpn.h:390
struct route_ipv6_list * route_ipv6_list
Definition: openvpn.h:178
time_t sent_push_reply_expiry
Definition: openvpn.h:451
time_t update_timeout_random_component
Definition: openvpn.h:423
bool uid_gid_specified
Definition: openvpn.h:136
struct ifconfig_pool_persist * ifconfig_pool_persist
Definition: openvpn.h:194
struct event_set * event_set
Definition: openvpn.h:248
int max_recv_size_remote
Definition: openvpn.h:337
struct key_type tls_auth_key_type
Definition: openvpn.h:67
int scheduled_exit_signal
Definition: openvpn.h:474
bool push_ifconfig_ipv6_defined
Definition: openvpn.h:456
Structure that wraps the TLS context.
Definition: ssl_mbedtls.h:104
struct context_0 * c0
Level 0 context.
Definition: openvpn.h:542
Level 0 context containing information related to the OpenVPN process.
Definition: openvpn.h:133
int max_send_size_remote
Definition: openvpn.h:339
time_t push_request_timeout
Definition: openvpn.h:465
struct tls_root_ctx ssl_ctx
Definition: openvpn.h:64
int mode
Role of this context within the OpenVPN process.
Definition: openvpn.h:517
char * options_string_remote
Definition: openvpn.h:325
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:533
struct fragment_master * fragment
Definition: openvpn.h:283
struct status_output * status_output
Definition: openvpn.h:181
struct plugin_list * plugins
List of plug-ins.
Definition: openvpn.h:532
struct http_proxy_info * http_proxy
Definition: openvpn.h:185
bool ifconfig_pool_persist_owned
Definition: openvpn.h:195
struct user_pass * auth_user_pass
Username and password for authentication.
Definition: openvpn.h:203
bool first_time
True on the first iteration of OpenVPN&#39;s main loop.
Definition: openvpn.h:508
bool do_up_ran
Definition: openvpn.h:431
bool fast_io
Definition: openvpn.h:444
char * options_string_local
Definition: openvpn.h:324
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
int original_recv_size
Definition: openvpn.h:335
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:537
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:270
unsigned int event_set_status
Definition: openvpn.h:267
struct tls_multi * tls_multi
TLS state structure for this VPN tunnel.
Definition: openvpn.h:351
in_addr_t push_ifconfig_remote_netmask
Definition: openvpn.h:453
int max_send_size_local
Definition: openvpn.h:338
int occ_op
Definition: openvpn.h:327
bool http_proxy_owned
Definition: openvpn.h:186
bool es_owned
Definition: openvpn.h:441
bool event_set_owned
Definition: openvpn.h:250
mbedtls_md_context_t md_ctx_t
Generic message digest context.
int tls_exit_signal
Definition: openvpn.h:369
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:419
counter_type link_read_bytes_auth
Definition: openvpn.h:302