OpenVPN
multi.c
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-2025 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
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#ifdef HAVE_SYS_INOTIFY_H
28#include <sys/inotify.h>
29#define INOTIFY_EVENT_BUFFER_SIZE 16384
30#endif
31
32#include "syshead.h"
33
34#include "forward.h"
35#include "multi.h"
36#include "push.h"
37#include "run_command.h"
38#include "otime.h"
39#include "gremlin.h"
40#include "ssl_verify.h"
41#include "ssl_ncp.h"
42#include "vlan.h"
43#include "auth_token.h"
44#include "route.h"
45#include <inttypes.h>
46#include <string.h>
47
48#include "memdbg.h"
49
50
51#include "crypto_backend.h"
52#include "ssl_util.h"
53#include "dco.h"
54#include "reflect_filter.h"
55
56/*#define MULTI_DEBUG_EVENT_LOOP*/
57
58#ifdef MULTI_DEBUG_EVENT_LOOP
59static const char *
60id(struct multi_instance *mi)
61{
62 if (mi)
63 {
64 return tls_common_name(mi->context.c2.tls_multi, false);
65 }
66 else
67 {
68 return "NULL";
69 }
70}
71#endif
72
73#ifdef ENABLE_MANAGEMENT
74static void
75set_cc_config(struct multi_instance *mi, struct buffer_list *cc_config)
76{
78 mi->cc_config = cc_config;
79}
80#endif
81
82static bool
83learn_address_script(const struct multi_context *m, const struct multi_instance *mi, const char *op,
84 const struct mroute_addr *addr)
85{
86 struct gc_arena gc = gc_new();
87 struct env_set *es;
88 bool ret = true;
89 struct plugin_list *plugins;
90
91 /* get environmental variable source */
92 if (mi && mi->context.c2.es)
93 {
94 es = mi->context.c2.es;
95 }
96 else
97 {
99 }
100
101 /* get plugin source */
102 if (mi)
103 {
104 plugins = mi->context.plugins;
105 }
106 else
107 {
108 plugins = m->top.plugins;
109 }
110
111 if (plugin_defined(plugins, OPENVPN_PLUGIN_LEARN_ADDRESS))
112 {
113 struct argv argv = argv_new();
114 argv_printf(&argv, "%s %s", op, mroute_addr_print(addr, &gc));
115 if (mi)
116 {
118 }
119 if (plugin_call(plugins, OPENVPN_PLUGIN_LEARN_ADDRESS, &argv, NULL, es)
120 != OPENVPN_PLUGIN_FUNC_SUCCESS)
121 {
122 msg(M_WARN, "WARNING: learn-address plugin call failed");
123 ret = false;
124 }
125 argv_free(&argv);
126 }
127
129 {
130 struct argv argv = argv_new();
131 setenv_str(es, "script_type", "learn-address");
133 argv_printf_cat(&argv, "%s %s", op, mroute_addr_print(addr, &gc));
134 if (mi)
135 {
137 }
138 if (!openvpn_run_script(&argv, es, 0, "--learn-address"))
139 {
140 ret = false;
141 }
142 argv_free(&argv);
143 }
144
145 gc_free(&gc);
146 return ret;
147}
148
149void
151{
152 /* write pool data to file */
155 {
157 }
158}
159
160static void
161multi_reap_range(const struct multi_context *m, uint32_t start_bucket, uint32_t end_bucket)
162{
163 struct gc_arena gc = gc_new();
164 struct hash_iterator hi;
165 struct hash_element *he;
166
167 dmsg(D_MULTI_DEBUG, "MULTI: REAP range %d -> %d", start_bucket, end_bucket);
168 hash_iterator_init_range(m->vhash, &hi, start_bucket, end_bucket);
169 while ((he = hash_iterator_next(&hi)) != NULL)
170 {
171 struct multi_route *r = (struct multi_route *)he->value;
172 if (!multi_route_defined(m, r))
173 {
174 dmsg(D_MULTI_DEBUG, "MULTI: REAP DEL %s", mroute_addr_print(&r->addr, &gc));
175 learn_address_script(m, NULL, "delete", &r->addr);
178 }
179 }
181 gc_free(&gc);
182}
183
184static void
186{
188}
189
190static struct multi_reap *
192{
193 struct multi_reap *mr;
194 ALLOC_OBJ(mr, struct multi_reap);
195 mr->bucket_base = 0;
197 mr->last_call = now;
198 return mr;
199}
200
201void
203{
204 struct multi_reap *mr = m->reaper;
205 if (mr->bucket_base >= hash_n_buckets(m->vhash))
206 {
207 mr->bucket_base = 0;
208 }
210 mr->bucket_base += mr->buckets_per_pass;
211 mr->last_call = now;
212}
213
214static void
216{
217 free(mr);
218}
219
220/*
221 * How many buckets in vhash to reap per pass.
222 */
223static uint32_t
224reap_buckets_per_pass(uint32_t n_buckets)
225{
226 return constrain_uint(n_buckets / REAP_DIVISOR, REAP_MIN, REAP_MAX);
227}
228
229#ifdef ENABLE_MANAGEMENT
230
231static uint32_t
232cid_hash_function(const void *key, uint32_t iv)
233{
234 const unsigned long *k = (const unsigned long *)key;
235 return (uint32_t)*k;
236}
237
238static bool
239cid_compare_function(const void *key1, const void *key2)
240{
241 const unsigned long *k1 = (const unsigned long *)key1;
242 const unsigned long *k2 = (const unsigned long *)key2;
243 return *k1 == *k2;
244}
245
246#endif
247
248#if defined(__GNUC__) || defined(__clang__)
249#pragma GCC diagnostic push
250#pragma GCC diagnostic ignored "-Wconversion"
251#endif
252
253#ifdef ENABLE_ASYNC_PUSH
254static uint32_t
255/*
256 * inotify watcher descriptors are used as hash value
257 */
258int_hash_function(const void *key, uint32_t iv)
259{
260 return (unsigned long)key;
261}
262
263static bool
264int_compare_function(const void *key1, const void *key2)
265{
266 return (unsigned long)key1 == (unsigned long)key2;
267}
268#endif
269
270/*
271 * Main initialization function, init multi_context object.
272 */
273static void
275{
276 struct multi_context *m = t->multi;
277 int dev = DEV_TYPE_UNDEF;
278
279 msg(D_MULTI_LOW, "MULTI: multi_init called, r=%d v=%d", t->options.real_hash_size,
281
282 /*
283 * Get tun/tap/null device type
284 */
286
287 /*
288 * Init our multi_context object.
289 */
290 CLEAR(*m);
291
292 /*
293 * Real address hash table (source port number is
294 * considered to be part of the address). Used
295 * to determine which client sent an incoming packet
296 * which is seen on the TCP/UDP socket.
297 */
300
301 /*
302 * Virtual address hash table. Used to determine
303 * which client to route a packet to.
304 */
307
308 /*
309 * This hash table is a clone of m->hash but with a
310 * bucket size of one so that it can be used
311 * for fast iteration through the list.
312 */
314
315#ifdef ENABLE_MANAGEMENT
317#endif
318
319#ifdef ENABLE_ASYNC_PUSH
320 /*
321 * Mapping between inotify watch descriptors and
322 * multi_instances.
323 */
324 m->inotify_watchers =
325 hash_init(t->options.real_hash_size, get_random(), int_hash_function, int_compare_function);
326#endif
327
328 /*
329 * This is our scheduler, for time-based wakeup
330 * events.
331 */
332 m->schedule = schedule_init();
333
334 /*
335 * Limit frequency of incoming connections to control
336 * DoS.
337 */
341
342 /*
343 * Allocate broadcast/multicast buffer list
344 */
346
347 /*
348 * Different status file format options are available
349 */
351
352 /*
353 * Possibly allocate an ifconfig pool, do it
354 * differently based on whether a tun or tap style
355 * tunnel.
356 */
358 {
360
361 if (dev == DEV_TYPE_TUN && t->options.topology == TOP_NET30)
362 {
364 }
365
371
372 /* reload pool data from file */
374 {
376 }
377 }
378
379 /*
380 * Help us keep track of routing table.
381 */
383
384 /*
385 * Initialize route and instance reaper.
386 */
388
389 /*
390 * Get local ifconfig address
391 */
392 CLEAR(m->local);
393 ASSERT(t->c1.tuntap);
395
396 /*
397 * Per-client limits
398 */
400
401 m->instances = calloc(m->max_clients, sizeof(struct multi_instance *));
402
403 m->top.c2.event_set = t->c2.event_set;
404
405 /*
406 * Initialize multi-socket I/O wait object
407 */
410
411 /*
412 * Allow client <-> client communication, without going through
413 * tun/tap interface and network stack?
414 */
416
417 /* initialize stale routes check timer */
419 {
420 msg(M_INFO,
421 "Initializing stale route check timer to run every %i seconds and to removing routes with activity timeout older than %i seconds",
424 }
425
427}
428
429const char *
430multi_instance_string(const struct multi_instance *mi, bool null, struct gc_arena *gc)
431{
432 if (mi)
433 {
435 const char *cn = tls_common_name(mi->context.c2.tls_multi, true);
436
437 if (cn)
438 {
439 buf_printf(&out, "%s/", cn);
440 }
441 buf_printf(&out, "%s", mroute_addr_print(&mi->real, gc));
443 && dco_enabled(&mi->context.options))
444 {
445 buf_printf(&out, " peer-id=%d", mi->context.c2.tls_multi->peer_id);
446 }
447 return BSTR(&out);
448 }
449 else if (null)
450 {
451 return NULL;
452 }
453 else
454 {
455 return "UNDEF";
456 }
457}
458
459static void
461{
462 struct gc_arena gc = gc_new();
463 const char *prefix = multi_instance_string(mi, true, &gc);
464 if (prefix)
465 {
466 strncpynt(mi->msg_prefix, prefix, sizeof(mi->msg_prefix));
467 }
468 else
469 {
470 mi->msg_prefix[0] = '\0';
471 }
472 set_prefix(mi);
473 gc_free(&gc);
474}
475
476void
478{
479 mi->msg_prefix[0] = '\0';
480 set_prefix(mi);
481}
482
483/*
484 * Tell the route helper about deleted iroutes so
485 * that it can update its mask of currently used
486 * CIDR netlengths.
487 */
488static void
490{
491 const struct iroute *ir;
492 const struct iroute_ipv6 *ir6;
493
494 dco_delete_iroutes(m, mi);
495
497 {
498 for (ir = mi->context.options.iroutes; ir != NULL; ir = ir->next)
499 {
501 }
502
503 for (ir6 = mi->context.options.iroutes_ipv6; ir6 != NULL; ir6 = ir6->next)
504 {
506 }
507 }
508}
509
510static void
511setenv_stats(struct multi_context *m, struct context *c)
512{
513 if (dco_enabled(&m->top.options))
514 {
515 if (dco_get_peer_stats_multi(&m->top.c1.tuntap->dco, false) < 0)
516 {
517 return;
518 }
519 }
520
521 setenv_counter(c->c2.es, "bytes_received", c->c2.link_read_bytes + c->c2.dco_read_bytes);
522 setenv_counter(c->c2.es, "bytes_sent", c->c2.link_write_bytes + c->c2.dco_write_bytes);
523}
524
525static void
527{
528 /* setenv client real IP address */
530
531 /* setenv stats */
532 setenv_stats(m, &mi->context);
533
534 /* setenv connection duration */
535 setenv_long_long(mi->context.c2.es, "time_duration", now - mi->created);
536}
537
538static void
540{
542
543 if (plugin_defined(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_DISCONNECT))
544 {
545 if (plugin_call(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_DISCONNECT, NULL, NULL,
546 mi->context.c2.es)
547 != OPENVPN_PLUGIN_FUNC_SUCCESS)
548 {
549 msg(M_WARN, "WARNING: client-disconnect plugin call failed");
550 }
551 }
552
554 {
555 struct argv argv = argv_new();
556 setenv_str(mi->context.c2.es, "script_type", "client-disconnect");
558 openvpn_run_script(&argv, mi->context.c2.es, 0, "--client-disconnect");
559 argv_free(&argv);
560 }
561#ifdef ENABLE_MANAGEMENT
562 if (management)
563 {
565 }
566#endif
567}
568
569void
570multi_close_instance(struct multi_context *m, struct multi_instance *mi, bool shutdown)
571{
572 ASSERT(!mi->halt);
573 mi->halt = true;
574 bool is_dgram = proto_is_dgram(mi->context.c2.link_sockets[0]->info.proto);
575
576 dmsg(D_MULTI_DEBUG, "MULTI: multi_close_instance called");
577
578 /* adjust current client connection count */
579 m->n_clients += mi->n_clients_delta;
580 mi->n_clients_delta = 0;
581
582 /* prevent dangling pointers */
583 if (m->pending == mi)
584 {
585 multi_set_pending(m, NULL);
586 }
587 if (m->earliest_wakeup == mi)
588 {
589 m->earliest_wakeup = NULL;
590 }
591
592 if (!shutdown)
593 {
594 if (mi->did_real_hash)
595 {
596 ASSERT(hash_remove(m->hash, &mi->real));
597 }
598 if (mi->did_iter)
599 {
600 ASSERT(hash_remove(m->iter, &mi->real));
601 }
602#ifdef ENABLE_MANAGEMENT
603 if (mi->did_cid_hash)
604 {
606 }
607#endif
608
609#ifdef ENABLE_ASYNC_PUSH
610 if (mi->inotify_watch != -1)
611 {
612 hash_remove(m->inotify_watchers, (void *)(unsigned long)mi->inotify_watch);
613 mi->inotify_watch = -1;
614 }
615#endif
616
618 {
619 m->instances[mi->context.c2.tls_multi->peer_id] = NULL;
620 }
621
623
624 ifconfig_pool_release(m->ifconfig_pool, mi->vaddr_handle, false);
625
626 if (mi->did_iroutes)
627 {
628 multi_del_iroutes(m, mi);
629 mi->did_iroutes = false;
630 }
631
632 if (!is_dgram)
633 {
635 }
636
638 }
639
640#ifdef ENABLE_MANAGEMENT
641 set_cc_config(mi, NULL);
642#endif
643
645 {
647 }
648
649 close_context(&mi->context, SIGTERM, CC_GC_FREE);
650
652
654
655 /*
656 * Don't actually delete the instance memory allocation yet,
657 * because virtual routes may still point to it. Let the
658 * vhash reaper deal with it.
659 */
661}
662
663/*
664 * Called on shutdown or restart.
665 */
666static void
668{
669 if (m->hash)
670 {
671 struct hash_iterator hi;
672 struct hash_element *he;
673
674 hash_iterator_init(m->iter, &hi);
675 while ((he = hash_iterator_next(&hi)))
676 {
677 struct multi_instance *mi = (struct multi_instance *)he->value;
678 mi->did_iter = false;
679 multi_close_instance(m, mi, true);
680 }
682
684
685 hash_free(m->hash);
686 hash_free(m->vhash);
687 hash_free(m->iter);
688#ifdef ENABLE_MANAGEMENT
690#endif
691 m->hash = NULL;
692
693 free(m->instances);
694
695#ifdef ENABLE_ASYNC_PUSH
696 hash_free(m->inotify_watchers);
697 m->inotify_watchers = NULL;
698#endif
699
701 mbuf_free(m->mbuf);
708 }
709}
710
711/*
712 * Create a client instance object for a newly connected client.
713 */
714struct multi_instance *
716 struct link_socket *sock)
717{
718 struct gc_arena gc = gc_new();
719 struct multi_instance *mi;
720
721 msg(D_MULTI_MEDIUM, "MULTI: multi_create_instance called");
722
724
725 mi->gc = gc_new();
727 mi->vaddr_handle = -1;
728 mi->created = now;
730
731 if (real)
732 {
733 mi->real = *real;
734 generate_prefix(mi);
735 }
736
737 inherit_context_child(&mi->context, &m->top, sock);
738 if (IS_SIG(&mi->context))
739 {
740 goto err;
741 }
742
744
745 if (hash_n_elements(m->hash) >= m->max_clients)
746 {
748 "MULTI: new incoming connection would exceed maximum number of clients (%d)",
749 m->max_clients);
750 goto err;
751 }
752
753 if (!real) /* TCP mode? */
754 {
756 {
757 goto err;
758 }
759 generate_prefix(mi);
760 }
761
762 if (!hash_add(m->iter, &mi->real, mi, false))
763 {
764 msg(D_MULTI_LOW, "MULTI: unable to add real address [%s] to iterator hash table",
765 mroute_addr_print(&mi->real, &gc));
766 goto err;
767 }
768 mi->did_iter = true;
769
770#ifdef ENABLE_MANAGEMENT
771 do
772 {
774 } while (!hash_add(m->cid_hash, &mi->context.c2.mda_context.cid, mi, false));
775 mi->did_cid_hash = true;
776#endif
777
778 mi->context.c2.push_request_received = false;
779#ifdef ENABLE_ASYNC_PUSH
780 mi->inotify_watch = -1;
781#endif
782
784 {
785 msg(D_MULTI_ERRORS, "MULTI: signal occurred during client instance initialization");
786 goto err;
787 }
788
790 mi->ev_arg.u.mi = mi;
791
792 gc_free(&gc);
793 return mi;
794
795err:
796 multi_close_instance(m, mi, false);
797 gc_free(&gc);
798 return NULL;
799}
800
801/*
802 * Dump tables -- triggered by SIGUSR2.
803 * If status file is defined, write to file.
804 * If status file is NULL, write to syslog.
805 */
806static void
807multi_print_status(struct multi_context *m, struct status_output *so, const int version)
808{
809 if (m->hash)
810 {
811 struct gc_arena gc_top = gc_new();
812 struct hash_iterator hi;
813 const struct hash_element *he;
814
815 status_reset(so);
816
817 if (dco_enabled(&m->top.options))
818 {
819 if (dco_get_peer_stats_multi(&m->top.c1.tuntap->dco, true) < 0)
820 {
821 return;
822 }
823 }
824
825 if (version == 1)
826 {
827 /*
828 * Status file version 1
829 */
830 status_printf(so, "OpenVPN CLIENT LIST");
831 status_printf(so, "Updated,%s", time_string(0, 0, false, &gc_top));
832 status_printf(so, "Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since");
833 hash_iterator_init(m->hash, &hi);
834 while ((he = hash_iterator_next(&hi)))
835 {
836 struct gc_arena gc = gc_new();
837 const struct multi_instance *mi = (struct multi_instance *)he->value;
838
839 if (!mi->halt)
840 {
841 status_printf(so, "%s,%s," counter_format "," counter_format ",%s",
843 mroute_addr_print(&mi->real, &gc),
846 time_string(mi->created, 0, false, &gc));
847 }
848 gc_free(&gc);
849 }
851
852 status_printf(so, "ROUTING TABLE");
853 status_printf(so, "Virtual Address,Common Name,Real Address,Last Ref");
854 hash_iterator_init(m->vhash, &hi);
855 while ((he = hash_iterator_next(&hi)))
856 {
857 struct gc_arena gc = gc_new();
858 const struct multi_route *route = (struct multi_route *)he->value;
859
861 {
862 const struct multi_instance *mi = route->instance;
863 const struct mroute_addr *ma = &route->addr;
864 char flags[2] = { 0, 0 };
865
866 if (route->flags & MULTI_ROUTE_CACHE)
867 {
868 flags[0] = 'C';
869 }
870 status_printf(so, "%s%s,%s,%s,%s", mroute_addr_print(ma, &gc), flags,
872 mroute_addr_print(&mi->real, &gc),
873 time_string(route->last_reference, 0, false, &gc));
874 }
875 gc_free(&gc);
876 }
878
879 status_printf(so, "GLOBAL STATS");
880 if (m->mbuf)
881 {
882 status_printf(so, "Max bcast/mcast queue length,%d", mbuf_maximum_queued(m->mbuf));
883 }
884
885 status_printf(so, "END");
886 }
887 else if (version == 2 || version == 3)
888 {
889 const char sep = (version == 3) ? '\t' : ',';
890
891 /*
892 * Status file version 2 and 3
893 */
894 status_printf(so, "TITLE%c%s", sep, title_string);
895 status_printf(so, "TIME%c%s%c%u", sep, time_string(now, 0, false, &gc_top), sep,
896 (unsigned int)now);
898 so,
899 "HEADER%cCLIENT_LIST%cCommon Name%cReal Address%cVirtual Address%cVirtual IPv6 Address%cBytes Received%cBytes Sent%cConnected Since%cConnected Since (time_t)%cUsername%cClient ID%cPeer ID%cData Channel Cipher",
900 sep, sep, sep, sep, sep, sep, sep, sep, sep, sep, sep, sep, sep);
901 hash_iterator_init(m->hash, &hi);
902 while ((he = hash_iterator_next(&hi)))
903 {
904 struct gc_arena gc = gc_new();
905 const struct multi_instance *mi = (struct multi_instance *)he->value;
906
907 if (!mi->halt)
908 {
910 so,
911 "CLIENT_LIST%c%s%c%s%c%s%c%s%c" counter_format "%c" counter_format
912 "%c%s%c%u%c%s%c"
913#ifdef ENABLE_MANAGEMENT
914 "%lu"
915#else
916 ""
917#endif
918 "%c%" PRIu32 "%c%s",
919 sep, tls_common_name(mi->context.c2.tls_multi, false), sep,
920 mroute_addr_print(&mi->real, &gc), sep,
925 time_string(mi->created, 0, false, &gc), sep, (unsigned int)mi->created,
926 sep, tls_username(mi->context.c2.tls_multi, false),
927#ifdef ENABLE_MANAGEMENT
928 sep, mi->context.c2.mda_context.cid,
929#else
930 sep,
931#endif
932 sep,
933 mi->context.c2.tls_multi ? mi->context.c2.tls_multi->peer_id : UINT32_MAX,
935 }
936 gc_free(&gc);
937 }
939
941 so,
942 "HEADER%cROUTING_TABLE%cVirtual Address%cCommon Name%cReal Address%cLast Ref%cLast Ref (time_t)",
943 sep, sep, sep, sep, sep, sep);
944 hash_iterator_init(m->vhash, &hi);
945 while ((he = hash_iterator_next(&hi)))
946 {
947 struct gc_arena gc = gc_new();
948 const struct multi_route *route = (struct multi_route *)he->value;
949
951 {
952 const struct multi_instance *mi = route->instance;
953 const struct mroute_addr *ma = &route->addr;
954 char flags[2] = { 0, 0 };
955
956 if (route->flags & MULTI_ROUTE_CACHE)
957 {
958 flags[0] = 'C';
959 }
960 status_printf(so, "ROUTING_TABLE%c%s%s%c%s%c%s%c%s%c%u", sep,
961 mroute_addr_print(ma, &gc), flags, sep,
963 mroute_addr_print(&mi->real, &gc), sep,
964 time_string(route->last_reference, 0, false, &gc), sep,
965 (unsigned int)route->last_reference);
966 }
967 gc_free(&gc);
968 }
970
971 if (m->mbuf)
972 {
973 status_printf(so, "GLOBAL_STATS%cMax bcast/mcast queue length%c%d", sep, sep,
974 mbuf_maximum_queued(m->mbuf));
975 }
976
977 status_printf(so, "GLOBAL_STATS%cdco_enabled%c%d", sep, sep,
978 dco_enabled(&m->top.options));
979 status_printf(so, "END");
980 }
981 else
982 {
983 status_printf(so, "ERROR: bad status format version number");
984 }
985
986#ifdef PACKET_TRUNCATION_CHECK
987 {
988 status_printf(so, "HEADER,ERRORS,Common Name,TUN Read Trunc,TUN Write Trunc,Pre-encrypt Trunc,Post-decrypt Trunc");
989 hash_iterator_init(m->hash, &hi);
990 while ((he = hash_iterator_next(&hi)))
991 {
992 struct gc_arena gc = gc_new();
993 const struct multi_instance *mi = (struct multi_instance *)he->value;
994
995 if (!mi->halt)
996 {
997 status_printf(so,
998 "ERRORS,%s," counter_format "," counter_format "," counter_format
999 "," counter_format,
1000 tls_common_name(mi->context.c2.tls_multi, false),
1001 m->top.c2.n_trunc_tun_read, mi->context.c2.n_trunc_tun_write,
1002 mi->context.c2.n_trunc_pre_encrypt,
1003 mi->context.c2.n_trunc_post_decrypt);
1004 }
1005 gc_free(&gc);
1006 }
1007 hash_iterator_free(&hi);
1008 }
1009#endif /* ifdef PACKET_TRUNCATION_CHECK */
1010
1011 status_flush(so);
1012 gc_free(&gc_top);
1013 }
1014
1015#ifdef ENABLE_ASYNC_PUSH
1016 if (m->inotify_watchers)
1017 {
1018 msg(D_MULTI_DEBUG, "inotify watchers count: %d", hash_n_elements(m->inotify_watchers));
1019 }
1020#endif
1021}
1022
1023/*
1024 * Learn a virtual address or route.
1025 * The learn will fail if the learn address
1026 * script/plugin fails. In this case the
1027 * return value may be != mi.
1028 * Return the instance which owns this route,
1029 * or NULL if none.
1030 */
1031static struct multi_instance *
1032multi_learn_addr(struct multi_context *m, struct multi_instance *mi, const struct mroute_addr *addr,
1033 const unsigned int flags)
1034{
1035 struct hash_element *he;
1036 const uint32_t hv = hash_value(m->vhash, addr);
1037 struct hash_bucket *bucket = hash_bucket(m->vhash, hv);
1038 struct multi_route *oldroute = NULL;
1039 struct multi_instance *owner = NULL;
1040 struct gc_arena gc = gc_new();
1041
1042 /* if route currently exists, get the instance which owns it */
1043 he = hash_lookup_fast(m->vhash, bucket, addr, hv);
1044 if (he)
1045 {
1046 oldroute = (struct multi_route *)he->value;
1047 }
1048 if (oldroute && multi_route_defined(m, oldroute))
1049 {
1050 owner = oldroute->instance;
1051 }
1052
1053 /* do we need to add address to hash table? */
1054 if ((!owner || owner != mi) && mroute_learnable_address(addr, &gc)
1055 && !mroute_addr_equal(addr, &m->local))
1056 {
1057 struct multi_route *newroute;
1058 bool learn_succeeded = false;
1059
1060 ALLOC_OBJ(newroute, struct multi_route);
1061 newroute->addr = *addr;
1062 newroute->instance = mi;
1063 newroute->flags = flags;
1064 newroute->last_reference = now;
1065 newroute->cache_generation = 0;
1066
1067 /* The cache is invalidated when cache_generation is incremented */
1069 {
1071 }
1072
1073 if (oldroute) /* route already exists? */
1074 {
1075 if (route_quota_test(mi) && learn_address_script(m, mi, "update", &newroute->addr))
1076 {
1077 learn_succeeded = true;
1078 owner = mi;
1080 route_quota_inc(mi);
1081
1082 /* delete old route */
1083 multi_route_del(oldroute);
1084
1085 /* modify hash table entry, replacing old route */
1086 he->key = &newroute->addr;
1087 he->value = newroute;
1088 }
1089 }
1090 else
1091 {
1092 if (route_quota_test(mi) && learn_address_script(m, mi, "add", &newroute->addr))
1093 {
1094 learn_succeeded = true;
1095 owner = mi;
1097 route_quota_inc(mi);
1098
1099 /* add new route */
1100 hash_add_fast(m->vhash, bucket, &newroute->addr, hv, newroute);
1101 }
1102 }
1103
1104 msg(D_MULTI_LOW, "MULTI: Learn%s: %s -> %s", learn_succeeded ? "" : " FAILED",
1105 mroute_addr_print(&newroute->addr, &gc), multi_instance_string(mi, false, &gc));
1106
1107 if (!learn_succeeded)
1108 {
1109 free(newroute);
1110 }
1111 }
1112 gc_free(&gc);
1113
1114 return owner;
1115}
1116
1117/*
1118 * Get client instance based on virtual address.
1119 */
1120static struct multi_instance *
1122 bool cidr_routing)
1123{
1124 struct multi_route *route;
1125 struct multi_instance *ret = NULL;
1126
1127 /* check for local address */
1128 if (mroute_addr_equal(addr, &m->local))
1129 {
1130 return NULL;
1131 }
1132
1133 route = (struct multi_route *)hash_lookup(m->vhash, addr);
1134
1135 /* does host route (possible cached) exist? */
1136 if (route && multi_route_defined(m, route))
1137 {
1138 struct multi_instance *mi = route->instance;
1139 route->last_reference = now;
1140 ret = mi;
1141 }
1142 else if (cidr_routing) /* do we need to regenerate a host route cache entry? */
1143 {
1144 struct mroute_helper *rh = m->route_helper;
1145 struct mroute_addr tryaddr;
1146 int i;
1147
1148 /* cycle through each CIDR length */
1149 for (i = 0; i < rh->n_net_len; ++i)
1150 {
1151 tryaddr = *addr;
1153 tryaddr.netbits = rh->net_len[i];
1155
1156 /* look up a possible route with netbits netmask */
1157 route = (struct multi_route *)hash_lookup(m->vhash, &tryaddr);
1158
1159 if (route && multi_route_defined(m, route))
1160 {
1161 /* found an applicable route, cache host route */
1162 struct multi_instance *mi = route->instance;
1164 ret = mi;
1165 break;
1166 }
1167 }
1168 }
1169
1170#ifdef ENABLE_DEBUG
1172 {
1173 struct gc_arena gc = gc_new();
1174 const char *addr_text = mroute_addr_print(addr, &gc);
1175 if (ret)
1176 {
1177 dmsg(D_MULTI_DEBUG, "GET INST BY VIRT: %s -> %s via %s", addr_text,
1178 multi_instance_string(ret, false, &gc), mroute_addr_print(&route->addr, &gc));
1179 }
1180 else
1181 {
1182 dmsg(D_MULTI_DEBUG, "GET INST BY VIRT: %s [failed]", addr_text);
1183 }
1184 gc_free(&gc);
1185 }
1186#endif
1187
1188 ASSERT(!(ret && ret->halt));
1189 return ret;
1190}
1191
1192/*
1193 * Helper function to multi_learn_addr().
1194 */
1195static struct multi_instance *
1196multi_learn_in_addr_t(struct multi_context *m, struct multi_instance *mi, in_addr_t a,
1197 int netbits, /* -1 if host route, otherwise # of network bits in address */
1198 bool primary)
1199{
1200 struct openvpn_sockaddr remote_si;
1201 struct mroute_addr addr = { 0 };
1202
1204 remote_si.addr.in4.sin_family = AF_INET;
1205 remote_si.addr.in4.sin_addr.s_addr = htonl(a);
1206 addr.proto = 0;
1208
1209 if (netbits >= 0)
1210 {
1211 addr.type |= MR_WITH_NETBITS;
1212 addr.netbits = (uint8_t)netbits;
1213 }
1214
1215 struct multi_instance *owner = multi_learn_addr(m, mi, &addr, 0);
1216#ifdef ENABLE_MANAGEMENT
1217 if (management && owner)
1218 {
1219 management_learn_addr(management, &mi->context.c2.mda_context, &addr, primary);
1220 }
1221#endif
1222 if (primary && multi_check_push_ifconfig_extra_route(mi, addr.v4.addr))
1223 {
1224 /* "primary" is the VPN ifconfig address of the peer */
1225 /* if it does not fall into the network defined by ifconfig_local
1226 * we install this as extra onscope address on the interface */
1227 addr.netbits = 32;
1228 addr.type |= MR_ONLINK_DCO_ADDR;
1229
1230 dco_install_iroute(m, mi, &addr);
1231 }
1232 else if (!primary)
1233 {
1234 ASSERT(netbits >= 0); /* DCO requires populated netbits */
1235 dco_install_iroute(m, mi, &addr);
1236 }
1237
1238 return owner;
1239}
1240
1241static struct multi_instance *
1242multi_learn_in6_addr(struct multi_context *m, struct multi_instance *mi, struct in6_addr a6,
1243 int netbits, /* -1 if host route, otherwise # of network bits in address */
1244 bool primary)
1245{
1246 struct mroute_addr addr = { 0 };
1247
1248 addr.len = 16;
1249 addr.type = MR_ADDR_IPV6;
1250 addr.netbits = 0;
1251 addr.v6.addr = a6;
1252
1253 if (netbits >= 0)
1254 {
1255 addr.type |= MR_WITH_NETBITS;
1256 addr.netbits = (uint8_t)netbits;
1258 }
1259
1260 struct multi_instance *owner = multi_learn_addr(m, mi, &addr, 0);
1261#ifdef ENABLE_MANAGEMENT
1262 if (management && owner)
1263 {
1264 management_learn_addr(management, &mi->context.c2.mda_context, &addr, primary);
1265 }
1266#endif
1267 if (primary && multi_check_push_ifconfig_ipv6_extra_route(mi, &addr.v6.addr))
1268 {
1269 /* "primary" is the VPN ifconfig address of the peer */
1270 /* if it does not fall into the network defined by ifconfig_local
1271 * we install this as extra onscope address on the interface */
1272 addr.netbits = 128;
1273 addr.type |= MR_ONLINK_DCO_ADDR;
1274
1275 dco_install_iroute(m, mi, &addr);
1276 }
1277 else if (!primary)
1278 {
1279 /* "primary" is the VPN ifconfig address of the peer and already
1280 * known to DCO, so only install "extra" iroutes (primary = false)
1281 */
1282 ASSERT(netbits >= 0); /* DCO requires populated netbits */
1283 dco_install_iroute(m, mi, &addr);
1284 }
1285
1286 return owner;
1287}
1288
1289/*
1290 * A new client has connected, add routes (server -> client)
1291 * to internal routing table.
1292 */
1293static void
1295{
1296 struct gc_arena gc = gc_new();
1297 const struct iroute *ir;
1298 const struct iroute_ipv6 *ir6;
1300 {
1301 mi->did_iroutes = true;
1302 for (ir = mi->context.options.iroutes; ir != NULL; ir = ir->next)
1303 {
1304 if (ir->netbits >= 0)
1305 {
1306 msg(D_MULTI_LOW, "MULTI: internal route %s/%d -> %s",
1307 print_in_addr_t(ir->network, 0, &gc), ir->netbits,
1308 multi_instance_string(mi, false, &gc));
1309 }
1310 else
1311 {
1312 msg(D_MULTI_LOW, "MULTI: internal route %s -> %s",
1313 print_in_addr_t(ir->network, 0, &gc), multi_instance_string(mi, false, &gc));
1314 }
1315
1317
1318 multi_learn_in_addr_t(m, mi, ir->network, ir->netbits, false);
1319 }
1320 for (ir6 = mi->context.options.iroutes_ipv6; ir6 != NULL; ir6 = ir6->next)
1321 {
1322 msg(D_MULTI_LOW, "MULTI: internal route %s/%d -> %s",
1323 print_in6_addr(ir6->network, 0, &gc), ir6->netbits,
1324 multi_instance_string(mi, false, &gc));
1325
1327
1328 multi_learn_in6_addr(m, mi, ir6->network, ir6->netbits, false);
1329 }
1330 }
1331 gc_free(&gc);
1332}
1333
1334/*
1335 * Given an instance (new_mi), delete all other instances which use the
1336 * same common name.
1337 */
1338static void
1340{
1341 if (new_mi)
1342 {
1343 const char *new_cn = tls_common_name(new_mi->context.c2.tls_multi, true);
1344 if (new_cn)
1345 {
1346 struct hash_iterator hi;
1347 struct hash_element *he;
1348 int count = 0;
1349
1350 hash_iterator_init(m->iter, &hi);
1351 while ((he = hash_iterator_next(&hi)))
1352 {
1353 struct multi_instance *mi = (struct multi_instance *)he->value;
1354 if (mi != new_mi && !mi->halt)
1355 {
1356 const char *cn = tls_common_name(mi->context.c2.tls_multi, true);
1357 if (cn && !strcmp(cn, new_cn))
1358 {
1359 mi->did_iter = false;
1360 multi_close_instance(m, mi, false);
1362 ++count;
1363 }
1364 }
1365 }
1366 hash_iterator_free(&hi);
1367
1368 if (count)
1369 {
1371 "MULTI: new connection by client '%s' will cause previous active sessions by this client to be dropped. Remember to use the --duplicate-cn option if you want multiple clients using the same certificate or username to concurrently connect.",
1372 new_cn);
1373 }
1374 }
1375 }
1376}
1377
1378static void
1380{
1381 struct gc_arena gc = gc_new();
1382 struct hash_iterator hi;
1383 struct hash_element *he;
1384
1385 dmsg(D_MULTI_DEBUG, "MULTI: Checking stale routes");
1387 while ((he = hash_iterator_next(&hi)) != NULL)
1388 {
1389 struct multi_route *r = (struct multi_route *)he->value;
1390 if (multi_route_defined(m, r)
1391 && difftime(now, r->last_reference) >= m->top.options.stale_routes_ageing_time)
1392 {
1393 dmsg(D_MULTI_DEBUG, "MULTI: Deleting stale route for address '%s'",
1394 mroute_addr_print(&r->addr, &gc));
1395 learn_address_script(m, NULL, "delete", &r->addr);
1396 multi_route_del(r);
1398 }
1399 }
1400 hash_iterator_free(&hi);
1401 gc_free(&gc);
1402}
1403
1404/*
1405 * Ensure that endpoint to be pushed to client
1406 * complies with --ifconfig-push-constraint directive.
1407 */
1408static bool
1410{
1411 const struct options *o = &c->options;
1413 {
1416 }
1417 else
1418 {
1419 return true;
1420 }
1421}
1422
1423/*
1424 * Select a virtual address for a new client instance.
1425 * Use an --ifconfig-push directive, if given (static IP).
1426 * Otherwise use an --ifconfig-pool address (dynamic IP).
1427 */
1428static void
1430{
1431 struct gc_arena gc = gc_new();
1432
1433 /*
1434 * If ifconfig addresses were set by dynamic config file,
1435 * release pool addresses, otherwise keep them.
1436 */
1438 {
1439 /* ifconfig addresses were set statically,
1440 * release dynamic allocation */
1441 if (mi->vaddr_handle >= 0)
1442 {
1444 mi->vaddr_handle = -1;
1445 }
1446
1447 mi->context.c2.push_ifconfig_defined = true;
1452
1453 /* the current implementation does not allow "static IPv4, pool IPv6",
1454 * (see below) so issue a warning if that happens - don't break the
1455 * session, though, as we don't even know if this client WANTS IPv6
1456 */
1459 {
1460 msg(M_INFO,
1461 "MULTI_sva: WARNING: if --ifconfig-push is used for IPv4, automatic IPv6 assignment from --ifconfig-ipv6-pool does not work. Use --ifconfig-ipv6-push for IPv6 then.");
1462 }
1463 }
1464 else if (m->ifconfig_pool && mi->vaddr_handle < 0) /* otherwise, choose a pool address */
1465 {
1466 in_addr_t local = 0, remote = 0;
1467 struct in6_addr remote_ipv6;
1468 const char *cn = NULL;
1469
1470 if (!mi->context.options.duplicate_cn)
1471 {
1472 cn = tls_common_name(mi->context.c2.tls_multi, true);
1473 }
1474
1475 CLEAR(remote_ipv6);
1476 mi->vaddr_handle =
1477 ifconfig_pool_acquire(m->ifconfig_pool, &local, &remote, &remote_ipv6, cn);
1478 if (mi->vaddr_handle >= 0)
1479 {
1480 const int tunnel_type = TUNNEL_TYPE(mi->context.c1.tuntap);
1481 const int tunnel_topology = TUNNEL_TOPOLOGY(mi->context.c1.tuntap);
1482
1483 msg(M_INFO, "MULTI_sva: pool returned IPv4=%s, IPv6=%s",
1485 : "(Not enabled)"),
1487 ? print_in6_addr(remote_ipv6, 0, &gc)
1488 : "(Not enabled)"));
1489
1491 {
1492 /* set push_ifconfig_remote_netmask from pool ifconfig address(es) */
1493 mi->context.c2.push_ifconfig_local = remote;
1494 if (tunnel_type == DEV_TYPE_TAP
1495 || (tunnel_type == DEV_TYPE_TUN && tunnel_topology == TOP_SUBNET))
1496 {
1500 {
1503 }
1504 }
1505 else if (tunnel_type == DEV_TYPE_TUN)
1506 {
1507 if (tunnel_topology == TOP_P2P)
1508 {
1510 }
1511 else if (tunnel_topology == TOP_NET30)
1512 {
1514 }
1515 }
1516
1518 {
1519 mi->context.c2.push_ifconfig_defined = true;
1520 }
1521 else
1522 {
1524 "MULTI: no --ifconfig-pool netmask parameter is available to push to %s",
1525 multi_instance_string(mi, false, &gc));
1526 }
1527 }
1528
1530 {
1531 mi->context.c2.push_ifconfig_ipv6_local = remote_ipv6;
1536 }
1537 }
1538 else
1539 {
1540 msg(D_MULTI_ERRORS, "MULTI: no free --ifconfig-pool addresses are available");
1541 }
1542 }
1543
1544 /* IPv6 push_ifconfig is a bit problematic - since IPv6 shares the
1545 * pool handling with IPv4, the combination "static IPv4, dynamic IPv6"
1546 * will fail (because no pool will be allocated in this case).
1547 * OTOH, this doesn't make too much sense in reality - and the other
1548 * way round ("dynamic IPv4, static IPv6") or "both static" makes sense
1549 * -> and so it's implemented right now
1550 */
1552 {
1557
1558 msg(M_INFO, "MULTI_sva: push_ifconfig_ipv6 %s/%d",
1561 }
1562
1563 gc_free(&gc);
1564}
1565
1566/*
1567 * Set virtual address environmental variables.
1568 */
1569static void
1571{
1572 setenv_del(mi->context.c2.es, "ifconfig_pool_local_ip");
1573 setenv_del(mi->context.c2.es, "ifconfig_pool_remote_ip");
1574 setenv_del(mi->context.c2.es, "ifconfig_pool_netmask");
1575
1577 {
1578 const int tunnel_type = TUNNEL_TYPE(mi->context.c1.tuntap);
1579 const int tunnel_topology = TUNNEL_TOPOLOGY(mi->context.c1.tuntap);
1580
1581 setenv_in_addr_t(mi->context.c2.es, "ifconfig_pool_remote_ip",
1583
1584 if (tunnel_type == DEV_TYPE_TAP
1585 || (tunnel_type == DEV_TYPE_TUN && tunnel_topology == TOP_SUBNET))
1586 {
1587 setenv_in_addr_t(mi->context.c2.es, "ifconfig_pool_netmask",
1589 }
1590 else if (tunnel_type == DEV_TYPE_TUN)
1591 {
1592 setenv_in_addr_t(mi->context.c2.es, "ifconfig_pool_local_ip",
1594 }
1595 }
1596
1597 setenv_del(mi->context.c2.es, "ifconfig_pool_local_ip6");
1598 setenv_del(mi->context.c2.es, "ifconfig_pool_remote_ip6");
1599 setenv_del(mi->context.c2.es, "ifconfig_pool_ip6_netbits");
1600
1602 {
1603 setenv_in6_addr(mi->context.c2.es, "ifconfig_pool_remote",
1605 setenv_in6_addr(mi->context.c2.es, "ifconfig_pool_local",
1607 setenv_int(mi->context.c2.es, "ifconfig_pool_ip6_netbits",
1609 }
1610}
1611
1612/*
1613 * Called after client-connect script is called
1614 */
1615static void
1616multi_client_connect_post(struct multi_context *m, struct multi_instance *mi, const char *dc_file,
1617 unsigned int *option_types_found)
1618{
1619 /* Did script generate a dynamic config file? */
1620 if (platform_test_file(dc_file))
1621 {
1623 CLIENT_CONNECT_OPT_MASK, option_types_found, mi->context.c2.es);
1624
1625 /*
1626 * If the --client-connect script generates a config file
1627 * with an --ifconfig-push directive, it will override any
1628 * --ifconfig-push directive from the --client-config-dir
1629 * directory or any --ifconfig-pool dynamic address.
1630 */
1633 }
1634}
1635
1636#ifdef ENABLE_PLUGIN
1637
1638/*
1639 * Called after client-connect plug-in is called
1640 */
1641static void
1643 const struct plugin_return *pr, unsigned int *option_types_found)
1644{
1645 struct plugin_return config;
1646
1647 plugin_return_get_column(pr, &config, "config");
1648
1649 /* Did script generate a dynamic config file? */
1650 if (plugin_return_defined(&config))
1651 {
1652 int i;
1653 for (i = 0; i < config.n; ++i)
1654 {
1655 if (config.list[i] && config.list[i]->value)
1656 {
1657 options_string_import(&mi->context.options, config.list[i]->value,
1659 option_types_found, mi->context.c2.es);
1660 }
1661 }
1662
1663 /*
1664 * If the --client-connect script generates a config file
1665 * with an --ifconfig-push directive, it will override any
1666 * --ifconfig-push directive from the --client-config-dir
1667 * directory or any --ifconfig-pool dynamic address.
1668 */
1671 }
1672}
1673
1674#endif /* ifdef ENABLE_PLUGIN */
1675
1676
1677/*
1678 * Called to load management-derived client-connect config
1679 */
1681multi_client_connect_mda(struct multi_context *m, struct multi_instance *mi, bool deferred,
1682 unsigned int *option_types_found)
1683{
1684 /* We never return CC_RET_DEFERRED */
1685 ASSERT(!deferred);
1687#ifdef ENABLE_MANAGEMENT
1688 if (mi->cc_config)
1689 {
1690 struct buffer_entry *be;
1691 for (be = mi->cc_config->head; be != NULL; be = be->next)
1692 {
1693 const char *opt = BSTR(&be->buf);
1695 CLIENT_CONNECT_OPT_MASK, option_types_found, mi->context.c2.es);
1696 }
1697
1698 /*
1699 * If the --client-connect script generates a config file
1700 * with an --ifconfig-push directive, it will override any
1701 * --ifconfig-push directive from the --client-config-dir
1702 * directory or any --ifconfig-pool dynamic address.
1703 */
1706
1707 ret = CC_RET_SUCCEEDED;
1708 }
1709#endif /* ifdef ENABLE_MANAGEMENT */
1710 return ret;
1711}
1712
1713static void
1715{
1716 struct gc_arena gc = gc_new();
1717
1718 /* setenv incoming cert common name for script */
1719 setenv_str(mi->context.c2.es, "common_name", tls_common_name(mi->context.c2.tls_multi, true));
1720
1721 /* setenv client real IP address */
1723
1724 /* setenv client virtual IP address */
1726
1727 /* setenv connection time */
1728 {
1729 const char *created_ascii = time_string(mi->created, 0, false, &gc);
1730 setenv_str(mi->context.c2.es, "time_ascii", created_ascii);
1731 setenv_long_long(mi->context.c2.es, "time_unix", mi->created);
1732 }
1733
1734 gc_free(&gc);
1735}
1736
1743static bool
1745{
1746 struct tls_multi *tls_multi = c->c2.tls_multi;
1747 const char *const peer_info = tls_multi->peer_info;
1748 struct options *o = &c->options;
1749
1750
1751 unsigned int proto = extract_iv_proto(peer_info);
1752 if (proto & IV_PROTO_DATA_V2)
1753 {
1754 tls_multi->use_peer_id = true;
1755 o->use_peer_id = true;
1756 }
1757 else if (dco_enabled(o))
1758 {
1759 msg(M_INFO, "Client does not support DATA_V2. Data channel offloading "
1760 "requires DATA_V2. Dropping client.");
1761 auth_set_client_reason(tls_multi, "Data channel negotiation "
1762 "failed (missing DATA_V2)");
1763 return false;
1764 }
1765
1766 /* Print a warning if we detect the client being in P2P mode and will
1767 * not accept our pushed ciphers */
1768 if (proto & IV_PROTO_NCP_P2P)
1769 {
1770 msg(M_WARN, "Note: peer reports running in P2P mode (no --pull/--client "
1771 "option). It will not negotiate ciphers with this server. "
1772 "Expect this connection to fail.");
1773 }
1774
1775 if (proto & IV_PROTO_REQUEST_PUSH)
1776 {
1777 c->c2.push_request_received = true;
1778 }
1779
1780 if (proto & IV_PROTO_TLS_KEY_EXPORT)
1781 {
1783 }
1784 else if (o->force_key_material_export)
1785 {
1786 msg(M_INFO, "PUSH: client does not support TLS Keying Material "
1787 "Exporters but --force-tls-key-material-export is enabled.");
1788 auth_set_client_reason(tls_multi, "Client incompatible with this "
1789 "server. Keying Material Exporters (RFC 5705) "
1790 "support missing. Upgrade to a client that "
1791 "supports this feature (OpenVPN 2.6.0+).");
1792 return false;
1793 }
1794 if (proto & IV_PROTO_DYN_TLS_CRYPT)
1795 {
1797 }
1798
1799 if (proto & IV_PROTO_CC_EXIT_NOTIFY)
1800 {
1802 }
1803
1804 /* Select cipher if client supports Negotiable Crypto Parameters */
1805
1806 /* if we have already created our key, we cannot *change* our own
1807 * cipher -> so log the fact and push the "what we have now" cipher
1808 * (so the client is always told what we expect it to use)
1809 */
1811 {
1812 msg(M_INFO,
1813 "PUSH: client wants to negotiate cipher (NCP), but "
1814 "server has already generated data channel keys, "
1815 "re-sending previously negotiated cipher '%s'",
1816 o->ciphername);
1817 return true;
1818 }
1819
1820 /*
1821 * Push the first cipher from --data-ciphers to the client that
1822 * the client announces to be supporting.
1823 */
1824 char *push_cipher =
1826 if (push_cipher)
1827 {
1828 /* Enable epoch data key format if supported and AEAD cipher in use */
1830 && cipher_kt_mode_aead(push_cipher))
1831 {
1833 }
1834
1835 o->ciphername = push_cipher;
1836 return true;
1837 }
1838
1839 /* NCP cipher negotiation failed. Try to figure out why exactly it
1840 * failed and give good error messages and potentially do a fallback
1841 * for non NCP clients */
1842 struct gc_arena gc = gc_new();
1843 bool ret = false;
1844
1845 const char *peer_ciphers = tls_peer_ncp_list(peer_info, &gc);
1846 /* If we are in a situation where we know the client ciphers, there is no
1847 * reason to fall back to a cipher that will not be accepted by the other
1848 * side, in this situation we fail the auth*/
1849 if (strlen(peer_ciphers) > 0)
1850 {
1851 msg(M_INFO,
1852 "PUSH: No common cipher between server and client. "
1853 "Server data-ciphers: '%s'%s, client supported ciphers '%s'",
1854 o->ncp_ciphers_conf, ncp_expanded_ciphers(o, &gc), peer_ciphers);
1855 }
1856 else if (tls_multi->remote_ciphername)
1857 {
1858 msg(M_INFO,
1859 "PUSH: No common cipher between server and client. "
1860 "Server data-ciphers: '%s'%s, client supports cipher '%s'",
1862 }
1863 else
1864 {
1865 msg(M_INFO, "PUSH: No NCP or OCC cipher data received from peer.");
1866
1868 {
1869 msg(M_INFO,
1870 "Using data channel cipher '%s' since "
1871 "--data-ciphers-fallback is set.",
1872 o->ciphername);
1873 ret = true;
1874 }
1875 else
1876 {
1877 msg(M_INFO, "Use --data-ciphers-fallback with the cipher the "
1878 "client is using if you want to allow the client to connect");
1879 }
1880 }
1881 if (!ret)
1882 {
1883 auth_set_client_reason(tls_multi, "Data channel cipher negotiation "
1884 "failed (no shared cipher)");
1885 }
1886
1887 gc_free(&gc);
1888 return ret;
1889}
1890
1895static void
1897{
1899 if (!ccs->deferred_ret_file)
1900 {
1901 return;
1902 }
1903
1904 setenv_del(mi->context.c2.es, "client_connect_deferred_file");
1906 {
1907 msg(D_MULTI_ERRORS, "MULTI: problem deleting temporary file: %s", ccs->deferred_ret_file);
1908 }
1909 free(ccs->deferred_ret_file);
1910 ccs->deferred_ret_file = NULL;
1911}
1912
1920static bool
1922{
1924 struct gc_arena gc = gc_new();
1925 const char *fn;
1926
1927 /* Delete file if it already exists */
1929
1931 if (!fn)
1932 {
1933 gc_free(&gc);
1934 return false;
1935 }
1936 ccs->deferred_ret_file = string_alloc(fn, NULL);
1937
1938 setenv_str(mi->context.c2.es, "client_connect_deferred_file", ccs->deferred_ret_file);
1939
1940 gc_free(&gc);
1941 return true;
1942}
1943
1952static enum client_connect_return
1954{
1956 FILE *fp = fopen(ccs->deferred_ret_file, "r");
1957 if (!fp)
1958 {
1959 return CC_RET_SKIPPED;
1960 }
1961
1963 const int c = fgetc(fp);
1964 switch (c)
1965 {
1966 case '0':
1967 ret = CC_RET_FAILED;
1968 break;
1969
1970 case '1':
1971 ret = CC_RET_SUCCEEDED;
1972 break;
1973
1974 case '2':
1975 ret = CC_RET_DEFERRED;
1976 break;
1977
1978 case EOF:
1979 if (feof(fp))
1980 {
1981 ret = CC_RET_SKIPPED;
1982 break;
1983 }
1984
1985 /* Not EOF but other error -> fall through to error state */
1986 default:
1987 /* We received an unknown/unexpected value. Assume failure. */
1988 msg(M_WARN, "WARNING: Unknown/unexpected value in deferred "
1989 "client-connect resultfile");
1990 ret = CC_RET_FAILED;
1991 }
1992 fclose(fp);
1993
1994 return ret;
1995}
1996
2002static void
2004{
2006 if (ccs->config_file)
2007 {
2008 setenv_del(mi->context.c2.es, "client_connect_config_file");
2009 if (!platform_unlink(ccs->config_file))
2010 {
2011 msg(D_MULTI_ERRORS, "MULTI: problem deleting temporary file: %s", ccs->config_file);
2012 }
2013 free(ccs->config_file);
2014 ccs->config_file = NULL;
2015 }
2016}
2017
2025static bool
2027{
2029 struct gc_arena gc = gc_new();
2030 const char *fn;
2031
2032 if (ccs->config_file)
2033 {
2035 }
2036
2038 if (!fn)
2039 {
2040 gc_free(&gc);
2041 return false;
2042 }
2043 ccs->config_file = string_alloc(fn, NULL);
2044
2045 setenv_str(mi->context.c2.es, "client_connect_config_file", ccs->config_file);
2046
2047 gc_free(&gc);
2048 return true;
2049}
2050
2051static enum client_connect_return
2053 bool deferred, unsigned int *option_types_found)
2054{
2056#ifdef ENABLE_PLUGIN
2057 ASSERT(m);
2058 ASSERT(mi);
2059 ASSERT(option_types_found);
2061
2062 /* deprecated callback, use a file for passing back return info */
2063 if (plugin_defined(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_CONNECT))
2064 {
2065 struct argv argv = argv_new();
2066 int call;
2067
2068 if (!deferred)
2069 {
2070 call = OPENVPN_PLUGIN_CLIENT_CONNECT;
2072 {
2073 ret = CC_RET_FAILED;
2074 goto cleanup;
2075 }
2076 }
2077 else
2078 {
2079 call = OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER;
2080 /* the initial call should have created these files */
2081 ASSERT(ccs->config_file);
2083 }
2084
2085 argv_printf(&argv, "%s", ccs->config_file);
2086 int plug_ret = plugin_call(mi->context.plugins, call, &argv, NULL, mi->context.c2.es);
2087 if (plug_ret == OPENVPN_PLUGIN_FUNC_SUCCESS)
2088 {
2089 ret = CC_RET_SUCCEEDED;
2090 }
2091 else if (plug_ret == OPENVPN_PLUGIN_FUNC_DEFERRED)
2092 {
2093 ret = CC_RET_DEFERRED;
2099 }
2100 else
2101 {
2102 msg(M_WARN, "WARNING: client-connect plugin call failed");
2103 ret = CC_RET_FAILED;
2104 }
2105
2106
2112 int file_ret = ccs_test_deferred_ret_file(mi);
2113
2114 if (file_ret == CC_RET_FAILED)
2115 {
2116 ret = CC_RET_FAILED;
2117 }
2118 else if (ret == CC_RET_SUCCEEDED && file_ret == CC_RET_DEFERRED)
2119 {
2120 ret = CC_RET_DEFERRED;
2121 }
2122
2123 /* if we still think we have succeeded, do postprocessing */
2124 if (ret == CC_RET_SUCCEEDED)
2125 {
2126 multi_client_connect_post(m, mi, ccs->config_file, option_types_found);
2127 }
2128cleanup:
2129 argv_free(&argv);
2130
2131 if (ret != CC_RET_DEFERRED)
2132 {
2135 }
2136 }
2137#endif /* ifdef ENABLE_PLUGIN */
2138 return ret;
2139}
2140
2141static enum client_connect_return
2143 bool deferred, unsigned int *option_types_found)
2144{
2146#ifdef ENABLE_PLUGIN
2147 ASSERT(m);
2148 ASSERT(mi);
2149 ASSERT(option_types_found);
2150
2151 int call = deferred ? OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER_V2 : OPENVPN_PLUGIN_CLIENT_CONNECT_V2;
2152 /* V2 callback, use a plugin_return struct for passing back return info */
2153 if (plugin_defined(mi->context.plugins, call))
2154 {
2155 struct plugin_return pr;
2156
2157 plugin_return_init(&pr);
2158
2159 int plug_ret = plugin_call(mi->context.plugins, call, NULL, &pr, mi->context.c2.es);
2160 if (plug_ret == OPENVPN_PLUGIN_FUNC_SUCCESS)
2161 {
2162 multi_client_connect_post_plugin(m, mi, &pr, option_types_found);
2163 ret = CC_RET_SUCCEEDED;
2164 }
2165 else if (plug_ret == OPENVPN_PLUGIN_FUNC_DEFERRED)
2166 {
2167 ret = CC_RET_DEFERRED;
2168 if (!(plugin_defined(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER_V2)))
2169 {
2170 msg(M_WARN, "A plugin that defers from the "
2171 "OPENVPN_PLUGIN_CLIENT_CONNECT_V2 call must also "
2172 "declare support for "
2173 "OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER_V2");
2174 ret = CC_RET_FAILED;
2175 }
2176 }
2177 else
2178 {
2179 msg(M_WARN, "WARNING: client-connect-v2 plugin call failed");
2180 ret = CC_RET_FAILED;
2181 }
2182
2183
2184 plugin_return_free(&pr);
2185 }
2186#endif /* ifdef ENABLE_PLUGIN */
2187 return ret;
2188}
2189
2190static enum client_connect_return
2192 unsigned int *option_types_found)
2193{
2194 ASSERT(mi);
2195 ASSERT(option_types_found);
2198
2200
2201 if (ret == CC_RET_SKIPPED)
2202 {
2203 /*
2204 * Skipped and deferred are equivalent in this context.
2205 * skipped means that the called program has not yet
2206 * written a return status implicitly needing more time
2207 * while deferred is the explicit notification that it
2208 * needs more time
2209 */
2210 ret = CC_RET_DEFERRED;
2211 }
2212
2213 if (ret == CC_RET_SUCCEEDED)
2214 {
2218 }
2219 if (ret == CC_RET_FAILED)
2220 {
2221 msg(M_INFO, "MULTI: deferred --client-connect script returned CC_RET_FAILED");
2224 }
2225 return ret;
2226}
2227
2231static enum client_connect_return
2233 unsigned int *option_types_found)
2234{
2235 if (deferred)
2236 {
2238 }
2239 ASSERT(m);
2240 ASSERT(mi);
2241
2244
2246 {
2247 struct argv argv = argv_new();
2248 struct gc_arena gc = gc_new();
2249
2250 setenv_str(mi->context.c2.es, "script_type", "client-connect");
2251
2253 {
2254 ret = CC_RET_FAILED;
2255 goto cleanup;
2256 }
2257
2259 argv_printf_cat(&argv, "%s", ccs->config_file);
2260
2261 if (openvpn_run_script(&argv, mi->context.c2.es, 0, "--client-connect"))
2262 {
2264 {
2265 ret = CC_RET_DEFERRED;
2266 }
2267 else
2268 {
2269 multi_client_connect_post(m, mi, ccs->config_file, option_types_found);
2270 ret = CC_RET_SUCCEEDED;
2271 }
2272 }
2273 else
2274 {
2275 ret = CC_RET_FAILED;
2276 }
2277cleanup:
2278 if (ret != CC_RET_DEFERRED)
2279 {
2282 }
2283 argv_free(&argv);
2284 gc_free(&gc);
2285 }
2286 return ret;
2287}
2288
2289static bool
2291 struct gc_arena *gc)
2292{
2293 if (!dco_enabled(&mi->context.options))
2294 {
2295 /* DCO not enabled, nothing to do, return sucess */
2296 return true;
2297 }
2298 int ret = dco_multi_add_new_peer(m, mi);
2299 if (ret < 0)
2300 {
2301 msg(D_DCO, "Cannot add peer to DCO for %s: %s (%d)", multi_instance_string(mi, false, gc),
2302 strerror(-ret), ret);
2303 return false;
2304 }
2305
2306 return true;
2307}
2308
2312static bool
2314{
2315 struct frame *frame_fragment = NULL;
2316#ifdef ENABLE_FRAGMENT
2317 if (c->options.ce.fragment)
2318 {
2319 frame_fragment = &c->c2.frame_fragment;
2320 }
2321#endif
2324 frame_fragment, get_link_socket_info(c),
2325 &c->c1.tuntap->dco))
2326 {
2327 msg(D_TLS_ERRORS, "TLS Error: initializing data channel failed");
2328 register_signal(c->sig, SIGUSR1, "process-push-msg-failed");
2329 return false;
2330 }
2331
2332 return true;
2333}
2334
2335static void
2337 const unsigned int option_types_found)
2338{
2339 ASSERT(m);
2340 ASSERT(mi);
2341
2342 struct gc_arena gc = gc_new();
2343 /*
2344 * Process sourced options.
2345 */
2346 do_deferred_options(&mi->context, option_types_found, false);
2347
2348 /*
2349 * make sure we got ifconfig settings from somewhere
2350 */
2352 {
2354 "MULTI: no dynamic or static remote "
2355 "--ifconfig address is available for %s",
2356 multi_instance_string(mi, false, &gc));
2357 }
2358
2359 /*
2360 * make sure that ifconfig settings comply with constraints
2361 */
2363 {
2364 const char *ifconfig_constraint_network =
2366 const char *ifconfig_constraint_netmask =
2368
2369 /* JYFIXME -- this should cause the connection to fail */
2371 "MULTI ERROR: primary virtual IP for %s (%s) "
2372 "violates tunnel network/netmask constraint (%s/%s)",
2373 multi_instance_string(mi, false, &gc),
2375 ifconfig_constraint_network, ifconfig_constraint_netmask);
2376 }
2377
2378 /* set our client's VPN endpoint for status reporting purposes */
2381
2382 /* set context-level authentication flag */
2384
2385 /* Since dco-win maintains iroute routing table (subnet -> peer),
2386 * peer must be added before iroutes. For other platforms it doesn't matter. */
2387
2388 /* authentication complete, calculate dynamic client specific options */
2390 {
2392 }
2393 /* only continue if setting protocol options worked */
2394 else if (!multi_client_setup_dco_initial(m, mi, &gc))
2395 {
2397 }
2398 /* Generate data channel keys only if setting protocol options
2399 * and DCO initial setup has not failed */
2401 {
2403 }
2404
2405 /* dco peer has been added, it is now safe for Windows to add iroutes */
2406
2407 /*
2408 * For routed tunnels, set up internal route to endpoint
2409 * plus add all iroute routes.
2410 */
2412 {
2414 {
2416 msg(D_MULTI_LOW, "MULTI: primary virtual IP for %s: %s",
2417 multi_instance_string(mi, false, &gc),
2419 }
2420
2422 {
2424 /* TODO: find out where addresses are "unlearned"!! */
2425 const char *ifconfig_local_ipv6 =
2427 msg(D_MULTI_LOW, "MULTI: primary virtual IPv6 for %s: %s",
2428 multi_instance_string(mi, false, &gc), ifconfig_local_ipv6);
2429 }
2430
2431 /* add routes locally, pointing to new client, if
2432 * --iroute options have been specified */
2433 multi_add_iroutes(m, mi);
2434
2435 /*
2436 * iroutes represent subnets which are "owned" by a particular
2437 * client. Therefore, do not actually push a route to a client
2438 * if it matches one of the client's iroutes.
2439 */
2441 }
2442 else if (mi->context.options.iroutes)
2443 {
2445 "MULTI: --iroute options rejected for %s -- iroute "
2446 "only works with tun-style tunnels",
2447 multi_instance_string(mi, false, &gc));
2448 }
2449
2450 /* send push reply if ready */
2452 {
2454 }
2455 gc_free(&gc);
2456}
2457
2458static void
2460{
2461 ASSERT(mi->context.c1.tuntap);
2462 /*
2463 * lock down the common name and cert hashes so they can't change
2464 * during future TLS renegotiations
2465 */
2468
2469 /* generate a msg() prefix for this client instance */
2470 generate_prefix(mi);
2471
2472 /* delete instances of previous clients with same common-name */
2473 if (!mi->context.options.duplicate_cn)
2474 {
2475 multi_delete_dup(m, mi);
2476 }
2477
2478 /* reset pool handle to null */
2479 mi->vaddr_handle = -1;
2480
2481 /* do --client-connect setenvs */
2483
2485}
2486
2493static enum client_connect_return
2495 bool deferred, unsigned int *option_types_found)
2496{
2497#ifdef USE_COMP
2498 struct options *o = &mi->context.options;
2499 const char *const peer_info = mi->context.c2.tls_multi->peer_info;
2500
2502 {
2503 if (peer_info && strstr(peer_info, "IV_COMP_STUBv2=1"))
2504 {
2505 push_option(o, "compress stub-v2", M_USAGE);
2506 }
2507 else
2508 {
2509 /* Client is old and does not support STUBv2 but since it
2510 * announced comp-lzo via OCC we assume it uses comp-lzo, so
2511 * switch to that and push the uncompressed variant. */
2512 push_option(o, "comp-lzo no", M_USAGE);
2513 o->comp.alg = COMP_ALG_STUB;
2514 *option_types_found |= OPT_P_COMP;
2515 }
2516 }
2517#endif
2518 return CC_RET_SUCCEEDED;
2519}
2520
2525static enum client_connect_return
2527 unsigned int *option_types_found)
2528{
2529 /* Since we never return a CC_RET_DEFERRED, this indicates a serious
2530 * problem */
2531 ASSERT(!deferred);
2534 {
2535 struct gc_arena gc = gc_new();
2536 const char *ccd_file = NULL;
2537
2538 const char *ccd_client =
2540 tls_common_name(mi->context.c2.tls_multi, false), &gc);
2541
2542 const char *ccd_default =
2544
2545
2546 /* try common-name file */
2547 if (platform_test_file(ccd_client))
2548 {
2549 ccd_file = ccd_client;
2550 }
2551 /* try default file */
2552 else if (platform_test_file(ccd_default))
2553 {
2554 ccd_file = ccd_default;
2555 }
2556
2557 if (ccd_file)
2558 {
2560 CLIENT_CONNECT_OPT_MASK, option_types_found, mi->context.c2.es);
2561 /*
2562 * Select a virtual address from either --ifconfig-push in
2563 * --client-config-dir file or --ifconfig-pool.
2564 */
2566
2568
2569 ret = CC_RET_SUCCEEDED;
2570 }
2571 gc_free(&gc);
2572 }
2573 return ret;
2574}
2575
2577 struct multi_context *m, struct multi_instance *mi, bool from_deferred,
2578 unsigned int *option_types_found);
2579
2589
2594static bool
2596{
2597 struct tls_multi *multi = mi->context.c2.tls_multi;
2598 struct options *options = &mi->context.options;
2599 struct tls_session *session = &multi->session[TM_ACTIVE];
2600
2601 if (!multi->locked_username)
2602 {
2603 msg(D_MULTI_ERRORS, "MULTI: Ignoring override-username as no "
2604 "user/password method is enabled. Enable "
2605 "--management-client-auth, --auth-user-pass-verify, or a "
2606 "plugin with user/password verify capability.");
2607 return false;
2608 }
2609
2610 if (!multi->locked_original_username
2611 && strcmp(multi->locked_username, options->override_username) != 0)
2612 {
2613 /* Check if the username length is acceptable */
2615 {
2616 return false;
2617 }
2618
2620 multi->locked_username = strdup(options->override_username);
2621
2622 /* Override also the common name if username should be set as common
2623 * name */
2624 if ((session->opt->ssl_flags & SSLF_USERNAME_AS_COMMON_NAME))
2625 {
2627 free(multi->locked_cn);
2628 multi->locked_cn = NULL;
2629 tls_lock_common_name(multi);
2630 }
2631
2632 /* Regenerate the auth-token if enabled */
2633 if (multi->auth_token_initial)
2634 {
2635 struct user_pass up;
2636 CLEAR(up);
2637 strncpynt(up.username, multi->locked_username, sizeof(up.username));
2638
2639 generate_auth_token(&up, multi);
2640 }
2641
2643 "MULTI: Note, override-username changes username "
2644 "from '%s' to '%s'",
2646 }
2647 return true;
2648}
2649/*
2650 * Called as soon as the SSL/TLS connection is authenticated.
2651 *
2652 * Will collect the client specific configuration from the different
2653 * sources like ccd files, connect plugins and management interface.
2654 *
2655 * This method starts with cas_context CAS_PENDING and will move the
2656 * state machine to either CAS_SUCCEEDED on success or
2657 * CAS_FAILED/CAS_PARTIAL on failure.
2658 *
2659 * Instance-specific directives to be processed (CLIENT_CONNECT_OPT_MASK)
2660 * include:
2661 *
2662 * iroute start-ip end-ip
2663 * ifconfig-push local remote-netmask
2664 * push
2665 *
2666 *
2667 */
2668static void
2670{
2671 /* We are only called for the CAS_PENDING_x states, so we
2672 * can ignore other states here */
2673 bool from_deferred = (mi->context.c2.tls_multi->multi_state != CAS_PENDING);
2674
2675 int *cur_handler_index = &mi->client_connect_defer_state.cur_handler_index;
2676 unsigned int *option_types_found = &mi->client_connect_defer_state.option_types_found;
2677
2678 /* We are called for the first time */
2679 if (!from_deferred)
2680 {
2681 *cur_handler_index = 0;
2682 *option_types_found = 0;
2683 /* Initially we have no handler that has returned a result */
2685
2687 }
2688
2689 bool cc_succeeded = true;
2690
2691 while (cc_succeeded && client_connect_handlers[*cur_handler_index] != NULL)
2692 {
2693 enum client_connect_return ret;
2694 ret = client_connect_handlers[*cur_handler_index](m, mi, from_deferred, option_types_found);
2695
2696 from_deferred = false;
2697
2698 switch (ret)
2699 {
2700 case CC_RET_SUCCEEDED:
2701 /*
2702 * Remember that we already had at least one handler
2703 * returning a result should we go to into deferred state
2704 */
2705 mi->context.c2.tls_multi->multi_state = CAS_PENDING_DEFERRED_PARTIAL;
2706 break;
2707
2708 case CC_RET_SKIPPED:
2709 /*
2710 * Move on with the next handler without modifying any
2711 * other state
2712 */
2713 break;
2714
2715 case CC_RET_DEFERRED:
2716 /*
2717 * we already set multi_status to DEFERRED_RESULT or
2718 * DEFERRED_NO_RESULT. We just return
2719 * from the function as having multi_status
2720 */
2721 return;
2722
2723 case CC_RET_FAILED:
2724 /*
2725 * One handler failed. We abort the chain and set the final
2726 * result to failed
2727 */
2728 cc_succeeded = false;
2729 break;
2730
2731 default:
2732 ASSERT(0);
2733 }
2734
2735 /*
2736 * Check for "disable" directive in client-config-dir file
2737 * or config file generated by --client-connect script.
2738 */
2739 if (mi->context.options.disable)
2740 {
2741 msg(D_MULTI_ERRORS, "MULTI: client has been rejected due to "
2742 "'disable' directive");
2743 cc_succeeded = false;
2744 }
2745
2746 (*cur_handler_index)++;
2747 }
2748
2750 {
2751 if (!override_locked_username(mi))
2752 {
2753 cc_succeeded = false;
2754 }
2755 }
2756
2757 /* Check if we have forbidding options in the current mode */
2758 if (dco_enabled(&mi->context.options)
2760 {
2761 msg(D_MULTI_ERRORS, "MULTI: client has been rejected due to incompatible DCO options");
2762 cc_succeeded = false;
2763 }
2764
2766 {
2767 msg(D_MULTI_ERRORS, "MULTI: client has been rejected due to invalid compression options");
2768 cc_succeeded = false;
2769 }
2770
2771 if (cc_succeeded)
2772 {
2773 multi_client_connect_late_setup(m, mi, *option_types_found);
2774 }
2775 else
2776 {
2777 /* run the disconnect script if we had a connect script that
2778 * did not fail */
2780 {
2782 }
2783
2785 }
2786
2787 /* increment number of current authenticated clients */
2788 ++m->n_clients;
2789 --mi->n_clients_delta;
2790
2791#ifdef ENABLE_MANAGEMENT
2792 if (management)
2793 {
2795 mi->context.c2.es);
2796 }
2797#endif
2798}
2799
2800#ifdef ENABLE_ASYNC_PUSH
2801/*
2802 * Called when inotify event is fired, which happens when acf
2803 * or connect-status file is closed or deleted.
2804 * Continues authentication and sends push_reply
2805 * (or be deferred again by client-connect)
2806 */
2807void
2808multi_process_file_closed(struct multi_context *m, const unsigned int mpp_flags)
2809{
2810 char buffer[INOTIFY_EVENT_BUFFER_SIZE];
2811 size_t buffer_i = 0;
2812 int r = read(m->top.c2.inotify_fd, buffer, INOTIFY_EVENT_BUFFER_SIZE);
2813
2814 while (buffer_i < r)
2815 {
2816 /* parse inotify events */
2817 struct inotify_event *pevent = (struct inotify_event *)&buffer[buffer_i];
2818 size_t event_size = sizeof(struct inotify_event) + pevent->len;
2819 buffer_i += event_size;
2820
2821 msg(D_MULTI_DEBUG, "MULTI: modified fd %d, mask %d", pevent->wd, pevent->mask);
2822
2823 struct multi_instance *mi =
2824 hash_lookup(m->inotify_watchers, (void *)(unsigned long)pevent->wd);
2825
2826 if (pevent->mask & IN_CLOSE_WRITE)
2827 {
2828 if (mi)
2829 {
2830 /* continue authentication, perform NCP negotiation and send push_reply */
2831 multi_process_post(m, mi, mpp_flags);
2832 }
2833 else
2834 {
2835 msg(D_MULTI_ERRORS, "MULTI: multi_instance not found!");
2836 }
2837 }
2838 else if (pevent->mask & IN_IGNORED)
2839 {
2840 /* this event is _always_ fired when watch is removed or file is deleted */
2841 if (mi)
2842 {
2843 hash_remove(m->inotify_watchers, (void *)(unsigned long)pevent->wd);
2844 mi->inotify_watch = -1;
2845 }
2846 }
2847 else
2848 {
2849 msg(D_MULTI_ERRORS, "MULTI: unknown mask %d", pevent->mask);
2850 }
2851 }
2852}
2853#endif /* ifdef ENABLE_ASYNC_PUSH */
2854
2855/*
2856 * Add a mbuf buffer to a particular
2857 * instance.
2858 */
2859void
2860multi_add_mbuf(struct multi_context *m, struct multi_instance *mi, struct mbuf_buffer *mb)
2861{
2862 if (multi_output_queue_ready(m, mi))
2863 {
2864 struct mbuf_item item;
2865 item.buffer = mb;
2866 item.instance = mi;
2867 mbuf_add_item(m->mbuf, &item);
2868 }
2869 else
2870 {
2871 msg(D_MULTI_DROPPED, "MULTI: packet dropped due to output saturation (multi_add_mbuf)");
2872 }
2873}
2874
2875/*
2876 * Add a packet to a client instance output queue.
2877 */
2878static inline void
2879multi_unicast(struct multi_context *m, const struct buffer *buf, struct multi_instance *mi)
2880{
2881 struct mbuf_buffer *mb;
2882
2883 if (BLEN(buf) > 0)
2884 {
2885 mb = mbuf_alloc_buf(buf);
2886 mb->flags = MF_UNICAST;
2887 multi_add_mbuf(m, mi, mb);
2888 mbuf_free_buf(mb);
2889 }
2890}
2891
2892/*
2893 * Broadcast a packet to all clients.
2894 */
2895static void
2896multi_bcast(struct multi_context *m, const struct buffer *buf,
2897 const struct multi_instance *sender_instance, uint16_t vid)
2898{
2899 struct hash_iterator hi;
2900 struct hash_element *he;
2901 struct multi_instance *mi;
2902 struct mbuf_buffer *mb;
2903
2904 if (BLEN(buf) > 0)
2905 {
2906#ifdef MULTI_DEBUG_EVENT_LOOP
2907 printf("BCAST len=%d\n", BLEN(buf));
2908#endif
2909 mb = mbuf_alloc_buf(buf);
2910 hash_iterator_init(m->iter, &hi);
2911
2912 while ((he = hash_iterator_next(&hi)))
2913 {
2914 mi = (struct multi_instance *)he->value;
2915 if (mi != sender_instance && !mi->halt)
2916 {
2917 if (vid != 0 && vid != mi->context.options.vlan_pvid)
2918 {
2919 continue;
2920 }
2921 multi_add_mbuf(m, mi, mb);
2922 }
2923 }
2924
2925 hash_iterator_free(&hi);
2926 mbuf_free_buf(mb);
2927 }
2928}
2929
2930/*
2931 * Given a time delta, indicating that we wish to be
2932 * awoken by the scheduler at time now + delta, figure
2933 * a sigma parameter (in microseconds) that represents
2934 * a sort of fuzz factor around delta, so that we're
2935 * really telling the scheduler to wake us up any time
2936 * between now + delta - sigma and now + delta + sigma.
2937 *
2938 * The sigma parameter helps the scheduler to run more efficiently.
2939 * Sigma should be no larger than TV_WITHIN_SIGMA_MAX_USEC
2940 */
2941static inline unsigned int
2942compute_wakeup_sigma(const struct timeval *delta)
2943{
2944 if (delta->tv_sec < 1)
2945 {
2946 /* if < 1 sec, fuzz = # of microseconds / 8 */
2947 return delta->tv_usec >> 3;
2948 }
2949 else
2950 {
2951 /* if < 10 minutes, fuzz = 13.1% of timeout */
2952 if (delta->tv_sec < 600)
2953 {
2954 return delta->tv_sec << 17;
2955 }
2956 else
2957 {
2958 return 120000000; /* if >= 10 minutes, fuzz = 2 minutes */
2959 }
2960 }
2961}
2962
2963static void
2965{
2966 /* calculate an absolute wakeup time */
2967 ASSERT(!openvpn_gettimeofday(&mi->wakeup, NULL));
2968 tv_add(&mi->wakeup, &mi->context.c2.timeval);
2969
2970 /* tell scheduler to wake us up at some point in the future */
2971 schedule_add_entry(m->schedule, (struct schedule_entry *)mi, &mi->wakeup,
2973}
2974
2975#if defined(ENABLE_ASYNC_PUSH)
2976static void
2977add_inotify_file_watch(struct multi_context *m, struct multi_instance *mi, int inotify_fd,
2978 const char *file)
2979{
2980 /* watch acf file */
2981 long watch_descriptor = inotify_add_watch(inotify_fd, file, IN_CLOSE_WRITE | IN_ONESHOT);
2982 if (watch_descriptor >= 0)
2983 {
2984 if (mi->inotify_watch != -1)
2985 {
2986 hash_remove(m->inotify_watchers, (void *)(unsigned long)mi->inotify_watch);
2987 }
2988 hash_add(m->inotify_watchers, (const uintptr_t *)watch_descriptor, mi, true);
2989 mi->inotify_watch = watch_descriptor;
2990 }
2991 else
2992 {
2993 msg(M_NONFATAL | M_ERRNO, "MULTI: inotify_add_watch error");
2994 }
2995}
2996#endif /* if defined(ENABLE_ASYNC_PUSH) */
2997
2998/*
2999 * Figure instance-specific timers, convert
3000 * earliest to absolute time in mi->wakeup,
3001 * call scheduler with our future wakeup time.
3002 *
3003 * Also close context on signal.
3004 */
3005bool
3006multi_process_post(struct multi_context *m, struct multi_instance *mi, const unsigned int flags)
3007{
3008 bool ret = true;
3009
3010 if (!IS_SIG(&mi->context)
3011 && ((flags & MPP_PRE_SELECT)
3012 || ((flags & MPP_CONDITIONAL_PRE_SELECT) && !ANY_OUT(&mi->context))))
3013 {
3014#if defined(ENABLE_ASYNC_PUSH)
3015 bool was_unauthenticated = true;
3016 struct key_state *ks = NULL;
3017 if (mi->context.c2.tls_multi)
3018 {
3020 was_unauthenticated = (ks->authenticated == KS_AUTH_FALSE);
3021 }
3022#endif
3023
3024 /* figure timeouts and fetch possible outgoing
3025 * to_link packets (such as ping or TLS control) */
3026 pre_select(&mi->context);
3027
3028#if defined(ENABLE_ASYNC_PUSH)
3029 /*
3030 * if we see the state transition from unauthenticated to deferred
3031 * and an auth_control_file, we assume it got just added and add
3032 * inotify watch to that file
3033 */
3034 if (ks && ks->plugin_auth.auth_control_file && was_unauthenticated
3035 && (ks->authenticated == KS_AUTH_DEFERRED))
3036 {
3037 add_inotify_file_watch(m, mi, m->top.c2.inotify_fd, ks->plugin_auth.auth_control_file);
3038 }
3039 if (ks && ks->script_auth.auth_control_file && was_unauthenticated
3040 && (ks->authenticated == KS_AUTH_DEFERRED))
3041 {
3042 add_inotify_file_watch(m, mi, m->top.c2.inotify_fd, ks->script_auth.auth_control_file);
3043 }
3044#endif
3045
3046 if (!IS_SIG(&mi->context))
3047 {
3048 /* connection is "established" when SSL/TLS key negotiation succeeds
3049 * and (if specified) auth user/pass succeeds */
3050
3052 {
3054 }
3055#if defined(ENABLE_ASYNC_PUSH)
3058 {
3059 add_inotify_file_watch(m, mi, m->top.c2.inotify_fd,
3061 }
3062#endif
3063 /* tell scheduler to wake us up at some point in the future */
3065 }
3066 }
3067
3068 if (IS_SIG(&mi->context))
3069 {
3070 if (flags & MPP_CLOSE_ON_SIGNAL)
3071 {
3073 ret = false;
3074 }
3075 }
3076 else
3077 {
3078 /* continue to pend on output? */
3079 multi_set_pending(m, ANY_OUT(&mi->context) ? mi : NULL);
3080
3081#ifdef MULTI_DEBUG_EVENT_LOOP
3082 printf("POST %s[%d] to=%d lo=%d/%d w=%" PRIi64 "/%ld\n", id(mi), (int)(mi == m->pending),
3083 mi ? mi->context.c2.to_tun.len : -1, mi ? mi->context.c2.to_link.len : -1,
3084 (mi && mi->context.c2.fragment) ? mi->context.c2.fragment->outgoing.len : -1,
3085 (int64_t)mi->context.c2.timeval.tv_sec, (long)mi->context.c2.timeval.tv_usec);
3086#endif
3087 }
3088
3089 if ((flags & MPP_RECORD_TOUCH) && m->mpp_touched)
3090 {
3091 *m->mpp_touched = mi;
3092 }
3093
3094 return ret;
3095}
3096
3105static void
3107{
3108 struct mroute_addr real = { 0 };
3109 struct hash *hash = m->hash;
3110 struct gc_arena gc = gc_new();
3111
3112 if (mi->real.type & MR_WITH_PROTO)
3113 {
3114 real.type |= MR_WITH_PROTO;
3115 real.proto = sock->info.proto;
3116 }
3117
3118 if (!mroute_extract_openvpn_sockaddr(&real, &m->top.c2.from.dest, true))
3119 {
3120 goto done;
3121 }
3122
3123 const uint32_t hv = hash_value(hash, &real);
3124 struct hash_bucket *bucket = hash_bucket(hash, hv);
3125
3126 /* make sure that we don't float to an address taken by another client */
3127 struct hash_element *he = hash_lookup_fast(hash, bucket, &real, hv);
3128 if (he)
3129 {
3130 struct multi_instance *ex_mi = (struct multi_instance *)he->value;
3131
3132 struct tls_multi *m1 = mi->context.c2.tls_multi;
3133 struct tls_multi *m2 = ex_mi->context.c2.tls_multi;
3134
3135 /* do not float if target address is taken by client with another cert */
3137 {
3138 msg(D_MULTI_LOW, "Disallow float to an address taken by another client %s",
3139 multi_instance_string(ex_mi, false, &gc));
3140
3141 mi->context.c2.buf.len = 0;
3142
3143 goto done;
3144 }
3145
3146 /* It doesn't make sense to let a peer float to the address it already
3147 * has, so we disallow it. This can happen if a DCO netlink notification
3148 * gets lost and we miss a floating step.
3149 */
3150 if (m1->peer_id == m2->peer_id)
3151 {
3152 msg(M_WARN,
3153 "disallowing peer %" PRIu32 " (%s) from floating to "
3154 "its own address (%s)",
3155 m1->peer_id, tls_common_name(mi->context.c2.tls_multi, false),
3156 mroute_addr_print(&mi->real, &gc));
3157 goto done;
3158 }
3159
3161 "closing instance %s due to float collision with %s "
3162 "using the same certificate",
3163 multi_instance_string(ex_mi, false, &gc), multi_instance_string(mi, false, &gc));
3164 multi_close_instance(m, ex_mi, false);
3165 }
3166
3167 msg(D_MULTI_MEDIUM, "peer %" PRIu32 " (%s) floated from %s to %s",
3171
3172 /* remove old address from hash table before changing address */
3173 ASSERT(hash_remove(m->hash, &mi->real));
3174 ASSERT(hash_remove(m->iter, &mi->real));
3175
3176 /* change external network address of the remote peer */
3177 mi->real = real;
3178 generate_prefix(mi);
3179
3180 mi->context.c2.from = m->top.c2.from;
3181 mi->context.c2.to_link_addr = &mi->context.c2.from;
3182
3183 /* inherit parent link_socket and link_socket_info */
3184 mi->context.c2.link_sockets[0] = sock;
3186
3188
3189 ASSERT(hash_add(m->hash, &mi->real, mi, false));
3190 ASSERT(hash_add(m->iter, &mi->real, mi, false));
3191
3192#ifdef ENABLE_MANAGEMENT
3193 ASSERT(hash_add(m->cid_hash, &mi->context.c2.mda_context.cid, mi, true));
3194#endif
3195
3196done:
3197 gc_free(&gc);
3198}
3199
3200/*
3201 * Called when an instance should be closed due to the
3202 * reception of a soft signal.
3203 */
3204void
3206{
3207 remap_signal(&mi->context);
3208 set_prefix(mi);
3209 print_signal(mi->context.sig, "client-instance", D_MULTI_LOW);
3210 clear_prefix();
3211 multi_close_instance(m, mi, false);
3212}
3213
3214#if (defined(ENABLE_DCO) && (defined(TARGET_LINUX) || defined(TARGET_FREEBSD))) \
3215 || defined(ENABLE_MANAGEMENT)
3216static void
3217multi_signal_instance(struct multi_context *m, struct multi_instance *mi, const int sig)
3218{
3219 mi->context.sig->signal_received = sig;
3221}
3222#endif
3223
3224#if defined(ENABLE_DCO)
3225static void
3226process_incoming_del_peer(struct multi_context *m, struct multi_instance *mi, dco_context_t *dco)
3227{
3228 const char *reason = "ovpn-dco: unknown reason";
3229 switch (dco->dco_del_peer_reason)
3230 {
3232 reason = "ovpn-dco: ping expired";
3233 break;
3234
3236 reason = "ovpn-dco: transport error";
3237 break;
3238
3240 reason = "ovpn-dco: transport disconnected";
3241 break;
3242
3244 /* We assume that is ourselves. Unfortunately, sometimes these
3245 * events happen with enough delay that they can have an order of
3246 *
3247 * dco_del_peer x
3248 * [new client connecting]
3249 * dco_new_peer x
3250 * event from dco_del_peer arrives.
3251 *
3252 * if we do not ignore this we get desynced with the kernel
3253 * since we assume the peer-id is free again. The other way would
3254 * be to send a dco_del_peer again
3255 */
3256 return;
3257 }
3258
3259 /* When kernel already deleted the peer, the socket is no longer
3260 * installed, and we do not need to clean up the state in the kernel */
3261 mi->context.c2.tls_multi->dco_peer_id = -1;
3262 mi->context.sig->signal_text = reason;
3263 multi_signal_instance(m, mi, SIGTERM);
3264}
3265
3266bool
3268{
3269 dco_context_t *dco = &m->top.c1.tuntap->dco;
3270
3271 struct multi_instance *mi = NULL;
3272
3273 int ret = dco_do_read(&m->top.c1.tuntap->dco);
3274
3275 int peer_id = dco->dco_message_peer_id;
3276
3277 /* no peer-specific message delivered -> nothing to process.
3278 * bail out right away
3279 */
3280 if (peer_id < 0)
3281 {
3282 return ret > 0;
3283 }
3284
3285 if ((peer_id < m->max_clients) && (m->instances[peer_id]))
3286 {
3287 mi = m->instances[peer_id];
3288 set_prefix(mi);
3289 if (dco->dco_message_type == OVPN_CMD_DEL_PEER)
3290 {
3291 process_incoming_del_peer(m, mi, dco);
3292 }
3293 else if (dco->dco_message_type == OVPN_CMD_FLOAT_PEER)
3294 {
3295 ASSERT(mi->context.c2.link_sockets[0]);
3297 &m->top.c2.from.dest,
3298 (struct sockaddr *)&dco->dco_float_peer_ss);
3300 CLEAR(dco->dco_float_peer_ss);
3301 }
3302 else if (dco->dco_message_type == OVPN_CMD_SWAP_KEYS)
3303 {
3305 }
3306 clear_prefix();
3307 }
3308 else
3309 {
3310 msglvl_t msglevel = D_DCO;
3311 if (dco->dco_message_type == OVPN_CMD_DEL_PEER
3312 && dco->dco_del_peer_reason == OVPN_DEL_PEER_REASON_USERSPACE)
3313 {
3314 /* we receive OVPN_CMD_DEL_PEER message with reason USERSPACE
3315 * after we kill the peer ourselves. This peer may have already
3316 * been deleted, so we end up here.
3317 * In this case, print the following debug message with DCO_DEBUG
3318 * level only to avoid polluting the standard DCO level with this
3319 * harmless event.
3320 */
3321 msglevel = D_DCO_DEBUG;
3322 }
3323 msg(msglevel,
3324 "Received DCO message for unknown peer-id: %d, "
3325 "type %d, del_peer_reason %d",
3326 peer_id, dco->dco_message_type, dco->dco_del_peer_reason);
3327 }
3328
3329 dco->dco_message_type = 0;
3330 dco->dco_message_peer_id = -1;
3331 dco->dco_del_peer_reason = -1;
3332 return ret > 0;
3333}
3334#endif /* if defined(ENABLE_DCO) */
3335
3336/*
3337 * Process packets in the TCP/UDP socket -> TUN/TAP interface direction,
3338 * i.e. client -> server direction.
3339 */
3340bool
3342 const unsigned int mpp_flags, struct link_socket *sock)
3343{
3344 struct gc_arena gc = gc_new();
3345
3346 struct context *c;
3347 struct mroute_addr src, dest;
3348 unsigned int mroute_flags;
3349 struct multi_instance *mi;
3350 bool ret = true;
3351 bool floated = false;
3352
3353 if (m->pending)
3354 {
3355 return true;
3356 }
3357
3358 if (!instance)
3359 {
3360#ifdef MULTI_DEBUG_EVENT_LOOP
3361 printf("TCP/UDP -> TUN [%d]\n", BLEN(&m->top.c2.buf));
3362#endif
3363 multi_set_pending(m, multi_get_create_instance_udp(m, &floated, sock));
3364 }
3365 else
3366 {
3367 multi_set_pending(m, instance);
3368 }
3369
3370 if (m->pending)
3371 {
3372 set_prefix(m->pending);
3373
3374 /* get instance context */
3375 c = &m->pending->context;
3376
3377 if (!instance)
3378 {
3379 /* transfer packet pointer from top-level context buffer to instance */
3380 c->c2.buf = m->top.c2.buf;
3381
3382 /* transfer from-addr from top-level context buffer to instance */
3383 if (!floated)
3384 {
3385 c->c2.from = m->top.c2.from;
3386 }
3387 }
3388
3389 if (BLEN(&c->c2.buf) > 0)
3390 {
3391 struct link_socket_info *lsi;
3392 const uint8_t *orig_buf;
3393
3394 /* decrypt in instance context */
3395
3396 lsi = &sock->info;
3397 orig_buf = c->c2.buf.data;
3398 if (process_incoming_link_part1(c, lsi, floated))
3399 {
3400 /* nonzero length means that we have a valid, decrypted packed */
3401 if (floated && c->c2.buf.len > 0)
3402 {
3403 multi_process_float(m, m->pending, sock);
3404 }
3405
3406 process_incoming_link_part2(c, lsi, orig_buf);
3407 }
3408
3410 {
3411 /* extract packet source and dest addresses */
3412 mroute_flags =
3414
3415 /* drop packet if extract failed */
3416 if (!(mroute_flags & MROUTE_EXTRACT_SUCCEEDED))
3417 {
3418 c->c2.to_tun.len = 0;
3419 }
3420 /* make sure that source address is associated with this client */
3421 else if (multi_get_instance_by_virtual_addr(m, &src, true) != m->pending)
3422 {
3423 /* IPv6 link-local address (fe80::xxx)? */
3424 if ((src.type & MR_ADDR_MASK) == MR_ADDR_IPV6
3425 && IN6_IS_ADDR_LINKLOCAL(&src.v6.addr))
3426 {
3427 /* do nothing, for now. TODO: add address learning */
3428 }
3429 else
3430 {
3432 "MULTI: bad source address from client [%s], packet dropped",
3433 mroute_addr_print(&src, &gc));
3434 }
3435 c->c2.to_tun.len = 0;
3436 }
3437 /* client-to-client communication enabled? */
3438 else if (m->enable_c2c)
3439 {
3440 /* multicast? */
3441 if (mroute_flags & MROUTE_EXTRACT_MCAST)
3442 {
3443 /* for now, treat multicast as broadcast */
3444 multi_bcast(m, &c->c2.to_tun, m->pending, 0);
3445 }
3446 else /* possible client to client routing */
3447 {
3448 ASSERT(!(mroute_flags & MROUTE_EXTRACT_BCAST));
3449 mi = multi_get_instance_by_virtual_addr(m, &dest, true);
3450
3451 /* if dest addr is a known client, route to it */
3452 if (mi)
3453 {
3454 {
3455 multi_unicast(m, &c->c2.to_tun, mi);
3457 }
3458 c->c2.to_tun.len = 0;
3459 }
3460 }
3461 }
3462 }
3463 else if (TUNNEL_TYPE(m->top.c1.tuntap) == DEV_TYPE_TAP)
3464 {
3465 uint16_t vid = 0;
3466
3467 if (m->top.options.vlan_tagging)
3468 {
3469 if (vlan_is_tagged(&c->c2.to_tun))
3470 {
3471 /* Drop VLAN-tagged frame. */
3472 msg(D_VLAN_DEBUG, "dropping incoming VLAN-tagged frame");
3473 c->c2.to_tun.len = 0;
3474 }
3475 else
3476 {
3477 vid = c->options.vlan_pvid;
3478 }
3479 }
3480 /* extract packet source and dest addresses */
3481 mroute_flags =
3482 mroute_extract_addr_from_packet(&src, &dest, vid, &c->c2.to_tun, DEV_TYPE_TAP);
3483
3484 if (mroute_flags & MROUTE_EXTRACT_SUCCEEDED)
3485 {
3486 if (multi_learn_addr(m, m->pending, &src, 0) == m->pending)
3487 {
3488 /* check for broadcast */
3489 if (m->enable_c2c)
3490 {
3491 if (mroute_flags & (MROUTE_EXTRACT_BCAST | MROUTE_EXTRACT_MCAST))
3492 {
3493 multi_bcast(m, &c->c2.to_tun, m->pending, vid);
3494 }
3495 else /* try client-to-client routing */
3496 {
3497 mi = multi_get_instance_by_virtual_addr(m, &dest, false);
3498
3499 /* if dest addr is a known client, route to it */
3500 if (mi)
3501 {
3502 multi_unicast(m, &c->c2.to_tun, mi);
3504 c->c2.to_tun.len = 0;
3505 }
3506 }
3507 }
3508 }
3509 else
3510 {
3512 "MULTI: bad source address from client [%s], packet dropped",
3513 mroute_addr_print(&src, &gc));
3514 c->c2.to_tun.len = 0;
3515 }
3516 }
3517 else
3518 {
3519 c->c2.to_tun.len = 0;
3520 }
3521 }
3522 }
3523
3524 /* postprocess and set wakeup */
3525 ret = multi_process_post(m, m->pending, mpp_flags);
3526
3527 clear_prefix();
3528 }
3529
3530 gc_free(&gc);
3531 return ret;
3532}
3533
3534/*
3535 * Process packets in the TUN/TAP interface -> TCP/UDP socket direction,
3536 * i.e. server -> client direction.
3537 */
3538bool
3539multi_process_incoming_tun(struct multi_context *m, const unsigned int mpp_flags)
3540{
3541 bool ret = true;
3542
3543 if (BLEN(&m->top.c2.buf) > 0)
3544 {
3545 unsigned int mroute_flags;
3546 struct mroute_addr src = { 0 }, dest = { 0 };
3547 const int dev_type = TUNNEL_TYPE(m->top.c1.tuntap);
3548 int16_t vid = 0;
3549
3550#ifdef MULTI_DEBUG_EVENT_LOOP
3551 printf("TUN -> TCP/UDP [%d]\n", BLEN(&m->top.c2.buf));
3552#endif
3553
3554 if (m->pending)
3555 {
3556 return true;
3557 }
3558
3559 if (dev_type == DEV_TYPE_TAP && m->top.options.vlan_tagging)
3560 {
3561 vid = vlan_decapsulate(&m->top, &m->top.c2.buf);
3562 if (vid < 0)
3563 {
3564 return false;
3565 }
3566 }
3567
3568 /*
3569 * Route an incoming tun/tap packet to
3570 * the appropriate multi_instance object.
3571 */
3572
3573 mroute_flags = mroute_extract_addr_from_packet(&src, &dest, vid, &m->top.c2.buf, dev_type);
3574
3576 {
3577 struct context *c;
3578
3579 /* broadcast or multicast dest addr? */
3580 if (mroute_flags & (MROUTE_EXTRACT_BCAST | MROUTE_EXTRACT_MCAST))
3581 {
3582 /* for now, treat multicast as broadcast */
3583 multi_bcast(m, &m->top.c2.buf, NULL, vid);
3584 }
3585 else
3586 {
3588 m, multi_get_instance_by_virtual_addr(m, &dest, dev_type == DEV_TYPE_TUN));
3589
3590 if (m->pending)
3591 {
3592 /* get instance context */
3593 c = &m->pending->context;
3594
3595 set_prefix(m->pending);
3596
3597 {
3599 {
3600 /* transfer packet pointer from top-level context buffer to instance */
3601 c->c2.buf = m->top.c2.buf;
3602 }
3603 else
3604 {
3605 /* drop packet */
3607 "MULTI: packet dropped due to output saturation (multi_process_incoming_tun)");
3608 buf_reset_len(&c->c2.buf);
3609 }
3610 }
3611
3612 /* encrypt in instance context */
3614
3615 /* postprocess and set wakeup */
3616 ret = multi_process_post(m, m->pending, mpp_flags);
3617
3618 clear_prefix();
3619 }
3620 }
3621 }
3622 }
3623 return ret;
3624}
3625
3626/*
3627 * Process a possible client-to-client/bcast/mcast message in the
3628 * queue.
3629 */
3630struct multi_instance *
3632{
3633 struct mbuf_item item;
3634
3635 if (mbuf_extract_item(ms, &item)) /* cleartext IP packet */
3636 {
3637 unsigned int pip_flags = PIPV4_PASSTOS | PIPV6_ICMP_NOHOST_SERVER;
3638
3639 set_prefix(item.instance);
3640 item.instance->context.c2.buf = item.buffer->buf;
3641 if (item.buffer->flags
3642 & MF_UNICAST) /* --mssfix doesn't make sense for broadcast or multicast */
3643 {
3644 pip_flags |= PIP_MSSFIX;
3645 }
3646 process_ip_header(&item.instance->context, pip_flags, &item.instance->context.c2.buf,
3647 item.instance->context.c2.link_sockets[0]);
3648 encrypt_sign(&item.instance->context, true);
3649 mbuf_free_buf(item.buffer);
3650
3651 dmsg(D_MULTI_DEBUG, "MULTI: C2C/MCAST/BCAST");
3652
3653 clear_prefix();
3654 return item.instance;
3655 }
3656 else
3657 {
3658 return NULL;
3659 }
3660}
3661
3662/*
3663 * Called when an I/O wait times out. Usually means that a particular
3664 * client instance object needs timer-based service.
3665 */
3666bool
3667multi_process_timeout(struct multi_context *m, const unsigned int mpp_flags)
3668{
3669 bool ret = true;
3670
3671#ifdef MULTI_DEBUG_EVENT_LOOP
3672 printf("%s -> TIMEOUT\n", id(m->earliest_wakeup));
3673#endif
3674
3675 /* instance marked for wakeup? */
3676 if (m->earliest_wakeup)
3677 {
3679 {
3683 }
3684 else
3685 {
3687 ret = multi_process_post(m, m->earliest_wakeup, mpp_flags);
3688 clear_prefix();
3689 }
3690 m->earliest_wakeup = NULL;
3691 }
3692 return ret;
3693}
3694
3695/*
3696 * Drop a TUN/TAP outgoing packet..
3697 */
3698void
3699multi_process_drop_outgoing_tun(struct multi_context *m, const unsigned int mpp_flags)
3700{
3701 struct multi_instance *mi = m->pending;
3702
3703 ASSERT(mi);
3704
3705 set_prefix(mi);
3706
3707 msg(D_MULTI_ERRORS, "MULTI: Outgoing TUN queue full, dropped packet len=%d",
3708 mi->context.c2.to_tun.len);
3709
3710 buf_reset(&mi->context.c2.to_tun);
3711
3712 multi_process_post(m, mi, mpp_flags);
3713 clear_prefix();
3714}
3715
3716/*
3717 * Per-client route quota management
3718 */
3719
3720void
3722{
3723 struct gc_arena gc = gc_new();
3725 "MULTI ROUTE: route quota (%d) exceeded for %s (see --max-routes-per-client option)",
3727 gc_free(&gc);
3728}
3729
3730#ifdef ENABLE_DEBUG
3731/*
3732 * Flood clients with random packets
3733 */
3734static void
3735gremlin_flood_clients(struct multi_context *m)
3736{
3737 const int level = GREMLIN_PACKET_FLOOD_LEVEL(m->top.options.gremlin);
3738 if (level)
3739 {
3740 struct gc_arena gc = gc_new();
3741 struct buffer buf = alloc_buf_gc(BUF_SIZE(&m->top.c2.frame), &gc);
3742 struct packet_flood_parms parm = get_packet_flood_parms(level);
3743 int i;
3744
3745 ASSERT(buf_init(&buf, m->top.c2.frame.buf.headroom));
3746 parm.packet_size = min_int(parm.packet_size, m->top.c2.frame.buf.payload_size);
3747
3748 msg(D_GREMLIN, "GREMLIN_FLOOD_CLIENTS: flooding clients with %d packets of size %d",
3749 parm.n_packets, parm.packet_size);
3750
3751 for (i = 0; i < parm.packet_size; ++i)
3752 {
3753 ASSERT(buf_write_u8(&buf, get_random() & 0xFF));
3754 }
3755
3756 for (i = 0; i < parm.n_packets; ++i)
3757 {
3758 multi_bcast(m, &buf, NULL, 0);
3759 }
3760
3761 gc_free(&gc);
3762 }
3763}
3764#endif /* ifdef ENABLE_DEBUG */
3765
3766static bool
3768{
3769 struct timeval null;
3770 CLEAR(null);
3772}
3773
3774/*
3775 * Process timers in the top-level context
3776 */
3777void
3779{
3780 /* possibly reap instances/routes in vhash */
3782
3783 /* possibly print to status log */
3784 if (m->top.c1.status_output)
3785 {
3787 {
3789 }
3790 }
3791
3792 /* possibly flush ifconfig-pool file */
3794
3795#ifdef ENABLE_DEBUG
3796 gremlin_flood_clients(m);
3797#endif
3798
3799 /* Should we check for stale routes? */
3801 {
3803 }
3804}
3805
3806static void
3808{
3809 inherit_context_top(&top->multi->top, top);
3811}
3812
3813static void
3819
3820static bool
3822{
3823 return (sig == SIGUSR1 || sig == SIGTERM || sig == SIGHUP || sig == SIGINT);
3824}
3825
3826static void
3828{
3829 struct hash_iterator hi;
3830 struct hash_element *he;
3831 struct timeval tv;
3832
3833 /* tell all clients to restart */
3834 hash_iterator_init(m->iter, &hi);
3835 while ((he = hash_iterator_next(&hi)))
3836 {
3837 struct multi_instance *mi = (struct multi_instance *)he->value;
3838 if (!mi->halt && proto_is_dgram(mi->context.c2.link_sockets[0]->info.proto))
3839 {
3840 send_control_channel_string(&mi->context, next_server ? "RESTART,[N]" : "RESTART",
3841 D_PUSH);
3843 }
3844 }
3845 hash_iterator_free(&hi);
3846
3847 /* reschedule signal */
3849 tv.tv_sec = 2;
3850 tv.tv_usec = 0;
3852
3854
3858
3859 signal_reset(m->top.sig, 0);
3860}
3861
3862/*
3863 * Return true if event loop should break,
3864 * false if it should continue.
3865 */
3866bool
3868{
3869 if (signal_reset(m->top.sig, SIGUSR2) == SIGUSR2)
3870 {
3871 struct status_output *so = status_open(NULL, 0, M_INFO, NULL, 0);
3873 status_close(so);
3874 return false;
3875 }
3879 {
3881 return false;
3882 }
3883 return true;
3884}
3885
3886/*
3887 * Management subsystem callbacks
3888 */
3889#ifdef ENABLE_MANAGEMENT
3890
3891static void
3892management_callback_status(void *arg, const int version, struct status_output *so)
3893{
3894 struct multi_context *m = (struct multi_context *)arg;
3895
3896 if (!version)
3897 {
3899 }
3900 else
3901 {
3902 multi_print_status(m, so, version);
3903 }
3904}
3905
3906static int
3908{
3909 struct multi_context *m = (struct multi_context *)arg;
3910 return m->n_clients;
3911}
3912
3913static int
3914management_callback_kill_by_cn(void *arg, const char *del_cn)
3915{
3916 struct multi_context *m = (struct multi_context *)arg;
3917 struct hash_iterator hi;
3918 struct hash_element *he;
3919 int count = 0;
3920
3921 hash_iterator_init(m->iter, &hi);
3922 while ((he = hash_iterator_next(&hi)))
3923 {
3924 struct multi_instance *mi = (struct multi_instance *)he->value;
3925 if (!mi->halt)
3926 {
3927 const char *cn = tls_common_name(mi->context.c2.tls_multi, false);
3928 if (cn && !strcmp(cn, del_cn))
3929 {
3930 multi_signal_instance(m, mi, SIGTERM);
3931 ++count;
3932 }
3933 }
3934 }
3935 hash_iterator_free(&hi);
3936 return count;
3937}
3938
3939static int
3940management_callback_kill_by_addr(void *arg, const in_addr_t addr, const uint16_t port, const uint8_t proto)
3941{
3942 struct multi_context *m = (struct multi_context *)arg;
3943 struct hash_iterator hi;
3944 struct hash_element *he;
3945 struct openvpn_sockaddr saddr;
3946 struct mroute_addr maddr;
3947 int count = 0;
3948
3949 CLEAR(saddr);
3950 saddr.addr.in4.sin_family = AF_INET;
3951 saddr.addr.in4.sin_addr.s_addr = htonl(addr);
3952 saddr.addr.in4.sin_port = htons(port);
3953 maddr.proto = proto;
3954 if (mroute_extract_openvpn_sockaddr(&maddr, &saddr, true))
3955 {
3956 hash_iterator_init(m->iter, &hi);
3957 while ((he = hash_iterator_next(&hi)))
3958 {
3959 struct multi_instance *mi = (struct multi_instance *)he->value;
3960 if (!mi->halt && mroute_addr_equal(&maddr, &mi->real))
3961 {
3962 multi_signal_instance(m, mi, SIGTERM);
3963 ++count;
3964 }
3965 }
3966 hash_iterator_free(&hi);
3967 }
3968 return count;
3969}
3970
3971#if defined(__GNUC__) || defined(__clang__)
3972#pragma GCC diagnostic pop
3973#endif
3974
3975static void
3977{
3978 struct multi_context *m = (struct multi_context *)arg;
3979 if (m->multi_io)
3980 {
3982 }
3983}
3984
3985struct multi_instance *
3986lookup_by_cid(struct multi_context *m, const unsigned long cid)
3987{
3988 if (m)
3989 {
3990 struct multi_instance *mi = (struct multi_instance *)hash_lookup(m->cid_hash, &cid);
3991 if (mi && !mi->halt)
3992 {
3993 return mi;
3994 }
3995 }
3996 return NULL;
3997}
3998
3999static bool
4000management_kill_by_cid(void *arg, const unsigned long cid, const char *kill_msg)
4001{
4002 struct multi_context *m = (struct multi_context *)arg;
4003 struct multi_instance *mi = lookup_by_cid(m, cid);
4004 if (mi)
4005 {
4006 send_restart(&mi->context, kill_msg); /* was: multi_signal_instance (m, mi, SIGTERM); */
4008 return true;
4009 }
4010 else
4011 {
4012 return false;
4013 }
4014}
4015
4016static bool
4017management_client_pending_auth(void *arg, const unsigned long cid, const unsigned int mda_key_id,
4018 const char *extra, unsigned int timeout)
4019{
4020 struct multi_context *m = (struct multi_context *)arg;
4021 struct multi_instance *mi = lookup_by_cid(m, cid);
4022
4023 if (mi)
4024 {
4025 struct tls_multi *multi = mi->context.c2.tls_multi;
4026 struct tls_session *session;
4027
4028 if (multi->session[TM_INITIAL].key[KS_PRIMARY].mda_key_id == mda_key_id)
4029 {
4030 session = &multi->session[TM_INITIAL];
4031 }
4032 else if (multi->session[TM_ACTIVE].key[KS_PRIMARY].mda_key_id == mda_key_id)
4033 {
4034 session = &multi->session[TM_ACTIVE];
4035 }
4036 else
4037 {
4038 return false;
4039 }
4040
4041 /* sends INFO_PRE and AUTH_PENDING messages to client */
4042 bool ret = send_auth_pending_messages(multi, session, extra, timeout);
4045 return ret;
4046 }
4047 return false;
4048}
4049
4050
4051static bool
4052management_client_auth(void *arg, const unsigned long cid, const unsigned int mda_key_id,
4053 const bool auth, const char *reason, const char *client_reason,
4054 struct buffer_list *cc_config) /* ownership transferred */
4055{
4056 struct multi_context *m = (struct multi_context *)arg;
4057 struct multi_instance *mi = lookup_by_cid(m, cid);
4058 bool cc_config_owned = true;
4059 bool ret = false;
4060
4061 if (mi)
4062 {
4063 ret = tls_authenticate_key(mi->context.c2.tls_multi, mda_key_id, auth, client_reason);
4064 if (ret)
4065 {
4066 if (auth)
4067 {
4069 {
4071 cc_config_owned = false;
4072 }
4073 }
4074 else if (reason)
4075 {
4076 msg(D_MULTI_LOW, "MULTI: connection rejected: %s, CLI:%s", reason,
4077 np(client_reason));
4078 }
4079 }
4080 }
4081 if (cc_config_owned && cc_config)
4082 {
4084 }
4085 return ret;
4086}
4087
4088static char *
4089management_get_peer_info(void *arg, const unsigned long cid)
4090{
4091 struct multi_context *m = (struct multi_context *)arg;
4092 struct multi_instance *mi = lookup_by_cid(m, cid);
4093 char *ret = NULL;
4094
4095 if (mi)
4096 {
4097 ret = mi->context.c2.tls_multi->peer_info;
4098 }
4099
4100 return ret;
4101}
4102
4103#endif /* ifdef ENABLE_MANAGEMENT */
4104
4105
4106void
4132
4133void
4135{
4136 /* max_clients must be less then max peer-id value */
4138
4139 for (int i = 0; i < m->max_clients; ++i)
4140 {
4141 if (!m->instances[i])
4142 {
4143 mi->context.c2.tls_multi->peer_id = i;
4144 m->instances[i] = mi;
4145 break;
4146 }
4147 }
4148
4149 /* should not really end up here, since multi_create_instance returns null
4150 * if amount of clients exceeds max_clients */
4152}
4153
4164static void
4165multi_get_timeout(struct multi_context *multi, struct timeval *timeval)
4166{
4167 multi_get_timeout_instance(multi, timeval);
4168
4169#ifdef ENABLE_MANAGEMENT
4170 if (management)
4171 {
4172 management_check_bytecount_server(multi, timeval);
4173 }
4174#endif /* ENABLE_MANAGEMENT */
4175}
4176
4177/**************************************************************************/
4184static void
4186{
4187 int status;
4188
4189 while (true)
4190 {
4191 /* wait on tun/socket list */
4192 multi_get_timeout(multi, &multi->top.c2.timeval);
4193 status = multi_io_wait(multi);
4194 MULTI_CHECK_SIG(multi);
4195
4196 /* check on status of coarse timers */
4198
4199 /* timeout? */
4200 if (status > 0)
4201 {
4202 /* process the I/O which triggered select */
4203 multi_io_process_io(multi);
4204 }
4205 else if (status == 0)
4206 {
4207 multi_io_action(multi, NULL, TA_TIMEOUT, false);
4208 }
4209
4210 MULTI_CHECK_SIG(multi);
4211 }
4212}
4213
4214/*
4215 * Top level event loop.
4216 */
4217void
4219{
4220 ASSERT(top->options.mode == MODE_SERVER);
4221
4222 struct multi_context multi;
4223
4224 top->mode = CM_TOP;
4225 top->multi = &multi;
4227
4228 /* initialize top-tunnel instance */
4230 if (IS_SIG(top))
4231 {
4232 return;
4233 }
4234
4235 /* initialize global multi_context object */
4236 multi_init(top);
4237
4238 /* initialize our cloned top object */
4240
4241 /* initialize management interface */
4243
4244 /* finished with initialization */
4245 initialization_sequence_completed(top, ISC_SERVER); /* --mode server --proto tcp-server */
4246
4247#ifdef ENABLE_ASYNC_PUSH
4248 multi.top.c2.inotify_fd = inotify_init();
4249 if (multi.top.c2.inotify_fd < 0)
4250 {
4251 msg(D_MULTI_ERRORS | M_ERRNO, "MULTI: inotify_init error");
4252 }
4253#endif
4254
4255 tunnel_server_loop(&multi);
4256
4257#ifdef ENABLE_ASYNC_PUSH
4258 close(top->c2.inotify_fd);
4259#endif
4260
4261 /* shut down management interface */
4263
4264 /* save ifconfig-pool */
4265 multi_ifconfig_pool_persist(&multi, true);
4266
4267 /* tear down tunnel instance (unless --persist-tun) */
4268 multi_uninit(&multi);
4269 multi_top_free(&multi);
4271}
4272
4273/* Searches for the address and deletes it if it is owned by the multi_instance */
4274static void
4275multi_unlearn_addr(struct multi_context *m, struct multi_instance *mi, const struct mroute_addr *addr)
4276{
4277 struct hash_element *he;
4278 const uint32_t hv = hash_value(m->vhash, addr);
4279 struct hash_bucket *bucket = hash_bucket(m->vhash, hv);
4280 struct multi_route *r = NULL;
4281
4282 /* if route currently exists, get the instance which owns it */
4283 he = hash_lookup_fast(m->vhash, bucket, addr, hv);
4284 if (he)
4285 {
4286 r = (struct multi_route *)he->value;
4287 }
4288
4289 /* if the route does not exist or exists but is not owned by the current instance, return */
4290 if (!r || r->instance != mi)
4291 {
4292 return;
4293 }
4294
4295 struct gc_arena gc = gc_new();
4296 msg(D_MULTI_LOW, "MULTI: Unlearn: %s -> %s", mroute_addr_print(&r->addr, &gc), multi_instance_string(mi, false, &gc));
4297 learn_address_script(m, NULL, "delete", &r->addr);
4299 multi_route_del(r);
4300
4301 gc_free(&gc);
4302}
4303
4309static void
4310multi_unlearn_in_addr_t(struct multi_context *m, struct multi_instance *mi, in_addr_t a)
4311{
4312 struct mroute_addr addr;
4313 CLEAR(addr);
4314
4315 addr.type = MR_ADDR_IPV4;
4316 addr.len = 4;
4317 addr.v4.addr = a;
4318
4319 multi_unlearn_addr(m, mi, &addr);
4320}
4321
4327static void
4329{
4330 struct mroute_addr addr;
4331 CLEAR(addr);
4332
4333 addr.type = MR_ADDR_IPV6;
4334 addr.len = 16;
4335 addr.v6.addr = a6;
4336
4337 multi_unlearn_addr(m, mi, &addr);
4338}
4339
4340/* Function to unlearn previous ifconfig of a client in the server multi_context after a PUSH_UPDATE */
4341void
4350
4351/* Function to unlearn previous ifconfig-ipv6 of a client in the server multi_context after a PUSH_UPDATE */
4352void
4354{
4355 struct in6_addr old_addr6;
4356 CLEAR(old_addr6);
4357 old_addr6 = mi->context.c2.push_ifconfig_ipv6_local;
4358 multi_unlearn_in6_addr(m, mi, old_addr6);
4361}
4362
4373void
4374update_vhash(struct multi_context *m, struct multi_instance *mi, const char *new_ip, const char *new_ipv6)
4375{
4376 if (new_ip)
4377 {
4378 /* Remove old IP */
4380 {
4381 unlearn_ifconfig(m, mi);
4382 }
4383
4384 /* Add new IP */
4385 struct in_addr new_addr;
4386 CLEAR(new_addr);
4387 if (inet_pton(AF_INET, new_ip, &new_addr) == 1
4388 && multi_learn_in_addr_t(m, mi, ntohl(new_addr.s_addr), -1, true))
4389 {
4390 mi->context.c2.push_ifconfig_defined = true;
4391 mi->context.c2.push_ifconfig_local = ntohl(new_addr.s_addr);
4392 }
4393 }
4394
4395 if (new_ipv6)
4396 {
4397 /* Remove old IPv6 */
4399 {
4400 unlearn_ifconfig_ipv6(m, mi);
4401 }
4402
4403 /* Add new IPv6 */
4404 struct in6_addr new_addr6;
4405 CLEAR(new_addr6);
4406 if (inet_pton(AF_INET6, new_ipv6, &new_addr6) == 1
4407 && multi_learn_in6_addr(m, mi, new_addr6, -1, true))
4408 {
4410 mi->context.c2.push_ifconfig_ipv6_local = new_addr6;
4411 }
4412 }
4413}
4414
4415bool
4417{
4418 struct options *o = &mi->context.options;
4419 in_addr_t local_addr, local_netmask;
4420
4422 {
4423 /* If we do not have a local address, we just return false as
4424 * this check doesn't make sense. */
4425 return false;
4426 }
4427
4428 /* if it falls into the network defined by ifconfig_local we assume
4429 * it is already known to DCO and only install "extra" iroutes */
4430 inet_pton(AF_INET, o->ifconfig_local, &local_addr);
4431 inet_pton(AF_INET, o->ifconfig_remote_netmask, &local_netmask);
4432
4433 return (local_addr & local_netmask) != (dest & local_netmask);
4434}
4435
4436bool
4438 struct in6_addr *dest)
4439{
4440 struct options *o = &mi->context.options;
4441
4443 {
4444 /* If we do not have a local address, we just return false as
4445 * this check doesn't make sense. */
4446 return false;
4447 }
4448
4449 /* if it falls into the network defined by ifconfig_local we assume
4450 * it is already known to DCO and only install "extra" iroutes */
4451 struct in6_addr ifconfig_local;
4452 if (inet_pton(AF_INET6, o->ifconfig_ipv6_local, &ifconfig_local) != 1)
4453 {
4454 return false;
4455 }
4456
4457 return (!ipv6_net_contains_host(&ifconfig_local, o->ifconfig_ipv6_netbits,
4458 dest));
4459}
void argv_parse_cmd(struct argv *argres, const char *cmdstr)
Parses a command string, tokenizes it and puts each element into a separate struct argv argument slot...
Definition argv.c:481
void argv_free(struct argv *a)
Frees all memory allocations allocated by the struct argv related functions.
Definition argv.c:101
bool argv_printf(struct argv *argres, const char *format,...)
printf() variant which populates a struct argv.
Definition argv.c:438
bool argv_printf_cat(struct argv *argres, const char *format,...)
printf() inspired argv concatenation.
Definition argv.c:462
struct argv argv_new(void)
Allocates a new struct argv and ensures it is initialised.
Definition argv.c:87
void generate_auth_token(const struct user_pass *up, struct tls_multi *multi)
Generate an auth token based on username and timestamp.
Definition auth_token.c:161
bool buf_printf(struct buffer *buf, const char *format,...)
Definition buffer.c:241
void buffer_list_free(struct buffer_list *ol)
Frees a buffer list and all the buffers in it.
Definition buffer.c:1163
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Definition buffer.c:89
char * string_alloc(const char *str, struct gc_arena *gc)
Definition buffer.c:649
#define ALLOC_OBJ(dptr, type)
Definition buffer.h:1047
#define BSTR(buf)
Definition buffer.h:128
static void buf_reset(struct buffer *buf)
Definition buffer.h:303
static bool buf_write_u8(struct buffer *dest, uint8_t data)
Definition buffer.h:684
#define BLEN(buf)
Definition buffer.h:126
static void buf_reset_len(struct buffer *buf)
Definition buffer.h:312
static void strncpynt(char *dest, const char *src, size_t maxlen)
Definition buffer.h:361
static void gc_free(struct gc_arena *a)
Definition buffer.h:1025
#define ALLOC_OBJ_CLEAR(dptr, type)
Definition buffer.h:1052
#define buf_init(buf, offset)
Definition buffer.h:209
static struct gc_arena gc_new(void)
Definition buffer.h:1017
static const char *const key1
Definition cert_data.h:55
#define CCD_DEFAULT
Definition common.h:63
#define counter_format
Definition common.h:32
bool check_compression_settings_valid(struct compress_options *info, msglvl_t msglevel)
Checks if the compression settings are valid.
Definition comp.c:162
#define COMP_ALG_STUB
support compression command byte and framing without actual compression
Definition comp.h:56
#define COMP_F_MIGRATE
push stub-v2 or comp-lzo no when we see a client with comp-lzo in occ
Definition comp.h:47
long int get_random(void)
Definition crypto.c:1718
const char * translate_cipher_name_to_openvpn(const char *cipher_name)
Translate a crypto library cipher name to an OpenVPN cipher name.
Definition crypto.c:1792
#define CO_USE_TLS_KEY_MATERIAL_EXPORT
Bit-flag indicating that data channel key derivation is done using TLS keying material export [RFC570...
Definition crypto.h:357
#define CO_USE_DYNAMIC_TLS_CRYPT
Bit-flag indicating that renegotiations are using tls-crypt with a TLS-EKM derived key.
Definition crypto.h:373
#define CO_EPOCH_DATA_KEY_FORMAT
Bit-flag indicating the epoch the data format.
Definition crypto.h:377
#define CO_USE_CC_EXIT_NOTIFY
Bit-flag indicating that explicit exit notifies should be sent via the control channel instead of usi...
Definition crypto.h:369
Data Channel Cryptography SSL library-specific backend interface.
bool cipher_kt_mode_aead(const char *ciphername)
Check if the supplied cipher is a supported AEAD mode cipher.
static int dco_get_peer_stats_multi(dco_context_t *dco, const bool raise_sigusr1_on_err)
Definition dco.h:368
static int dco_do_read(dco_context_t *dco)
Definition dco.h:308
static void dco_install_iroute(struct multi_context *m, struct multi_instance *mi, struct mroute_addr *addr)
Definition dco.h:358
static bool dco_check_option(msglvl_t msglevel, const struct options *o)
Definition dco.h:273
void * dco_context_t
Definition dco.h:258
static int dco_multi_add_new_peer(struct multi_context *m, struct multi_instance *mi)
Definition dco.h:352
static void dco_delete_iroutes(struct multi_context *m, struct multi_instance *mi)
Definition dco.h:363
void setenv_counter(struct env_set *es, const char *name, counter_type value)
Definition env_set.c:283
void setenv_int(struct env_set *es, const char *name, int value)
Definition env_set.c:291
void setenv_str(struct env_set *es, const char *name, const char *value)
Definition env_set.c:307
struct env_set * env_set_create(struct gc_arena *gc)
Definition env_set.c:156
void setenv_long_long(struct env_set *es, const char *name, long long value)
Definition env_set.c:299
void setenv_del(struct env_set *es, const char *name)
Definition env_set.c:352
#define D_PUSH
Definition errlevel.h:82
#define D_MULTI_ERRORS
Definition errlevel.h:64
#define D_VLAN_DEBUG
Definition errlevel.h:153
#define D_IMPORT_ERRORS
Definition errlevel.h:63
#define D_ROUTE_QUOTA
Definition errlevel.h:89
#define D_MULTI_MEDIUM
Definition errlevel.h:101
#define D_DCO
Definition errlevel.h:93
#define D_MULTI_DEBUG
Definition errlevel.h:126
#define D_MULTI_DROPPED
Definition errlevel.h:100
#define D_DCO_DEBUG
Definition errlevel.h:117
#define D_MULTI_LOW
Definition errlevel.h:85
#define D_TLS_ERRORS
Definition errlevel.h:58
#define M_INFO
Definition errlevel.h:54
#define D_GREMLIN
Definition errlevel.h:77
@ EVENT_ARG_MULTI_INSTANCE
Definition event.h:136
void reschedule_multi_process(struct context *c)
Reschedule tls_multi_process.
Definition forward.c:395
bool send_control_channel_string(struct context *c, const char *str, msglvl_t msglevel)
Definition forward.c:402
void pre_select(struct context *c)
Definition forward.c:1966
void process_ip_header(struct context *c, unsigned int flags, struct buffer *buf, struct link_socket *sock)
Definition forward.c:1658
void extract_dco_float_peer_addr(const sa_family_t socket_family, struct openvpn_sockaddr *out_osaddr, const struct sockaddr *float_sa)
Transfers float_sa data extracted from an incoming DCO PEER_FLOAT_NTF to out_osaddr for later process...
Definition forward.c:1210
Interface functions to the internal and external multiplexers.
#define PIP_MSSFIX
Definition forward.h:319
#define PIPV6_ICMP_NOHOST_SERVER
Definition forward.h:324
static struct link_socket_info * get_link_socket_info(struct context *c)
Definition forward.h:333
#define ANY_OUT(c)
Definition forward.h:39
static void register_activity(struct context *c, const int size)
Definition forward.h:346
#define PIPV4_PASSTOS
Definition forward.h:318
#define TM_INITIAL
As yet un-trusted tls_session \ being negotiated.
Definition ssl_common.h:546
#define KS_PRIMARY
Primary key state index.
Definition ssl_common.h:465
#define TM_ACTIVE
Active tls_session.
Definition ssl_common.h:545
void encrypt_sign(struct context *c, bool comp_frag)
Process a data channel packet that will be sent through a VPN tunnel.
Definition forward.c:618
void tunnel_server(struct context *top)
Main event loop for OpenVPN in server mode.
Definition multi.c:4218
static void tunnel_server_loop(struct multi_context *multi)
Main event loop for OpenVPN in point-to-multipoint server mode.
Definition multi.c:4185
bool process_incoming_link_part1(struct context *c, struct link_socket_info *lsi, bool floated)
Starts processing a packet read from the external network interface.
Definition forward.c:984
void process_incoming_link_part2(struct context *c, struct link_socket_info *lsi, const uint8_t *orig_buf)
Continues processing a packet read from the external network interface.
Definition forward.c:1117
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:3341
struct multi_instance * multi_get_create_instance_udp(struct multi_context *m, bool *floated, struct link_socket *sock)
Get, and if necessary create, the multi_instance associated with a packet's source address.
Definition mudp.c:190
void process_incoming_tun(struct context *c, struct link_socket *out_sock)
Process a packet read from the virtual tun/tap network interface.
Definition forward.c:1473
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:3539
void uninit_management_callback(void)
Definition init.c:4411
void initialization_sequence_completed(struct context *c, const unsigned int flags)
Definition init.c:1528
void close_instance(struct context *c)
Definition init.c:4751
void inherit_context_top(struct context *dest, const struct context *src)
Definition init.c:4912
void free_context_buffers(struct context_buffers *b)
Definition init.c:3681
void init_instance_handle_signals(struct context *c, const struct env_set *env, const unsigned int flags)
Definition init.c:4437
bool do_deferred_options(struct context *c, const unsigned int found, const bool is_update)
Definition init.c:2581
void inherit_context_child(struct context *dest, const struct context *src, struct link_socket *sock)
Definition init.c:4824
void context_clear_2(struct context *c)
Definition init.c:87
void close_context(struct context *c, int sig, unsigned int flags)
Definition init.c:4958
struct context_buffers * init_context_buffers(const struct frame *frame)
Definition init.c:3656
void management_show_net_callback(void *arg, const msglvl_t msglevel)
Definition init.c:4261
#define CC_GC_FREE
Definition init.h:115
#define CC_HARD_USR1_TO_HUP
Definition init.h:117
#define ISC_SERVER
Definition init.h:127
static unsigned int constrain_uint(unsigned int x, unsigned int min, unsigned int max)
Definition integer.h:139
static int min_int(int x, int y)
Definition integer.h:105
static SERVICE_STATUS status
Definition interactive.c:51
@ route
Definition interactive.c:85
@ read
bool event_timeout_trigger(struct event_timeout *et, struct timeval *tv, const int et_const_retry)
This is the principal function for testing and triggering recurring timers.
Definition interval.c:47
#define ETT_DEFAULT
Definition interval.h:222
static void event_timeout_init(struct event_timeout *et, interval_t n, const time_t last)
Initialises a timer struct.
Definition interval.h:172
void hash_iterator_free(struct hash_iterator *hi)
Definition list.c:270
struct hash_element * hash_iterator_next(struct hash_iterator *hi)
Definition list.c:276
void hash_iterator_delete_element(struct hash_iterator *hi)
Definition list.c:308
struct hash_element * hash_lookup_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint32_t hv)
Definition list.c:79
void hash_iterator_init(struct hash *hash, struct hash_iterator *hi)
Definition list.c:234
struct hash * hash_init(const uint32_t n_buckets, const uint32_t iv, uint32_t(*hash_function)(const void *key, uint32_t iv), bool(*compare_function)(const void *key1, const void *key2))
Definition list.c:37
void hash_free(struct hash *hash)
Definition list.c:60
bool hash_add(struct hash *hash, const void *key, void *value, bool replace)
Definition list.c:137
void hash_remove_by_value(struct hash *hash, void *value)
Definition list.c:165
void hash_iterator_init_range(struct hash *hash, struct hash_iterator *hi, uint32_t start_bucket, uint32_t end_bucket)
Definition list.c:213
static bool hash_remove(struct hash *hash, const void *key)
Definition list.h:161
static void * hash_lookup(struct hash *hash, const void *key)
Definition list.h:128
static uint32_t hash_n_elements(const struct hash *hash)
Definition list.h:110
static uint32_t hash_n_buckets(const struct hash *hash)
Definition list.h:116
static uint32_t hash_value(const struct hash *hash, const void *key)
Definition list.h:104
static void hash_add_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint32_t hv, void *value)
Definition list.h:146
void management_check_bytecount_server(struct multi_context *multi, struct timeval *timeval)
Definition manage.c:4187
void management_notify_client_close(struct management *management, struct man_def_auth_context *mdac, const struct env_set *es)
Definition manage.c:3026
void management_learn_addr(struct management *management, struct man_def_auth_context *mdac, const struct mroute_addr *addr, const bool primary)
Definition manage.c:3038
void management_connection_established(struct management *management, struct man_def_auth_context *mdac, const struct env_set *es)
Definition manage.c:3016
void management_set_callback(struct management *man, const struct management_callback *cb)
Definition manage.c:2773
#define MCF_SERVER
Definition manage.h:175
void mbuf_add_item(struct mbuf_set *ms, const struct mbuf_item *item)
Definition mbuf.c:97
struct mbuf_buffer * mbuf_alloc_buf(const struct buffer *buf)
Definition mbuf.c:73
void mbuf_free_buf(struct mbuf_buffer *mb)
Definition mbuf.c:84
void mbuf_dereference_instance(struct mbuf_set *ms, struct multi_instance *mi)
Definition mbuf.c:160
bool mbuf_extract_item(struct mbuf_set *ms, struct mbuf_item *item)
Definition mbuf.c:119
void mbuf_free(struct mbuf_set *ms)
Definition mbuf.c:57
struct mbuf_set * mbuf_init(unsigned int size)
Definition mbuf.c:43
#define MF_UNICAST
Definition mbuf.h:45
static int mbuf_maximum_queued(const struct mbuf_set *ms)
Definition mbuf.h:91
void mroute_addr_mask_host_bits(struct mroute_addr *ma)
Definition mroute.c:318
void mroute_helper_add_iroute46(struct mroute_helper *mh, int netbits)
Definition mroute.c:520
const char * mroute_addr_print_ex(const struct mroute_addr *ma, const unsigned int flags, struct gc_arena *gc)
Definition mroute.c:377
bool mroute_extract_openvpn_sockaddr(struct mroute_addr *addr, const struct openvpn_sockaddr *osaddr, bool use_port)
Definition mroute.c:254
const char * mroute_addr_print(const struct mroute_addr *ma, struct gc_arena *gc)
Definition mroute.c:371
uint32_t mroute_addr_hash_function(const void *key, uint32_t iv)
Definition mroute.c:358
void mroute_helper_del_iroute46(struct mroute_helper *mh, int netbits)
Definition mroute.c:535
bool mroute_learnable_address(const struct mroute_addr *addr, struct gc_arena *gc)
Definition mroute.c:63
bool mroute_addr_compare_function(const void *key1, const void *key2)
Definition mroute.c:365
struct mroute_helper * mroute_helper_init(int ageable_ttl_secs)
Definition mroute.c:482
void mroute_addr_init(struct mroute_addr *addr)
Definition mroute.c:38
void mroute_helper_free(struct mroute_helper *mh)
Definition mroute.c:551
#define MROUTE_EXTRACT_SUCCEEDED
Definition mroute.h:38
#define MROUTE_EXTRACT_MCAST
Definition mroute.h:40
static unsigned int mroute_extract_addr_from_packet(struct mroute_addr *src, struct mroute_addr *dest, uint16_t vid, const struct buffer *buf, int tunnel_type)
Definition mroute.h:183
#define MR_WITH_NETBITS
Definition mroute.h:70
#define MR_WITH_PROTO
Definition mroute.h:76
static void mroute_extract_in_addr_t(struct mroute_addr *dest, const in_addr_t src)
Definition mroute.h:244
#define MR_ADDR_IPV4
Definition mroute.h:62
#define MR_ONLINK_DCO_ADDR
Definition mroute.h:79
static bool mroute_addr_equal(const struct mroute_addr *a1, const struct mroute_addr *a2)
Definition mroute.h:209
#define MAPF_SHOW_FAMILY
Definition mroute.h:158
#define MROUTE_EXTRACT_BCAST
Definition mroute.h:39
#define MR_ADDR_IPV6
Definition mroute.h:63
#define MR_ADDR_MASK
Definition mroute.h:64
void multi_tcp_instance_specific_free(struct multi_instance *mi)
Definition mtcp.c:124
void multi_tcp_delete_event(struct multi_io *multi_io, event_t event)
Definition mtcp.c:130
bool multi_tcp_instance_specific_init(struct multi_context *m, struct multi_instance *mi)
Definition mtcp.c:102
void multi_tcp_dereference_instance(struct multi_io *multi_io, struct multi_instance *mi)
Definition mtcp.c:139
#define BUF_SIZE(f)
Definition mtu.h:178
static const char * np(const char *str)
Definition multi-auth.c:146
static struct multi_instance * multi_learn_addr(struct multi_context *m, struct multi_instance *mi, const struct mroute_addr *addr, const unsigned int flags)
Definition multi.c:1032
static void multi_schedule_context_wakeup(struct multi_context *m, struct multi_instance *mi)
Definition multi.c:2964
static void multi_unlearn_in_addr_t(struct multi_context *m, struct multi_instance *mi, in_addr_t a)
Definition multi.c:4310
static void multi_client_connect_early_setup(struct multi_context *m, struct multi_instance *mi)
Definition multi.c:2459
static void multi_reap_free(struct multi_reap *mr)
Definition multi.c:215
static bool ccs_gen_deferred_ret_file(struct multi_instance *mi)
Create a temporary file for the return value of client connect and puts it into the client_connect_de...
Definition multi.c:1921
static void multi_process_float(struct multi_context *m, struct multi_instance *mi, struct link_socket *sock)
Handles peer floating.
Definition multi.c:3106
static void multi_reap_range(const struct multi_context *m, uint32_t start_bucket, uint32_t end_bucket)
Definition multi.c:161
struct multi_instance * multi_create_instance(struct multi_context *m, const struct mroute_addr *real, struct link_socket *sock)
Definition multi.c:715
static void multi_add_iroutes(struct multi_context *m, struct multi_instance *mi)
Definition multi.c:1294
void multi_ifconfig_pool_persist(struct multi_context *m, bool force)
Definition multi.c:150
static void multi_top_free(struct multi_context *m)
Definition multi.c:3814
static void multi_signal_instance(struct multi_context *m, struct multi_instance *mi, const int sig)
Definition multi.c:3217
void multi_reap_process_dowork(const struct multi_context *m)
Definition multi.c:202
static bool override_locked_username(struct multi_instance *mi)
Overrides the locked username with the username of –override-username.
Definition multi.c:2595
static int management_callback_n_clients(void *arg)
Definition multi.c:3907
bool multi_process_signal(struct multi_context *m)
Definition multi.c:3867
static void multi_select_virtual_addr(struct multi_context *m, struct multi_instance *mi)
Definition multi.c:1429
static enum client_connect_return multi_client_connect_call_script(struct multi_context *m, struct multi_instance *mi, bool deferred, unsigned int *option_types_found)
Runs the –client-connect script if one is defined.
Definition multi.c:2232
static void multi_get_timeout(struct multi_context *multi, struct timeval *timeval)
Determines the earliest wakeup interval based on periodic operations.
Definition multi.c:4165
void multi_close_instance_on_signal(struct multi_context *m, struct multi_instance *mi)
Definition multi.c:3205
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:3006
static void multi_connection_established(struct multi_context *m, struct multi_instance *mi)
Definition multi.c:2669
void multi_process_per_second_timers_dowork(struct multi_context *m)
Definition multi.c:3778
static void multi_set_virtual_addr_env(struct multi_instance *mi)
Definition multi.c:1570
static void multi_client_connect_setenv(struct multi_instance *mi)
Definition multi.c:1714
struct multi_instance * multi_get_queue(struct mbuf_set *ms)
Definition multi.c:3631
bool multi_process_timeout(struct multi_context *m, const unsigned int mpp_flags)
Definition multi.c:3667
static void multi_unlearn_addr(struct multi_context *m, struct multi_instance *mi, const struct mroute_addr *addr)
Definition multi.c:4275
static int management_callback_kill_by_cn(void *arg, const char *del_cn)
Definition multi.c:3914
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:4437
static bool stale_route_check_trigger(struct multi_context *m)
Definition multi.c:3767
struct multi_instance * lookup_by_cid(struct multi_context *m, const unsigned long cid)
Definition multi.c:3986
static bool management_kill_by_cid(void *arg, const unsigned long cid, const char *kill_msg)
Definition multi.c:4000
static void multi_init(struct context *t)
Definition multi.c:274
static void generate_prefix(struct multi_instance *mi)
Definition multi.c:460
static void multi_del_iroutes(struct multi_context *m, struct multi_instance *mi)
Definition multi.c:489
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:4416
static void management_delete_event(void *arg, event_t event)
Definition multi.c:3976
static bool multi_client_setup_dco_initial(struct multi_context *m, struct multi_instance *mi, struct gc_arena *gc)
Definition multi.c:2290
static void multi_uninit(struct multi_context *m)
Definition multi.c:667
const char * multi_instance_string(const struct multi_instance *mi, bool null, struct gc_arena *gc)
Definition multi.c:430
static void multi_client_connect_late_setup(struct multi_context *m, struct multi_instance *mi, const unsigned int option_types_found)
Definition multi.c:2336
static unsigned int compute_wakeup_sigma(const struct timeval *delta)
Definition multi.c:2942
static bool learn_address_script(const struct multi_context *m, const struct multi_instance *mi, const char *op, const struct mroute_addr *addr)
Definition multi.c:83
static void multi_unicast(struct multi_context *m, const struct buffer *buf, struct multi_instance *mi)
Definition multi.c:2879
void multi_close_instance(struct multi_context *m, struct multi_instance *mi, bool shutdown)
Definition multi.c:570
static bool management_client_pending_auth(void *arg, const unsigned long cid, const unsigned int mda_key_id, const char *extra, unsigned int timeout)
Definition multi.c:4017
enum client_connect_return(* multi_client_connect_handler)(struct multi_context *m, struct multi_instance *mi, bool from_deferred, unsigned int *option_types_found)
Definition multi.c:2576
static void multi_push_restart_schedule_exit(struct multi_context *m, bool next_server)
Definition multi.c:3827
static void multi_top_init(struct context *top)
Definition multi.c:3807
static void multi_client_disconnect_script(struct multi_context *m, struct multi_instance *mi)
Definition multi.c:539
static bool ccs_gen_config_file(struct multi_instance *mi)
Create a temporary file for the config directives of the client connect script and puts it into the c...
Definition multi.c:2026
static void check_stale_routes(struct multi_context *m)
Definition multi.c:1379
static bool management_client_auth(void *arg, const unsigned long cid, const unsigned int mda_key_id, const bool auth, const char *reason, const char *client_reason, struct buffer_list *cc_config)
Definition multi.c:4052
static void multi_bcast(struct multi_context *m, const struct buffer *buf, const struct multi_instance *sender_instance, uint16_t vid)
Definition multi.c:2896
static enum client_connect_return multi_client_connect_compress_migrate(struct multi_context *m, struct multi_instance *mi, bool deferred, unsigned int *option_types_found)
Do the necessary modification for doing the compress migrate.
Definition multi.c:2494
static int management_callback_kill_by_addr(void *arg, const in_addr_t addr, const uint16_t port, const uint8_t proto)
Definition multi.c:3940
static void multi_reap_all(const struct multi_context *m)
Definition multi.c:185
void multi_add_mbuf(struct multi_context *m, struct multi_instance *mi, struct mbuf_buffer *mb)
Definition multi.c:2860
static void multi_print_status(struct multi_context *m, struct status_output *so, const int version)
Definition multi.c:807
void route_quota_exceeded(const struct multi_instance *mi)
Definition multi.c:3721
void ungenerate_prefix(struct multi_instance *mi)
Definition multi.c:477
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:4134
static enum client_connect_return multi_client_connect_call_plugin_v2(struct multi_context *m, struct multi_instance *mi, bool deferred, unsigned int *option_types_found)
Definition multi.c:2142
static struct multi_reap * multi_reap_new(uint32_t buckets_per_pass)
Definition multi.c:191
static enum client_connect_return multi_client_connect_source_ccd(struct multi_context *m, struct multi_instance *mi, bool deferred, unsigned int *option_types_found)
Try to source a dynamic config file from the –client-config-dir directory.
Definition multi.c:2526
static enum client_connect_return multi_client_connect_call_plugin_v1(struct multi_context *m, struct multi_instance *mi, bool deferred, unsigned int *option_types_found)
Definition multi.c:2052
static void multi_client_connect_post(struct multi_context *m, struct multi_instance *mi, const char *dc_file, unsigned int *option_types_found)
Definition multi.c:1616
static void multi_delete_dup(struct multi_context *m, struct multi_instance *new_mi)
Definition multi.c:1339
static struct multi_instance * multi_get_instance_by_virtual_addr(struct multi_context *m, const struct mroute_addr *addr, bool cidr_routing)
Definition multi.c:1121
void init_management_callback_multi(struct multi_context *m)
Definition multi.c:4107
static void multi_unlearn_in6_addr(struct multi_context *m, struct multi_instance *mi, struct in6_addr a6)
Definition multi.c:4328
static enum client_connect_return ccs_test_deferred_ret_file(struct multi_instance *mi)
Tests whether the deferred return value file exists and returns the contained return value.
Definition multi.c:1953
static uint32_t reap_buckets_per_pass(uint32_t n_buckets)
Definition multi.c:224
static char * management_get_peer_info(void *arg, const unsigned long cid)
Definition multi.c:4089
static enum client_connect_return multi_client_connect_script_deferred(struct multi_context *m, struct multi_instance *mi, unsigned int *option_types_found)
Definition multi.c:2191
static bool multi_client_set_protocol_options(struct context *c)
Calculates the options that depend on the client capabilities based on local options and available pe...
Definition multi.c:1744
void unlearn_ifconfig_ipv6(struct multi_context *m, struct multi_instance *mi)
Definition multi.c:4353
static void multi_client_connect_post_plugin(struct multi_context *m, struct multi_instance *mi, const struct plugin_return *pr, unsigned int *option_types_found)
Definition multi.c:1642
static struct multi_instance * multi_learn_in_addr_t(struct multi_context *m, struct multi_instance *mi, in_addr_t a, int netbits, bool primary)
Definition multi.c:1196
static bool is_exit_restart(int sig)
Definition multi.c:3821
static bool ifconfig_push_constraint_satisfied(const struct context *c)
Definition multi.c:1409
static void set_cc_config(struct multi_instance *mi, struct buffer_list *cc_config)
Definition multi.c:75
void multi_process_drop_outgoing_tun(struct multi_context *m, const unsigned int mpp_flags)
Definition multi.c:3699
static bool cid_compare_function(const void *key1, const void *key2)
Definition multi.c:239
void unlearn_ifconfig(struct multi_context *m, struct multi_instance *mi)
Definition multi.c:4342
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:4374
enum client_connect_return multi_client_connect_mda(struct multi_context *m, struct multi_instance *mi, bool deferred, unsigned int *option_types_found)
Definition multi.c:1681
static bool multi_client_generate_tls_keys(struct context *c)
Generates the data channel keys.
Definition multi.c:2313
static struct multi_instance * multi_learn_in6_addr(struct multi_context *m, struct multi_instance *mi, struct in6_addr a6, int netbits, bool primary)
Definition multi.c:1242
static void setenv_stats(struct multi_context *m, struct context *c)
Definition multi.c:511
static uint32_t cid_hash_function(const void *key, uint32_t iv)
Definition multi.c:232
static void management_callback_status(void *arg, const int version, struct status_output *so)
Definition multi.c:3892
static void ccs_delete_config_file(struct multi_instance *mi)
Deletes the temporary file for the config directives of the client connect script and removes it into...
Definition multi.c:2003
static void ccs_delete_deferred_ret_file(struct multi_instance *mi)
Delete the temporary file for the return value of client connect It also removes it from client_conne...
Definition multi.c:1896
static const multi_client_connect_handler client_connect_handlers[]
Definition multi.c:2580
static void multi_client_disconnect_setenv(struct multi_context *m, struct multi_instance *mi)
Definition multi.c:526
Header file for server-mode related structures and functions.
bool multi_process_incoming_dco(struct multi_context *m)
Process an incoming DCO message (from kernel space).
#define MPP_CONDITIONAL_PRE_SELECT
Definition multi.h:275
client_connect_return
Return values used by the client connect call-back functions.
Definition multi.h:226
@ CC_RET_DEFERRED
Definition multi.h:229
@ CC_RET_FAILED
Definition multi.h:227
@ CC_RET_SKIPPED
Definition multi.h:230
@ CC_RET_SUCCEEDED
Definition multi.h:228
static bool multi_output_queue_ready(const struct multi_context *m, const struct multi_instance *mi)
Definition multi.h:392
#define MULTI_PREFIX_MAX_LENGTH
Definition multi.h:44
#define MULTI_CHECK_SIG(m)
Definition multi.h:699
#define REAP_MIN
Definition multi.h:551
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
static void multi_reap_process(const struct multi_context *m)
Definition multi.h:565
static void route_quota_inc(struct multi_instance *mi)
Definition multi.h:432
#define MULTI_ROUTE_CACHE
Definition multi.h:241
static void clear_prefix(void)
Definition multi.h:532
static bool multi_route_defined(const struct multi_context *m, const struct multi_route *r)
Definition multi.h:488
#define REAP_DIVISOR
Definition multi.h:550
#define MULTI_CACHE_ROUTE_TTL
Definition multi.h:558
#define MPP_CLOSE_ON_SIGNAL
Definition multi.h:276
#define REAP_MAX
Definition multi.h:552
static void multi_instance_dec_refcount(struct multi_instance *mi)
Definition multi.h:469
static void multi_instance_inc_refcount(struct multi_instance *mi)
Definition multi.h:463
static void multi_get_timeout_instance(struct multi_context *m, struct timeval *dest)
Definition multi.h:593
#define CLIENT_CONNECT_OPT_MASK
Definition multi.h:653
static void multi_set_pending(struct multi_context *m, struct multi_instance *mi)
Definition multi.h:702
#define MULTI_ROUTE_AGEABLE
Definition multi.h:242
#define MPP_RECORD_TOUCH
Definition multi.h:277
#define MPP_PRE_SELECT
Definition multi.h:274
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
struct multi_io * multi_io_init(const int maxclients)
Definition multi_io.c:116
void multi_io_process_io(struct multi_context *m)
Definition multi_io.c:418
void multi_io_free(struct multi_io *multi_io)
Definition multi_io.c:155
int multi_io_wait(struct multi_context *m)
Definition multi_io.c:166
void multi_io_action(struct multi_context *m, struct multi_instance *mi, int action, bool poll)
Definition multi_io.c:554
#define TA_TIMEOUT
Definition multi_io.h:45
#define CLEAR(x)
Definition basic.h:32
#define M_OPTERR
Definition error.h:101
static bool check_debug_level(msglvl_t level)
Definition error.h:259
#define M_USAGE
Definition error.h:107
#define M_NONFATAL
Definition error.h:91
#define dmsg(flags,...)
Definition error.h:172
#define msg(flags,...)
Definition error.h:152
unsigned int msglvl_t
Definition error.h:77
#define ASSERT(x)
Definition error.h:219
#define M_WARN
Definition error.h:92
#define M_ERRNO
Definition error.h:95
static bool is_cas_pending(enum multi_status cas)
Definition openvpn.h:209
#define MAX_PEER_ID
Definition openvpn.h:553
#define CM_TOP
Definition openvpn.h:483
const char title_string[]
Definition options.c:71
bool has_udp_in_local_list(const struct options *options)
Definition options.c:9325
#define MODE_SERVER
Definition options.h:264
static bool dco_enabled(const struct options *o)
Returns whether the current configuration has dco enabled.
Definition options.h:995
void options_string_import(struct options *options, const char *config, const msglvl_t msglevel, const unsigned int permission_mask, unsigned int *option_types_found, struct env_set *es)
void options_server_import(struct options *o, const char *filename, msglvl_t msglevel, unsigned int permission_mask, unsigned int *option_types_found, struct env_set *es)
#define OPT_P_COMP
Definition options.h:741
const char * time_string(time_t t, long usec, bool show_usec, struct gc_arena *gc)
Definition otime.c:109
struct frequency_limit * frequency_limit_init(int max, int per)
Definition otime.c:150
time_t now
Definition otime.c:33
void frequency_limit_free(struct frequency_limit *f)
Definition otime.c:165
static int openvpn_gettimeofday(struct timeval *tv, void *tz)
Definition otime.h:68
static void tv_add(struct timeval *dest, const struct timeval *src)
Definition otime.h:136
@ OVPN_DEL_PEER_REASON_EXPIRED
@ OVPN_DEL_PEER_REASON_TRANSPORT_DISCONNECT
@ OVPN_DEL_PEER_REASON_TRANSPORT_ERROR
@ OVPN_DEL_PEER_REASON_USERSPACE
@ OVPN_CMD_FLOAT_PEER
@ OVPN_CMD_SWAP_KEYS
@ OVPN_CMD_DEL_PEER
bool platform_test_file(const char *filename)
Return true if filename can be opened for read.
Definition platform.c:656
const char * platform_create_temp_file(const char *directory, const char *prefix, struct gc_arena *gc)
Create a temporary file in directory, returns the filename of the created file.
Definition platform.c:540
const char * platform_gen_path(const char *directory, const char *filename, struct gc_arena *gc)
Put a directory and filename together.
Definition platform.c:592
bool platform_unlink(const char *filename)
Definition platform.c:487
void plugin_return_free(struct plugin_return *pr)
Definition plugin.c:986
void plugin_return_get_column(const struct plugin_return *src, struct plugin_return *dest, const char *colname)
Definition plugin.c:972
bool plugin_defined(const struct plugin_list *pl, const int type)
Definition plugin.c:904
static void plugin_return_init(struct plugin_return *pr)
Definition plugin.h:163
static int plugin_call(const struct plugin_list *pl, const int type, const struct argv *av, struct plugin_return *pr, struct env_set *es)
Definition plugin.h:195
static bool plugin_return_defined(const struct plugin_return *pr)
Definition plugin.h:157
ifconfig_pool_handle ifconfig_pool_acquire(struct ifconfig_pool *pool, in_addr_t *local, in_addr_t *remote, struct in6_addr *remote_ipv6, const char *common_name)
Definition pool.c:297
bool ifconfig_pool_release(struct ifconfig_pool *pool, ifconfig_pool_handle hand, const bool hard)
Definition pool.c:349
void ifconfig_pool_write(struct ifconfig_pool_persist *persist, const struct ifconfig_pool *pool)
Definition pool.c:712
bool ifconfig_pool_write_trigger(struct ifconfig_pool_persist *persist)
Definition pool.c:573
void ifconfig_pool_free(struct ifconfig_pool *pool)
Definition pool.c:281
void ifconfig_pool_read(struct ifconfig_pool_persist *persist, struct ifconfig_pool *pool)
Definition pool.c:586
struct ifconfig_pool * ifconfig_pool_init(const bool ipv4_pool, enum pool_type type, in_addr_t start, in_addr_t end, const bool duplicate_cn, const bool ipv6_pool, const struct in6_addr ipv6_base, const int ipv6_netbits)
Definition pool.c:140
pool_type
Definition pool.h:35
@ IFCONFIG_POOL_30NET
Definition pool.h:36
@ IFCONFIG_POOL_INDIV
Definition pool.h:37
#define DEV_TYPE_TAP
Definition proto.h:36
#define DEV_TYPE_UNDEF
Definition proto.h:34
#define TOP_NET30
Definition proto.h:41
#define DEV_TYPE_TUN
Definition proto.h:35
#define TOP_P2P
Definition proto.h:42
#define TOP_SUBNET
Definition proto.h:43
int process_incoming_push_request(struct context *c)
Definition push.c:982
void push_option(struct options *o, const char *opt, msglvl_t msglevel)
Definition push.c:892
void send_restart(struct context *c, const char *kill_msg)
Definition push.c:494
bool send_auth_pending_messages(struct tls_multi *tls_multi, struct tls_session *session, const char *extra, unsigned int timeout)
Sends the auth pending control messages to a client.
Definition push.c:438
void remove_iroutes_from_push_route_list(struct options *o)
Definition push.c:1144
bool management_callback_send_push_update_by_cid(void *arg, unsigned long cid, const char *options)
Definition push_util.c:371
bool management_callback_send_push_update_broadcast(void *arg, const char *options)
Definition push_util.c:363
void initial_rate_limit_free(struct initial_packet_rate_limit *irl)
free the initial-packet rate limiter structure
struct initial_packet_rate_limit * initial_rate_limit_init(int max_per_period, int period_length)
allocate and initialize the initial-packet rate limiter structure
bool ipv6_net_contains_host(const struct in6_addr *network, unsigned int bits, const struct in6_addr *host)
check whether an IPv6 host address is covered by a given network/bits
Definition route.c:714
static int openvpn_run_script(const struct argv *a, const struct env_set *es, const unsigned int flags, const char *hook)
Will run a script and return the exit code of the script if between 0 and 255, -1 otherwise.
Definition run_command.h:89
void schedule_remove_entry(struct schedule *s, struct schedule_entry *e)
Definition schedule.c:421
struct schedule * schedule_init(void)
Definition schedule.c:406
void schedule_free(struct schedule *s)
Definition schedule.c:415
static void schedule_add_entry(struct schedule *s, struct schedule_entry *e, const struct timeval *tv, unsigned int sigma)
Definition schedule.h:97
int signal_reset(struct signal_info *si, int signum)
Clear the signal if its current value equals signum.
Definition sig.c:262
void remap_signal(struct context *c)
Definition sig.c:588
void throw_signal(const int signum)
Throw a hard signal.
Definition sig.c:175
void print_signal(const struct signal_info *si, const char *title, msglvl_t msglevel)
Definition sig.c:290
void register_signal(struct signal_info *si, int signum, const char *signal_text)
Register a soft signal in the signal_info struct si respecting priority.
Definition sig.c:228
#define IS_SIG(c)
Definition sig.h:47
void setenv_trusted(struct env_set *es, const struct link_socket_info *info)
Definition socket.c:1892
void setenv_in_addr_t(struct env_set *es, const char *name_prefix, in_addr_t addr, const unsigned int flags)
void setenv_in6_addr(struct env_set *es, const char *name_prefix, const struct in6_addr *addr, const unsigned int flags)
const char * print_in6_addr(struct in6_addr a6, unsigned int flags, struct gc_arena *gc)
const char * print_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_arena *gc)
#define IA_EMPTY_IF_UNDEF
Definition socket_util.h:89
static bool proto_is_dgram(int proto)
Return if the protocol is datagram (UDP)
#define SA_SET_IF_NONZERO
void tls_update_remote_addr(struct tls_multi *multi, const struct link_socket_actual *addr)
Updates remote address in TLS sessions.
Definition ssl.c:4092
void tls_session_soft_reset(struct tls_multi *tls_multi)
Definition ssl.c:1778
bool tls_session_update_crypto_params(struct tls_multi *multi, struct tls_session *session, struct options *options, struct frame *frame, struct frame *frame_fragment, struct link_socket_info *lsi, dco_context_t *dco)
Update TLS session crypto parameters (cipher and auth) and derive data channel keys based on the supp...
Definition ssl.c:1650
#define IV_PROTO_CC_EXIT_NOTIFY
Support for explicit exit notify via control channel This also includes support for the protocol-flag...
Definition ssl.h:102
#define IV_PROTO_DATA_EPOCH
Support the extended packet id and epoch format for data channel packets.
Definition ssl.h:111
#define IV_PROTO_DATA_V2
Support P_DATA_V2.
Definition ssl.h:80
#define IV_PROTO_TLS_KEY_EXPORT
Supports key derivation via TLS key material exporter [RFC5705].
Definition ssl.h:87
#define IV_PROTO_DYN_TLS_CRYPT
Support to dynamic tls-crypt (renegotiation with TLS-EKM derived tls-crypt key)
Definition ssl.h:108
#define IV_PROTO_REQUEST_PUSH
Assume client will send a push request and server does not need to wait for a push-request to send a ...
Definition ssl.h:84
#define IV_PROTO_NCP_P2P
Support doing NCP in P2P mode.
Definition ssl.h:95
@ CAS_CONNECT_DONE
Definition ssl_common.h:594
@ CAS_PENDING_DEFERRED
Waiting on an async option import handler.
Definition ssl_common.h:584
@ CAS_WAITING_AUTH
Initial TLS connection established but deferred auth is not yet finished.
Definition ssl_common.h:582
@ CAS_PENDING_DEFERRED_PARTIAL
at least handler succeeded but another is still pending
Definition ssl_common.h:585
@ CAS_PENDING
Options import (Connect script/plugin, ccd,...)
Definition ssl_common.h:583
@ CAS_NOT_CONNECTED
Definition ssl_common.h:581
@ CAS_FAILED
Option import failed or explicitly denied the client.
Definition ssl_common.h:586
@ KS_AUTH_FALSE
Key state is not authenticated
Definition ssl_common.h:154
@ KS_AUTH_DEFERRED
Key state authentication is being deferred, by async auth.
Definition ssl_common.h:155
static const struct key_state * get_primary_key(const struct tls_multi *multi)
gets an item of key_state objects in the order they should be scanned by data channel modules.
Definition ssl_common.h:756
#define SSLF_USERNAME_AS_COMMON_NAME
Definition ssl_common.h:426
char * ncp_get_best_cipher(const char *server_list, const char *peer_info, const char *remote_cipher, struct gc_arena *gc)
Iterates through the ciphers in server_list and return the first cipher that is also supported by the...
Definition ssl_ncp.c:246
const char * tls_peer_ncp_list(const char *peer_info, struct gc_arena *gc)
Returns the support cipher list from the peer according to the IV_NCP and IV_CIPHER values in peer_in...
Definition ssl_ncp.c:225
const char * ncp_expanded_ciphers(struct options *o, struct gc_arena *gc)
returns the o->ncp_ciphers in brackets, e.g.
Definition ssl_ncp.c:627
Control Channel SSL/Data dynamic negotiation Module This file is split from ssl.h to be able to unit ...
unsigned int extract_iv_proto(const char *peer_info)
Extracts the IV_PROTO variable and returns its value or 0 if it cannot be extracted.
Definition ssl_util.c:60
SSL utility functions.
bool ssl_verify_username_length(struct tls_session *session, const char *username)
Checks if the username length is valid to use.
bool tls_authenticate_key(struct tls_multi *multi, const unsigned int mda_key_id, const bool auth, const char *client_reason)
bool cert_hash_compare(const struct cert_hash_set *chs1, const struct cert_hash_set *chs2)
Compares certificates hashes, returns true if hashes are equal.
Definition ssl_verify.c:229
void tls_lock_cert_hash_set(struct tls_multi *multi)
Locks the certificate hash set used in the given tunnel.
Definition ssl_verify.c:286
void tls_lock_common_name(struct tls_multi *multi)
Locks the common name field for the given tunnel.
Definition ssl_verify.c:132
const char * tls_username(const struct tls_multi *multi, const bool null)
Returns the username field for the given tunnel.
Definition ssl_verify.c:172
void set_common_name(struct tls_session *session, const char *common_name)
Sets the common name field for the given tunnel.
Definition ssl_verify.c:85
void auth_set_client_reason(struct tls_multi *multi, const char *client_reason)
Sets the reason why authentication of a client failed.
Definition ssl_verify.c:803
const char * tls_common_name(const struct tls_multi *multi, const bool null)
Returns the common name field for the given tunnel.
Definition ssl_verify.c:107
Control Channel Verification Module.
bool status_trigger(struct status_output *so)
Definition status.c:123
void status_printf(struct status_output *so, const char *format,...)
Definition status.c:217
struct status_output * status_open(const char *filename, const int refresh_freq, const int msglevel, const struct virtual_output *vout, const unsigned int flags)
Definition status.c:59
void status_flush(struct status_output *so)
Definition status.c:147
void status_reset(struct status_output *so)
Definition status.c:138
bool status_close(struct status_output *so)
Definition status.c:178
Definition argv.h:35
Definition buffer.h:1107
struct buffer_entry * next
Definition buffer.h:1109
struct buffer buf
Definition buffer.h:1108
struct buffer_entry * head
Definition buffer.h:1114
Wrapper structure for dynamically allocated memory.
Definition buffer.h:60
uint8_t * data
Pointer to the allocated memory.
Definition buffer.h:67
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:65
Detached client connection state.
Definition multi.h:71
char * config_file
The temporary file name that contains the config directives returned by the client-connect script.
Definition multi.h:88
unsigned int option_types_found
Definition multi.h:76
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
unsigned int flags
Definition comp.h:77
int explicit_exit_notification
Definition options.h:150
int fragment
Definition options.h:141
struct ifconfig_pool_persist * ifconfig_pool_persist
Definition openvpn.h:197
struct status_output * status_output
Definition openvpn.h:185
struct tuntap * tuntap
Tun/tap virtual network interface.
Definition openvpn.h:172
bool push_request_received
Definition openvpn.h:427
counter_type link_read_bytes
Definition openvpn.h:266
counter_type link_write_bytes
Definition openvpn.h:269
bool push_ifconfig_ipv6_defined
Definition openvpn.h:434
struct fragment_master * fragment
Definition openvpn.h:252
bool push_ifconfig_defined
Definition openvpn.h:428
counter_type dco_read_bytes
Definition openvpn.h:267
struct man_def_auth_context mda_context
Definition openvpn.h:453
counter_type dco_write_bytes
Definition openvpn.h:270
struct env_set * es
Definition openvpn.h:420
struct tls_multi * tls_multi
TLS state structure for this VPN tunnel.
Definition openvpn.h:323
struct frame frame
Definition openvpn.h:248
struct in6_addr push_ifconfig_ipv6_remote
Definition openvpn.h:437
struct link_socket_actual from
Definition openvpn.h:245
struct frame frame_fragment
Definition openvpn.h:253
int push_ifconfig_ipv6_netbits
Definition openvpn.h:436
struct buffer to_link
Definition openvpn.h:377
struct buffer to_tun
Definition openvpn.h:376
struct in6_addr push_ifconfig_ipv6_local
Definition openvpn.h:435
struct link_socket ** link_sockets
Definition openvpn.h:237
in_addr_t push_ifconfig_local_alias
Definition openvpn.h:432
struct link_socket_info ** link_socket_infos
Definition openvpn.h:238
struct link_socket_actual * to_link_addr
Definition openvpn.h:244
in_addr_t push_ifconfig_remote_netmask
Definition openvpn.h:431
struct buffer buf
Definition openvpn.h:375
struct timeval timeval
Time to next event of timers and similar.
Definition openvpn.h:396
struct event_set * event_set
Definition openvpn.h:230
struct context_buffers * buffers
Definition openvpn.h:367
in_addr_t push_ifconfig_local
Definition openvpn.h:430
Contains all state information for one tunnel.
Definition openvpn.h:474
int mode
Role of this context within the OpenVPN process.
Definition openvpn.h:487
struct multi_context * multi
Pointer to the main P2MP context.
Definition openvpn.h:492
struct signal_info * sig
Internal error signaling object.
Definition openvpn.h:503
struct plugin_list * plugins
List of plug-ins.
Definition openvpn.h:505
struct context_2 c2
Level 2 context.
Definition openvpn.h:517
struct env_set * es
Set of environment variables.
Definition openvpn.h:499
struct options options
Options loaded from command line or configuration file.
Definition openvpn.h:475
struct context_1 c1
Level 1 context.
Definition openvpn.h:516
Security parameter state for processing data channel packets.
Definition crypto.h:293
struct key_ctx_bi key_ctx_bi
OpenSSL cipher and HMAC contexts for both sending and receiving directions.
Definition crypto.h:294
int signal_received
Definition multi.h:62
struct timeval wakeup
Definition multi.h:63
struct multi_instance * mi
Definition event.h:146
union event_arg::@1 u
event_arg_t type
Definition event.h:143
struct buffer outgoing
Buffer containing the remaining parts of the fragmented packet being sent.
Definition fragment.h:172
Packet geometry parameters.
Definition mtu.h:103
int payload_size
the maximum size that a payload that our buffers can hold from either tun device or network link.
Definition mtu.h:108
int headroom
the headroom in the buffer, this is choosen to allow all potential header to be added before the pack...
Definition mtu.h:114
struct frame::@8 buf
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:116
void * value
Definition list.h:41
const void * key
Definition list.h:42
Definition list.h:53
struct iroute_ipv6 * next
Definition route.h:269
unsigned int netbits
Definition route.h:268
struct in6_addr network
Definition route.h:267
in_addr_t network
Definition route.h:260
int netbits
Definition route.h:261
struct iroute * next
Definition route.h:262
Container for bidirectional cipher and HMAC key material.
Definition crypto.h:240
bool initialized
Definition crypto.h:285
Security parameter state of one TLS and data channel key session.
Definition ssl_common.h:208
struct auth_deferred_status plugin_auth
Definition ssl_common.h:268
unsigned int mda_key_id
Definition ssl_common.h:263
struct auth_deferred_status script_auth
Definition ssl_common.h:269
enum ks_auth_state authenticated
Definition ssl_common.h:259
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:152
unsigned long cid
Definition manage.h:64
char *(* get_peer_info)(void *arg, const unsigned long cid)
Definition manage.h:191
bool(* client_auth)(void *arg, const unsigned long cid, const unsigned int mda_key_id, const bool auth, const char *reason, const char *client_reason, struct buffer_list *cc_config)
Definition manage.h:186
int(* kill_by_addr)(void *arg, const in_addr_t addr, const uint16_t port, const uint8_t proto)
Definition manage.h:181
void(* delete_event)(void *arg, event_t event)
Definition manage.h:182
bool(* push_update_broadcast)(void *arg, const char *options)
Definition manage.h:199
bool(* push_update_by_cid)(void *arg, unsigned long cid, const char *options)
Definition manage.h:200
bool(* client_pending_auth)(void *arg, const unsigned long cid, const unsigned int kid, const char *extra, unsigned int timeout)
Definition manage.h:189
int(* n_clients)(void *arg)
Definition manage.h:183
void(* status)(void *arg, const int version, struct status_output *so)
Definition manage.h:178
void(* show_net)(void *arg, const msglvl_t msglevel)
Definition manage.h:179
unsigned int flags
Definition manage.h:176
int(* kill_by_cn)(void *arg, const char *common_name)
Definition manage.h:180
bool(* kill_by_cid)(void *arg, const unsigned long cid, const char *kill_msg)
Definition manage.h:185
unsigned int flags
Definition mbuf.h:46
struct buffer buf
Definition mbuf.h:42
struct mbuf_buffer * buffer
Definition mbuf.h:51
struct multi_instance * instance
Definition mbuf.h:52
uint16_t vid
Definition mroute.h:94
struct mroute_addr::@2::@6 v6
uint8_t addr[OPENVPN_ETH_ALEN]
Definition mroute.h:93
struct mroute_addr::@2::@5 v4
uint8_t proto
Definition mroute.h:84
uint8_t type
Definition mroute.h:85
in_port_t port
Definition mroute.h:99
uint8_t len
Definition mroute.h:83
uint8_t netbits
Definition mroute.h:86
unsigned int cache_generation
Definition mroute.h:133
Main OpenVPN server state structure.
Definition multi.h:163
int n_clients
Definition multi.h:189
struct mroute_addr local
Definition multi.h:184
struct schedule * schedule
Definition multi.h:174
struct mbuf_set * mbuf
Set of buffers for passing data channel packets between VPN tunnel instances.
Definition multi.h:175
struct initial_packet_rate_limit * initial_rate_limiter
Definition multi.h:181
struct deferred_signal_schedule_entry deferred_shutdown_signal
Definition multi.h:219
int max_clients
Definition multi.h:186
struct multi_reap * reaper
Definition multi.h:183
struct multi_io * multi_io
I/O state and events tracker.
Definition multi.h:178
struct hash * hash
VPN tunnel instances indexed by real address of the remote peer.
Definition multi.h:167
struct hash * cid_hash
Definition multi.h:192
unsigned long cid_counter
Definition multi.h:193
struct event_timeout stale_routes_check_et
Definition multi.h:212
int tcp_queue_limit
Definition multi.h:187
struct ifconfig_pool * ifconfig_pool
Definition multi.h:179
struct frequency_limit * new_connection_limiter
Definition multi.h:180
struct context top
Storage structure for process-wide configuration.
Definition multi.h:202
int status_file_version
Definition multi.h:188
struct multi_instance * pending
Definition multi.h:196
struct hash * vhash
VPN tunnel instances indexed by virtual address of remote hosts.
Definition multi.h:169
struct hash * iter
VPN tunnel instances indexed by real address of the remote peer, optimized for iteration.
Definition multi.h:171
struct multi_instance ** instances
Array of multi_instances.
Definition multi.h:164
struct multi_instance ** mpp_touched
Definition multi.h:198
bool enable_c2c
Definition multi.h:185
struct multi_instance * earliest_wakeup
Definition multi.h:197
struct mroute_helper * route_helper
Definition multi.h:182
Server-mode state structure for one single VPN tunnel.
Definition multi.h:102
struct buffer_list * cc_config
Definition multi.h:138
struct client_connect_defer_state client_connect_defer_state
Definition multi.h:145
bool did_cid_hash
Definition multi.h:137
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
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:140
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
bool did_iter
Definition multi.h:135
struct context context
The context structure storing state for this VPN tunnel.
Definition multi.h:143
int n_clients_delta
Definition multi.h:141
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:238
time_t last_reference
Definition multi.h:246
unsigned int cache_generation
Definition multi.h:245
unsigned int flags
Definition multi.h:243
struct multi_instance * instance
Definition multi.h:239
union openvpn_sockaddr::@27 addr
struct sockaddr_in in4
Definition socket_util.h:43
struct compress_options comp
Definition options.h:412
int push_ifconfig_ipv6_netbits
Definition options.h:525
int max_routes_per_client
Definition options.h:538
const char * ncp_ciphers_conf
The original ncp_ciphers specified by the user in the configuration.
Definition options.h:579
int status_file_version
Definition options.h:406
in_addr_t push_ifconfig_constraint_network
Definition options.h:520
const char * tmp_dir
Definition options.h:468
unsigned int imported_protocol_flags
Definition options.h:723
int stale_routes_ageing_time
Definition options.h:540
bool duplicate_cn
Definition options.h:529
uint32_t real_hash_size
Definition options.h:501
bool use_peer_id
Definition options.h:703
in_addr_t ifconfig_pool_netmask
Definition options.h:493
const char * ifconfig_ipv6_local
Definition options.h:328
int cf_max
Definition options.h:531
const char * dev_type
Definition options.h:322
bool push_ifconfig_defined
Definition options.h:515
bool ifconfig_pool_defined
Definition options.h:490
bool vlan_tagging
Definition options.h:712
in_addr_t ifconfig_pool_end
Definition options.h:492
bool ifconfig_ipv6_pool_defined
Definition options.h:497
const char * client_disconnect_script
Definition options.h:504
int n_bcast_buf
Definition options.h:511
const char * ifconfig_local
Definition options.h:326
struct connection_entry ce
Definition options.h:293
struct iroute_ipv6 * iroutes_ipv6
Definition options.h:514
in_addr_t push_ifconfig_local_alias
Definition options.h:518
int topology
Definition options.h:325
const char * ncp_ciphers
Definition options.h:580
uint32_t virtual_hash_size
Definition options.h:502
const char * learn_address_script
Definition options.h:505
const char * ciphername
Definition options.h:575
int cf_initial_max
Definition options.h:534
int stale_routes_check_interval
Definition options.h:539
uint16_t vlan_pvid
Definition options.h:714
int mode
Definition options.h:265
int ifconfig_ipv6_pool_netbits
Definition options.h:499
in_addr_t push_ifconfig_constraint_netmask
Definition options.h:521
bool enable_ncp_fallback
If defined fall back to ciphername if NCP fails.
Definition options.h:576
in_addr_t push_ifconfig_local
Definition options.h:516
struct gc_arena gc
Definition options.h:256
bool push_ifconfig_constraint_defined
Definition options.h:519
int cf_initial_per
Definition options.h:535
bool force_key_material_export
Definition options.h:710
struct iroute * iroutes
Definition options.h:513
struct in6_addr push_ifconfig_ipv6_remote
Definition options.h:526
const char * client_connect_script
Definition options.h:503
bool push_ifconfig_ipv6_defined
Definition options.h:523
int tcp_queue_limit
Definition options.h:512
const char * override_username
Definition options.h:510
struct in6_addr push_ifconfig_ipv6_local
Definition options.h:524
int max_clients
Definition options.h:537
const char * ifconfig_remote_netmask
Definition options.h:327
bool enable_c2c
Definition options.h:528
int cf_per
Definition options.h:532
in_addr_t ifconfig_pool_start
Definition options.h:491
in_addr_t push_ifconfig_remote_netmask
Definition options.h:517
const char * dev
Definition options.h:321
struct in6_addr ifconfig_ipv6_pool_base
Definition options.h:498
const char * client_config_dir
Definition options.h:507
int ifconfig_ipv6_netbits
Definition options.h:329
struct openvpn_plugin_string_list * list[MAX_PLUGINS]
Definition plugin.h:106
Definition schedule.h:44
const char * signal_text
Definition sig.h:44
volatile int signal_received
Definition sig.h:42
Security parameter state for a single VPN tunnel.
Definition ssl_common.h:612
char * auth_token_initial
The first auth-token we sent to a client.
Definition ssl_common.h:684
bool remote_usescomp
remote announced comp-lzo in OCC string
Definition ssl_common.h:704
dco_context_t * dco
Definition ssl_common.h:726
char * peer_info
A multi-line string of general-purpose info received from peer over control channel.
Definition ssl_common.h:673
char * remote_ciphername
cipher specified in peer's config file
Definition ssl_common.h:703
char * locked_username
The locked username is the username we assume the client is using.
Definition ssl_common.h:650
enum multi_status multi_state
Definition ssl_common.h:633
struct tls_session session[TM_SIZE]
Array of tls_session objects representing control channel sessions with the remote peer.
Definition ssl_common.h:712
struct cert_hash_set * locked_cert_hash_set
Definition ssl_common.h:656
char * locked_cn
Our locked common name, username, and cert hashes (cannot change during the life of this tls_multi ob...
Definition ssl_common.h:645
uint32_t peer_id
Definition ssl_common.h:700
bool use_peer_id
Definition ssl_common.h:701
char * locked_original_username
The username that client initially used before being overridden by –override-user.
Definition ssl_common.h:654
int dco_peer_id
This is the handle that DCO uses to identify this session with the kernel.
Definition ssl_common.h:724
bool data_epoch_supported
whether our underlying data channel supports new data channel features (epoch keys with AEAD tag at t...
Definition ssl_common.h:382
Security parameter state of a single session within a VPN tunnel.
Definition ssl_common.h:490
struct key_state key[KS_SIZE]
Definition ssl_common.h:525
struct tls_options * opt
Definition ssl_common.h:492
in_addr_t local
Definition tun.h:210
struct in6_addr local_ipv6
Definition tun.h:213
dco_context_t dco
Definition tun.h:249
in_addr_t remote_netmask
Definition tun.h:211
char username[USER_PASS_LEN]
Definition misc.h:67
struct env_set * es
static int cleanup(void **state)
struct gc_arena gc
Definition test_ssl.c:131
int dev_type_enum(const char *dev, const char *dev_type)
Definition tun.c:521
#define TUNNEL_TOPOLOGY(tt)
Definition tun.h:187
#define TUNNEL_TYPE(tt)
Definition tun.h:184
int16_t vlan_decapsulate(const struct context *c, struct buffer *buf)
Definition vlan.c:81
bool vlan_is_tagged(const struct buffer *buf)
Definition vlan.c:261