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