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 */
71 };
72 
73 /*
74  * struct packet_id_persist should be empty if we are not
75  * building with crypto.
76  */
77 #ifndef PACKET_ID_H
79 {
80  int dummy;
81 };
82 static inline void
84 {
85 }
86 #endif
87 
88 /*
89  * Packet processing buffers.
90  */
92 {
93  /* miscellaneous buffer, used by ping, occ, etc. */
94  struct buffer aux_buf;
95 
96  /* workspace buffers used by crypto routines */
97  struct buffer encrypt_buf;
98  struct buffer decrypt_buf;
99 
100  /* workspace buffers for compression */
101 #ifdef USE_COMP
102  struct buffer compress_buf;
103  struct buffer decompress_buf;
104 #endif
105 
106  /*
107  * Buffers used to read from TUN device
108  * and TCP/UDP port.
109  */
110  struct buffer read_link_buf;
111  struct buffer read_tun_buf;
112 };
113 
114 /*
115  * always-persistent context variables
116  */
118 {
120 };
121 
122 
123 /**************************************************************************/
132 struct context_0
133 {
134  /* workspace for --user/--group */
136  /* helper which tells us whether we should keep trying to drop privileges */
140 };
141 
142 
152 struct context_1
153 {
158  /* tunnel session keys */
159  struct key_schedule ks;
160 
161  /* preresolved and cached host names */
163 
164  /* persist crypto sequence number to/from file */
165  struct packet_id_persist pid_persist;
166 
167  struct tuntap *tuntap;
176  /* list of --route-ipv6 directives */
178 
179  /* --status file */
182 
183  /* HTTP proxy object */
186 
187  /* SOCKS proxy object */
190 
191 #if P2MP
192 
193 #if P2MP_SERVER
194  /* persist --ifconfig-pool db to file */
197 #endif
198 
199  /* if client mode, hash of option strings we pulled from server */
200  struct sha256_digest pulled_options_digest_save;
209  const char *ciphername;
210  const char *authname;
211  int keysize;
212 #endif
213 };
214 
224 struct context_2
225 {
226  struct gc_arena gc;
230  /* our global wait events */
234 
235  /* event flags returned by io_wait */
236 #define SOCKET_READ (1<<0)
237 #define SOCKET_WRITE (1<<1)
238 #define TUN_READ (1<<2)
239 #define TUN_WRITE (1<<3)
240 #define ES_ERROR (1<<4)
241 #define ES_TIMEOUT (1<<5)
242 #ifdef ENABLE_MANAGEMENT
243 #define MANAGEMENT_READ (1<<6)
244 #define MANAGEMENT_WRITE (1<<7)
245 #endif
246 #ifdef ENABLE_ASYNC_PUSH
247 #define FILE_CLOSED (1<<8)
248 #endif
249 
250  unsigned int event_set_status;
251 
252  struct link_socket *link_socket; /* socket used for TCP/UDP connection to remote */
255  const struct link_socket *accept_from; /* possibly do accept() on a parent link_socket */
256 
257  struct link_socket_actual *to_link_addr; /* IP address of remote */
258  struct link_socket_actual from; /* address of incoming datagram */
259 
260  /* MTU frame parameters */
261  struct frame frame; /* Active frame parameters */
262  struct frame frame_initial; /* Restored on new session */
263 
264 #ifdef ENABLE_FRAGMENT
265  /* Object to handle advanced MTU negotiation and datagram fragmentation */
267  struct frame frame_fragment;
268  struct frame frame_fragment_omit;
269 #endif
270 
271 #ifdef ENABLE_FEATURE_SHAPER
272  /*
273  * Traffic shaper object.
274  */
275  struct shaper shaper;
276 #endif
277 
278  /*
279  * Statistics
280  */
286 #ifdef PACKET_TRUNCATION_CHECK
287  counter_type n_trunc_tun_read;
288  counter_type n_trunc_tun_write;
289  counter_type n_trunc_pre_encrypt;
290  counter_type n_trunc_post_decrypt;
291 #endif
292 
293  /*
294  * Timer objects for ping and inactivity
295  * timeout features.
296  */
297  struct event_timeout wait_for_connect;
298  struct event_timeout ping_send_interval;
299  struct event_timeout ping_rec_interval;
300 
301  /* --inactive */
302  struct event_timeout inactivity_interval;
304 
305 #ifdef ENABLE_OCC
306  /* the option strings must match across peers */
309 
310  int occ_op; /* INIT to -1 */
312  struct event_timeout occ_interval;
313 #endif
314 
315  /*
316  * Keep track of maximum packet size received so far
317  * (of authenticated packets).
318  */
319  int original_recv_size; /* temporary */
320  int max_recv_size_local; /* max packet size received */
321  int max_recv_size_remote; /* max packet size received by remote */
322  int max_send_size_local; /* max packet size sent */
323  int max_send_size_remote; /* max packet size sent by remote */
324 
325 #ifdef ENABLE_OCC
326  /* remote wants us to send back a load test packet of this size */
328 
329  struct event_timeout occ_mtu_load_test_interval;
331 #endif
332 
333  /*
334  * TLS-mode crypto objects.
335  */
350  /* used to optimize calls to tls_multi_process */
351  struct interval tmp_int;
352 
353  /* throw this signal on TLS errors */
355 
362  struct event_timeout packet_id_persist_interval;
363 
364 #ifdef USE_COMP
365  struct compress_context *comp_context;
369 #endif
370 
371  /*
372  * Buffers used for packet processing.
373  */
375  bool buffers_owned; /* if true, we should free all buffers on close */
376 
377  /*
378  * These buffers don't actually allocate storage, they are used
379  * as pointers to the allocated buffers in
380  * struct context_buffers.
381  */
382  struct buffer buf;
383  struct buffer to_tun;
384  struct buffer to_link;
385 
386  /* should we print R|W|r|w to console on packet transfers? */
387  bool log_rw;
388 
389  /* route stuff */
390  struct event_timeout route_wakeup;
391  struct event_timeout route_wakeup_expire;
392 
393  /* did we open tun/tap dev during this cycle? */
395 
396  /*
397  * Event loop info
398  */
399 
400  /* how long to wait on link/tun read before we will need to be serviced */
401  struct timeval timeval;
402 
403  /* next wakeup for processing coarse timers (>1 sec resolution) */
405 
406  /* maintain a random delta to add to timeouts to avoid contexts
407  * waking up simultaneously */
409  struct timeval timeout_random_component;
410 
411  /* Timer for everything up to the first packet from the *OpenVPN* server
412  * socks, http proxy, and tcp packets do not count */
413  struct event_timeout server_poll_interval;
414 
415  /* indicates that the do_up_delay function has run */
416  bool do_up_ran;
417 
418 #ifdef ENABLE_OCC
419  /* indicates that we have received a SIGTERM when
420  * options->explicit_exit_notification is enabled,
421  * but we have not exited yet */
423  struct event_timeout explicit_exit_notification_interval;
424 #endif
425 
426  /* environmental variables to pass to scripts */
427  struct env_set *es;
428  bool es_owned;
429 
430  /* don't wait for TUN/TAP/UDP to be ready to accept write */
431  bool fast_io;
432 
433 #if P2MP
434 
435 #if P2MP_SERVER
436  /* --ifconfig endpoints to be pushed to client */
438 #ifdef ENABLE_ASYNC_PUSH
439  bool push_request_received;
440 #endif
446 
448  struct in6_addr push_ifconfig_ipv6_local;
450  struct in6_addr push_ifconfig_ipv6_remote;
451 
452  /* client authentication state, CAS_SUCCEEDED must be 0 */
453 #define CAS_SUCCEEDED 0
454 #define CAS_PENDING 1
455 #define CAS_FAILED 2
456 #define CAS_PARTIAL 3 /* at least one client-connect script/plugin
457  * succeeded while a later one in the chain failed */
459 #endif /* if P2MP_SERVER */
460 
461  struct event_timeout push_request_interval;
464 
465  /* hash of pulled options, so we can compare when options change */
468  struct sha256_digest pulled_options_digest;
469 
470  struct event_timeout scheduled_exit;
472 #endif /* if P2MP */
473 
474  /* packet filter */
475 #ifdef ENABLE_PF
476  struct pf_context pf;
477 #endif
478 
479 #ifdef MANAGEMENT_DEF_AUTH
480  struct man_def_auth_context mda_context;
481 #endif
482 
483 #ifdef ENABLE_ASYNC_PUSH
484  int inotify_fd; /* descriptor for monitoring file changes */
485 #endif
486 };
487 
488 
500 struct context
501 {
502  struct options options;
505  bool first_time;
508  /* context modes */
509 #define CM_P2P 0 /* standalone point-to-point session or client */
510 #define CM_TOP 1 /* top level of a multi-client or point-to-multipoint server */
511 #define CM_TOP_CLONE 2 /* clone of a CM_TOP context for one thread */
512 #define CM_CHILD_UDP 3 /* child context of a CM_TOP or CM_THREAD */
513 #define CM_CHILD_TCP 4 /* child context of a CM_TOP or CM_THREAD */
514  int mode;
519  struct gc_arena gc;
523  struct env_set *es;
525  struct signal_info *sig;
535  struct context_persist persist;
537  struct context_0 *c0;
538  struct context_1 c1;
539  struct context_2 c2;
540 };
541 
542 /*
543  * Check for a signal when inside an event loop
544  */
545 #define EVENT_LOOP_CHECK_SIGNAL(c, func, arg) \
546  if (IS_SIG(c)) \
547  { \
548  const int brk = func(arg); \
549  perf_pop(); \
550  if (brk) { \
551  break;} \
552  else { \
553  continue;} \
554  }
555 
556 /*
557  * Macros for referencing objects which may not
558  * have been compiled in.
559  */
560 
561 #define TLS_MODE(c) ((c)->c2.tls_multi != NULL)
562 #define PROTO_DUMP_FLAGS (check_debug_level(D_LINK_RW_VERBOSE) ? (PD_SHOW_DATA|PD_VERBOSE) : 0)
563 #define PROTO_DUMP(buf, gc) protocol_dump((buf), \
564  PROTO_DUMP_FLAGS \
565  |(c->c2.tls_multi ? PD_TLS : 0) \
566  |(c->options.tls_auth_file ? c->c1.ks.key_type.hmac_length : 0), \
567  gc)
568 
569 #define CIPHER_ENABLED(c) (c->c1.ks.key_type.cipher != NULL)
570 
571 /* this represents "disabled peer-id" */
572 #define MAX_PEER_ID 0xFFFFFF
573 
574 #endif /* ifndef OPENVPN_H */
struct env_set * es
Definition: openvpn.h:427
const char * ciphername
Data channel cipher from config file.
Definition: openvpn.h:209
bool socks_proxy_owned
Definition: openvpn.h:189
counter_type tun_read_bytes
Definition: openvpn.h:281
Security parameter state for processing data channel packets.
Definition: crypto.h:232
bool log_rw
Definition: openvpn.h:387
Definition: tun.h:131
Level 1 context containing state that persists across SIGUSR1 restarts.
Definition: openvpn.h:152
Fragmentation and reassembly state for one VPN tunnel instance.
Definition: fragment.h:136
counter_type link_write_bytes
Definition: openvpn.h:285
bool did_open_tun
Definition: openvpn.h:394
struct cached_dns_entry * dns_cache
Definition: openvpn.h:162
Contains all state information for one tunnel.
Definition: openvpn.h:500
Packet geometry parameters.
Definition: mtu.h:93
struct env_set * es
Set of environment variables.
Definition: openvpn.h:523
bool push_ifconfig_defined
Definition: openvpn.h:441
int occ_mtu_load_size
Definition: openvpn.h:327
in_addr_t push_ifconfig_local
Definition: openvpn.h:443
static void packet_id_persist_init(struct packet_id_persist *p)
Definition: openvpn.h:83
Security parameter state for a single VPN tunnel.
Definition: ssl_common.h:503
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:339
int keysize
Data channel keysize from config file.
Definition: openvpn.h:211
int occ_mtu_load_n_tries
Definition: openvpn.h:330
bool pulled_options_digest_init_done
Definition: openvpn.h:466
struct context_buffers * buffers
Definition: openvpn.h:374
struct link_socket_info * link_socket_info
Definition: openvpn.h:254
const struct link_socket * accept_from
Definition: openvpn.h:255
struct socks_proxy_info * socks_proxy
Definition: openvpn.h:188
#define in_addr_t
Definition: config-msvc.h:104
struct signal_info * sig
Internal error signaling object.
Definition: openvpn.h:525
bool did_pre_pull_restore
Definition: openvpn.h:463
int max_recv_size_local
Definition: openvpn.h:320
int occ_n_tries
Definition: openvpn.h:311
struct tuntap * tuntap
Tun/tap virtual network interface.
Definition: openvpn.h:167
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:168
int context_auth
Definition: openvpn.h:458
time_t explicit_exit_notification_time_wait
Definition: openvpn.h:422
bool uid_gid_chroot_set
Definition: openvpn.h:137
md_ctx_t * pulled_options_state
Definition: openvpn.h:467
counter_type tun_write_bytes
Definition: openvpn.h:282
counter_type link_read_bytes
Definition: openvpn.h:283
struct link_socket_actual * to_link_addr
Definition: openvpn.h:257
int inactivity_bytes
Definition: openvpn.h:303
struct route_list * route_list
List of routing information.
Definition: openvpn.h:172
in_addr_t push_ifconfig_local_alias
Definition: openvpn.h:445
int push_ifconfig_ipv6_netbits
Definition: openvpn.h:449
Container for one set of cipher and/or HMAC contexts.
Definition: crypto.h:164
int event_set_max
Definition: openvpn.h:232
int restart_sleep_seconds
Definition: openvpn.h:119
unsigned int counter_type
Definition: common.h:38
struct link_socket * link_socket
Definition: openvpn.h:252
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:210
bool buffers_owned
Definition: openvpn.h:375
struct route_ipv6_list * route_ipv6_list
Definition: openvpn.h:177
time_t sent_push_reply_expiry
Definition: openvpn.h:442
time_t update_timeout_random_component
Definition: openvpn.h:408
bool uid_gid_specified
Definition: openvpn.h:135
struct ifconfig_pool_persist * ifconfig_pool_persist
Definition: openvpn.h:195
struct event_set * event_set
Definition: openvpn.h:231
int max_recv_size_remote
Definition: openvpn.h:321
struct key_type tls_auth_key_type
Definition: openvpn.h:67
int scheduled_exit_signal
Definition: openvpn.h:471
bool push_ifconfig_ipv6_defined
Definition: openvpn.h:447
Structure that wraps the TLS context.
Definition: ssl_mbedtls.h:90
struct context_0 * c0
Level 0 context.
Definition: openvpn.h:537
Level 0 context containing information related to the OpenVPN process.
Definition: openvpn.h:132
int max_send_size_remote
Definition: openvpn.h:323
struct tls_root_ctx ssl_ctx
Definition: openvpn.h:64
int mode
Role of this context within the OpenVPN process.
Definition: openvpn.h:514
char * options_string_remote
Definition: openvpn.h:308
bool push_reply_deferred
Definition: openvpn.h:437
int n_sent_push_requests
Definition: openvpn.h:462
Definition: misc.h:62
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:528
struct fragment_master * fragment
Definition: openvpn.h:266
struct status_output * status_output
Definition: openvpn.h:180
struct plugin_list * plugins
List of plug-ins.
Definition: openvpn.h:527
struct http_proxy_info * http_proxy
Definition: openvpn.h:184
bool ifconfig_pool_persist_owned
Definition: openvpn.h:196
struct user_pass * auth_user_pass
Username and password for authentication.
Definition: openvpn.h:205
bool first_time
True on the first iteration of OpenVPN&#39;s main loop.
Definition: openvpn.h:505
bool do_up_ran
Definition: openvpn.h:416
bool fast_io
Definition: openvpn.h:431
char * options_string_local
Definition: openvpn.h:307
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
int original_recv_size
Definition: openvpn.h:319
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:532
bool status_output_owned
Definition: openvpn.h:181
Wrapper struct to pass around SHA256 digests.
Definition: crypto.h:132
bool link_socket_owned
Definition: openvpn.h:253
unsigned int event_set_status
Definition: openvpn.h:250
struct tls_multi * tls_multi
TLS state structure for this VPN tunnel.
Definition: openvpn.h:336
in_addr_t push_ifconfig_remote_netmask
Definition: openvpn.h:444
int max_send_size_local
Definition: openvpn.h:322
int occ_op
Definition: openvpn.h:310
bool http_proxy_owned
Definition: openvpn.h:185
bool es_owned
Definition: openvpn.h:428
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:354
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:404
counter_type link_read_bytes_auth
Definition: openvpn.h:284