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-2026 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, see <https://www.gnu.org/licenses/>.
21 */
22
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 "multi_io.h"
41#include "vlan.h"
42#include "reflect_filter.h"
43
44#define MULTI_PREFIX_MAX_LENGTH 256
45
46/*
47 * Walk (don't run) through the routing table,
48 * deleting old entries, and possibly multi_instance
49 * structs as well which have been marked for deletion.
50 */
52{
53 uint32_t bucket_base;
55 time_t last_call;
56};
57
58
65
71{
72 /* Index of currently executed handler. */
74 /* Remember which option classes where processed for delayed option
75 * handling. */
77
83
89};
90
102{
103 struct schedule_entry se; /* this must be the first element of the structure,
104 * We cast between this and schedule_entry so the
105 * beginning of the struct must be identical */
106
112 struct gc_arena gc;
113 bool halt;
115 int route_count; /* number of routes (including cached routes) owned by this instance */
116 time_t created;
120 struct timeval wakeup; /* absolute time */
125
126 /* queued outgoing data in Server/TCP mode */
127 unsigned int tcp_rwflags;
130
131 in_addr_t reporting_addr; /* IP address shown in status listing */
132 struct in6_addr reporting_addr_ipv6; /* IPv6 address in status listing */
133
135#ifdef ENABLE_MANAGEMENT
138#endif
140 int n_clients_delta; /* added to multi_context.n_clients when instance is closed */
141
145#ifdef ENABLE_ASYNC_PUSH
146 int inotify_watch; /* watch descriptor for acf */
147#endif
148};
149
150
162{
166 uint32_t max_peerid;
169 struct hash *hash;
171 struct hash *vhash;
174 struct mbuf_set *mbuf;
185 uint32_t max_clients;
188 int n_clients; /* current number of authenticated clients */
189
190#ifdef ENABLE_MANAGEMENT
191 struct hash *cid_hash;
192 unsigned long cid_counter;
193#endif
194
200
201 struct context top;
204 /*
205 * Timer object for stale route check
206 */
208
209#ifdef ENABLE_ASYNC_PUSH
210 /* mapping between inotify watch descriptors and multi_instances */
211 struct hash *inotify_watchers;
212#endif
213
215};
216
227
228/*
229 * Host route
230 */
232{
235
236#define MULTI_ROUTE_CACHE (1 << 0)
237#define MULTI_ROUTE_AGEABLE (1 << 1)
238#define MULTI_ROUTE_PERMANENT (1 << 2) /* config-derived (iroute / pushed ifconfig); never stale-aged */
239 unsigned int flags;
240
241 unsigned int cache_generation;
243};
244
245
246/**************************************************************************/
253void tunnel_server(struct context *top);
254
255
256const char *multi_instance_string(const struct multi_instance *mi, bool null, struct gc_arena *gc);
257
258/*
259 * Called by mtcp.c, mudp.c, or other (to be written) protocol drivers
260 */
261
263 const struct mroute_addr *real,
264 struct link_socket *sock);
265
266void multi_close_instance(struct multi_context *m, struct multi_instance *mi, bool shutdown);
267
268bool multi_process_timeout(struct multi_context *m, const unsigned int mpp_flags);
269
270#define MPP_PRE_SELECT (1 << 0)
271#define MPP_CLOSE_ON_SIGNAL (1 << 1)
272#define MPP_RECORD_TOUCH (1 << 2)
273
274
275/**************************************************************************/
297bool multi_process_post(struct multi_context *m, struct multi_instance *mi,
298 const unsigned int flags);
299
306
307/**************************************************************************/
331bool multi_process_incoming_link(struct multi_context *m, struct multi_instance *instance,
332 const unsigned int mpp_flags, struct link_socket *sock);
333
334
350bool multi_process_incoming_tun(struct multi_context *m, const unsigned int mpp_flags);
351
352
353void multi_process_drop_outgoing_tun(struct multi_context *m, const unsigned int mpp_flags);
354
355struct multi_instance *multi_get_queue(struct mbuf_set *ms);
356
357void multi_add_mbuf(struct multi_context *m, struct multi_instance *mi, struct mbuf_buffer *mb);
358
359void multi_ifconfig_pool_persist(struct multi_context *m, bool force);
360
361bool multi_process_signal(struct multi_context *m);
362
364
366
367#ifdef ENABLE_ASYNC_PUSH
375void multi_process_file_closed(struct multi_context *m, const unsigned int mpp_flags);
376
377#endif
378
379#if defined(__GNUC__) || defined(__clang__)
380#pragma GCC diagnostic push
381#pragma GCC diagnostic ignored "-Wsign-compare"
382#endif
383
384/*
385 * Return true if our output queue is not full
386 */
387static inline bool
388multi_output_queue_ready(const struct multi_context *m, const struct multi_instance *mi)
389{
390 if (mi->tcp_link_out_deferred)
391 {
393 }
394 else
395 {
396 return true;
397 }
398}
399
400#if defined(__GNUC__) || defined(__clang__)
401#pragma GCC diagnostic pop
402#endif
403
404/*
405 * Determine which instance has pending output
406 * and prepare the output for sending in
407 * the to_link buffer.
408 */
409static inline struct multi_instance *
411{
412 struct multi_instance *mi = NULL;
413
414 if (m->pending)
415 {
416 mi = m->pending;
417 }
418 else if (mbuf_defined(m->mbuf))
419 {
420 mi = multi_get_queue(m->mbuf);
421 }
422 return mi;
423}
424
425/*
426 * Per-client route quota management
427 */
428
429void route_quota_exceeded(const struct multi_instance *mi);
430
431static inline void
433{
434 ++mi->route_count;
435}
436
437static inline void
439{
440 --mi->route_count;
441}
442
443/* can we add a new route? */
444static inline bool
446{
448 {
450 return false;
451 }
452 else
453 {
454 return true;
455 }
456}
457
458/*
459 * Instance reference counting
460 */
461
462static inline void
464{
465 ++mi->refcount;
466}
467
468static inline void
470{
471 if (--mi->refcount <= 0)
472 {
473 gc_free(&mi->gc);
474 free(mi);
475 }
476}
477
478static inline void
480{
481 struct multi_instance *mi = route->instance;
482 route_quota_dec(mi);
484 free(route);
485}
486
487static inline bool
488multi_route_defined(const struct multi_context *m, const struct multi_route *r)
489{
490 if (r->instance->halt)
491 {
492 return false;
493 }
494 else if ((r->flags & MULTI_ROUTE_CACHE)
496 {
497 return false;
498 }
499 else if ((r->flags & MULTI_ROUTE_AGEABLE)
501 {
502 return false;
503 }
504 else
505 {
506 return true;
507 }
508}
509
510/*
511 * Takes prefix away from multi_instance.
512 */
513void ungenerate_prefix(struct multi_instance *mi);
514
515/*
516 * Set a msg() function prefix with our current client instance ID.
517 */
518
519static 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
531static 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
560void multi_reap_process_dowork(const struct multi_context *m);
561
563
564static inline void
566{
567 if (m->reaper->last_call != now)
568 {
570 }
571}
572
573static inline void
582
583/*
584 * Updates \c dest with the earliest timeout as a delta relative to the current
585 * time and sets \c m->earliest_wakeup to the \c multi_instance with the
586 * soonest scheduled wakeup.
587 *
588 * @param m Pointer to the multi context
589 * @param dest Pointer to a timeval struct that will hold the earliest timeout
590 * delta.
591 */
592static inline void
593multi_get_timeout_instance(struct multi_context *m, struct timeval *dest)
594{
595 struct timeval tv, current;
596
597 CLEAR(tv);
599 if (m->earliest_wakeup)
600 {
601 ASSERT(!openvpn_gettimeofday(&current, NULL));
602 tv_delta(dest, &current, &tv);
603 if (dest->tv_sec >= REAP_MAX_WAKEUP)
604 {
605 m->earliest_wakeup = NULL;
606 dest->tv_sec = REAP_MAX_WAKEUP;
607 dest->tv_usec = 0;
608 }
609 }
610 else
611 {
612 dest->tv_sec = REAP_MAX_WAKEUP;
613 dest->tv_usec = 0;
614 }
615}
616
617
635static inline bool
636multi_process_outgoing_tun(struct multi_context *m, const unsigned int mpp_flags)
637{
638 struct multi_instance *mi = m->pending;
639 bool ret = true;
640
641 ASSERT(mi);
642#ifdef MULTI_DEBUG_EVENT_LOOP
643 printf("%s -> TUN len=%d\n", id(mi), mi->context.c2.to_tun.len);
644#endif
645 set_prefix(mi);
648 ret = multi_process_post(m, mi, mpp_flags);
649 clear_prefix();
650 return ret;
651}
652
653#define CLIENT_CONNECT_OPT_MASK \
654 (OPT_P_INSTANCE | OPT_P_INHERIT | OPT_P_PUSH | OPT_P_TIMER | OPT_P_CONFIG | OPT_P_ECHO \
655 | OPT_P_COMP | OPT_P_SOCKFLAGS)
656
657static inline bool
659 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
679bool
681
692bool
694 struct in6_addr *dest);
695
696/*
697 * Check for signals.
698 */
699#define MULTI_CHECK_SIG(m) EVENT_LOOP_CHECK_SIGNAL(&(m)->top, multi_process_signal, (m))
700
701static inline void
703{
704 m->pending = mi;
705}
714void multi_assign_peer_id(struct multi_context *m, struct multi_instance *mi);
715
716#ifdef ENABLE_MANAGEMENT
717struct multi_instance *
718lookup_by_cid(struct multi_context *m, const unsigned long cid);
719#endif
720
721void
722update_vhash(struct multi_context *m, struct multi_instance *mi, const char *new_ip, const char *new_ipv6);
723void unlearn_ifconfig(struct multi_context *m, struct multi_instance *mi);
724void unlearn_ifconfig_ipv6(struct multi_context *m, struct multi_instance *mi);
725
726#endif /* MULTI_H */
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1912
void * dco_context_t
Definition dco.h:259
Interface functions to the internal and external multiplexers.
void tunnel_server(struct context *top)
Main event loop for OpenVPN in server mode.
Definition multi.c:4235
bool multi_process_incoming_link(struct multi_context *m, struct multi_instance *instance, const unsigned int mpp_flags, struct link_socket *sock)
Demultiplex and process a packet received over the external network interface.
Definition multi.c:3497
void process_outgoing_link(struct context *c, struct link_socket *sock)
Write a packet to the external network interface.
Definition forward.c:1746
void process_outgoing_tun(struct context *c, struct link_socket *in_sock)
Write a packet to the virtual tun/tap network interface.
Definition forward.c:1880
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:636
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:3560
@ route
Definition interactive.c:86
static bool mbuf_defined(const struct mbuf_set *ms)
Definition mbuf.h:81
static unsigned int mbuf_len(const struct mbuf_set *ms)
Definition mbuf.h:87
client_connect_return
Return values used by the client connect call-back functions.
Definition multi.h:221
@ CC_RET_DEFERRED
Definition multi.h:224
@ CC_RET_FAILED
Definition multi.h:222
@ CC_RET_SKIPPED
Definition multi.h:225
@ CC_RET_SUCCEEDED
Definition multi.h:223
struct multi_instance * multi_create_instance(struct multi_context *m, const struct mroute_addr *real, struct link_socket *sock)
Definition multi.c:702
static bool multi_output_queue_ready(const struct multi_context *m, const struct multi_instance *mi)
Definition multi.h:388
#define REAP_MAX_WAKEUP
Definition multi.h:549
void multi_ifconfig_pool_persist(struct multi_context *m, bool force)
Definition multi.c:150
#define MULTI_PREFIX_MAX_LENGTH
Definition multi.h:44
void multi_reap_process_dowork(const struct multi_context *m)
Definition multi.c:202
static void route_quota_dec(struct multi_instance *mi)
Definition multi.h:438
bool multi_process_signal(struct multi_context *m)
Definition multi.c:3880
void multi_close_instance_on_signal(struct multi_context *m, struct multi_instance *mi)
Definition multi.c:3219
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:2980
void multi_process_per_second_timers_dowork(struct multi_context *m)
Definition multi.c:3798
struct multi_instance * multi_get_queue(struct mbuf_set *ms)
Definition multi.c:3651
bool multi_process_timeout(struct multi_context *m, const unsigned int mpp_flags)
Definition multi.c:3687
static void set_prefix(struct multi_instance *mi)
Definition multi.h:520
static void multi_route_del(struct multi_route *route)
Definition multi.h:479
bool multi_check_push_ifconfig_ipv6_extra_route(struct multi_instance *mi, struct in6_addr *dest)
Determines if the ifconfig_ipv6_local address falls into the range of the local IP addresses of the V...
Definition multi.c:4462
static void multi_reap_process(const struct multi_context *m)
Definition multi.h:565
struct multi_instance * lookup_by_cid(struct multi_context *m, const unsigned long cid)
Definition multi.c:3987
static void route_quota_inc(struct multi_instance *mi)
Definition multi.h:432
void multi_process_incoming_dco(dco_context_t *dco)
Process an incoming DCO message (from kernel space).
bool multi_check_push_ifconfig_extra_route(struct multi_instance *mi, in_addr_t dest)
Determines if the ifconfig_push_local address falls into the range of the local IP addresses of the V...
Definition multi.c:4441
#define MULTI_ROUTE_CACHE
Definition multi.h:236
const char * multi_instance_string(const struct multi_instance *mi, bool null, struct gc_arena *gc)
Definition multi.c:418
static void clear_prefix(void)
Definition multi.h:532
void multi_close_instance(struct multi_context *m, struct multi_instance *mi, bool shutdown)
Definition multi.c:558
static bool multi_route_defined(const struct multi_context *m, const struct multi_route *r)
Definition multi.h:488
static struct multi_instance * multi_process_outgoing_link_pre(struct multi_context *m)
Definition multi.h:410
static void multi_instance_dec_refcount(struct multi_instance *mi)
Definition multi.h:469
void multi_add_mbuf(struct multi_context *m, struct multi_instance *mi, struct mbuf_buffer *mb)
Definition multi.c:2837
static void multi_instance_inc_refcount(struct multi_instance *mi)
Definition multi.h:463
void route_quota_exceeded(const struct multi_instance *mi)
Definition multi.c:3741
void ungenerate_prefix(struct multi_instance *mi)
Definition multi.c:465
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:4145
static void multi_get_timeout_instance(struct multi_context *m, struct timeval *dest)
Definition multi.h:593
static void multi_set_pending(struct multi_context *m, struct multi_instance *mi)
Definition multi.h:702
void init_management_callback_multi(struct multi_context *m)
Definition multi.c:4118
#define MULTI_ROUTE_AGEABLE
Definition multi.h:237
void unlearn_ifconfig_ipv6(struct multi_context *m, struct multi_instance *mi)
Definition multi.c:4373
void multi_process_drop_outgoing_tun(struct multi_context *m, const unsigned int mpp_flags)
Definition multi.c:3719
void unlearn_ifconfig(struct multi_context *m, struct multi_instance *mi)
Definition multi.c:4361
void update_vhash(struct multi_context *m, struct multi_instance *mi, const char *new_ip, const char *new_ipv6)
Update the vhash with new IP/IPv6 addresses in the multi_context when a push-update message containin...
Definition multi.c:4395
static bool multi_process_outgoing_link_dowork(struct multi_context *m, struct multi_instance *mi, const unsigned int mpp_flags)
Definition multi.h:658
static void multi_process_per_second_timers(struct multi_context *m)
Definition multi.h:574
static bool route_quota_test(const struct multi_instance *mi)
Definition multi.h:445
#define CLEAR(x)
Definition basic.h:32
static void msg_set_prefix(const char *prefix)
Definition error.h:332
#define ASSERT(x)
Definition error.h:219
time_t now
Definition otime.c:33
static void tv_delta(struct timeval *dest, const struct timeval *t1, const struct timeval *t2)
Definition otime.h:143
static int openvpn_gettimeofday(struct timeval *tv, void *tz)
Definition otime.h:71
int ifconfig_pool_handle
Definition pool.h:72
static struct schedule_entry * schedule_get_earliest_wakeup(struct schedule *s, struct timeval *wakeup)
Definition schedule.h:116
A singly-linked list of buffers, with head/tail pointers for O(1) push.
Definition buffer.h:2106
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:76
Detached client connection state.
Definition multi.h:71
uint64_t option_types_found
Definition multi.h:76
char * config_file
The temporary file name that contains the config directives returned by the client-connect script.
Definition multi.h:88
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:82
struct buffer to_tun
Definition openvpn.h:376
struct link_socket ** link_sockets
Definition openvpn.h:238
Contains all state information for one tunnel.
Definition openvpn.h:471
struct context_2 c2
Level 2 context.
Definition openvpn.h:514
struct options options
Options loaded from command line or configuration file.
Definition openvpn.h:472
Definition multi.h:60
int signal_received
Definition multi.h:62
struct timeval wakeup
Definition multi.h:63
struct schedule_entry se
Definition multi.h:61
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
Definition list.h:56
struct that handles all the rate limiting logic for initial responses
int ageable_ttl_secs
Definition mroute.h:134
unsigned int cache_generation
Definition mroute.h:133
Main OpenVPN server state structure.
Definition multi.h:162
int n_clients
Definition multi.h:188
struct mroute_addr local
Definition multi.h:183
struct schedule * schedule
Definition multi.h:173
time_t per_second_trigger
Definition multi.h:199
struct mbuf_set * mbuf
Set of buffers for passing data channel packets between VPN tunnel instances.
Definition multi.h:174
struct initial_packet_rate_limit * initial_rate_limiter
Definition multi.h:180
struct deferred_signal_schedule_entry deferred_shutdown_signal
Definition multi.h:214
uint32_t max_peerid
highest currently allocated peer-id and maximum allocated/valid index in instances
Definition multi.h:166
struct multi_reap * reaper
Definition multi.h:182
struct multi_io * multi_io
I/O state and events tracker.
Definition multi.h:177
struct hash * hash
VPN tunnel instances indexed by real address of the remote peer.
Definition multi.h:169
struct context_buffers * context_buffers
Definition multi.h:198
struct hash * cid_hash
Definition multi.h:191
unsigned long cid_counter
Definition multi.h:192
struct event_timeout stale_routes_check_et
Definition multi.h:207
int tcp_queue_limit
Definition multi.h:186
struct ifconfig_pool * ifconfig_pool
Definition multi.h:178
struct frequency_limit * new_connection_limiter
Definition multi.h:179
uint32_t max_clients
Definition multi.h:185
struct context top
Storage structure for process-wide configuration.
Definition multi.h:201
int status_file_version
Definition multi.h:187
struct multi_instance * pending
Definition multi.h:195
struct hash * vhash
VPN tunnel instances indexed by virtual address of remote hosts.
Definition multi.h:171
struct multi_instance ** instances
Array of multi_instances with the size of max_clients.
Definition multi.h:163
struct multi_instance ** mpp_touched
Definition multi.h:197
bool enable_c2c
Definition multi.h:184
struct multi_instance * earliest_wakeup
Definition multi.h:196
struct mroute_helper * route_helper
Definition multi.h:181
Server-mode state structure for one single VPN tunnel.
Definition multi.h:102
struct mbuf_set * tcp_link_out_deferred
Definition multi.h:128
struct buffer_list * cc_config
Definition multi.h:137
bool did_cid_hash
Definition multi.h:136
time_t created
Time at which a VPN tunnel instance was created.
Definition multi.h:116
in_addr_t reporting_addr
Definition multi.h:131
unsigned int tcp_rwflags
Definition multi.h:127
char msg_prefix[MULTI_PREFIX_MAX_LENGTH]
Definition multi.h:124
struct mroute_addr real
External network address of the remote peer.
Definition multi.h:121
bool did_iroutes
Definition multi.h:139
bool socket_set_called
Definition multi.h:129
int route_count
Definition multi.h:115
ifconfig_pool_handle vaddr_handle
Definition multi.h:123
bool did_real_hash
Definition multi.h:134
struct gc_arena gc
Definition multi.h:112
struct in6_addr reporting_addr_ipv6
Definition multi.h:132
struct timeval wakeup
Definition multi.h:120
struct event_arg ev_arg
this struct will store a pointer to either mi or link_socket, depending on the event type,...
Definition multi.h:107
struct context context
The context structure storing state for this VPN tunnel.
Definition multi.h:142
struct schedule_entry se
Definition multi.h:103
int n_clients_delta
Definition multi.h:140
time_t last_call
Definition multi.h:55
uint32_t buckets_per_pass
Definition multi.h:54
uint32_t bucket_base
Definition multi.h:53
struct mroute_addr addr
Definition multi.h:233
time_t last_reference
Definition multi.h:242
unsigned int cache_generation
Definition multi.h:241
unsigned int flags
Definition multi.h:239
struct multi_instance * instance
Definition multi.h:234
int max_routes_per_client
Definition options.h:536
Definition schedule.h:41
uint32_t in_addr_t
Definition syshead.h:52
struct gc_arena gc
Definition test_ssl.c:122
void vlan_process_outgoing_tun(struct multi_context *m, struct multi_instance *mi)
Definition vlan.c:291