OpenVPN
multi.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-2021 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 
28 #ifndef MULTI_H
29 #define MULTI_H
30 
31 #include "init.h"
32 #include "forward.h"
33 #include "mroute.h"
34 #include "mbuf.h"
35 #include "list.h"
36 #include "schedule.h"
37 #include "pool.h"
38 #include "mudp.h"
39 #include "mtcp.h"
40 #include "perf.h"
41 #include "vlan.h"
42 
43 #define MULTI_PREFIX_MAX_LENGTH 256
44 
45 /*
46  * Walk (don't run) through the routing table,
47  * deleting old entries, and possibly multi_instance
48  * structs as well which have been marked for deletion.
49  */
50 struct multi_reap
51 {
54  time_t last_call;
55 };
56 
57 
59 {
60  struct schedule_entry se;
62  struct timeval wakeup;
63 };
64 
70 {
71  /* Index of currently executed handler. */
73  /* Remember which option classes where processed for delayed option
74  * handling. */
75  unsigned int option_types_found;
76 
82 
87  char *config_file;
88 };
89 
101  struct schedule_entry se; /* this must be the first element of the structure */
102  struct gc_arena gc;
103  bool halt;
104  int refcount;
105  int route_count; /* number of routes (including cached routes) owned by this instance */
106  time_t created;
110  struct timeval wakeup; /* absolute time */
111  struct mroute_addr real;
114  char msg_prefix[MULTI_PREFIX_MAX_LENGTH];
115 
116  /* queued outgoing data in Server/TCP mode */
117  unsigned int tcp_rwflags;
120 
121  in_addr_t reporting_addr; /* IP address shown in status listing */
122  struct in6_addr reporting_addr_ipv6; /* IPv6 address in status listing */
123 
125  bool did_iter;
126 #ifdef ENABLE_MANAGEMENT
129 #endif
131  int n_clients_delta; /* added to multi_context.n_clients when instance is closed */
132 
133  struct context context;
136 #ifdef ENABLE_ASYNC_PUSH
137  int inotify_watch; /* watch descriptor for acf */
138 #endif
139 };
140 
141 
156  struct hash *hash;
158  struct hash *vhash;
160  struct hash *iter;
164  struct mbuf_set *mbuf;
167  struct multi_tcp *mtcp;
173  struct mroute_addr local;
178  int n_clients; /* current number of authenticated clients */
179 
180 #ifdef ENABLE_MANAGEMENT
181  struct hash *cid_hash;
182  unsigned long cid_counter;
183 #endif
184 
190 
191  struct context top;
194  /*
195  * Timer object for stale route check
196  */
197  struct event_timeout stale_routes_check_et;
198 
199 #ifdef ENABLE_ASYNC_PUSH
200  /* mapping between inotify watch descriptors and multi_instances */
201  struct hash *inotify_watchers;
202 #endif
203 
204  struct deferred_signal_schedule_entry deferred_shutdown_signal;
205 };
206 
211 {
216 };
217 
218 /*
219  * Host route
220  */
222 {
225 
226 #define MULTI_ROUTE_CACHE (1<<0)
227 #define MULTI_ROUTE_AGEABLE (1<<1)
228  unsigned int flags;
229 
230  unsigned int cache_generation;
232 };
233 
234 
235 /**************************************************************************/
247 void tunnel_server(struct context *top);
248 
249 
250 const char *multi_instance_string(const struct multi_instance *mi, bool null, struct gc_arena *gc);
251 
252 /*
253  * Called by mtcp.c, mudp.c, or other (to be written) protocol drivers
254  */
255 
256 void multi_init(struct multi_context *m, struct context *t, bool tcp_mode);
257 
258 void multi_uninit(struct multi_context *m);
259 
260 void multi_top_init(struct multi_context *m, const struct context *top);
261 
262 void multi_top_free(struct multi_context *m);
263 
264 struct multi_instance *multi_create_instance(struct multi_context *m, const struct mroute_addr *real);
265 
266 void multi_close_instance(struct multi_context *m, struct multi_instance *mi, bool shutdown);
267 
268 bool multi_process_timeout(struct multi_context *m, const unsigned int mpp_flags);
269 
278 void multi_process_float(struct multi_context *m, struct multi_instance *mi);
279 
280 #define MPP_PRE_SELECT (1<<0)
281 #define MPP_CONDITIONAL_PRE_SELECT (1<<1)
282 #define MPP_CLOSE_ON_SIGNAL (1<<2)
283 #define MPP_RECORD_TOUCH (1<<3)
284 
285 
286 /**************************************************************************/
308 bool multi_process_post(struct multi_context *m, struct multi_instance *mi, const unsigned int flags);
309 
310 
311 /**************************************************************************/
334 bool multi_process_incoming_link(struct multi_context *m, struct multi_instance *instance, const unsigned int mpp_flags);
335 
336 
352 bool multi_process_incoming_tun(struct multi_context *m, const unsigned int mpp_flags);
353 
354 
355 void multi_process_drop_outgoing_tun(struct multi_context *m, const unsigned int mpp_flags);
356 
357 void multi_print_status(struct multi_context *m, struct status_output *so, const int version);
358 
359 struct multi_instance *multi_get_queue(struct mbuf_set *ms);
360 
361 void multi_add_mbuf(struct multi_context *m,
362  struct multi_instance *mi,
363  struct mbuf_buffer *mb);
364 
365 void multi_ifconfig_pool_persist(struct multi_context *m, bool force);
366 
367 bool multi_process_signal(struct multi_context *m);
368 
370 
372 
373 #ifdef ENABLE_ASYNC_PUSH
374 
381 void multi_process_file_closed(struct multi_context *m, const unsigned int mpp_flags);
382 
383 #endif
384 
385 /*
386  * Return true if our output queue is not full
387  */
388 static inline bool
390  const struct multi_instance *mi)
391 {
392  if (mi->tcp_link_out_deferred)
393  {
395  }
396  else
397  {
398  return true;
399  }
400 }
401 
402 /*
403  * Determine which instance has pending output
404  * and prepare the output for sending in
405  * the to_link buffer.
406  */
407 static inline struct multi_instance *
409 {
410  struct multi_instance *mi = NULL;
411 
412  if (m->pending)
413  {
414  mi = m->pending;
415  }
416  else if (mbuf_defined(m->mbuf))
417  {
418  mi = multi_get_queue(m->mbuf);
419  }
420  return mi;
421 }
422 
423 /*
424  * Per-client route quota management
425  */
426 
427 void route_quota_exceeded(const struct multi_instance *mi);
428 
429 static inline void
431 {
432  ++mi->route_count;
433 }
434 
435 static inline void
437 {
438  --mi->route_count;
439 }
440 
441 /* can we add a new route? */
442 static inline bool
444 {
446  {
448  return false;
449  }
450  else
451  {
452  return true;
453  }
454 }
455 
456 /*
457  * Instance reference counting
458  */
459 
460 static inline void
462 {
463  ++mi->refcount;
464 }
465 
466 static inline void
468 {
469  if (--mi->refcount <= 0)
470  {
471  gc_free(&mi->gc);
472  free(mi);
473  }
474 }
475 
476 static inline void
478 {
479  struct multi_instance *mi = route->instance;
480  route_quota_dec(mi);
482  free(route);
483 }
484 
485 static inline bool
487  const struct multi_route *r)
488 {
489  if (r->instance->halt)
490  {
491  return false;
492  }
493  else if ((r->flags & MULTI_ROUTE_CACHE)
495  {
496  return false;
497  }
498  else if ((r->flags & MULTI_ROUTE_AGEABLE)
500  {
501  return false;
502  }
503  else
504  {
505  return true;
506  }
507 }
508 
509 /*
510  * Takes prefix away from multi_instance.
511  */
512 void
514 
515 /*
516  * Set a msg() function prefix with our current client instance ID.
517  */
518 
519 static inline void
521 {
522 #ifdef MULTI_DEBUG_EVENT_LOOP
523  if (mi->msg_prefix[0])
524  {
525  printf("[%s]\n", mi->msg_prefix);
526  }
527 #endif
528  msg_set_prefix(mi->msg_prefix[0] ? mi->msg_prefix : NULL);
529 }
530 
531 static inline void
533 {
534 #ifdef MULTI_DEBUG_EVENT_LOOP
535  printf("[NULL]\n");
536 #endif
537  msg_set_prefix(NULL);
538 }
539 
540 /*
541  * Instance Reaper
542  *
543  * Reaper constants. The reaper is the process where the virtual address
544  * and virtual route hash table is scanned for dead entries which are
545  * then removed. The hash table could potentially be quite large, so we
546  * don't want to reap in a single pass.
547  */
548 
549 #define REAP_MAX_WAKEUP 10 /* Do reap pass at least once per n seconds */
550 #define REAP_DIVISOR 256 /* How many passes to cover whole hash table */
551 #define REAP_MIN 16 /* Minimum number of buckets per pass */
552 #define REAP_MAX 1024 /* Maximum number of buckets per pass */
553 
554 /*
555  * Mark a cached host route for deletion after this
556  * many seconds without any references.
557  */
558 #define MULTI_CACHE_ROUTE_TTL 60
559 
560 void multi_reap_process_dowork(const struct multi_context *m);
561 
563 
564 static inline void
566 {
567  if (m->reaper->last_call != now)
568  {
570  }
571 }
572 
573 static inline void
575 {
576  if (m->per_second_trigger != now)
577  {
579  m->per_second_trigger = now;
580  }
581 }
582 
583 /*
584  * Compute earliest timeout expiry from the set of
585  * all instances. Output:
586  *
587  * m->earliest_wakeup : instance needing the earliest service.
588  * dest : earliest timeout as a delta in relation
589  * to current time.
590  */
591 static inline void
592 multi_get_timeout(struct multi_context *m, struct timeval *dest)
593 {
594  struct timeval tv, current;
595 
596  CLEAR(tv);
598  if (m->earliest_wakeup)
599  {
600  ASSERT(!openvpn_gettimeofday(&current, NULL));
601  tv_delta(dest, &current, &tv);
602  if (dest->tv_sec >= REAP_MAX_WAKEUP)
603  {
604  m->earliest_wakeup = NULL;
605  dest->tv_sec = REAP_MAX_WAKEUP;
606  dest->tv_usec = 0;
607  }
608  }
609  else
610  {
611  dest->tv_sec = REAP_MAX_WAKEUP;
612  dest->tv_usec = 0;
613  }
614 }
615 
616 
634 static inline bool
635 multi_process_outgoing_tun(struct multi_context *m, const unsigned int mpp_flags)
636 {
637  struct multi_instance *mi = m->pending;
638  bool ret = true;
639 
640  ASSERT(mi);
641 #ifdef MULTI_DEBUG_EVENT_LOOP
642  printf("%s -> TUN len=%d\n",
643  id(mi),
644  mi->context.c2.to_tun.len);
645 #endif
646  set_prefix(mi);
649  ret = multi_process_post(m, mi, mpp_flags);
650  clear_prefix();
651  return ret;
652 }
653 
654 #define CLIENT_CONNECT_OPT_MASK (OPT_P_INSTANCE | OPT_P_INHERIT \
655  |OPT_P_PUSH | OPT_P_TIMER | OPT_P_CONFIG \
656  |OPT_P_ECHO | OPT_P_COMP | OPT_P_SOCKFLAGS)
657 
658 static inline bool
659 multi_process_outgoing_link_dowork(struct multi_context *m, struct multi_instance *mi, const unsigned int mpp_flags)
660 {
661  bool ret = true;
662  set_prefix(mi);
664  ret = multi_process_post(m, mi, mpp_flags);
665  clear_prefix();
666  return ret;
667 }
668 
669 /*
670  * Check for signals.
671  */
672 #define MULTI_CHECK_SIG(m) EVENT_LOOP_CHECK_SIGNAL(&(m)->top, multi_process_signal, (m))
673 
674 static inline void
676 {
677  m->pending = mi;
678 }
687 void multi_assign_peer_id(struct multi_context *m, struct multi_instance *mi);
688 
689 
690 #endif /* MULTI_H */
struct multi_instance ** mpp_touched
Definition: multi.h:187
static void multi_instance_inc_refcount(struct multi_instance *mi)
Definition: multi.h:461
bool socket_set_called
Definition: multi.h:119
struct mroute_helper * route_helper
Definition: multi.h:171
time_t per_second_trigger
Definition: multi.h:189
struct multi_instance ** instances
Array of multi_instances.
Definition: multi.h:153
struct options options
Options loaded from command line or configuration file.
Definition: openvpn.h:463
bool did_cid_hash
Definition: multi.h:127
static void route_quota_inc(struct multi_instance *mi)
Definition: multi.h:430
#define MULTI_ROUTE_CACHE
Definition: multi.h:226
int signal_received
Definition: multi.h:61
unsigned int option_types_found
Definition: multi.h:75
void ungenerate_prefix(struct multi_instance *mi)
Definition: multi.c:505
bool multi_process_timeout(struct multi_context *m, const unsigned int mpp_flags)
Definition: multi.c:3420
Contains all state information for one tunnel.
Definition: openvpn.h:461
static void gc_free(struct gc_arena *a)
Definition: buffer.h:1023
void multi_reap_process_dowork(const struct multi_context *m)
Definition: multi.c:225
static void tv_delta(struct timeval *dest, const struct timeval *t1, const struct timeval *t2)
Definition: otime.h:220
void multi_top_init(struct multi_context *m, const struct context *top)
Definition: multi.c:3560
static struct multi_instance * multi_process_outgoing_link_pre(struct multi_context *m)
Definition: multi.h:408
time_t created
Time at which a VPN tunnel instance was created.
Definition: multi.h:106
struct multi_reap * reaper
Definition: multi.h:172
struct multi_instance * instance
Definition: multi.h:224
#define MULTI_ROUTE_AGEABLE
Definition: multi.h:227
int status_file_version
Definition: multi.h:177
void multi_uninit(struct multi_context *m)
Definition: multi.c:689
#define in_addr_t
Definition: config-msvc.h:103
ifconfig_pool_handle vaddr_handle
Definition: multi.h:113
bool did_iter
Definition: multi.h:125
void multi_top_free(struct multi_context *m)
Definition: multi.c:3567
char msg_prefix[MULTI_PREFIX_MAX_LENGTH]
Definition: multi.h:114
static bool multi_route_defined(const struct multi_context *m, const struct multi_route *r)
Definition: multi.h:486
unsigned int cache_generation
Definition: mroute.h:132
#define ASSERT(x)
Definition: error.h:204
void init_management_callback_multi(struct multi_context *m)
Definition: multi.c:3872
struct multi_instance * multi_get_queue(struct mbuf_set *ms)
Definition: multi.c:3386
bool multi_process_incoming_tun(struct multi_context *m, const unsigned int mpp_flags)
Determine the destination VPN tunnel of a packet received over the virtual tun/tap network interface ...
Definition: multi.c:3289
unsigned int flags
Definition: multi.h:228
void vlan_process_outgoing_tun(struct multi_context *m, struct multi_instance *mi)
Definition: vlan.c:298
int n_clients
Definition: multi.h:178
static void clear_prefix(void)
Definition: multi.h:532
bool did_real_hash
Definition: multi.h:124
struct mbuf_set * tcp_link_out_deferred
Definition: multi.h:118
int ifconfig_pool_handle
Definition: pool.h:71
int n_clients_delta
Definition: multi.h:131
static bool route_quota_test(const struct multi_instance *mi)
Definition: multi.h:443
list flags
int len
Length in bytes of the actual content within the allocated memory.
Definition: buffer.h:66
void multi_process_per_second_timers_dowork(struct multi_context *m)
Definition: multi.c:3531
#define MULTI_PREFIX_MAX_LENGTH
Definition: multi.h:43
static bool multi_process_outgoing_tun(struct multi_context *m, const unsigned int mpp_flags)
Send a packet over the virtual tun/tap network interface to its locally reachable destination...
Definition: multi.h:635
void multi_assign_peer_id(struct multi_context *m, struct multi_instance *mi)
Assigns a peer-id to a a client and adds the instance to the the instances array of the multi_context...
Definition: multi.c:3896
Definition: mbuf.h:56
static void multi_route_del(struct multi_route *route)
Definition: multi.h:477
uint8_t addr[OPENVPN_ETH_ALEN]
Definition: mroute.h:84
void multi_ifconfig_pool_persist(struct multi_context *m, bool force)
Definition: multi.c:163
bool multi_process_signal(struct multi_context *m)
Definition: multi.c:3620
#define CLEAR(x)
Definition: basic.h:33
time_t last_reference
Definition: multi.h:231
static void route_quota_dec(struct multi_instance *mi)
Definition: multi.h:436
static bool multi_process_outgoing_link_dowork(struct multi_context *m, struct multi_instance *mi, const unsigned int mpp_flags)
Definition: multi.h:659
int refcount
Definition: multi.h:104
int ageable_ttl_secs
Definition: mroute.h:133
time_t now
Definition: otime.c:36
void multi_close_instance_on_signal(struct multi_context *m, struct multi_instance *mi)
Definition: multi.c:3646
static void set_prefix(struct multi_instance *mi)
Definition: multi.h:520
bool did_iroutes
Definition: multi.h:130
struct hash * iter
VPN tunnel instances indexed by real address of the remote peer, optimized for iteration.
Definition: multi.h:160
static bool multi_output_queue_ready(const struct multi_context *m, const struct multi_instance *mi)
Definition: multi.h:389
static unsigned int mbuf_len(const struct mbuf_set *ms)
Definition: mbuf.h:86
unsigned int cache_generation
Definition: multi.h:230
struct context_buffers * context_buffers
Definition: multi.h:188
Definition: schedule.h:44
const char * multi_instance_string(const struct multi_instance *mi, bool null, struct gc_arena *gc)
Definition: multi.c:463
bool multi_process_post(struct multi_context *m, struct multi_instance *mi, const unsigned int flags)
Perform postprocessing of a VPN tunnel instance.
Definition: multi.c:2911
struct multi_instance * multi_create_instance(struct multi_context *m, const struct mroute_addr *real)
Definition: multi.c:736
struct context context
The context structure storing state for this VPN tunnel.
Definition: multi.h:133
struct multi_instance * pending
Definition: multi.h:185
int buckets_per_pass
Definition: multi.h:53
struct gc_arena gc
Definition: multi.h:102
static void multi_reap_process(const struct multi_context *m)
Definition: multi.h:565
int bucket_base
Definition: multi.h:52
in_addr_t reporting_addr
Definition: multi.h:121
int max_routes_per_client
Definition: options.h:481
struct schedule * schedule
Definition: multi.h:163
char * config_file
The temporary file name that contains the config directives returned by the client-connect script...
Definition: multi.h:87
void multi_close_instance(struct multi_context *m, struct multi_instance *mi, bool shutdown)
Definition: multi.c:586
int max_clients
Definition: multi.h:175
void process_outgoing_link(struct context *c)
Write a packet to the external network interface.
Definition: forward.c:1542
struct hash * vhash
VPN tunnel instances indexed by virtual address of remote hosts.
Definition: multi.h:158
struct multi_instance * earliest_wakeup
Definition: multi.h:186
Interface functions to the internal and external multiplexers.
unsigned long cid_counter
Definition: multi.h:182
struct context_2 c2
Level 2 context.
Definition: openvpn.h:502
struct mbuf_set * mbuf
Set of buffers for passing data channel packets between VPN tunnel instances.
Definition: multi.h:164
struct ifconfig_pool * ifconfig_pool
Definition: multi.h:169
void multi_process_float(struct multi_context *m, struct multi_instance *mi)
Handles peer floating.
Definition: multi.c:3008
Definition: mtcp.h:36
static int openvpn_gettimeofday(struct timeval *tv, void *tz)
Definition: otime.h:64
static void multi_get_timeout(struct multi_context *m, struct timeval *dest)
Definition: multi.h:592
bool halt
Definition: multi.h:103
void tunnel_server(struct context *top)
Main event loop for OpenVPN in server mode.
Definition: multi.c:3921
struct multi_tcp * mtcp
State specific to OpenVPN using TCP as external transport.
Definition: multi.h:167
#define REAP_MAX_WAKEUP
Definition: multi.h:549
struct mroute_addr real
External network address of the remote peer.
Definition: multi.h:111
unsigned int tcp_rwflags
Definition: multi.h:117
Definition: multi.h:58
struct hash * cid_hash
Definition: multi.h:181
struct buffer_list * cc_config
Definition: multi.h:128
Main OpenVPN server state structure.
Definition: multi.h:152
time_t last_call
Definition: multi.h:54
static bool mbuf_defined(const struct mbuf_set *ms)
Definition: mbuf.h:80
void route_quota_exceeded(const struct multi_instance *mi)
Definition: multi.c:3473
int route_count
Definition: multi.h:105
static void multi_set_pending(struct multi_context *m, struct multi_instance *mi)
Definition: multi.h:675
struct hash * hash
VPN tunnel instances indexed by real address of the remote peer.
Definition: multi.h:156
bool multi_process_incoming_link(struct multi_context *m, struct multi_instance *instance, const unsigned int mpp_flags)
Demultiplex and process a packet received over the external network interface.
Definition: multi.c:3085
void multi_print_status(struct multi_context *m, struct status_output *so, const int version)
Definition: multi.c:826
char * deferred_ret_file
The temporary file name that contains the return status of the client-connect script if it exits with...
Definition: multi.h:81
bool enable_c2c
Definition: multi.h:174
struct frequency_limit * new_connection_limiter
Definition: multi.h:170
#define free
Definition: cmocka.c:1850
int tcp_queue_limit
Definition: multi.h:176
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
Detached client connection state.
Definition: multi.h:69
static struct schedule_entry * schedule_get_earliest_wakeup(struct schedule *s, struct timeval *wakeup)
Definition: schedule.h:118
void multi_add_mbuf(struct multi_context *m, struct multi_instance *mi, struct mbuf_buffer *mb)
Definition: multi.c:2751
static void multi_instance_dec_refcount(struct multi_instance *mi)
Definition: multi.h:467
client_connect_return
Return values used by the client connect call-back functions.
Definition: multi.h:210
Server-mode state structure for one single VPN tunnel.
Definition: multi.h:100
struct buffer to_tun
Definition: openvpn.h:366
Definition: list.h:58
void multi_init(struct multi_context *m, struct context *t, bool tcp_mode)
Definition: multi.c:292
static void multi_process_per_second_timers(struct multi_context *m)
Definition: multi.h:574
void process_outgoing_tun(struct context *c)
Write a packet to the virtual tun/tap network interface.
Definition: forward.c:1690
static void msg_set_prefix(const char *prefix)
Definition: error.h:319
void multi_process_drop_outgoing_tun(struct multi_context *m, const unsigned int mpp_flags)
Definition: multi.c:3451