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-2026 OpenVPN Inc <sales@openvpn.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <https://www.gnu.org/licenses/>.
21 */
22
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 "sid_hash.h"
46#include <inttypes.h>
47#include <string.h>
48
49#include "memdbg.h"
50
51
52#include "crypto_backend.h"
53#include "ssl_util.h"
54#include "dco.h"
55#include "reflect_filter.h"
56#include "acc.h"
57
58/*#define MULTI_DEBUG_EVENT_LOOP*/
59
60#ifdef MULTI_DEBUG_EVENT_LOOP
61static const char *
62id(const struct multi_instance *mi)
63{
64 if (mi)
65 {
66 return tls_common_name(mi->context.c2.tls_multi, false);
67 }
68 else
69 {
70 return "NULL";
71 }
72}
73#endif
74
75#ifdef ENABLE_MANAGEMENT
76static void
77set_cc_config(struct multi_instance *mi, struct buffer_list *cc_config)
78{
80 mi->cc_config = cc_config;
81}
82#endif
83
84static bool
85learn_address_script(const struct multi_context *m, const struct multi_instance *mi, const char *op,
86 const struct mroute_addr *addr)
87{
88 struct gc_arena gc = gc_new();
89 struct env_set *es;
90 bool ret = true;
91 const struct plugin_list *plugins;
92
93 /* get environmental variable source */
94 if (mi && mi->context.c2.es)
95 {
96 es = mi->context.c2.es;
97 }
98 else
99 {
100 es = env_set_create(&gc);
101 }
102
103 /* get plugin source */
104 if (mi)
105 {
106 plugins = mi->context.plugins;
107 }
108 else
109 {
110 plugins = m->top.plugins;
111 }
112
113 if (plugin_defined(plugins, OPENVPN_PLUGIN_LEARN_ADDRESS))
114 {
115 struct argv argv = argv_new();
116 argv_printf(&argv, "%s %s", op, mroute_addr_print(addr, &gc));
117 if (mi)
118 {
120 }
121 if (plugin_call(plugins, OPENVPN_PLUGIN_LEARN_ADDRESS, &argv, NULL, es)
122 != OPENVPN_PLUGIN_FUNC_SUCCESS)
123 {
124 msg(M_WARN, "WARNING: learn-address plugin call failed");
125 ret = false;
126 }
127 argv_free(&argv);
128 }
129
131 {
132 struct argv argv = argv_new();
133 setenv_str(es, "script_type", "learn-address");
135 argv_printf_cat(&argv, "%s %s", op, mroute_addr_print(addr, &gc));
136 if (mi)
137 {
139 }
140 if (!openvpn_run_script(&argv, es, 0, "--learn-address"))
141 {
142 ret = false;
143 }
144 argv_free(&argv);
145 }
146
147 gc_free(&gc);
148 return ret;
149}
150
151void
153{
154 /* write pool data to file */
157 {
159 }
160}
161
162static void
163multi_reap_range(const struct multi_context *m, uint32_t start_bucket, uint32_t end_bucket)
164{
165 struct gc_arena gc = gc_new();
166 struct hash_iterator hi;
167 struct hash_element *he;
168
169 dmsg(D_MULTI_DEBUG, "MULTI: REAP range %u -> %u", start_bucket, end_bucket);
170 hash_iterator_init_range(m->vhash, &hi, start_bucket, end_bucket);
171 while ((he = hash_iterator_next(&hi)) != NULL)
172 {
173 struct multi_route *r = (struct multi_route *)he->value;
174 if (!multi_route_defined(m, r))
175 {
176 dmsg(D_MULTI_DEBUG, "MULTI: REAP DEL %s", mroute_addr_print(&r->addr, &gc));
177 learn_address_script(m, NULL, "delete", &r->addr);
180 }
181 }
183 gc_free(&gc);
184}
185
186static void
188{
190}
191
192static struct multi_reap *
194{
195 struct multi_reap *mr;
196 ALLOC_OBJ(mr, struct multi_reap);
197 mr->bucket_base = 0;
199 mr->last_call = now;
200 return mr;
201}
202
203void
205{
206 struct multi_reap *mr = m->reaper;
207 if (mr->bucket_base >= hash_n_buckets(m->vhash))
208 {
209 mr->bucket_base = 0;
210 }
212 mr->bucket_base += mr->buckets_per_pass;
213 mr->last_call = now;
214}
215
216static void
218{
219 free(mr);
220}
221
222/*
223 * How many buckets in vhash to reap per pass.
224 */
225static uint32_t
226reap_buckets_per_pass(uint32_t n_buckets)
227{
228 return constrain_uint(n_buckets / REAP_DIVISOR, REAP_MIN, REAP_MAX);
229}
230
231#ifdef ENABLE_MANAGEMENT
232
233static uint64_t
234cid_hash_function(const void *key, const uint8_t hash_key[HASH_KEY_LEN])
235{
236 const unsigned long *k = (const unsigned long *)key;
237 return (uint64_t)*k;
238}
239
240static bool
241cid_compare_function(const void *key1, const void *key2)
242{
243 const unsigned long *k1 = (const unsigned long *)key1;
244 const unsigned long *k2 = (const unsigned long *)key2;
245 return *k1 == *k2;
246}
247
248#endif
249
250#if defined(__GNUC__) || defined(__clang__)
251#pragma GCC diagnostic push
252#pragma GCC diagnostic ignored "-Wsign-conversion"
253#endif
254
255#ifdef ENABLE_ASYNC_PUSH
256static uint64_t
257/*
258 * inotify watcher descriptors are used as hash value
259 */
260int_hash_function(const void *key, const uint8_t hash_key[HASH_KEY_LEN])
261{
262 return (uintptr_t)key;
263}
264
265static bool
266int_compare_function(const void *key1, const void *key2)
267{
268 return (uintptr_t)key1 == (uintptr_t)key2;
269}
270#endif
271
272/*
273 * Main initialization function, init multi_context object.
274 */
275static void
277{
278 struct multi_context *m = t->multi;
279 int dev = DEV_TYPE_UNDEF;
280
281 msg(D_MULTI_LOW, "MULTI: multi_init called, r=%" PRIu32 " v=%" PRIu32 " s=%" PRIu32,
283
284 /*
285 * Get tun/tap/null device type
286 */
288
289 /*
290 * Init our multi_context object.
291 */
292 CLEAR(*m);
293
294 /*
295 * Real address hash table (source port number is
296 * considered to be part of the address). Used
297 * to determine which client sent an incoming packet
298 * which is seen on the TCP/UDP socket.
299 */
302
303 /*
304 * Virtual address hash table. Used to determine
305 * which client to route a packet to.
306 */
309
310 /*
311 * Peer session id hash table. Used to lookup a session by the session
312 * id of one of its active sessions */
315
316#ifdef ENABLE_MANAGEMENT
318#endif
319
320#ifdef ENABLE_ASYNC_PUSH
321 /*
322 * Mapping between inotify watch descriptors and
323 * multi_instances.
324 */
325 m->inotify_watchers =
326 hash_init(t->options.real_hash_size, int_hash_function, int_compare_function);
327#endif
328
329 /*
330 * This is our scheduler, for time-based wakeup
331 * events.
332 */
333 m->schedule = schedule_init();
334
335 /*
336 * Limit frequency of incoming connections to control
337 * DoS.
338 */
342
343 /*
344 * Allocate broadcast/multicast buffer list
345 */
347
348 /*
349 * Different status file format options are available
350 */
352
353 /*
354 * Possibly allocate an ifconfig pool, do it
355 * differently based on whether a tun or tap style
356 * tunnel.
357 */
359 {
361
362 if (dev == DEV_TYPE_TUN && t->options.topology == TOP_NET30)
363 {
365 }
366
372
373 /* reload pool data from file */
375 {
377 }
378 }
379
380 /*
381 * Help us keep track of routing table.
382 */
384
385 /*
386 * Initialize route and instance reaper.
387 */
389
390 /*
391 * Get local ifconfig address
392 */
393 CLEAR(m->local);
394 ASSERT(t->c1.tuntap);
396
397 /*
398 * Per-client limits
399 */
401
402 m->instances = calloc(m->max_clients, sizeof(struct multi_instance *));
404
405 m->top.c2.event_set = t->c2.event_set;
406
407 /*
408 * Initialize multi-socket I/O wait object
409 */
412
413 /*
414 * Allow client <-> client communication, without going through
415 * tun/tap interface and network stack?
416 */
418
419 /* initialize stale routes check timer */
421 {
422 msg(M_INFO,
423 "Initializing stale route check timer to run every %i seconds and to removing routes with activity timeout older than %i seconds",
426 }
427
429}
430
431const char *
432multi_instance_string(const struct multi_instance *mi, bool null, struct gc_arena *gc)
433{
434 if (mi)
435 {
437 const char *cn = tls_common_name(mi->context.c2.tls_multi, true);
438
439 if (cn)
440 {
441 buf_printf(&out, "%s/", cn);
442 }
443 buf_printf(&out, "%s", mroute_addr_print(&mi->real, gc));
444
445 bool debug_rx_pid = (check_debug_level(D_DCO_DEBUG) && dco_enabled(&mi->context.options))
447
448 if (mi->context.c2.tls_multi && debug_rx_pid)
449 {
450 buf_printf(&out, " rx-peer-id=%u", mi->context.c2.tls_multi->rx_peer_id);
451 }
452 return BSTR(&out);
453 }
454 else if (null)
455 {
456 return NULL;
457 }
458 else
459 {
460 return "UNDEF";
461 }
462}
463
464static void
466{
467 struct gc_arena gc = gc_new();
468 const char *prefix = multi_instance_string(mi, true, &gc);
469 if (prefix)
470 {
471 strlcpy(mi->msg_prefix, prefix, sizeof(mi->msg_prefix));
472 }
473 else
474 {
475 mi->msg_prefix[0] = '\0';
476 }
477 set_prefix(mi);
478 gc_free(&gc);
479}
480
481void
483{
484 mi->msg_prefix[0] = '\0';
485 set_prefix(mi);
486}
487
488/*
489 * Tell the route helper about deleted iroutes so
490 * that it can update its mask of currently used
491 * CIDR netlengths.
492 */
493static void
495{
496 const struct iroute *ir;
497 const struct iroute_ipv6 *ir6;
498
499 /* check if DCO iroutes were already removed when scheduling a delayed exit */
500 if (mi->context.did_dco_iroutes)
501 {
502 mi->context.did_dco_iroutes = false;
504 }
505
507 {
508 for (ir = mi->context.options.iroutes; ir != NULL; ir = ir->next)
509 {
511 }
512
513 for (ir6 = mi->context.options.iroutes_ipv6; ir6 != NULL; ir6 = ir6->next)
514 {
516 }
517 }
518}
519
520static void
522{
523 setenv_counter(c->c2.es, "bytes_received", c->c2.link_read_bytes + c->c2.dco_read_bytes);
524 setenv_counter(c->c2.es, "bytes_sent", c->c2.link_write_bytes + c->c2.dco_write_bytes);
525}
526
527static void
529{
530 /* setenv client real IP address */
532
533 /* setenv stats */
534 setenv_stats(&mi->context);
535
536 /* setenv connection duration */
537 setenv_long_long(mi->context.c2.es, "time_duration", now - mi->created);
538}
539
540static void
542{
544
545 if (plugin_defined(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_DISCONNECT))
546 {
547 if (plugin_call(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_DISCONNECT, NULL, NULL,
548 mi->context.c2.es)
549 != OPENVPN_PLUGIN_FUNC_SUCCESS)
550 {
551 msg(M_WARN, "WARNING: client-disconnect plugin call failed");
552 }
553 }
554
556 {
557 struct argv argv = argv_new();
558 setenv_str(mi->context.c2.es, "script_type", "client-disconnect");
560 openvpn_run_script(&argv, mi->context.c2.es, 0, "--client-disconnect");
561 argv_free(&argv);
562 }
563#ifdef ENABLE_MANAGEMENT
564 if (management)
565 {
567 }
568#endif
569}
570
571void
572multi_close_instance(struct multi_context *m, struct multi_instance *mi, bool shutdown)
573{
574 ASSERT(!mi->halt);
575 mi->halt = true;
576 bool is_dgram = proto_is_dgram(mi->context.c2.link_sockets[0]->info.proto);
577
578 dmsg(D_MULTI_DEBUG, "MULTI: multi_close_instance called");
579
580 /* adjust current client connection count */
581 m->n_clients += mi->n_clients_delta;
582 mi->n_clients_delta = 0;
583
584 /* prevent dangling pointers */
585 if (m->pending == mi)
586 {
587 multi_set_pending(m, NULL);
588 }
589 if (m->earliest_wakeup == mi)
590 {
591 m->earliest_wakeup = NULL;
592 }
593
594 /* The sid hash holds a reference on the instance: drop it on
595 * instance close, otherwise the instance is never freed */
597 {
599 }
600
601 if (!shutdown)
602 {
603 if (mi->did_real_hash)
604 {
605 ASSERT(hash_remove(m->hash, &mi->real));
606 }
607#ifdef ENABLE_MANAGEMENT
608 if (mi->did_cid_hash)
609 {
611 }
612#endif
613
614#ifdef ENABLE_ASYNC_PUSH
615 if (mi->inotify_watch != -1)
616 {
617 hash_remove(m->inotify_watchers, (void *)(uintptr_t)mi->inotify_watch);
618 mi->inotify_watch = -1;
619 }
620#endif
621
623 {
624 m->instances[mi->context.c2.tls_multi->rx_peer_id] = NULL;
625
626 /* Adjust the max_peerid as this might have been the highest
627 * peer id instance */
628 while (m->max_peerid > 0 && m->instances[m->max_peerid] == NULL)
629 {
630 m->max_peerid--;
631 }
632
634 }
635
637
638 ifconfig_pool_release(m->ifconfig_pool, mi->vaddr_handle, false);
639
640 if (mi->did_iroutes)
641 {
642 multi_del_iroutes(m, mi);
643 mi->did_iroutes = false;
644 }
645
646 if (!is_dgram)
647 {
649 }
650
652 }
653
654#ifdef ENABLE_MANAGEMENT
655 set_cc_config(mi, NULL);
656#endif
657
659 {
661 }
662
664
666
668
669 /*
670 * Don't actually delete the instance memory allocation yet,
671 * because virtual routes may still point to it. Let the
672 * vhash reaper deal with it.
673 */
675}
676
677/*
678 * Called on shutdown or restart.
679 */
680static void
682{
683 if (m->hash)
684 {
685 /* fetch final stats while all peers can still be mapped to their instances */
686 if (dco_enabled(&m->top.options))
687 {
689 }
690
691 for (uint32_t i = 0; i <= m->max_peerid; i++)
692 {
693 struct multi_instance *mi = m->instances[i];
694 if (mi)
695 {
696 multi_close_instance(m, mi, true);
697 }
698 }
699
701
702 hash_free(m->hash);
703 hash_free(m->vhash);
705#ifdef ENABLE_MANAGEMENT
707#endif
708 m->hash = NULL;
709
710 free(m->instances);
711
712#ifdef ENABLE_ASYNC_PUSH
713 hash_free(m->inotify_watchers);
714 m->inotify_watchers = NULL;
715#endif
716
718 mbuf_free(m->mbuf);
725 }
726}
727
728/*
729 * Create a client instance object for a newly connected client.
730 */
731struct multi_instance *
733 struct link_socket *sock)
734{
735 struct multi_instance *mi;
736
737 msg(D_MULTI_MEDIUM, "MULTI: multi_create_instance called");
738
740
741 mi->gc = gc_new();
743 mi->vaddr_handle = -1;
744 mi->created = now;
746
747 if (real)
748 {
749 mi->real = *real;
750 generate_prefix(mi);
751 }
752
753 inherit_context_child(&mi->context, &m->top, sock);
754 if (IS_SIG(&mi->context))
755 {
756 goto err;
757 }
758
760
761 if (m->assigned_peer_ids >= m->max_clients)
762 {
764 "MULTI: new incoming connection would exceed maximum number of clients (%u)",
765 m->max_clients);
766 goto err;
767 }
768
769 if (!real) /* TCP mode? */
770 {
772 {
773 goto err;
774 }
775 generate_prefix(mi);
776 }
777
778#ifdef ENABLE_MANAGEMENT
779 do
780 {
782 } while (!hash_add(m->cid_hash, &mi->context.c2.mda_context.cid, mi, false));
783 mi->did_cid_hash = true;
784#endif
785
786 mi->context.c2.push_request_received = false;
787#ifdef ENABLE_ASYNC_PUSH
788 mi->inotify_watch = -1;
789#endif
790
792 {
793 msg(D_MULTI_ERRORS, "MULTI: signal occurred during client instance initialization");
794 goto err;
795 }
796
798 mi->ev_arg.u.mi = mi;
799
800 return mi;
801
802err:
803 multi_close_instance(m, mi, false);
804 return NULL;
805}
806
807/*
808 * Dump tables -- triggered by SIGUSR2.
809 * If status file is defined, write to file.
810 * If status file is NULL, write to syslog.
811 */
812static void
813multi_print_status(struct multi_context *m, struct status_output *so, const int version)
814{
815 struct gc_arena gc_top = gc_new();
816 struct hash_iterator hi;
817 const struct hash_element *he;
818
819 status_reset(so);
820
821 if (dco_enabled(&m->top.options))
822 {
823 if (dco_get_peer_stats_multi(&m->top.c1.tuntap->dco, true) < 0)
824 {
825 return;
826 }
827 }
828
829 if (version == 1)
830 {
831 /*
832 * Status file version 1
833 */
834 status_printf(so, "OpenVPN CLIENT LIST");
835 status_printf(so, "Updated,%s", time_string(0, 0, false, &gc_top));
836 status_printf(so, "Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since");
837
838 for (uint32_t i = 0; i <= m->max_peerid; i++)
839 {
840 const struct multi_instance *mi = m->instances[i];
841
842 struct gc_arena gc = gc_new();
843 if (mi && !mi->halt)
844 {
845 status_printf(so, "%s,%s," counter_format "," counter_format ",%s",
847 mroute_addr_print(&mi->real, &gc),
850 time_string(mi->created, 0, false, &gc));
851 }
852 gc_free(&gc);
853 }
854
855 status_printf(so, "ROUTING TABLE");
856 status_printf(so, "Virtual Address,Common Name,Real Address,Last Ref");
857 hash_iterator_init(m->vhash, &hi);
858 while ((he = hash_iterator_next(&hi)))
859 {
860 struct gc_arena gc = gc_new();
861 const struct multi_route *route = (struct multi_route *)he->value;
862
864 {
865 const struct multi_instance *mi = route->instance;
866 const struct mroute_addr *ma = &route->addr;
867 char flags[2] = { 0, 0 };
868
869 if (route->flags & MULTI_ROUTE_CACHE)
870 {
871 flags[0] = 'C';
872 }
873 status_printf(so, "%s%s,%s,%s,%s", mroute_addr_print(ma, &gc), flags,
875 mroute_addr_print(&mi->real, &gc),
876 time_string(route->last_reference, 0, false, &gc));
877 }
878 gc_free(&gc);
879 }
881
882 status_printf(so, "GLOBAL STATS");
883 if (m->mbuf)
884 {
885 status_printf(so, "Max bcast/mcast queue length,%d", mbuf_maximum_queued(m->mbuf));
886 }
887
888 status_printf(so, "END");
889 }
890 else if (version == 2 || version == 3)
891 {
892 const char sep = (version == 3) ? '\t' : ',';
893
894 /*
895 * Status file version 2 and 3
896 */
897 status_printf(so, "TITLE%c%s", sep, title_string);
898 status_printf(so, "TIME%c%s%c%u", sep, time_string(now, 0, false, &gc_top), sep,
899 (unsigned int)now);
901 so,
902 "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",
903 sep, sep, sep, sep, sep, sep, sep, sep, sep, sep, sep, sep, sep);
904
905 for (uint32_t i = 0; i <= m->max_peerid; i++)
906 {
907 const struct multi_instance *mi = m->instances[i];
908 struct gc_arena gc = gc_new();
909
910 if (mi && !mi->halt)
911 {
913 so,
914 "CLIENT_LIST%c%s%c%s%c%s%c%s%c" counter_format "%c" counter_format
915 "%c%s%c%u%c%s%c"
916#ifdef ENABLE_MANAGEMENT
917 "%lu"
918#else
919 ""
920#endif
921 "%c%" PRIu32 "%c%s",
922 sep, tls_common_name(mi->context.c2.tls_multi, false), sep,
923 mroute_addr_print(&mi->real, &gc), sep,
928 time_string(mi->created, 0, false, &gc), sep, (unsigned int)mi->created,
929 sep, tls_username(mi->context.c2.tls_multi, false),
930#ifdef ENABLE_MANAGEMENT
931 sep, mi->context.c2.mda_context.cid,
932#else
933 sep,
934#endif
937 }
938 gc_free(&gc);
939 }
940
942 so,
943 "HEADER%cROUTING_TABLE%cVirtual Address%cCommon Name%cReal Address%cLast Ref%cLast Ref (time_t)",
944 sep, sep, sep, sep, sep, sep);
945 hash_iterator_init(m->vhash, &hi);
946 while ((he = hash_iterator_next(&hi)))
947 {
948 struct gc_arena gc = gc_new();
949 const struct multi_route *route = (struct multi_route *)he->value;
950
952 {
953 const struct multi_instance *mi = route->instance;
954 const struct mroute_addr *ma = &route->addr;
955 char flags[2] = { 0, 0 };
956
957 if (route->flags & MULTI_ROUTE_CACHE)
958 {
959 flags[0] = 'C';
960 }
961 status_printf(so, "ROUTING_TABLE%c%s%s%c%s%c%s%c%s%c%u", sep,
962 mroute_addr_print(ma, &gc), flags, sep,
963 tls_common_name(mi->context.c2.tls_multi, false), sep,
964 mroute_addr_print(&mi->real, &gc), sep,
965 time_string(route->last_reference, 0, false, &gc), sep,
966 (unsigned int)route->last_reference);
967 }
968 gc_free(&gc);
969 }
971
972 if (m->mbuf)
973 {
974 status_printf(so, "GLOBAL_STATS%cMax bcast/mcast queue length%c%d", sep, sep,
976 }
977
978 status_printf(so, "GLOBAL_STATS%cdco_enabled%c%d", sep, sep,
979 dco_enabled(&m->top.options));
980 status_printf(so, "END");
981 }
982 else
983 {
984 status_printf(so, "ERROR: bad status format version number");
985 }
986
987 status_flush(so);
988 gc_free(&gc_top);
989
990#ifdef ENABLE_ASYNC_PUSH
991 if (m->inotify_watchers)
992 {
993 msg(D_MULTI_DEBUG, "inotify watchers count: %u", hash_n_elements(m->inotify_watchers));
994 }
995#endif
996}
997
998/*
999 * Learn a virtual address or route.
1000 * The learn will fail if the learn address
1001 * script/plugin fails. In this case the
1002 * return value may be != mi.
1003 * Return the instance which owns this route,
1004 * or NULL if none.
1005 */
1006static struct multi_instance *
1007multi_learn_addr(struct multi_context *m, struct multi_instance *mi, const struct mroute_addr *addr,
1008 const unsigned int flags)
1009{
1010 struct hash_element *he;
1011 const uint64_t hv = hash_value(m->vhash, addr);
1012 struct hash_bucket *bucket = hash_bucket(m->vhash, hv);
1013 struct multi_route *oldroute = NULL;
1014 struct multi_instance *owner = NULL;
1015 struct gc_arena gc = gc_new();
1016
1017 /* if route currently exists, get the instance which owns it */
1018 he = hash_lookup_fast(m->vhash, bucket, addr, hv);
1019 if (he)
1020 {
1021 oldroute = (struct multi_route *)he->value;
1022 }
1023 if (oldroute && multi_route_defined(m, oldroute))
1024 {
1025 owner = oldroute->instance;
1026 }
1027
1028 /* do we need to add address to hash table? */
1029 if ((!owner || owner != mi) && mroute_learnable_address(addr, &gc)
1030 && !mroute_addr_equal(addr, &m->local))
1031 {
1032 struct multi_route *newroute;
1033 bool learn_succeeded = false;
1034
1035 ALLOC_OBJ(newroute, struct multi_route);
1036 newroute->addr = *addr;
1037 newroute->instance = mi;
1038 newroute->flags = flags;
1039 newroute->last_reference = now;
1040 newroute->cache_generation = 0;
1041
1042 /* The cache is invalidated when cache_generation is incremented */
1044 {
1046 }
1047
1048 if (oldroute) /* route already exists? */
1049 {
1050 if (route_quota_test(mi) && learn_address_script(m, mi, "update", &newroute->addr))
1051 {
1052 learn_succeeded = true;
1053 owner = mi;
1055 route_quota_inc(mi);
1056
1057 /* delete old route */
1058 multi_route_del(oldroute);
1059
1060 /* modify hash table entry, replacing old route */
1061 he->key = &newroute->addr;
1062 he->value = newroute;
1063 }
1064 }
1065 else
1066 {
1067 if (route_quota_test(mi) && learn_address_script(m, mi, "add", &newroute->addr))
1068 {
1069 learn_succeeded = true;
1070 owner = mi;
1072 route_quota_inc(mi);
1073
1074 /* add new route */
1075 hash_add_fast(m->vhash, bucket, &newroute->addr, hv, newroute);
1076 }
1077 }
1078
1079 msg(D_MULTI_LOW, "MULTI: Learn%s: %s -> %s", learn_succeeded ? "" : " FAILED",
1080 mroute_addr_print(&newroute->addr, &gc), multi_instance_string(mi, false, &gc));
1081
1082 if (!learn_succeeded)
1083 {
1084 free(newroute);
1085 }
1086 }
1087 gc_free(&gc);
1088
1089 return owner;
1090}
1091
1092/*
1093 * Get client instance based on virtual address.
1094 */
1095static struct multi_instance *
1097 bool cidr_routing)
1098{
1099 struct multi_route *route;
1100 struct multi_instance *ret = NULL;
1101
1102 /* check for local address */
1103 if (mroute_addr_equal(addr, &m->local))
1104 {
1105 return NULL;
1106 }
1107
1108 route = (struct multi_route *)hash_lookup(m->vhash, addr);
1109
1110 /* does host route (possible cached) exist? */
1111 if (route && multi_route_defined(m, route))
1112 {
1113 struct multi_instance *mi = route->instance;
1114 route->last_reference = now;
1115 ret = mi;
1116 }
1117 else if (cidr_routing) /* do we need to regenerate a host route cache entry? */
1118 {
1119 struct mroute_helper *rh = m->route_helper;
1120 struct mroute_addr tryaddr;
1121 int i;
1122
1123 /* cycle through each CIDR length */
1124 for (i = 0; i < rh->n_net_len; ++i)
1125 {
1126 tryaddr = *addr;
1127 tryaddr.type |= MR_WITH_NETBITS;
1128 tryaddr.netbits = rh->net_len[i];
1130
1131 /* look up a possible route with netbits netmask */
1132 route = (struct multi_route *)hash_lookup(m->vhash, &tryaddr);
1133
1134 if (route && multi_route_defined(m, route))
1135 {
1136 /* found an applicable route, cache host route */
1137 struct multi_instance *mi = route->instance;
1139 ret = mi;
1140 break;
1141 }
1142 }
1143 }
1144
1145#ifndef ENABLE_SMALL
1147 {
1148 struct gc_arena gc = gc_new();
1149 const char *addr_text = mroute_addr_print(addr, &gc);
1150 if (ret)
1151 {
1152 dmsg(D_MULTI_DEBUG, "GET INST BY VIRT: %s -> %s via %s", addr_text,
1153 multi_instance_string(ret, false, &gc), mroute_addr_print(&route->addr, &gc));
1154 }
1155 else
1156 {
1157 dmsg(D_MULTI_DEBUG, "GET INST BY VIRT: %s [failed]", addr_text);
1158 }
1159 gc_free(&gc);
1160 }
1161#endif
1162
1163 ASSERT(!(ret && ret->halt));
1164 return ret;
1165}
1166
1167/*
1168 * Helper function to multi_learn_addr().
1169 */
1170static struct multi_instance *
1172 int netbits, /* -1 if host route, otherwise # of network bits in address */
1173 bool primary)
1174{
1175 struct openvpn_sockaddr remote_si;
1176 struct mroute_addr addr = { 0 };
1177
1178 CLEAR(remote_si);
1179 remote_si.addr.in4.sin_family = AF_INET;
1180 remote_si.addr.in4.sin_addr.s_addr = htonl(a);
1181 addr.proto = 0;
1182 ASSERT(mroute_extract_openvpn_sockaddr(&addr, &remote_si, false));
1183
1184 if (netbits >= 0)
1185 {
1186 addr.type |= MR_WITH_NETBITS;
1187 addr.netbits = (uint8_t)netbits;
1188 }
1189
1190 struct multi_instance *owner = multi_learn_addr(m, mi, &addr, MULTI_ROUTE_PERMANENT);
1191#ifdef ENABLE_MANAGEMENT
1192 if (management && owner)
1193 {
1194 management_learn_addr(&mi->context.c2.mda_context, &addr, primary);
1195 }
1196#endif
1197 if (primary && multi_check_push_ifconfig_extra_route(&mi->context.options, addr.v4.addr))
1198 {
1199 /* "primary" is the VPN ifconfig address of the peer */
1200 /* if it does not fall into the network defined by ifconfig_local
1201 * we install this as extra onscope address on the interface */
1202 addr.netbits = 32;
1203 addr.type |= MR_ONLINK_DCO_ADDR;
1204
1205 dco_install_iroute(m, mi, &addr);
1206 }
1207 else if (!primary)
1208 {
1209 ASSERT(netbits >= 0); /* DCO requires populated netbits */
1210 dco_install_iroute(m, mi, &addr);
1211 }
1212
1213 return owner;
1214}
1215
1216static struct multi_instance *
1217multi_learn_in6_addr(struct multi_context *m, struct multi_instance *mi, struct in6_addr a6,
1218 int netbits, /* -1 if host route, otherwise # of network bits in address */
1219 bool primary)
1220{
1221 struct mroute_addr addr = { 0 };
1222
1223 addr.len = 16;
1224 addr.type = MR_ADDR_IPV6;
1225 addr.netbits = 0;
1226 addr.v6.addr = a6;
1227
1228 if (netbits >= 0)
1229 {
1230 addr.type |= MR_WITH_NETBITS;
1231 addr.netbits = (uint8_t)netbits;
1233 }
1234
1235 struct multi_instance *owner = multi_learn_addr(m, mi, &addr, MULTI_ROUTE_PERMANENT);
1236#ifdef ENABLE_MANAGEMENT
1237 if (management && owner)
1238 {
1239 management_learn_addr(&mi->context.c2.mda_context, &addr, primary);
1240 }
1241#endif
1243 {
1244 /* "primary" is the VPN ifconfig address of the peer */
1245 /* if it does not fall into the network defined by ifconfig_local
1246 * we install this as extra onscope address on the interface */
1247 addr.netbits = 128;
1248 addr.type |= MR_ONLINK_DCO_ADDR;
1249
1250 dco_install_iroute(m, mi, &addr);
1251 }
1252 else if (!primary)
1253 {
1254 /* "primary" is the VPN ifconfig address of the peer and already
1255 * known to DCO, so only install "extra" iroutes (primary = false)
1256 */
1257 ASSERT(netbits >= 0); /* DCO requires populated netbits */
1258 dco_install_iroute(m, mi, &addr);
1259 }
1260
1261 return owner;
1262}
1263
1264/*
1265 * A new client has connected, add routes (server -> client)
1266 * to internal routing table.
1267 */
1268static void
1270{
1271 struct gc_arena gc = gc_new();
1272 const struct iroute *ir;
1273 const struct iroute_ipv6 *ir6;
1275 {
1276 mi->did_iroutes = true;
1277 /* multi_learn_in{6}_addr_t takes care of installing the DCO iroute */
1278 mi->context.did_dco_iroutes = true;
1279 for (ir = mi->context.options.iroutes; ir != NULL; ir = ir->next)
1280 {
1281 if (ir->netbits >= 0)
1282 {
1283 msg(D_MULTI_LOW, "MULTI: internal route %s/%d -> %s",
1284 print_in_addr_t(ir->network, 0, &gc), ir->netbits,
1285 multi_instance_string(mi, false, &gc));
1286 }
1287 else
1288 {
1289 msg(D_MULTI_LOW, "MULTI: internal route %s -> %s",
1290 print_in_addr_t(ir->network, 0, &gc), multi_instance_string(mi, false, &gc));
1291 }
1292
1294
1295 multi_learn_in_addr_t(m, mi, ir->network, ir->netbits, false);
1296 }
1297 for (ir6 = mi->context.options.iroutes_ipv6; ir6 != NULL; ir6 = ir6->next)
1298 {
1299 msg(D_MULTI_LOW, "MULTI: internal route %s/%u -> %s",
1300 print_in6_addr(ir6->network, 0, &gc), ir6->netbits,
1301 multi_instance_string(mi, false, &gc));
1302
1304
1305 multi_learn_in6_addr(m, mi, ir6->network, ir6->netbits, false);
1306 }
1307 }
1308 gc_free(&gc);
1309}
1310
1311/*
1312 * Given an instance (new_mi), delete all other instances which use the
1313 * same common name.
1314 */
1315static void
1316multi_delete_dup(struct multi_context *m, const struct multi_instance *new_mi)
1317{
1318 if (new_mi)
1319 {
1320 const char *new_cn = tls_common_name(new_mi->context.c2.tls_multi, true);
1321 if (new_cn)
1322 {
1323 int count = 0;
1324
1325 for (uint32_t i = 0; i <= m->max_peerid; i++)
1326 {
1327 struct multi_instance *mi = m->instances[i];
1328 if (mi && mi != new_mi && !mi->halt)
1329 {
1330 const char *cn = tls_common_name(mi->context.c2.tls_multi, true);
1331 if (cn && !strcmp(cn, new_cn))
1332 {
1333 multi_close_instance(m, mi, false);
1334 ++count;
1335 }
1336 }
1337 }
1338
1339 if (count)
1340 {
1342 "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.",
1343 new_cn);
1344 }
1345 }
1346 }
1347}
1348
1349static void
1351{
1352 struct gc_arena gc = gc_new();
1353 struct hash_iterator hi;
1354 struct hash_element *he;
1355
1356 dmsg(D_MULTI_DEBUG, "MULTI: Checking stale routes");
1358 while ((he = hash_iterator_next(&hi)) != NULL)
1359 {
1360 struct multi_route *r = (struct multi_route *)he->value;
1362 && difftime(now, r->last_reference) >= m->top.options.stale_routes_ageing_time)
1363 {
1364 dmsg(D_MULTI_DEBUG, "MULTI: Deleting stale route for address '%s'",
1365 mroute_addr_print(&r->addr, &gc));
1366 learn_address_script(m, NULL, "delete", &r->addr);
1367 multi_route_del(r);
1369 }
1370 }
1371 hash_iterator_free(&hi);
1372 gc_free(&gc);
1373}
1374
1375/*
1376 * Ensure that endpoint to be pushed to client
1377 * complies with --ifconfig-push-constraint directive.
1378 */
1379static bool
1381{
1382 const struct options *o = &c->options;
1384 {
1387 }
1388 else
1389 {
1390 return true;
1391 }
1392}
1393
1394/*
1395 * Select a virtual address for a new client instance.
1396 * Use an --ifconfig-push directive, if given (static IP).
1397 * Otherwise use an --ifconfig-pool address (dynamic IP).
1398 */
1399static void
1401{
1402 struct gc_arena gc = gc_new();
1403
1404 /*
1405 * If ifconfig addresses were set by dynamic config file,
1406 * release pool addresses, otherwise keep them.
1407 */
1409 {
1410 /* ifconfig addresses were set statically,
1411 * release dynamic allocation */
1412 if (mi->vaddr_handle >= 0)
1413 {
1415 mi->vaddr_handle = -1;
1416 }
1417
1418 mi->context.c2.push_ifconfig_defined = true;
1423
1424 /* the current implementation does not allow "static IPv4, pool IPv6",
1425 * (see below) so issue a warning if that happens - don't break the
1426 * session, though, as we don't even know if this client WANTS IPv6
1427 */
1430 {
1431 msg(M_INFO,
1432 "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.");
1433 }
1434 }
1435 else if (m->ifconfig_pool && mi->vaddr_handle < 0) /* otherwise, choose a pool address */
1436 {
1437 in_addr_t local = 0, remote = 0;
1438 struct in6_addr remote_ipv6;
1439 const char *cn = NULL;
1440
1441 if (!mi->context.options.duplicate_cn)
1442 {
1443 cn = tls_common_name(mi->context.c2.tls_multi, true);
1444 }
1445
1446 CLEAR(remote_ipv6);
1447 mi->vaddr_handle =
1448 ifconfig_pool_acquire(m->ifconfig_pool, &local, &remote, &remote_ipv6, cn);
1449 if (mi->vaddr_handle >= 0)
1450 {
1451 const int tunnel_type = TUNNEL_TYPE(mi->context.c1.tuntap);
1452 const int tunnel_topology = TUNNEL_TOPOLOGY(mi->context.c1.tuntap);
1453
1454 msg(M_INFO, "MULTI_sva: pool returned IPv4=%s, IPv6=%s",
1456 : "(Not enabled)"),
1458 ? print_in6_addr(remote_ipv6, 0, &gc)
1459 : "(Not enabled)"));
1460
1462 {
1463 /* set push_ifconfig_remote_netmask from pool ifconfig address(es) */
1464 mi->context.c2.push_ifconfig_local = remote;
1465 if (tunnel_type == DEV_TYPE_TAP
1466 || (tunnel_type == DEV_TYPE_TUN && tunnel_topology == TOP_SUBNET))
1467 {
1471 {
1474 }
1475 }
1476 else if (tunnel_type == DEV_TYPE_TUN)
1477 {
1478 if (tunnel_topology == TOP_P2P)
1479 {
1481 }
1482 else if (tunnel_topology == TOP_NET30)
1483 {
1485 }
1486 }
1487
1489 {
1490 mi->context.c2.push_ifconfig_defined = true;
1491 }
1492 else
1493 {
1495 "MULTI: no --ifconfig-pool netmask parameter is available to push to %s",
1496 multi_instance_string(mi, false, &gc));
1497 }
1498 }
1499
1501 {
1502 mi->context.c2.push_ifconfig_ipv6_local = remote_ipv6;
1507 }
1508 }
1509 else
1510 {
1511 msg(D_MULTI_ERRORS, "MULTI: no free --ifconfig-pool addresses are available");
1512 }
1513 }
1514
1515 /* IPv6 push_ifconfig is a bit problematic - since IPv6 shares the
1516 * pool handling with IPv4, the combination "static IPv4, dynamic IPv6"
1517 * will fail (because no pool will be allocated in this case).
1518 * OTOH, this doesn't make too much sense in reality - and the other
1519 * way round ("dynamic IPv4, static IPv6") or "both static" makes sense
1520 * -> and so it's implemented right now
1521 */
1523 {
1528
1529 msg(M_INFO, "MULTI_sva: push_ifconfig_ipv6 %s/%d",
1532 }
1533
1534 gc_free(&gc);
1535}
1536
1537/*
1538 * Set virtual address environmental variables.
1539 */
1540static void
1542{
1543 setenv_del(mi->context.c2.es, "ifconfig_pool_local_ip");
1544 setenv_del(mi->context.c2.es, "ifconfig_pool_remote_ip");
1545 setenv_del(mi->context.c2.es, "ifconfig_pool_netmask");
1546
1548 {
1549 const int tunnel_type = TUNNEL_TYPE(mi->context.c1.tuntap);
1550 const int tunnel_topology = TUNNEL_TOPOLOGY(mi->context.c1.tuntap);
1551
1552 setenv_in_addr_t(mi->context.c2.es, "ifconfig_pool_remote_ip",
1554
1555 if (tunnel_type == DEV_TYPE_TAP
1556 || (tunnel_type == DEV_TYPE_TUN && tunnel_topology == TOP_SUBNET))
1557 {
1558 setenv_in_addr_t(mi->context.c2.es, "ifconfig_pool_netmask",
1560 }
1561 else if (tunnel_type == DEV_TYPE_TUN)
1562 {
1563 setenv_in_addr_t(mi->context.c2.es, "ifconfig_pool_local_ip",
1565 }
1566 }
1567
1568 setenv_del(mi->context.c2.es, "ifconfig_pool_local_ip6");
1569 setenv_del(mi->context.c2.es, "ifconfig_pool_remote_ip6");
1570 setenv_del(mi->context.c2.es, "ifconfig_pool_ip6_netbits");
1571
1573 {
1574 setenv_in6_addr(mi->context.c2.es, "ifconfig_pool_remote",
1576 setenv_in6_addr(mi->context.c2.es, "ifconfig_pool_local",
1578 setenv_int(mi->context.c2.es, "ifconfig_pool_ip6_netbits",
1580 }
1581}
1582
1583/*
1584 * Called after client-connect script is called
1585 */
1586static void
1587multi_client_connect_post(struct multi_context *m, struct multi_instance *mi, const char *dc_file,
1588 uint64_t *option_types_found)
1589{
1590 /* Did script generate a dynamic config file? */
1591 if (platform_test_file(dc_file))
1592 {
1594 CLIENT_CONNECT_OPT_MASK, option_types_found, mi->context.c2.es);
1595
1596 /*
1597 * If the --client-connect script generates a config file
1598 * with an --ifconfig-push directive, it will override any
1599 * --ifconfig-push directive from the --client-config-dir
1600 * directory or any --ifconfig-pool dynamic address.
1601 */
1604 }
1605}
1606
1607#ifdef ENABLE_PLUGIN
1608
1609/*
1610 * Called after client-connect plug-in is called
1611 */
1612static void
1614 const struct plugin_return *pr, uint64_t *option_types_found)
1615{
1616 struct plugin_return config;
1617
1618 plugin_return_get_column(pr, &config, "config");
1619
1620 /* Did script generate a dynamic config file? */
1621 if (plugin_return_defined(&config))
1622 {
1623 int i;
1624 for (i = 0; i < config.n; ++i)
1625 {
1626 if (config.list[i] && config.list[i]->value)
1627 {
1628 options_string_import(&mi->context.options, config.list[i]->value,
1630 option_types_found, mi->context.c2.es);
1631 }
1632 }
1633
1634 /*
1635 * If the --client-connect script generates a config file
1636 * with an --ifconfig-push directive, it will override any
1637 * --ifconfig-push directive from the --client-config-dir
1638 * directory or any --ifconfig-pool dynamic address.
1639 */
1642 }
1643}
1644
1645#endif /* ifdef ENABLE_PLUGIN */
1646
1647
1648/*
1649 * Called to load management-derived client-connect config
1650 */
1652multi_client_connect_mda(struct multi_context *m, struct multi_instance *mi, bool deferred,
1653 uint64_t *option_types_found)
1654{
1655 /* We never return CC_RET_DEFERRED */
1656 ASSERT(!deferred);
1658#ifdef ENABLE_MANAGEMENT
1659 if (mi->cc_config)
1660 {
1661 struct buffer_entry *be;
1662 for (be = mi->cc_config->head; be != NULL; be = be->next)
1663 {
1664 const char *opt = BSTR(&be->buf);
1666 CLIENT_CONNECT_OPT_MASK, option_types_found, mi->context.c2.es);
1667 }
1668
1669 /*
1670 * If the --client-connect script generates a config file
1671 * with an --ifconfig-push directive, it will override any
1672 * --ifconfig-push directive from the --client-config-dir
1673 * directory or any --ifconfig-pool dynamic address.
1674 */
1677
1678 ret = CC_RET_SUCCEEDED;
1679 }
1680#endif /* ifdef ENABLE_MANAGEMENT */
1681 return ret;
1682}
1683
1684static void
1686{
1687 struct gc_arena gc = gc_new();
1688
1689 /* setenv incoming cert common name for script */
1690 setenv_str(mi->context.c2.es, "common_name", tls_common_name(mi->context.c2.tls_multi, true));
1691
1692 /* setenv client real IP address */
1694
1695 /* setenv client virtual IP address */
1697
1698 /* setenv connection time */
1699 {
1700 const char *created_ascii = time_string(mi->created, 0, false, &gc);
1701 setenv_str(mi->context.c2.es, "time_ascii", created_ascii);
1702 setenv_long_long(mi->context.c2.es, "time_unix", mi->created);
1703 }
1704
1705 gc_free(&gc);
1706}
1707
1714static bool
1716{
1717 struct tls_multi *tls_multi = c->c2.tls_multi;
1718 const char *const peer_info = tls_multi->peer_info;
1719 struct options *o = &c->options;
1720
1721
1722 unsigned int proto = extract_iv_proto(peer_info);
1723 if (proto & IV_PROTO_DATA_V2)
1724 {
1725 tls_multi->use_peer_id = true;
1726 o->use_peer_id = true;
1727 }
1728 else if (dco_enabled(o))
1729 {
1730 msg(M_INFO, "Client does not support DATA_V2. Data channel offloading "
1731 "requires DATA_V2. Dropping client.");
1732 auth_set_client_reason(tls_multi, "Data channel negotiation "
1733 "failed (missing DATA_V2)");
1734 return false;
1735 }
1736
1737 /* Print a warning if we detect the client being in P2P mode and will
1738 * not accept our pushed ciphers */
1739 if (proto & IV_PROTO_NCP_P2P)
1740 {
1741 msg(M_WARN, "Note: peer reports running in P2P mode (no --pull/--client "
1742 "option). It will not negotiate ciphers with this server. "
1743 "Expect this connection to fail.");
1744 }
1745
1746 if (proto & IV_PROTO_REQUEST_PUSH)
1747 {
1748 c->c2.push_request_received = true;
1749 }
1750
1751 if (proto & IV_PROTO_TLS_KEY_EXPORT)
1752 {
1754 }
1755 else if (o->force_key_material_export)
1756 {
1757 msg(M_INFO, "PUSH: client does not support TLS Keying Material "
1758 "Exporters but --force-tls-key-material-export is enabled.");
1759 auth_set_client_reason(tls_multi, "Client incompatible with this "
1760 "server. Keying Material Exporters (RFC 5705) "
1761 "support missing. Upgrade to a client that "
1762 "supports this feature (OpenVPN 2.6.0+).");
1763 return false;
1764 }
1765 if (proto & IV_PROTO_DYN_TLS_CRYPT)
1766 {
1768 }
1769
1770 if (proto & IV_PROTO_CC_EXIT_NOTIFY)
1771 {
1773 }
1774
1775 /* Select cipher if client supports Negotiable Crypto Parameters */
1776
1777 /* if we have already created our key, we cannot *change* our own
1778 * cipher -> so log the fact and push the "what we have now" cipher
1779 * (so the client is always told what we expect it to use)
1780 */
1782 {
1783 msg(M_INFO,
1784 "PUSH: client wants to negotiate cipher (NCP), but "
1785 "server has already generated data channel keys, "
1786 "re-sending previously negotiated cipher '%s'",
1787 o->ciphername);
1788 return true;
1789 }
1790
1791 /*
1792 * Push the first cipher from --data-ciphers to the client that
1793 * the client announces to be supporting.
1794 */
1795 const char *push_cipher =
1797 if (push_cipher)
1798 {
1799 /* Enable epoch data key format if supported and AEAD cipher in use */
1801 && cipher_kt_mode_aead(push_cipher))
1802 {
1804 }
1805
1806 o->ciphername = push_cipher;
1807 return true;
1808 }
1809
1810 /* NCP cipher negotiation failed. Try to figure out why exactly it
1811 * failed and give good error messages and potentially do a fallback
1812 * for non NCP clients */
1813 struct gc_arena gc = gc_new();
1814 bool ret = false;
1815
1816 const char *peer_ciphers = tls_peer_ncp_list(peer_info, &gc);
1817 /* If we are in a situation where we know the client ciphers, there is no
1818 * reason to fall back to a cipher that will not be accepted by the other
1819 * side, in this situation we fail the auth*/
1820 if (strlen(peer_ciphers) > 0)
1821 {
1822 msg(M_INFO,
1823 "PUSH: No common cipher between server and client. "
1824 "Server data-ciphers: '%s'%s, client supported ciphers '%s'",
1825 o->ncp_ciphers_conf, ncp_expanded_ciphers(o, &gc), peer_ciphers);
1826 }
1827 else if (tls_multi->remote_ciphername)
1828 {
1829 msg(M_INFO,
1830 "PUSH: No common cipher between server and client. "
1831 "Server data-ciphers: '%s'%s, client supports cipher '%s'",
1833 }
1834 else
1835 {
1836 msg(M_INFO, "PUSH: No NCP or OCC cipher data received from peer.");
1837
1838 if (o->enable_ncp_fallback)
1839 {
1840 msg(M_INFO,
1841 "Using data channel cipher '%s' since "
1842 "--data-ciphers-fallback is set.",
1843 o->ciphername);
1844 ret = true;
1845 }
1846 else
1847 {
1848 msg(M_INFO, "Use --data-ciphers-fallback with the cipher the "
1849 "client is using if you want to allow the client to connect");
1850 }
1851 }
1852 if (!ret)
1853 {
1854 auth_set_client_reason(tls_multi, "Data channel cipher negotiation "
1855 "failed (no shared cipher)");
1856 }
1857
1858 gc_free(&gc);
1859 return ret;
1860}
1861
1866static void
1868{
1870 if (!ccs->deferred_ret_file)
1871 {
1872 return;
1873 }
1874
1875 setenv_del(mi->context.c2.es, "client_connect_deferred_file");
1877 {
1878 msg(D_MULTI_ERRORS, "MULTI: problem deleting temporary file: %s", ccs->deferred_ret_file);
1879 }
1880 free(ccs->deferred_ret_file);
1881 ccs->deferred_ret_file = NULL;
1882}
1883
1891static bool
1893{
1895 struct gc_arena gc = gc_new();
1896 const char *fn;
1897
1898 /* Delete file if it already exists */
1900
1902 if (!fn)
1903 {
1904 gc_free(&gc);
1905 return false;
1906 }
1907 ccs->deferred_ret_file = string_alloc(fn, NULL);
1908
1909 setenv_str(mi->context.c2.es, "client_connect_deferred_file", ccs->deferred_ret_file);
1910
1911 gc_free(&gc);
1912 return true;
1913}
1914
1923static enum client_connect_return
1925{
1926 const struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state);
1927 FILE *fp = fopen(ccs->deferred_ret_file, "r");
1928 if (!fp)
1929 {
1930 return CC_RET_SKIPPED;
1931 }
1932
1934 const int c = fgetc(fp);
1935 switch (c)
1936 {
1937 case '0':
1938 ret = CC_RET_FAILED;
1939 break;
1940
1941 case '1':
1942 ret = CC_RET_SUCCEEDED;
1943 break;
1944
1945 case '2':
1946 ret = CC_RET_DEFERRED;
1947 break;
1948
1949 case EOF:
1950 if (feof(fp))
1951 {
1952 ret = CC_RET_SKIPPED;
1953 break;
1954 }
1955
1956 /* Not EOF but other error -> fall through to error state */
1957 default:
1958 /* We received an unknown/unexpected value. Assume failure. */
1959 msg(M_WARN, "WARNING: Unknown/unexpected value in deferred "
1960 "client-connect resultfile");
1961 ret = CC_RET_FAILED;
1962 }
1963 fclose(fp);
1964
1965 return ret;
1966}
1967
1973static void
1975{
1977 if (ccs->config_file)
1978 {
1979 setenv_del(mi->context.c2.es, "client_connect_config_file");
1980 if (!platform_unlink(ccs->config_file))
1981 {
1982 msg(D_MULTI_ERRORS, "MULTI: problem deleting temporary file: %s", ccs->config_file);
1983 }
1984 free(ccs->config_file);
1985 ccs->config_file = NULL;
1986 }
1987}
1988
1996static bool
1998{
2000 struct gc_arena gc = gc_new();
2001 const char *fn;
2002
2003 if (ccs->config_file)
2004 {
2006 }
2007
2009 if (!fn)
2010 {
2011 gc_free(&gc);
2012 return false;
2013 }
2014 ccs->config_file = string_alloc(fn, NULL);
2015
2016 setenv_str(mi->context.c2.es, "client_connect_config_file", ccs->config_file);
2017
2018 gc_free(&gc);
2019 return true;
2020}
2021
2022static enum client_connect_return
2024 bool deferred, uint64_t *option_types_found)
2025{
2027#ifdef ENABLE_PLUGIN
2028 ASSERT(m);
2029 ASSERT(mi);
2030 ASSERT(option_types_found);
2032
2033 /* deprecated callback, use a file for passing back return info */
2034 if (plugin_defined(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_CONNECT))
2035 {
2036 struct argv argv = argv_new();
2037 int call;
2038
2039 if (!deferred)
2040 {
2041 call = OPENVPN_PLUGIN_CLIENT_CONNECT;
2043 {
2044 ret = CC_RET_FAILED;
2045 goto cleanup;
2046 }
2047 }
2048 else
2049 {
2050 call = OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER;
2051 /* the initial call should have created these files */
2052 ASSERT(ccs->config_file);
2054 }
2055
2056 argv_printf(&argv, "%s", ccs->config_file);
2057 int plug_ret = plugin_call(mi->context.plugins, call, &argv, NULL, mi->context.c2.es);
2058 if (plug_ret == OPENVPN_PLUGIN_FUNC_SUCCESS)
2059 {
2060 ret = CC_RET_SUCCEEDED;
2061 }
2062 else if (plug_ret == OPENVPN_PLUGIN_FUNC_DEFERRED)
2063 {
2064 ret = CC_RET_DEFERRED;
2070 }
2071 else
2072 {
2073 msg(M_WARN, "WARNING: client-connect plugin call failed");
2074 ret = CC_RET_FAILED;
2075 }
2076
2077
2083 int file_ret = ccs_test_deferred_ret_file(mi);
2084
2085 if (file_ret == CC_RET_FAILED)
2086 {
2087 ret = CC_RET_FAILED;
2088 }
2089 else if (ret == CC_RET_SUCCEEDED && file_ret == CC_RET_DEFERRED)
2090 {
2091 ret = CC_RET_DEFERRED;
2092 }
2093
2094 /* if we still think we have succeeded, do postprocessing */
2095 if (ret == CC_RET_SUCCEEDED)
2096 {
2097 multi_client_connect_post(m, mi, ccs->config_file, option_types_found);
2098 }
2099cleanup:
2100 argv_free(&argv);
2101
2102 if (ret != CC_RET_DEFERRED)
2103 {
2106 }
2107 }
2108#endif /* ifdef ENABLE_PLUGIN */
2109 return ret;
2110}
2111
2112static enum client_connect_return
2114 bool deferred, uint64_t *option_types_found)
2115{
2117#ifdef ENABLE_PLUGIN
2118 ASSERT(m);
2119 ASSERT(mi);
2120 ASSERT(option_types_found);
2121
2122 int call = deferred ? OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER_V2 : OPENVPN_PLUGIN_CLIENT_CONNECT_V2;
2123 /* V2 callback, use a plugin_return struct for passing back return info */
2124 if (plugin_defined(mi->context.plugins, call))
2125 {
2126 struct plugin_return pr;
2127
2128 plugin_return_init(&pr);
2129
2130 int plug_ret = plugin_call(mi->context.plugins, call, NULL, &pr, mi->context.c2.es);
2131 if (plug_ret == OPENVPN_PLUGIN_FUNC_SUCCESS)
2132 {
2133 multi_client_connect_post_plugin(m, mi, &pr, option_types_found);
2134 ret = CC_RET_SUCCEEDED;
2135 }
2136 else if (plug_ret == OPENVPN_PLUGIN_FUNC_DEFERRED)
2137 {
2138 ret = CC_RET_DEFERRED;
2139 if (!(plugin_defined(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER_V2)))
2140 {
2141 msg(M_WARN, "A plugin that defers from the "
2142 "OPENVPN_PLUGIN_CLIENT_CONNECT_V2 call must also "
2143 "declare support for "
2144 "OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER_V2");
2145 ret = CC_RET_FAILED;
2146 }
2147 }
2148 else
2149 {
2150 msg(M_WARN, "WARNING: client-connect-v2 plugin call failed");
2151 ret = CC_RET_FAILED;
2152 }
2153
2154
2155 plugin_return_free(&pr);
2156 }
2157#endif /* ifdef ENABLE_PLUGIN */
2158 return ret;
2159}
2160
2161static enum client_connect_return
2163 uint64_t *option_types_found)
2164{
2165 ASSERT(mi);
2166 ASSERT(option_types_found);
2167 const struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state);
2169
2171
2172 if (ret == CC_RET_SKIPPED)
2173 {
2174 /*
2175 * Skipped and deferred are equivalent in this context.
2176 * skipped means that the called program has not yet
2177 * written a return status implicitly needing more time
2178 * while deferred is the explicit notification that it
2179 * needs more time
2180 */
2181 ret = CC_RET_DEFERRED;
2182 }
2183
2184 if (ret == CC_RET_SUCCEEDED)
2185 {
2189 }
2190 if (ret == CC_RET_FAILED)
2191 {
2192 msg(M_INFO, "MULTI: deferred --client-connect script returned CC_RET_FAILED");
2195 }
2196 return ret;
2197}
2198
2202static enum client_connect_return
2204 uint64_t *option_types_found)
2205{
2206 if (deferred)
2207 {
2209 }
2210 ASSERT(m);
2211 ASSERT(mi);
2212
2215
2217 {
2218 struct argv argv = argv_new();
2219
2220 setenv_str(mi->context.c2.es, "script_type", "client-connect");
2221
2223 {
2224 ret = CC_RET_FAILED;
2225 goto cleanup;
2226 }
2227
2229 argv_printf_cat(&argv, "%s", ccs->config_file);
2230
2231 if (openvpn_run_script(&argv, mi->context.c2.es, 0, "--client-connect"))
2232 {
2234 {
2235 ret = CC_RET_DEFERRED;
2236 }
2237 else
2238 {
2239 multi_client_connect_post(m, mi, ccs->config_file, option_types_found);
2240 ret = CC_RET_SUCCEEDED;
2241 }
2242 }
2243 else
2244 {
2245 ret = CC_RET_FAILED;
2246 }
2247cleanup:
2248 if (ret != CC_RET_DEFERRED)
2249 {
2252 }
2253 argv_free(&argv);
2254 }
2255 return ret;
2256}
2257
2258static bool
2260 struct gc_arena *gc)
2261{
2262 if (!dco_enabled(&mi->context.options))
2263 {
2264 /* DCO not enabled, nothing to do, return sucess */
2265 return true;
2266 }
2267 int ret = dco_multi_add_new_peer(m, mi);
2268 if (ret < 0)
2269 {
2270 msg(D_DCO, "Cannot add peer to DCO for %s: %s (%d)", multi_instance_string(mi, false, gc),
2271 strerror(-ret), ret);
2272 return false;
2273 }
2274
2275 return true;
2276}
2277
2281static bool
2283{
2284 struct frame *frame_fragment = NULL;
2285#ifdef ENABLE_FRAGMENT
2286 if (c->options.ce.fragment)
2287 {
2288 frame_fragment = &c->c2.frame_fragment;
2289 }
2290#endif
2293 frame_fragment, get_link_socket_info(c),
2294 &c->c1.tuntap->dco))
2295 {
2296 msg(D_TLS_ERRORS, "TLS Error: initializing data channel failed");
2297 register_signal(c->sig, SIGUSR1, "process-push-msg-failed");
2298 return false;
2299 }
2300
2301 return true;
2302}
2303
2304static void
2306 const uint64_t option_types_found)
2307{
2308 ASSERT(m);
2309 ASSERT(mi);
2310
2311 struct gc_arena gc = gc_new();
2312 /*
2313 * Process sourced options.
2314 */
2315 do_deferred_options(&mi->context, option_types_found, false);
2316
2317 /*
2318 * make sure we got ifconfig settings from somewhere
2319 */
2321 {
2323 "MULTI: no dynamic or static remote "
2324 "--ifconfig address is available for %s",
2325 multi_instance_string(mi, false, &gc));
2326 }
2327
2328 /*
2329 * make sure that ifconfig settings comply with constraints
2330 */
2332 {
2333 const char *ifconfig_constraint_network =
2335 const char *ifconfig_constraint_netmask =
2337
2338 /* JYFIXME -- this should cause the connection to fail */
2340 "MULTI ERROR: primary virtual IP for %s (%s) "
2341 "violates tunnel network/netmask constraint (%s/%s)",
2342 multi_instance_string(mi, false, &gc),
2344 ifconfig_constraint_network, ifconfig_constraint_netmask);
2345 }
2346
2347 /* set our client's VPN endpoint for status reporting purposes */
2350
2351 /* set context-level authentication flag */
2353
2354 /* Since dco-win maintains iroute routing table (subnet -> peer),
2355 * peer must be added before iroutes. For other platforms it doesn't matter. */
2356
2357 /* authentication complete, calculate dynamic client specific options */
2359 {
2361 }
2362 /* only continue if setting protocol options worked */
2363 else if (!multi_client_setup_dco_initial(m, mi, &gc))
2364 {
2366 }
2367 /* Generate data channel keys only if setting protocol options
2368 * and DCO initial setup has not failed */
2370 {
2372 }
2373
2374 /* dco peer has been added, it is now safe for Windows to add iroutes */
2375
2376 /*
2377 * For routed tunnels, set up internal route to endpoint
2378 * plus add all iroute routes.
2379 */
2381 {
2383 {
2385 msg(D_MULTI_LOW, "MULTI: primary virtual IP for %s: %s",
2386 multi_instance_string(mi, false, &gc),
2388 }
2389
2391 {
2393 /* TODO: find out where addresses are "unlearned"!! */
2394 const char *ifconfig_local_ipv6 =
2396 msg(D_MULTI_LOW, "MULTI: primary virtual IPv6 for %s: %s",
2397 multi_instance_string(mi, false, &gc), ifconfig_local_ipv6);
2398 }
2399
2400 /* add routes locally, pointing to new client, if
2401 * --iroute options have been specified */
2402 multi_add_iroutes(m, mi);
2403
2404 /*
2405 * iroutes represent subnets which are "owned" by a particular
2406 * client. Therefore, do not actually push a route to a client
2407 * if it matches one of the client's iroutes.
2408 */
2410 }
2411 else if (mi->context.options.iroutes)
2412 {
2414 "MULTI: --iroute options rejected for %s -- iroute "
2415 "only works with tun-style tunnels",
2416 multi_instance_string(mi, false, &gc));
2417 }
2418
2419 /* send push reply if ready */
2421 {
2423 }
2424 gc_free(&gc);
2425}
2426
2427static void
2429{
2430 ASSERT(mi->context.c1.tuntap);
2431 /*
2432 * lock down the common name and cert hashes so they can't change
2433 * during future TLS renegotiations
2434 */
2437
2438 /* generate a msg() prefix for this client instance */
2439 generate_prefix(mi);
2440
2441 /* delete instances of previous clients with same common-name */
2442 if (!mi->context.options.duplicate_cn)
2443 {
2444 multi_delete_dup(m, mi);
2445 }
2446
2447 /* reset pool handle to null */
2448 mi->vaddr_handle = -1;
2449
2450 /* do --client-connect setenvs */
2452
2454}
2455
2456static bool
2457multi_check_dest_addr_allowed(struct multi_context *m, struct multi_instance *mi, const struct mroute_addr *real);
2458
2464static enum client_connect_return
2466 bool deferred, uint64_t *option_types_found)
2467{
2468 if (proto_is_tcp(mi->real.proto))
2469 {
2470 /* For TCP connection we neither need to be able to map incoming
2471 * DATA_V1 packets to the multi_instance nor do we need to ensure
2472 * that two TCP connections are not using the same address as the
2473 * OS takes care of that. */
2474 return CC_RET_SUCCEEDED;
2475 }
2476
2477 /* If the address is already taken up by another client we fail the new
2478 * connection */
2479 if (!multi_check_dest_addr_allowed(m, mi, &mi->real))
2480 {
2482 "MULTI: client IP address and port already assigned to another "
2483 "client, terminating connection");
2484 return CC_RET_FAILED;
2485 }
2486
2487 ASSERT(!mi->did_real_hash);
2488 ASSERT(hash_add(m->hash, &mi->real, mi, false));
2489 mi->did_real_hash = true;
2490 return CC_RET_SUCCEEDED;
2491}
2492
2499static enum client_connect_return
2501 bool deferred, uint64_t *option_types_found)
2502{
2503#ifdef USE_COMP
2504 struct options *o = &mi->context.options;
2505 const char *const peer_info = mi->context.c2.tls_multi->peer_info;
2506
2508 {
2509 if (peer_info && strstr(peer_info, "IV_COMP_STUBv2=1"))
2510 {
2511 push_option(o, "compress stub-v2", M_USAGE);
2512 }
2513 else
2514 {
2515 /* Client is old and does not support STUBv2 but since it
2516 * announced comp-lzo via OCC we assume it uses comp-lzo, so
2517 * switch to that and push the uncompressed variant. */
2518 push_option(o, "comp-lzo no", M_USAGE);
2519 o->comp.alg = COMP_ALG_STUB;
2520 *option_types_found |= OPT_P_COMP;
2521 }
2522 }
2523#endif
2524 return CC_RET_SUCCEEDED;
2525}
2526
2531static enum client_connect_return
2533 uint64_t *option_types_found)
2534{
2535 /* Since we never return a CC_RET_DEFERRED, this indicates a serious
2536 * problem */
2537 ASSERT(!deferred);
2540 {
2541 struct gc_arena gc = gc_new();
2542 const char *ccd_file = NULL;
2543
2544 const char *ccd_client =
2546 tls_common_name(mi->context.c2.tls_multi, false), &gc);
2547
2548 const char *ccd_default =
2550
2551
2552 /* try common-name file */
2553 if (platform_test_file(ccd_client))
2554 {
2555 ccd_file = ccd_client;
2556 }
2557 /* try default file */
2558 else if (platform_test_file(ccd_default))
2559 {
2560 ccd_file = ccd_default;
2561 }
2562
2563 if (ccd_file)
2564 {
2566 CLIENT_CONNECT_OPT_MASK, option_types_found, mi->context.c2.es);
2567 /*
2568 * Select a virtual address from either --ifconfig-push in
2569 * --client-config-dir file or --ifconfig-pool.
2570 */
2572
2574
2575 ret = CC_RET_SUCCEEDED;
2576 }
2577 gc_free(&gc);
2578 }
2579 return ret;
2580}
2581
2583 struct multi_context *m, struct multi_instance *mi, bool from_deferred,
2584 uint64_t *option_types_found);
2585
2596
2601static bool
2603{
2604 struct tls_multi *multi = mi->context.c2.tls_multi;
2605 const struct options *options = &mi->context.options;
2606 struct tls_session *session = &multi->session[TM_ACTIVE];
2607
2608 if (!multi->locked_username)
2609 {
2610 msg(D_MULTI_ERRORS, "MULTI: Ignoring override-username as no "
2611 "user/password method is enabled. Enable "
2612 "--management-client-auth, --auth-user-pass-verify, or a "
2613 "plugin with user/password verify capability.");
2614 return false;
2615 }
2616
2617 if (!multi->locked_original_username
2618 && strcmp(multi->locked_username, options->override_username) != 0)
2619 {
2620 /* Check if the username length is acceptable */
2622 {
2623 return false;
2624 }
2625
2628
2629 /* Override also the common name if username should be set as common
2630 * name */
2631 if ((session->opt->ssl_flags & SSLF_USERNAME_AS_COMMON_NAME))
2632 {
2634 free(multi->locked_cn);
2635 multi->locked_cn = NULL;
2636 tls_lock_common_name(multi);
2637 }
2638
2639 /* Regenerate the auth-token if enabled */
2640 if (multi->auth_token_initial)
2641 {
2642 struct user_pass up;
2643 CLEAR(up);
2644 strlcpy(up.username, multi->locked_username, sizeof(up.username));
2645
2646 generate_auth_token(&up, multi);
2647 }
2648
2650 "MULTI: Note, override-username changes username "
2651 "from '%s' to '%s'",
2653 }
2654 return true;
2655}
2656
2657/*
2658 * Called as soon as the SSL/TLS connection is authenticated.
2659 *
2660 * Will collect the client specific configuration from the different
2661 * sources like ccd files, connect plugins and management interface.
2662 *
2663 * This method starts with cas_context CAS_PENDING and will move the
2664 * state machine to either CAS_SUCCEEDED on success or
2665 * CAS_FAILED/CAS_PARTIAL on failure.
2666 *
2667 * Instance-specific directives to be processed (CLIENT_CONNECT_OPT_MASK)
2668 * include:
2669 *
2670 * iroute start-ip end-ip
2671 * ifconfig-push local remote-netmask
2672 * push
2673 *
2674 *
2675 */
2676static void
2678{
2679 /* We are only called for the CAS_PENDING_x states, so we
2680 * can ignore other states here */
2681 bool from_deferred = (mi->context.c2.tls_multi->multi_state != CAS_PENDING);
2682
2683 int *cur_handler_index = &mi->client_connect_defer_state.cur_handler_index;
2684 uint64_t *option_types_found = &mi->client_connect_defer_state.option_types_found;
2685
2686 /* We are called for the first time */
2687 if (!from_deferred)
2688 {
2689 *cur_handler_index = 0;
2690 *option_types_found = 0;
2691 /* Initially we have no handler that has returned a result */
2693
2695 }
2696
2697 bool cc_succeeded = true;
2698
2699 while (cc_succeeded && client_connect_handlers[*cur_handler_index] != NULL)
2700 {
2701 enum client_connect_return ret;
2702 ret = client_connect_handlers[*cur_handler_index](m, mi, from_deferred, option_types_found);
2703
2704 from_deferred = false;
2705
2706 switch (ret)
2707 {
2708 case CC_RET_SUCCEEDED:
2709 /*
2710 * Remember that we already had at least one handler
2711 * returning a result should we go to into deferred state
2712 */
2713 mi->context.c2.tls_multi->multi_state = CAS_PENDING_DEFERRED_PARTIAL;
2714 break;
2715
2716 case CC_RET_SKIPPED:
2717 /*
2718 * Move on with the next handler without modifying any
2719 * other state
2720 */
2721 break;
2722
2723 case CC_RET_DEFERRED:
2724 /*
2725 * we already set multi_status to DEFERRED_RESULT or
2726 * DEFERRED_NO_RESULT. We just return
2727 * from the function as having multi_status
2728 */
2729 return;
2730
2731 case CC_RET_FAILED:
2732 /*
2733 * One handler failed. We abort the chain and set the final
2734 * result to failed
2735 */
2736 cc_succeeded = false;
2737 break;
2738
2739 default:
2740 ASSERT(0);
2741 }
2742
2743 /*
2744 * Check for "disable" directive in client-config-dir file
2745 * or config file generated by --client-connect script.
2746 */
2747 if (mi->context.options.disable)
2748 {
2749 msg(D_MULTI_ERRORS, "MULTI: client has been rejected due to "
2750 "'disable' directive");
2751 cc_succeeded = false;
2752 }
2753
2754 (*cur_handler_index)++;
2755 }
2756
2758 {
2759 if (!override_locked_username(mi))
2760 {
2761 cc_succeeded = false;
2762 }
2763 }
2764
2765 /* Check if we have forbidding options in the current mode */
2766 if (dco_enabled(&mi->context.options)
2768 {
2769 msg(D_MULTI_ERRORS, "MULTI: client has been rejected due to incompatible DCO options");
2770 cc_succeeded = false;
2771 }
2772
2774 {
2775 msg(D_MULTI_ERRORS, "MULTI: client has been rejected due to invalid compression options");
2776 cc_succeeded = false;
2777 }
2778
2779 if (cc_succeeded)
2780 {
2781 multi_client_connect_late_setup(m, mi, *option_types_found);
2782 }
2783 else
2784 {
2785 /* run the disconnect script if we had a connect script that
2786 * did not fail */
2788 {
2790 }
2791
2793 }
2794
2795 /* increment number of current authenticated clients */
2796 ++m->n_clients;
2797 --mi->n_clients_delta;
2798
2799#ifdef ENABLE_MANAGEMENT
2800 if (management)
2801 {
2803 mi->context.c2.es);
2804 }
2805#endif
2806}
2807
2808#ifdef ENABLE_ASYNC_PUSH
2809/*
2810 * Called when inotify event is fired, which happens when acf
2811 * or connect-status file is closed or deleted.
2812 * Continues authentication and sends push_reply
2813 * (or be deferred again by client-connect)
2814 */
2815void
2816multi_process_file_closed(struct multi_context *m, const unsigned int mpp_flags)
2817{
2818 char buffer[INOTIFY_EVENT_BUFFER_SIZE];
2819 ssize_t buffer_i = 0;
2820 ssize_t r = read(m->top.c2.inotify_fd, buffer, INOTIFY_EVENT_BUFFER_SIZE);
2821 if (r < 0)
2822 {
2823 msg(M_WARN | M_ERRNO, "MULTI: multi_process_file_closed error");
2824 return;
2825 }
2826
2827 while (buffer_i < r)
2828 {
2829 /* parse inotify events */
2830 struct inotify_event *pevent = (struct inotify_event *)&buffer[buffer_i];
2831 size_t event_size = sizeof(struct inotify_event) + pevent->len;
2832 buffer_i += event_size;
2833
2834 msg(D_MULTI_DEBUG, "MULTI: modified fd %d, mask %d", pevent->wd, pevent->mask);
2835
2836 struct multi_instance *mi =
2837 hash_lookup(m->inotify_watchers, (void *)(uintptr_t)pevent->wd);
2838
2839 if (pevent->mask & IN_CLOSE_WRITE)
2840 {
2841 if (mi)
2842 {
2843 /* continue authentication, perform NCP negotiation and send push_reply */
2844 multi_process_post(m, mi, mpp_flags);
2845 }
2846 else
2847 {
2848 msg(D_MULTI_ERRORS, "MULTI: multi_instance not found!");
2849 }
2850 }
2851 else if (pevent->mask & IN_IGNORED)
2852 {
2853 /* this event is _always_ fired when watch is removed or file is deleted */
2854 if (mi)
2855 {
2856 hash_remove(m->inotify_watchers, (void *)(uintptr_t)pevent->wd);
2857 mi->inotify_watch = -1;
2858 }
2859 }
2860 else
2861 {
2862 msg(D_MULTI_ERRORS, "MULTI: unknown mask %d", pevent->mask);
2863 }
2864 }
2865}
2866#endif /* ifdef ENABLE_ASYNC_PUSH */
2867
2868/*
2869 * Add a mbuf buffer to a particular
2870 * instance.
2871 */
2872static void
2873multi_add_mbuf(struct multi_context *m, struct multi_instance *mi, struct mbuf_buffer *mb)
2874{
2875 if (multi_output_queue_ready(m, mi))
2876 {
2877 struct mbuf_item item;
2878 item.buffer = mb;
2879 item.instance = mi;
2880 mbuf_add_item(m->mbuf, &item);
2881 }
2882 else
2883 {
2884 msg(D_MULTI_DROPPED, "MULTI: packet dropped due to output saturation (multi_add_mbuf)");
2885 }
2886}
2887
2888/*
2889 * Add a packet to a client instance output queue.
2890 */
2891static inline void
2892multi_unicast(struct multi_context *m, const struct buffer *buf, struct multi_instance *mi)
2893{
2894 struct mbuf_buffer *mb;
2895
2896 if (BLEN(buf) > 0)
2897 {
2898 mb = mbuf_alloc_buf(buf);
2899 mb->flags = MF_UNICAST;
2900 multi_add_mbuf(m, mi, mb);
2901 mbuf_free_buf(mb);
2902 }
2903}
2904
2905/*
2906 * Broadcast a packet to all clients.
2907 */
2908static void
2909multi_bcast(struct multi_context *m, const struct buffer *buf,
2910 const struct multi_instance *sender_instance, uint16_t vid)
2911{
2912 struct mbuf_buffer *mb;
2913
2914 if (BLEN(buf) > 0)
2915 {
2916#ifdef MULTI_DEBUG_EVENT_LOOP
2917 printf("BCAST len=%d\n", BLEN(buf));
2918#endif
2919 mb = mbuf_alloc_buf(buf);
2920
2921 for (uint32_t i = 0; i <= m->max_peerid; i++)
2922 {
2923 struct multi_instance *mi = m->instances[i];
2924
2925 if (mi && mi != sender_instance && !mi->halt)
2926 {
2927 if (vid != 0 && vid != mi->context.options.vlan_pvid)
2928 {
2929 continue;
2930 }
2931 multi_add_mbuf(m, mi, mb);
2932 }
2933 }
2934 mbuf_free_buf(mb);
2935 }
2936}
2937
2938/*
2939 * Given a time delta, indicating that we wish to be
2940 * awoken by the scheduler at time now + delta, figure
2941 * a sigma parameter (in microseconds) that represents
2942 * a sort of fuzz factor around delta, so that we're
2943 * really telling the scheduler to wake us up any time
2944 * between now + delta - sigma and now + delta + sigma.
2945 *
2946 * The sigma parameter helps the scheduler to run more efficiently.
2947 * Sigma should be no larger than TV_WITHIN_SIGMA_MAX_USEC
2948 */
2949static inline unsigned int
2950compute_wakeup_sigma(const struct timeval *delta)
2951{
2952 ASSERT(delta->tv_sec >= 0);
2953 ASSERT(delta->tv_usec >= 0);
2954 if (delta->tv_sec < 1)
2955 {
2956 /* if < 1 sec, fuzz = # of microseconds / 8 */
2957 return (unsigned int)(delta->tv_usec >> 3);
2958 }
2959 else
2960 {
2961 /* if < 10 minutes, fuzz = 13.1% of timeout */
2962 if (delta->tv_sec < 600)
2963 {
2964 return (unsigned int)(delta->tv_sec << 17);
2965 }
2966 else
2967 {
2968 return 120 * 1000000; /* if >= 10 minutes, fuzz = 2 minutes */
2969 }
2970 }
2971}
2972
2973static void
2975{
2976 /* calculate an absolute wakeup time */
2977 ASSERT(!openvpn_gettimeofday(&mi->wakeup, NULL));
2978 tv_add(&mi->wakeup, &mi->context.c2.timeval);
2979
2980 /* tell scheduler to wake us up at some point in the future */
2981 schedule_add_entry(m->schedule, (struct schedule_entry *)mi, &mi->wakeup,
2983}
2984
2985#if defined(ENABLE_ASYNC_PUSH)
2986static void
2987add_inotify_file_watch(struct multi_context *m, struct multi_instance *mi, int inotify_fd,
2988 const char *file)
2989{
2990 /* watch acf file */
2991 int watch_descriptor = inotify_add_watch(inotify_fd, file, IN_CLOSE_WRITE | IN_ONESHOT);
2992 if (watch_descriptor >= 0)
2993 {
2994 if (mi->inotify_watch != -1)
2995 {
2996 hash_remove(m->inotify_watchers, (void *)(uintptr_t)mi->inotify_watch);
2997 }
2998 hash_add(m->inotify_watchers, (void *)(uintptr_t)watch_descriptor, mi, true);
2999 mi->inotify_watch = watch_descriptor;
3000 }
3001 else
3002 {
3003 msg(M_NONFATAL | M_ERRNO, "MULTI: inotify_add_watch error");
3004 }
3005}
3006#endif /* if defined(ENABLE_ASYNC_PUSH) */
3007
3008/*
3009 * Figure instance-specific timers, convert
3010 * earliest to absolute time in mi->wakeup,
3011 * call scheduler with our future wakeup time.
3012 *
3013 * Also close context on signal.
3014 */
3015bool
3016multi_process_post(struct multi_context *m, struct multi_instance *mi, const unsigned int flags)
3017{
3018 bool ret = true;
3019
3020 if (!IS_SIG(&mi->context)
3021 && ((flags & MPP_PRE_SELECT)))
3022 {
3023#if defined(ENABLE_ASYNC_PUSH)
3024 bool was_unauthenticated = true;
3025 const struct key_state *ks = NULL;
3026 if (mi->context.c2.tls_multi)
3027 {
3029 was_unauthenticated = (ks->authenticated == KS_AUTH_FALSE);
3030 }
3031#endif
3032
3033 /* figure timeouts and fetch possible outgoing
3034 * to_link packets (such as ping or TLS control) */
3035 pre_select(&mi->context);
3036
3037#if defined(ENABLE_ASYNC_PUSH)
3038 /*
3039 * if we see the state transition from unauthenticated to deferred
3040 * and an auth_control_file, we assume it got just added and add
3041 * inotify watch to that file
3042 */
3043 if (ks && ks->plugin_auth.auth_control_file && was_unauthenticated
3044 && (ks->authenticated == KS_AUTH_DEFERRED))
3045 {
3046 add_inotify_file_watch(m, mi, m->top.c2.inotify_fd, ks->plugin_auth.auth_control_file);
3047 }
3048 if (ks && ks->script_auth.auth_control_file && was_unauthenticated
3049 && (ks->authenticated == KS_AUTH_DEFERRED))
3050 {
3051 add_inotify_file_watch(m, mi, m->top.c2.inotify_fd, ks->script_auth.auth_control_file);
3052 }
3053#endif
3054
3055 if (!IS_SIG(&mi->context))
3056 {
3057 /* connection is "established" when SSL/TLS key negotiation succeeds
3058 * and (if specified) auth user/pass succeeds */
3059
3061 {
3063 }
3064#if defined(ENABLE_ASYNC_PUSH)
3067 {
3068 add_inotify_file_watch(m, mi, m->top.c2.inotify_fd,
3070 }
3071#endif
3072 /* tell scheduler to wake us up at some point in the future */
3074 }
3075 }
3076
3077 if (IS_SIG(&mi->context))
3078 {
3079 if (flags & MPP_CLOSE_ON_SIGNAL)
3080 {
3082 ret = false;
3083 }
3084 }
3085 else
3086 {
3087 /* continue to pend on output? */
3088 multi_set_pending(m, ANY_OUT(&mi->context) ? mi : NULL);
3089
3090#ifdef MULTI_DEBUG_EVENT_LOOP
3091 printf("POST %s[%d] to=%d lo=%d/%d w=%" PRIi64 "/%ld\n", id(mi), (int)(mi == m->pending),
3093 mi->context.c2.fragment ? mi->context.c2.fragment->outgoing.len : -1,
3094 (int64_t)mi->context.c2.timeval.tv_sec, (long)mi->context.c2.timeval.tv_usec);
3095#endif
3096 }
3097
3098 if ((flags & MPP_RECORD_TOUCH) && m->mpp_touched)
3099 {
3100 *m->mpp_touched = mi;
3101 }
3102
3103 return ret;
3104}
3105
3116static bool
3118 const struct mroute_addr *real)
3119{
3120 struct hash *hash = m->hash;
3121 const uint64_t hv = hash_value(hash, real);
3122 struct hash_bucket *bucket = hash_bucket(hash, hv);
3123
3124 /* make sure that we don't assign the client to an address taken by
3125 * another client */
3126 struct hash_element *he = hash_lookup_fast(hash, bucket, real, hv);
3127 if (!he)
3128 {
3129 /* Address is not taken, everything is fine. */
3130 return true;
3131 }
3132
3133 struct multi_instance *ex_mi = he->value;
3134
3135 const struct tls_multi *m1 = mi->context.c2.tls_multi;
3136 const struct tls_multi *m2 = ex_mi->context.c2.tls_multi;
3137
3138 struct gc_arena gc = gc_new();
3139 int ret = false;
3140
3141 /* do not allow if target address is taken by client with another cert */
3143 {
3144 msg(D_MULTI_LOW, "Disallow float/connect to an address taken by another client %s",
3145 multi_instance_string(ex_mi, false, &gc));
3146
3147 mi->context.c2.buf.len = 0;
3148 goto done;
3149 }
3150
3151 /* do not allow if target address has a different username */
3152 if (m1->locked_username || m2->locked_username)
3153 {
3154 if (!m1->locked_username || !m2->locked_username
3155 || strcmp(m1->locked_username, m2->locked_username) != 0)
3156 {
3157 msg(D_MULTI_LOW, "Disallow float/connect to an address taken by another client %s",
3158 multi_instance_string(ex_mi, false, &gc));
3159 mi->context.c2.buf.len = 0;
3160 goto done;
3161 }
3162 }
3163
3164 /* It doesn't make sense to let a peer float to the address it already
3165 * has, so we disallow it. This can happen if a DCO netlink notification
3166 * gets lost and we miss a floating step.
3167 */
3168 if (m1->rx_peer_id == m2->rx_peer_id)
3169 {
3170 msg(M_WARN,
3171 "disallowing peer %" PRIu32 " (%s) from floating to "
3172 "its own address (%s)",
3174 mroute_addr_print(&mi->real, &gc));
3175 goto done;
3176 }
3177
3179 "closing instance %s due to float collision with %s "
3180 "using the same certificate and username",
3181 multi_instance_string(ex_mi, false, &gc), multi_instance_string(mi, false, &gc));
3182 multi_close_instance(m, ex_mi, false);
3183 ret = true;
3184
3185done:
3186 gc_free(&gc);
3187 return ret;
3188}
3189
3198static void
3200{
3201 struct mroute_addr real = { 0 };
3202
3203 if (mi->real.type & MR_WITH_PROTO)
3204 {
3205 real.type |= MR_WITH_PROTO;
3206 real.proto = sock->info.proto;
3207 }
3208
3209 if (!mroute_extract_openvpn_sockaddr(&real, &m->top.c2.from.dest, true))
3210 {
3211 return;
3212 }
3213
3214 if (!multi_check_dest_addr_allowed(m, mi, &real))
3215 {
3216 return;
3217 }
3218
3219 struct gc_arena gc = gc_new();
3220
3221 msg(D_MULTI_MEDIUM, "peer %" PRIu32 " (%s) floated from %s to %s",
3223 tls_common_name(mi->context.c2.tls_multi, false),
3226
3227 /* remove old address from hash table before changing address */
3228 ASSERT(hash_remove(m->hash, &mi->real));
3229
3230 /* change external network address of the remote peer */
3231 mi->real = real;
3232 generate_prefix(mi);
3233
3234 mi->context.c2.from = m->top.c2.from;
3235 mi->context.c2.to_link_addr = &mi->context.c2.from;
3236
3237 /* inherit parent link_socket and link_socket_info */
3238 mi->context.c2.link_sockets[0] = sock;
3240
3242
3243 ASSERT(hash_add(m->hash, &mi->real, mi, false));
3244
3245#ifdef ENABLE_MANAGEMENT
3246 ASSERT(hash_add(m->cid_hash, &mi->context.c2.mda_context.cid, mi, true));
3247#endif
3248
3249 gc_free(&gc);
3250}
3251
3252/*
3253 * Called when an instance should be closed due to the
3254 * reception of a soft signal.
3255 */
3256void
3258{
3259 remap_signal(&mi->context);
3260 set_prefix(mi);
3261 print_signal(mi->context.sig, "client-instance", D_MULTI_LOW);
3262 clear_prefix();
3263 multi_close_instance(m, mi, false);
3264}
3265
3266#if (defined(ENABLE_DCO) && (defined(TARGET_LINUX) || defined(TARGET_FREEBSD))) \
3267 || defined(ENABLE_MANAGEMENT)
3268static void
3269multi_signal_instance(struct multi_context *m, struct multi_instance *mi, const int sig)
3270{
3271 mi->context.sig->signal_received = sig;
3273}
3274#endif
3275
3276#if defined(ENABLE_DCO)
3277static void
3278process_incoming_del_peer(struct multi_context *m, struct multi_instance *mi, const dco_context_t *dco)
3279{
3280 const char *reason = "ovpn-dco: unknown reason";
3281 switch (dco->dco_del_peer_reason)
3282 {
3284 reason = "ovpn-dco: ping expired";
3285 break;
3286
3288 reason = "ovpn-dco: transport error";
3289 break;
3290
3292 reason = "ovpn-dco: transport disconnected";
3293 break;
3294
3296 /* We assume that is ourselves. Unfortunately, sometimes these
3297 * events happen with enough delay that they can have an order of
3298 *
3299 * dco_del_peer x
3300 * [new client connecting]
3301 * dco_new_peer x
3302 * event from dco_del_peer arrives.
3303 *
3304 * if we do not ignore this we get desynced with the kernel
3305 * since we assume the peer-id is free again. The other way would
3306 * be to send a dco_del_peer again
3307 */
3308 return;
3309 }
3310
3311 /* When kernel already deleted the peer, the socket is no longer
3312 * installed, and we do not need to clean up the state in the kernel */
3313 mi->context.c2.tls_multi->dco_peer_id = -1;
3314 mi->context.sig->signal_text = reason;
3316}
3317
3318void
3320{
3321 ASSERT(dco->c->multi);
3322
3323 struct multi_context *m = dco->c->multi;
3324
3325 int peer_id = dco->dco_message_peer_id;
3326
3327 /* no peer-specific message delivered -> nothing to process.
3328 * bail out right away
3329 */
3330 if (peer_id < 0)
3331 {
3332 return;
3333 }
3334
3335 if (((uint32_t)peer_id < m->max_clients) && m->instances[peer_id])
3336 {
3337 struct multi_instance *mi = m->instances[peer_id];
3338 set_prefix(mi);
3339 if (dco->dco_message_type == OVPN_CMD_DEL_PEER)
3340 {
3341 process_incoming_del_peer(m, mi, dco);
3342 }
3343 else if (dco->dco_message_type == OVPN_CMD_FLOAT_PEER)
3344 {
3345 ASSERT(mi->context.c2.link_sockets[0]);
3347 &m->top.c2.from.dest,
3348 (struct sockaddr *)&dco->dco_float_peer_ss);
3350 CLEAR(dco->dco_float_peer_ss);
3351 }
3352 else if (dco->dco_message_type == OVPN_CMD_SWAP_KEYS)
3353 {
3355 }
3356 clear_prefix();
3357 }
3358 else
3359 {
3360 msglvl_t msglevel = D_DCO;
3361 if (dco->dco_message_type == OVPN_CMD_DEL_PEER
3362 && dco->dco_del_peer_reason == OVPN_DEL_PEER_REASON_USERSPACE)
3363 {
3364 /* we receive OVPN_CMD_DEL_PEER message with reason USERSPACE
3365 * after we kill the peer ourselves. This peer may have already
3366 * been deleted, so we end up here.
3367 * In this case, print the following debug message with DCO_DEBUG
3368 * level only to avoid polluting the standard DCO level with this
3369 * harmless event.
3370 */
3371 msglevel = D_DCO_DEBUG;
3372 }
3373 msg(msglevel,
3374 "Received DCO message for unknown peer-id: %d, "
3375 "type %d, del_peer_reason %d",
3376 peer_id, dco->dco_message_type, dco->dco_del_peer_reason);
3377 }
3378}
3379#endif /* if defined(ENABLE_DCO) */
3380
3387static void
3388multi_process_incoming_link_data(struct multi_context *m, bool floated, struct link_socket *sock)
3389{
3390 struct link_socket_info *lsi = &sock->info;
3391 const uint8_t *orig_buf;
3392
3393 /* decrypt in instance context */
3394 struct context *c = &m->pending->context;
3395
3396 orig_buf = c->c2.buf.data;
3397 if (process_incoming_link_part1(c, lsi, floated))
3398 {
3399 /* nonzero length means that we have a valid, decrypted packed */
3400 if (floated && c->c2.buf.len > 0)
3401 {
3402 multi_process_float(m, m->pending, sock);
3403 }
3404
3405 process_incoming_link_part2(c, lsi, orig_buf);
3406 }
3407
3409 {
3410 struct mroute_addr src, dest;
3411 /* extract packet source and dest addresses */
3412 unsigned int 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 {
3431 struct gc_arena gc = gc_new();
3433 "MULTI: bad source address from client [%s], packet dropped",
3434 mroute_addr_print(&src, &gc));
3435 gc_free(&gc);
3436 }
3437 c->c2.to_tun.len = 0;
3438 }
3439 /* client-to-client communication enabled? */
3440 else if (m->enable_c2c)
3441 {
3442 /* multicast? */
3443 if (mroute_flags & MROUTE_EXTRACT_MCAST)
3444 {
3445 /* for now, treat multicast as broadcast */
3446 multi_bcast(m, &c->c2.to_tun, m->pending, 0);
3447 }
3448 else /* possible client to client routing */
3449 {
3450 ASSERT(!(mroute_flags & MROUTE_EXTRACT_BCAST));
3451 struct multi_instance *mi = multi_get_instance_by_virtual_addr(m, &dest, true);
3452
3453 /* if dest addr is a known client, route to it */
3454 if (mi)
3455 {
3456 {
3457 multi_unicast(m, &c->c2.to_tun, mi);
3459 }
3460 c->c2.to_tun.len = 0;
3461 }
3462 }
3463 }
3464 }
3465 else if (TUNNEL_TYPE(m->top.c1.tuntap) == DEV_TYPE_TAP)
3466 {
3467 uint16_t vid = 0;
3468
3469 if (m->top.options.vlan_tagging)
3470 {
3471 if (vlan_is_tagged(&c->c2.to_tun))
3472 {
3473 /* Drop VLAN-tagged frame. */
3474 msg(D_VLAN_DEBUG, "dropping incoming VLAN-tagged frame");
3475 c->c2.to_tun.len = 0;
3476 }
3477 else
3478 {
3479 vid = c->options.vlan_pvid;
3480 }
3481 }
3482 /* extract packet source and dest addresses */
3483 struct mroute_addr src, dest;
3484 /* extract packet source and dest addresses */
3485 unsigned int mroute_flags =
3487
3488 if (mroute_flags & MROUTE_EXTRACT_SUCCEEDED)
3489 {
3490 if (multi_learn_addr(m, m->pending, &src, 0) == m->pending)
3491 {
3492 /* check for broadcast */
3493 if (m->enable_c2c)
3494 {
3495 if (mroute_flags & (MROUTE_EXTRACT_BCAST | MROUTE_EXTRACT_MCAST))
3496 {
3497 multi_bcast(m, &c->c2.to_tun, m->pending, vid);
3498 }
3499 else /* try client-to-client routing */
3500 {
3501 struct multi_instance *mi = multi_get_instance_by_virtual_addr(m, &dest, false);
3502
3503 /* if dest addr is a known client, route to it */
3504 if (mi)
3505 {
3506 multi_unicast(m, &c->c2.to_tun, mi);
3508 c->c2.to_tun.len = 0;
3509 }
3510 }
3511 }
3512 }
3513 else
3514 {
3515 struct gc_arena gc = gc_new();
3517 "MULTI: bad source address from client [%s], packet dropped",
3518 mroute_addr_print(&src, &gc));
3519 c->c2.to_tun.len = 0;
3520 gc_free(&gc);
3521 }
3522 }
3523 else
3524 {
3525 c->c2.to_tun.len = 0;
3526 }
3527 }
3528}
3529
3530/*
3531 * Process packets in the TCP/UDP socket -> TUN/TAP interface direction,
3532 * i.e. client -> server direction.
3533 */
3534bool
3536 const unsigned int mpp_flags, struct link_socket *sock)
3537{
3538 struct context *c;
3539 bool ret = true;
3540 bool floated = false;
3541
3542 if (m->pending)
3543 {
3544 return true;
3545 }
3546
3547 if (!instance)
3548 {
3549#ifdef MULTI_DEBUG_EVENT_LOOP
3550 printf("TCP/UDP -> TUN [%d]\n", BLEN(&m->top.c2.buf));
3551#endif
3552 multi_set_pending(m, multi_get_create_instance_udp(m, &floated, sock));
3553 }
3554 else
3555 {
3556 multi_set_pending(m, instance);
3557 }
3558
3559 if (!m->pending)
3560 {
3561 return true;
3562 }
3563 set_prefix(m->pending);
3564
3565 /* get instance context */
3566 c = &m->pending->context;
3567
3568 if (!instance)
3569 {
3570 /* transfer packet pointer from top-level context buffer to instance */
3571 c->c2.buf = m->top.c2.buf;
3572
3573 /* transfer from-addr from top-level context buffer to instance */
3574 if (!floated)
3575 {
3576 c->c2.from = m->top.c2.from;
3577 }
3578 }
3579
3580 if (BLEN(&c->c2.buf) > 0)
3581 {
3582 multi_process_incoming_link_data(m, floated, sock);
3583 }
3584
3585 /* postprocess and set wakeup */
3586 ret = multi_process_post(m, m->pending, mpp_flags);
3587
3588 clear_prefix();
3589
3590 return ret;
3591}
3592
3593/*
3594 * Process packets in the TUN/TAP interface -> TCP/UDP socket direction,
3595 * i.e. server -> client direction.
3596 */
3597bool
3598multi_process_incoming_tun(struct multi_context *m, const unsigned int mpp_flags)
3599{
3600 bool ret = true;
3601
3602 if (BLEN(&m->top.c2.buf) > 0)
3603 {
3604 unsigned int mroute_flags;
3605 struct mroute_addr src = { 0 }, dest = { 0 };
3606 const int dev_type = TUNNEL_TYPE(m->top.c1.tuntap);
3607 int16_t vid = 0;
3608
3609#ifdef MULTI_DEBUG_EVENT_LOOP
3610 printf("TUN -> TCP/UDP [%d]\n", BLEN(&m->top.c2.buf));
3611#endif
3612
3613 if (m->pending)
3614 {
3615 return true;
3616 }
3617
3618 if (dev_type == DEV_TYPE_TAP && m->top.options.vlan_tagging)
3619 {
3620 vid = vlan_decapsulate(&m->top, &m->top.c2.buf);
3621 if (vid < 0)
3622 {
3623 return false;
3624 }
3625 }
3626
3627 /*
3628 * Route an incoming tun/tap packet to
3629 * the appropriate multi_instance object.
3630 */
3631 mroute_flags = mroute_extract_addr_from_packet(&src, &dest, vid, &m->top.c2.buf, dev_type);
3632
3633 if (mroute_flags & MROUTE_EXTRACT_SUCCEEDED)
3634 {
3635 struct context *c;
3636
3637 /* broadcast or multicast dest addr? */
3638 if (mroute_flags & (MROUTE_EXTRACT_BCAST | MROUTE_EXTRACT_MCAST))
3639 {
3640 /* for now, treat multicast as broadcast */
3641 multi_bcast(m, &m->top.c2.buf, NULL, vid);
3642 }
3643 else
3644 {
3646 m, multi_get_instance_by_virtual_addr(m, &dest, dev_type == DEV_TYPE_TUN));
3647
3648 if (m->pending)
3649 {
3650 /* get instance context */
3651 c = &m->pending->context;
3652
3653 set_prefix(m->pending);
3654
3655 {
3657 {
3658 /* transfer packet pointer from top-level context buffer to instance */
3659 c->c2.buf = m->top.c2.buf;
3660 }
3661 else
3662 {
3663 /* drop packet */
3665 "MULTI: packet dropped due to output saturation (multi_process_incoming_tun)");
3666 buf_reset_len(&c->c2.buf);
3667 }
3668 }
3669
3670 /* encrypt in instance context */
3672
3673 /* postprocess and set wakeup */
3674 ret = multi_process_post(m, m->pending, mpp_flags);
3675
3676 clear_prefix();
3677 }
3678 }
3679 }
3680 }
3681 return ret;
3682}
3683
3684/*
3685 * Process a possible client-to-client/bcast/mcast message in the
3686 * queue.
3687 */
3688struct multi_instance *
3690{
3691 struct mbuf_item item;
3692
3693 if (mbuf_extract_item(ms, &item)) /* cleartext IP packet */
3694 {
3695 unsigned int pip_flags = PIPV4_PASSTOS | PIPV6_ICMP_NOHOST_SERVER;
3696
3697 set_prefix(item.instance);
3698 item.instance->context.c2.buf = item.buffer->buf;
3699 if (item.buffer->flags
3700 & MF_UNICAST) /* --mssfix doesn't make sense for broadcast or multicast */
3701 {
3702 pip_flags |= PIP_MSSFIX;
3703 }
3704 process_ip_header(&item.instance->context, pip_flags, &item.instance->context.c2.buf,
3705 item.instance->context.c2.link_sockets[0]);
3706 encrypt_sign(&item.instance->context, true);
3707 mbuf_free_buf(item.buffer);
3708
3709 dmsg(D_MULTI_DEBUG, "MULTI: C2C/MCAST/BCAST");
3710
3711 clear_prefix();
3712 return item.instance;
3713 }
3714 else
3715 {
3716 return NULL;
3717 }
3718}
3719
3720/*
3721 * Called when an I/O wait times out. Usually means that a particular
3722 * client instance object needs timer-based service.
3723 */
3724bool
3725multi_process_timeout(struct multi_context *m, const unsigned int mpp_flags)
3726{
3727 bool ret = true;
3728
3729#ifdef MULTI_DEBUG_EVENT_LOOP
3730 printf("%s -> TIMEOUT\n", id(m->earliest_wakeup));
3731#endif
3732
3733 /* instance marked for wakeup? */
3734 if (m->earliest_wakeup)
3735 {
3737 {
3741 }
3742 else
3743 {
3745 ret = multi_process_post(m, m->earliest_wakeup, mpp_flags);
3746 clear_prefix();
3747 }
3748 m->earliest_wakeup = NULL;
3749 }
3750 return ret;
3751}
3752
3753/*
3754 * Per-client route quota management
3755 */
3756
3757void
3759{
3760 struct gc_arena gc = gc_new();
3762 "MULTI ROUTE: route quota (%d) exceeded for %s (see --max-routes-per-client option)",
3764 gc_free(&gc);
3765}
3766
3767#ifdef ENABLE_DEBUG
3768/*
3769 * Flood clients with random packets
3770 */
3771static void
3772gremlin_flood_clients(struct multi_context *m)
3773{
3774 const int level = GREMLIN_PACKET_FLOOD_LEVEL(m->top.options.gremlin);
3775 if (level)
3776 {
3777 struct gc_arena gc = gc_new();
3778 struct buffer buf = alloc_buf_gc(BUF_SIZE(&m->top.c2.frame), &gc);
3779 struct packet_flood_parms parm = get_packet_flood_parms(level);
3780 int i;
3781
3782 ASSERT(buf_init(&buf, m->top.c2.frame.buf.headroom));
3783 parm.packet_size = min_int(parm.packet_size, m->top.c2.frame.buf.payload_size);
3784
3785 msg(D_GREMLIN, "GREMLIN_FLOOD_CLIENTS: flooding clients with %d packets of size %d",
3786 parm.n_packets, parm.packet_size);
3787
3788 for (i = 0; i < parm.packet_size; ++i)
3789 {
3790 ASSERT(buf_write_u8(&buf, (uint8_t)(get_random() & 0xFF)));
3791 }
3792
3793 for (i = 0; i < parm.n_packets; ++i)
3794 {
3795 multi_bcast(m, &buf, NULL, 0);
3796 }
3797
3798 gc_free(&gc);
3799 }
3800}
3801#endif /* ifdef ENABLE_DEBUG */
3802
3803static bool
3805{
3806 struct timeval null;
3807 CLEAR(null);
3809}
3810
3811/*
3812 * Process timers in the top-level context
3813 */
3814void
3816{
3817 /* possibly reap instances/routes in vhash */
3819
3820 /* possibly print to status log */
3821 if (m->top.c1.status_output)
3822 {
3824 {
3826 }
3827 }
3828
3829 /* possibly flush ifconfig-pool file */
3831
3832#ifdef ENABLE_DEBUG
3833 gremlin_flood_clients(m);
3834#endif
3835
3836 /* Should we check for stale routes? */
3838 {
3840 }
3841}
3842
3843static void
3845{
3846 inherit_context_top(&top->multi->top, top);
3848}
3849
3850static void
3856
3857static bool
3859{
3860 return (sig == SIGUSR1 || sig == SIGTERM || sig == SIGHUP || sig == SIGINT);
3861}
3862
3863static void
3865{
3866 /* tell all clients to restart */
3867 for (uint32_t i = 0; i <= m->max_peerid; i++)
3868 {
3869 struct multi_instance *mi = m->instances[i];
3870 if (mi && !mi->halt && proto_is_dgram(mi->context.c2.link_sockets[0]->info.proto))
3871 {
3872 send_control_channel_string(&mi->context, next_server ? "RESTART,[N]" : "RESTART",
3873 D_PUSH);
3875 }
3876 }
3877
3878 /* reschedule signal */
3880 struct timeval tv = { .tv_sec = 2, .tv_usec = 0 };
3882
3884
3888
3889 signal_reset(m->top.sig, 0);
3890}
3891
3892/*
3893 * Return true if event loop should break,
3894 * false if it should continue.
3895 */
3896bool
3898{
3899 if (signal_reset(m->top.sig, SIGUSR2) == SIGUSR2)
3900 {
3901 struct status_output *so = status_open(NULL, 0, M_INFO, NULL, 0);
3903 status_close(so);
3904 return false;
3905 }
3909 {
3911 return false;
3912 }
3913 return true;
3914}
3915
3916/*
3917 * Management subsystem callbacks
3918 */
3919#ifdef ENABLE_MANAGEMENT
3920
3921static void
3922management_callback_status(void *arg, const int version, struct status_output *so)
3923{
3924 struct multi_context *m = (struct multi_context *)arg;
3925
3926 if (!version)
3927 {
3929 }
3930 else
3931 {
3932 multi_print_status(m, so, version);
3933 }
3934}
3935
3936static int
3938{
3939 const struct multi_context *m = (struct multi_context *)arg;
3940 return m->n_clients;
3941}
3942
3943static int
3944management_callback_kill_by_cn(void *arg, const char *del_cn)
3945{
3946 struct multi_context *m = (struct multi_context *)arg;
3947 int count = 0;
3948
3949 for (uint32_t i = 0; i <= m->max_peerid; i++)
3950 {
3951 struct multi_instance *mi = m->instances[i];
3952 if (mi && !mi->halt)
3953 {
3954 const char *cn = tls_common_name(mi->context.c2.tls_multi, false);
3955 if (cn && !strcmp(cn, del_cn))
3956 {
3958 ++count;
3959 }
3960 }
3961 }
3962 return count;
3963}
3964
3965static int
3966management_callback_kill_by_addr(void *arg, const in_addr_t addr, const uint16_t port, const uint8_t proto)
3967{
3968 struct multi_context *m = (struct multi_context *)arg;
3969 struct openvpn_sockaddr saddr;
3970 struct mroute_addr maddr;
3971 int count = 0;
3972
3973 CLEAR(saddr);
3974 saddr.addr.in4.sin_family = AF_INET;
3975 saddr.addr.in4.sin_addr.s_addr = htonl(addr);
3976 saddr.addr.in4.sin_port = htons(port);
3977 maddr.proto = proto;
3978 if (mroute_extract_openvpn_sockaddr(&maddr, &saddr, true))
3979 {
3980 for (uint32_t i = 0; i <= m->max_peerid; i++)
3981 {
3982 struct multi_instance *mi = m->instances[i];
3983 if (mi && !mi->halt && mroute_addr_equal(&maddr, &mi->real))
3984 {
3986 ++count;
3987 }
3988 }
3989 }
3990 return count;
3991}
3992
3993static void
3995{
3996 struct multi_context *m = (struct multi_context *)arg;
3997 if (m->multi_io)
3998 {
4000 }
4001}
4002
4003struct multi_instance *
4004lookup_by_cid(struct multi_context *m, const unsigned long cid)
4005{
4006 if (m)
4007 {
4008 struct multi_instance *mi = (struct multi_instance *)hash_lookup(m->cid_hash, &cid);
4009 if (mi && !mi->halt)
4010 {
4011 return mi;
4012 }
4013 }
4014 return NULL;
4015}
4016
4017static bool
4018management_kill_by_cid(void *arg, const unsigned long cid, const char *kill_msg)
4019{
4020 struct multi_context *m = (struct multi_context *)arg;
4021 struct multi_instance *mi = lookup_by_cid(m, cid);
4022 if (mi)
4023 {
4024 send_restart(&mi->context, kill_msg); /* was: multi_signal_instance (m, mi, SIGTERM); */
4026 return true;
4027 }
4028 else
4029 {
4030 return false;
4031 }
4032}
4033
4034static struct tls_session *
4036 const unsigned int mda_key_id)
4037{
4038 if (multi->session[TM_INITIAL].key[KS_PRIMARY].mda_key_id == mda_key_id)
4039 {
4040 return &multi->session[TM_INITIAL];
4041 }
4042 else if (multi->session[TM_ACTIVE].key[KS_PRIMARY].mda_key_id == mda_key_id)
4043 {
4044 return &multi->session[TM_ACTIVE];
4045 }
4046 else
4047 {
4048 return NULL;
4049 }
4050}
4051
4052static const char *
4054{
4055 struct context *c = &mi->context;
4056 struct tls_multi *multi = c->c2.tls_multi;
4057
4058 if (!multi->acc_negotiated_protocols)
4059 {
4060 msg(M_CLIENT, "ACC: No negotiated protocols");
4061 return NULL;
4062 }
4063
4064 struct buffer buf = alloc_buf_gc(1024, gc);
4065
4066 buf_printf(&buf, "ACC:%d A:6 %s", multi->acc_max_message_length,
4068 return buf_str(&buf);
4069}
4070
4071
4072static bool
4073management_client_pending_auth(void *arg, const unsigned long cid, const unsigned int mda_key_id,
4074 const char *extra, unsigned int timeout)
4075{
4076 struct multi_context *m = (struct multi_context *)arg;
4077 struct multi_instance *mi = lookup_by_cid(m, cid);
4078
4079 if (mi)
4080 {
4081 struct tls_multi *multi = mi->context.c2.tls_multi;
4082
4083 struct gc_arena gc = gc_new();
4084 if (!strcmp(extra, "ACC"))
4085 {
4086 extra = calculate_app_control_extra(mi, &gc);
4087 if (!extra)
4088 {
4089 gc_free(&gc);
4090 return false;
4091 }
4092 }
4093 struct tls_session *session = lookup_session_by_mda_key_id(multi, mda_key_id);
4094
4095 if (!session)
4096 {
4097 gc_free(&gc);
4098 return false;
4099 }
4100
4101 /* sends INFO_PRE and AUTH_PENDING messages to client */
4102 bool ret = send_auth_pending_messages(multi, session, extra, timeout);
4105 gc_free(&gc);
4106 return ret;
4107 }
4108 return false;
4109}
4110
4111
4112static bool
4113management_client_auth(void *arg, const unsigned long cid, const unsigned int mda_key_id,
4114 const bool auth, const char *reason, const char *client_reason,
4115 struct buffer_list *cc_config) /* ownership transferred */
4116{
4117 struct multi_context *m = (struct multi_context *)arg;
4118 struct multi_instance *mi = lookup_by_cid(m, cid);
4119 bool cc_config_owned = true;
4120 bool ret = false;
4121
4122 if (mi)
4123 {
4124 ret = tls_authenticate_key(mi->context.c2.tls_multi, mda_key_id, auth, client_reason);
4125 if (ret)
4126 {
4127 if (auth)
4128 {
4130 {
4132 cc_config_owned = false;
4133 }
4134 }
4135 else if (reason)
4136 {
4137 msg(D_MULTI_LOW, "MULTI: connection rejected: %s, CLI:%s", reason,
4138 np(client_reason));
4139 }
4140 }
4141 }
4142 if (cc_config_owned && cc_config)
4143 {
4145 }
4146 return ret;
4147}
4148
4149
4150static bool
4152 const unsigned long cid,
4153 const unsigned int mda_key_id,
4154 struct buffer_list *input) /* ownership transferred */
4155{
4156 struct multi_context *m = (struct multi_context *)arg;
4157 struct multi_instance *mi = lookup_by_cid(m, cid);
4158
4159 bool ret = false;
4160 if (mi && buffer_list_defined(input))
4161 {
4162 struct tls_multi *multi = mi->context.c2.tls_multi;
4163 struct tls_session *session = lookup_session_by_mda_key_id(multi, mda_key_id);
4164 if (!session)
4165 {
4166 msg(M_CLIENT, "client session not found");
4167 buffer_list_free(input);
4168 return false;
4169 }
4170
4171 ret = management_send_acc_message(&mi->context, multi, session, input);
4173 }
4174
4175 buffer_list_free(input);
4176 return ret;
4177}
4178
4179
4180static char *
4181management_get_peer_info(void *arg, const unsigned long cid)
4182{
4183 struct multi_context *m = (struct multi_context *)arg;
4184 struct multi_instance *mi = lookup_by_cid(m, cid);
4185 char *ret = NULL;
4186
4187 if (mi)
4188 {
4189 ret = mi->context.c2.tls_multi->peer_info;
4190 }
4191
4192 return ret;
4193}
4194
4195#endif /* ifdef ENABLE_MANAGEMENT */
4196
4197
4198void
4225
4226void
4228{
4229 /* max_clients must be less then max peer-id value */
4231
4232 for (uint32_t i = 0; i < m->max_clients; ++i)
4233 {
4234 if (!m->instances[i])
4235 {
4236 mi->context.c2.tls_multi->rx_peer_id = i;
4237 m->instances[i] = mi;
4238 break;
4239 }
4240 }
4241
4242 /* should not really end up here, since multi_create_instance returns null
4243 * if amount of clients exceeds max_clients and this method would then
4244 * also not have been called */
4246
4248 {
4250 }
4251
4252 m->assigned_peer_ids++;
4253}
4254
4265static void
4266multi_get_timeout(struct multi_context *multi, struct timeval *timeval)
4267{
4268 multi_get_timeout_instance(multi, timeval);
4269
4270#ifdef ENABLE_MANAGEMENT
4271 if (management)
4272 {
4273 management_check_bytecount_server(multi, timeval);
4274 }
4275#endif /* ENABLE_MANAGEMENT */
4276}
4277
4278/**************************************************************************/
4285static void
4287{
4288 while (true)
4289 {
4290 /* wait on tun/socket list */
4291 multi_get_timeout(multi, &multi->top.c2.timeval);
4292 const int status = multi_io_wait(multi);
4293 MULTI_CHECK_SIG(multi);
4294
4295 /* check on status of coarse timers */
4297
4298 /* timeout? */
4299 if (status > 0)
4300 {
4301 /* process the I/O which triggered select */
4302 multi_io_process_io(multi);
4303 }
4304 else if (status == 0)
4305 {
4306 multi_io_action(multi, NULL, TA_TIMEOUT, false);
4307 }
4308
4309 MULTI_CHECK_SIG(multi);
4310 }
4311}
4312
4313/*
4314 * Top level event loop.
4315 */
4316void
4318{
4319 ASSERT(top->options.mode == MODE_SERVER);
4320
4321 struct multi_context multi;
4322
4323 top->mode = CM_TOP;
4324 // cppcheck-suppress autoVariables ; yes, we know this is dangerous
4325 top->multi = &multi;
4327
4328 /* initialize top-tunnel instance */
4330 if (IS_SIG(top))
4331 {
4332 return;
4333 }
4334
4335 /* initialize global multi_context object */
4336 multi_init(top);
4337
4338 /* initialize our cloned top object */
4340
4341 /* initialize management interface */
4343
4344 /* finished with initialization */
4345 initialization_sequence_completed(top, ISC_SERVER); /* --mode server --proto tcp-server */
4346
4347#ifdef ENABLE_ASYNC_PUSH
4348 multi.top.c2.inotify_fd = inotify_init();
4349 if (multi.top.c2.inotify_fd < 0)
4350 {
4351 msg(D_MULTI_ERRORS | M_ERRNO, "MULTI: inotify_init error");
4352 }
4353#endif
4354
4355 tunnel_server_loop(&multi);
4356
4357#ifdef ENABLE_ASYNC_PUSH
4358 msg(D_LOW, "%s: close multi.top.c2.inotify_fd (%d)",
4359 __func__, multi.top.c2.inotify_fd);
4360 close(multi.top.c2.inotify_fd);
4361#endif
4362
4363 /* shut down management interface */
4365
4366 /* save ifconfig-pool */
4367 multi_ifconfig_pool_persist(&multi, true);
4368
4369 /* tear down tunnel instance (unless --persist-tun) */
4370 multi_uninit(&multi);
4371 multi_top_free(&multi);
4373}
4374
4375/* Searches for the address and deletes it if it is owned by the multi_instance */
4376static void
4377multi_unlearn_addr(struct multi_context *m, const struct multi_instance *mi, const struct mroute_addr *addr)
4378{
4379 struct hash_element *he;
4380 const uint64_t hv = hash_value(m->vhash, addr);
4381 struct hash_bucket *bucket = hash_bucket(m->vhash, hv);
4382 struct multi_route *r = NULL;
4383
4384 /* if route currently exists, get the instance which owns it */
4385 he = hash_lookup_fast(m->vhash, bucket, addr, hv);
4386 if (he)
4387 {
4388 r = (struct multi_route *)he->value;
4389 }
4390
4391 /* if the route does not exist or exists but is not owned by the current instance, return */
4392 if (!r || r->instance != mi)
4393 {
4394 return;
4395 }
4396
4397 struct gc_arena gc = gc_new();
4398 msg(D_MULTI_LOW, "MULTI: Unlearn: %s -> %s", mroute_addr_print(&r->addr, &gc), multi_instance_string(mi, false, &gc));
4399 learn_address_script(m, NULL, "delete", &r->addr);
4401 multi_route_del(r);
4402
4403 gc_free(&gc);
4404}
4405
4411static void
4413{
4414 struct mroute_addr addr;
4415 CLEAR(addr);
4416
4417 addr.type = MR_ADDR_IPV4;
4418 addr.len = 4;
4419 addr.v4.addr = a;
4420
4421 multi_unlearn_addr(m, mi, &addr);
4422}
4423
4429static void
4430multi_unlearn_in6_addr(struct multi_context *m, const struct multi_instance *mi, struct in6_addr a6)
4431{
4432 struct mroute_addr addr;
4433 CLEAR(addr);
4434
4435 addr.type = MR_ADDR_IPV6;
4436 addr.len = 16;
4437 addr.v6.addr = a6;
4438
4439 multi_unlearn_addr(m, mi, &addr);
4440}
4441
4442/* Function to unlearn previous ifconfig of a client in the server multi_context after a PUSH_UPDATE */
4443void
4445{
4446 in_addr_t old_addr = 0;
4447 old_addr = htonl(mi->context.c2.push_ifconfig_local);
4448 multi_unlearn_in_addr_t(m, mi, old_addr);
4449 mi->context.c2.push_ifconfig_defined = false;
4451 mi->reporting_addr = 0;
4452}
4453
4454/* Function to unlearn previous ifconfig-ipv6 of a client in the server multi_context after a PUSH_UPDATE */
4455void
4457{
4458 struct in6_addr old_addr6;
4459 CLEAR(old_addr6);
4460 old_addr6 = mi->context.c2.push_ifconfig_ipv6_local;
4461 multi_unlearn_in6_addr(m, mi, old_addr6);
4465}
4466
4477void
4478update_vhash(struct multi_context *m, struct multi_instance *mi, const char *new_ip, const char *new_ipv6)
4479{
4480 if (new_ip)
4481 {
4482 /* Remove old IP */
4484 {
4485 unlearn_ifconfig(m, mi);
4486 }
4487
4488 /* Add new IP */
4489 struct in_addr new_addr;
4490 CLEAR(new_addr);
4491 if (inet_pton(AF_INET, new_ip, &new_addr) == 1
4492 && multi_learn_in_addr_t(m, mi, ntohl(new_addr.s_addr), -1, true))
4493 {
4494 mi->context.c2.push_ifconfig_defined = true;
4495 mi->context.c2.push_ifconfig_local = ntohl(new_addr.s_addr);
4496 /* set our client's VPN endpoint for status reporting purposes */
4498 }
4499 }
4500
4501 if (new_ipv6)
4502 {
4503 /* Remove old IPv6 */
4505 {
4506 unlearn_ifconfig_ipv6(m, mi);
4507 }
4508
4509 /* Add new IPv6 */
4510 struct in6_addr new_addr6;
4511 CLEAR(new_addr6);
4512 if (inet_pton(AF_INET6, new_ipv6, &new_addr6) == 1
4513 && multi_learn_in6_addr(m, mi, new_addr6, -1, true))
4514 {
4516 mi->context.c2.push_ifconfig_ipv6_local = new_addr6;
4517 /* set our client's VPN endpoint for status reporting purposes */
4519 }
4520 }
4521}
4522
4523bool
4525{
4526 in_addr_t local_addr, local_netmask;
4527
4529 {
4530 /* If we do not have a local address, we just return false as
4531 * this check doesn't make sense. */
4532 return false;
4533 }
4534
4535 /* if it falls into the network defined by ifconfig_local we assume
4536 * it is already known to DCO and only install "extra" iroutes */
4537 inet_pton(AF_INET, o->ifconfig_local, &local_addr);
4538 inet_pton(AF_INET, o->ifconfig_remote_netmask, &local_netmask);
4539
4540 return (local_addr & local_netmask) != (dest & local_netmask);
4541}
4542
4543bool
4544multi_check_push_ifconfig_ipv6_extra_route(const struct options *o, const struct in6_addr *dest)
4545{
4547 {
4548 /* If we do not have a local address, we just return false as
4549 * this check doesn't make sense. */
4550 return false;
4551 }
4552
4553 /* if it falls into the network defined by ifconfig_local we assume
4554 * it is already known to DCO and only install "extra" iroutes */
4555 struct in6_addr ifconfig_local;
4556 if (inet_pton(AF_INET6, o->ifconfig_ipv6_local, &ifconfig_local) != 1)
4557 {
4558 return false;
4559 }
4560
4561 return (!ipv6_net_contains_host(&ifconfig_local, o->ifconfig_ipv6_netbits,
4562 dest));
4563}
4564
4565#if defined(__GNUC__) || defined(__clang__)
4566#pragma GCC diagnostic pop
4567#endif
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:490
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:447
bool argv_printf_cat(struct argv *argres, const char *format,...)
printf() inspired argv concatenation.
Definition argv.c:471
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:182
bool buffer_list_defined(const struct buffer_list *ol)
Checks if the list is valid and non-empty.
Definition buffer.c:1205
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
Definition buffer.c:231
void buffer_list_free(struct buffer_list *ol)
Frees a buffer list and all the buffers in it.
Definition buffer.c:1195
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Allocate a buffer of the given size under garbage collection.
Definition buffer.c:77
char * string_alloc(const char *str, struct gc_arena *gc)
Duplicate a string, allocating memory under garbage collection.
Definition buffer.c:619
#define ALLOC_OBJ(dptr, type)
Allocate memory for a single object of the given type.
Definition buffer.h:2025
#define BSTR(buf)
Return the buffer content pointer cast to char *.
Definition buffer.h:157
static bool buf_write_u8(struct buffer *dest, uint8_t data)
Append a uint8_t to a buffer.
Definition buffer.h:1288
#define BLEN(buf)
Return the length of the buffer content in bytes.
Definition buffer.h:151
static void buf_reset_len(struct buffer *buf)
Reset the length and offset of a buffer to zero.
Definition buffer.h:616
static void check_malloc_return(void *p)
Abort if a memory allocation returned NULL.
Definition buffer.h:2144
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1974
#define ALLOC_OBJ_CLEAR(dptr, type)
Allocate and zero-initialise memory for a single object of the given type.
Definition buffer.h:2036
#define buf_init(buf, offset)
Definition buffer.h:364
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Definition buffer.h:1958
static char * buf_str(struct buffer *buf)
Return the buffer content pointer cast to char *.
Definition buffer.h:572
#define CCD_DEFAULT
Definition common.h:63
#define counter_format
Definition common.h:32
bool check_compression_settings_valid(const struct compress_options *info, msglvl_t msglevel)
Checks if the compression settings are valid.
Definition comp.c:156
#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
size_t strlcpy(char *dest, const char *src, size_t maxlen)
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:1776
int64_t get_random(void)
an analogue to the random() function, but use prng_bytes and also int64_t instead of long to avoid LL...
Definition crypto.c:1705
#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:347
#define CO_USE_DYNAMIC_TLS_CRYPT
Bit-flag indicating that renegotiations are using tls-crypt with a TLS-EKM derived key.
Definition crypto.h:363
#define CO_EPOCH_DATA_KEY_FORMAT
Bit-flag indicating the epoch the data format.
Definition crypto.h:367
#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:359
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:369
static void dco_delete_iroutes(openvpn_net_ctx_t *net_ctx, const struct context *c)
Definition dco.h:364
static void dco_install_iroute(struct multi_context *m, struct multi_instance *mi, struct mroute_addr *addr)
Definition dco.h:359
static bool dco_check_option(msglvl_t msglevel, const struct options *o)
Definition dco.h:274
void * dco_context_t
Definition dco.h:259
static int dco_multi_add_new_peer(const struct multi_context *m, struct multi_instance *mi)
Definition dco.h:353
void setenv_counter(struct env_set *es, const char *name, counter_type value)
Definition env_set.c:281
void setenv_int(struct env_set *es, const char *name, int value)
Definition env_set.c:289
void setenv_str(struct env_set *es, const char *name, const char *value)
Definition env_set.c:305
struct env_set * env_set_create(struct gc_arena *gc)
Definition env_set.c:155
void setenv_long_long(struct env_set *es, const char *name, long long value)
Definition env_set.c:297
void setenv_del(struct env_set *es, const char *name)
Definition env_set.c:350
#define D_PUSH
Definition errlevel.h:81
#define D_MULTI_ERRORS
Definition errlevel.h:64
#define D_VLAN_DEBUG
Definition errlevel.h:150
#define D_IMPORT_ERRORS
Definition errlevel.h:63
#define D_ROUTE_QUOTA
Definition errlevel.h:88
#define D_MULTI_MEDIUM
Definition errlevel.h:99
#define D_DCO
Definition errlevel.h:92
#define D_MULTI_DEBUG
Definition errlevel.h:124
#define D_MULTI_DROPPED
Definition errlevel.h:98
#define D_DCO_DEBUG
Definition errlevel.h:115
#define D_MULTI_LOW
Definition errlevel.h:84
#define D_TLS_ERRORS
Definition errlevel.h:58
#define D_LOW
Definition errlevel.h:95
#define M_INFO
Definition errlevel.h:54
#define D_GREMLIN
Definition errlevel.h:76
const struct rw_handle * event_t
Definition event.h:89
@ EVENT_ARG_MULTI_INSTANCE
Definition event.h:134
void reschedule_multi_process(struct context *c)
Reschedule tls_multi_process.
Definition forward.c:407
bool send_control_channel_string(struct context *c, const char *str, msglvl_t msglevel)
Definition forward.c:414
void pre_select(struct context *c)
Definition forward.c:1968
void process_ip_header(struct context *c, unsigned int flags, struct buffer *buf, struct link_socket *sock)
Definition forward.c:1670
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:1230
Interface functions to the internal and external multiplexers.
#define PIP_MSSFIX
Definition forward.h:337
static void register_activity(struct context *c, const int64_t size)
Definition forward.h:364
#define PIPV6_ICMP_NOHOST_SERVER
Definition forward.h:342
static struct link_socket_info * get_link_socket_info(struct context *c)
Definition forward.h:351
#define ANY_OUT(c)
Definition forward.h:39
#define PIPV4_PASSTOS
Definition forward.h:336
#define TM_INITIAL
As yet un-trusted tls_session \ being negotiated.
Definition ssl_common.h:553
#define KS_PRIMARY
Primary key state index.
Definition ssl_common.h:472
#define TM_ACTIVE
Active tls_session.
Definition ssl_common.h:552
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:654
void tunnel_server(struct context *top)
Main event loop for OpenVPN in server mode.
Definition multi.c:4317
static void tunnel_server_loop(struct multi_context *multi)
Main event loop for OpenVPN in point-to-multipoint server mode.
Definition multi.c:4286
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:1143
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:3535
bool process_incoming_link_part1(struct context *c, const struct link_socket_info *lsi, bool floated)
Starts processing a packet read from the external network interface.
Definition forward.c:1010
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:407
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:1491
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:3598
void uninit_management_callback(void)
Definition init.c:4322
void initialization_sequence_completed(struct context *c, const unsigned int flags)
Definition init.c:1487
void close_instance(struct context *c)
Definition init.c:4656
void inherit_context_top(struct context *dest, const struct context *src)
Definition init.c:4809
void free_context_buffers(struct context_buffers *b)
Definition init.c:3602
void init_instance_handle_signals(struct context *c, const struct env_set *env, const unsigned int flags)
Definition init.c:4634
void inherit_context_child(struct context *dest, const struct context *src, struct link_socket *sock)
Definition init.c:4729
void context_clear_2(struct context *c)
Definition init.c:90
void close_context(struct context *c, int sig, unsigned int flags)
Definition init.c:4855
bool do_deferred_options(struct context *c, const uint64_t found, const bool is_update)
Definition init.c:2559
struct context_buffers * init_context_buffers(const struct frame *frame)
Definition init.c:3577
void management_show_net_callback(void *arg, const msglvl_t msglevel)
Definition init.c:4153
#define CC_GC_FREE
Definition init.h:109
#define CC_HARD_USR1_TO_HUP
Definition init.h:111
#define ISC_SERVER
Definition init.h:121
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:52
@ route
Definition interactive.c:86
@ 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:42
#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:272
struct hash_element * hash_iterator_next(struct hash_iterator *hi)
Definition list.c:278
void hash_iterator_delete_element(struct hash_iterator *hi)
Definition list.c:310
void hash_iterator_init(struct hash *hash, struct hash_iterator *hi)
Definition list.c:236
struct hash * hash_init(const uint32_t n_buckets, uint64_t(*hash_function)(const void *key, const uint8_t hash_key[HASH_KEY_LEN]), bool(*compare_function)(const void *key1, const void *key2))
Definition list.c:36
void hash_free(struct hash *hash)
Definition list.c:62
struct hash_element * hash_lookup_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint64_t hv)
Definition list.c:81
bool hash_add(struct hash *hash, const void *key, void *value, bool replace)
Definition list.c:139
void hash_remove_by_value(struct hash *hash, const void *value)
Definition list.c:167
void hash_iterator_init_range(struct hash *hash, struct hash_iterator *hi, uint32_t start_bucket, uint32_t end_bucket)
Definition list.c:215
static bool hash_remove(struct hash *hash, const void *key)
Definition list.h:164
static void * hash_lookup(struct hash *hash, const void *key)
Definition list.h:131
#define HASH_KEY_LEN
Definition list.h:53
static uint32_t hash_n_elements(const struct hash *hash)
Definition list.h:113
static uint32_t hash_n_buckets(const struct hash *hash)
Definition list.h:119
static void hash_add_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint64_t hv, void *value)
Definition list.h:149
static struct hash_bucket * hash_bucket(struct hash *hash, uint64_t hv)
Definition list.h:125
static uint64_t hash_value(const struct hash *hash, const void *key)
Definition list.h:107
void management_check_bytecount_server(struct multi_context *multi, struct timeval *timeval)
Definition manage.c:4386
bool management_send_acc_message(struct context *c, struct tls_multi *multi, struct tls_session *session, struct buffer_list *input)
Definition manage.c:4442
void management_connection_established(struct management *man, struct man_def_auth_context *mdac, const struct env_set *es)
Definition manage.c:3175
void management_notify_client_close(const struct management *man, struct man_def_auth_context *mdac, const struct env_set *es)
Definition manage.c:3185
void management_set_callback(struct management *man, const struct management_callback *cb)
Definition manage.c:2932
void management_learn_addr(struct man_def_auth_context *mdac, const struct mroute_addr *addr, const bool primary)
Definition manage.c:3198
#define MCF_SERVER
Definition manage.h:175
void mbuf_dereference_instance(struct mbuf_set *ms, const struct multi_instance *mi)
Definition mbuf.c:168
void mbuf_add_item(struct mbuf_set *ms, const struct mbuf_item *item)
Definition mbuf.c:104
struct mbuf_buffer * mbuf_alloc_buf(const struct buffer *buf)
Definition mbuf.c:65
void mbuf_free_buf(struct mbuf_buffer *mb)
Definition mbuf.c:76
bool mbuf_extract_item(struct mbuf_set *ms, struct mbuf_item *item)
Definition mbuf.c:126
void mbuf_free(struct mbuf_set *ms)
Definition mbuf.c:50
struct mbuf_set * mbuf_init(unsigned int size)
Definition mbuf.c:38
#define MF_UNICAST
Definition mbuf.h:47
static int mbuf_maximum_queued(const struct mbuf_set *ms)
Definition mbuf.h:93
void mroute_addr_mask_host_bits(struct mroute_addr *ma)
Definition mroute.c:319
void mroute_helper_add_iroute46(struct mroute_helper *mh, int netbits)
Definition mroute.c:521
const char * mroute_addr_print_ex(const struct mroute_addr *ma, const unsigned int flags, struct gc_arena *gc)
Definition mroute.c:378
bool mroute_extract_openvpn_sockaddr(struct mroute_addr *addr, const struct openvpn_sockaddr *osaddr, bool use_port)
Definition mroute.c:255
const char * mroute_addr_print(const struct mroute_addr *ma, struct gc_arena *gc)
Definition mroute.c:372
void mroute_helper_del_iroute46(struct mroute_helper *mh, int netbits)
Definition mroute.c:536
bool mroute_learnable_address(const struct mroute_addr *addr, struct gc_arena *gc)
Definition mroute.c:64
bool mroute_addr_compare_function(const void *key1, const void *key2)
Definition mroute.c:366
struct mroute_helper * mroute_helper_init(int ageable_ttl_secs)
Definition mroute.c:483
void mroute_addr_init(struct mroute_addr *addr)
Definition mroute.c:39
uint64_t mroute_addr_hash_function(const void *key, const uint8_t hash_key[HASH_KEY_LEN])
Definition mroute.c:359
void mroute_helper_free(struct mroute_helper *mh)
Definition mroute.c:552
#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
bool multi_tcp_instance_specific_init(const struct multi_context *m, struct multi_instance *mi)
Definition mtcp.c:75
void multi_tcp_instance_specific_free(struct multi_instance *mi)
Definition mtcp.c:101
void multi_tcp_dereference_instance(struct multi_io *multi_io, struct multi_instance *mi)
Definition mtcp.c:107
#define BUF_SIZE(f)
Definition mtu.h:188
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:1007
static void multi_schedule_context_wakeup(struct multi_context *m, struct multi_instance *mi)
Definition multi.c:2974
bool multi_check_push_ifconfig_ipv6_extra_route(const struct options *o, const 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:4544
static void multi_client_connect_early_setup(struct multi_context *m, struct multi_instance *mi)
Definition multi.c:2428
static enum client_connect_return multi_client_connect_real_addr(struct multi_context *m, struct multi_instance *mi, bool deferred, uint64_t *option_types_found)
This sets up the client real address (outer tunnel addr) in the hash map for data channel packet look...
Definition multi.c:2465
static void multi_reap_free(struct multi_reap *mr)
Definition multi.c:217
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:1892
static void multi_process_float(struct multi_context *m, struct multi_instance *mi, struct link_socket *sock)
Handles peer floating.
Definition multi.c:3199
static void multi_reap_range(const struct multi_context *m, uint32_t start_bucket, uint32_t end_bucket)
Definition multi.c:163
struct multi_instance * multi_create_instance(struct multi_context *m, const struct mroute_addr *real, struct link_socket *sock)
Definition multi.c:732
static void multi_add_iroutes(struct multi_context *m, struct multi_instance *mi)
Definition multi.c:1269
void multi_ifconfig_pool_persist(struct multi_context *m, bool force)
Definition multi.c:152
static enum client_connect_return multi_client_connect_compress_migrate(struct multi_context *m, struct multi_instance *mi, bool deferred, uint64_t *option_types_found)
Do the necessary modification for doing the compress migrate.
Definition multi.c:2500
static void multi_top_free(struct multi_context *m)
Definition multi.c:3851
static void multi_unlearn_addr(struct multi_context *m, const struct multi_instance *mi, const struct mroute_addr *addr)
Definition multi.c:4377
static void multi_signal_instance(struct multi_context *m, struct multi_instance *mi, const int sig)
Definition multi.c:3269
void multi_reap_process_dowork(const struct multi_context *m)
Definition multi.c:204
static bool override_locked_username(struct multi_instance *mi)
Overrides the locked username with the username of –override-username.
Definition multi.c:2602
static int management_callback_n_clients(void *arg)
Definition multi.c:3937
static bool multi_check_dest_addr_allowed(struct multi_context *m, struct multi_instance *mi, const struct mroute_addr *real)
This methods checks if a client instance is allowed to use an address.
Definition multi.c:3117
bool multi_process_signal(struct multi_context *m)
Definition multi.c:3897
static void multi_select_virtual_addr(struct multi_context *m, struct multi_instance *mi)
Definition multi.c:1400
static void multi_get_timeout(struct multi_context *multi, struct timeval *timeval)
Determines the earliest wakeup interval based on periodic operations.
Definition multi.c:4266
void multi_close_instance_on_signal(struct multi_context *m, struct multi_instance *mi)
Definition multi.c:3257
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:3016
static void multi_unlearn_in6_addr(struct multi_context *m, const struct multi_instance *mi, struct in6_addr a6)
Definition multi.c:4430
static void multi_delete_dup(struct multi_context *m, const struct multi_instance *new_mi)
Definition multi.c:1316
static void multi_connection_established(struct multi_context *m, struct multi_instance *mi)
Definition multi.c:2677
void multi_process_per_second_timers_dowork(struct multi_context *m)
Definition multi.c:3815
static void multi_set_virtual_addr_env(struct multi_instance *mi)
Definition multi.c:1541
static void multi_client_connect_setenv(struct multi_instance *mi)
Definition multi.c:1685
struct multi_instance * multi_get_queue(struct mbuf_set *ms)
Definition multi.c:3689
static void setenv_stats(struct context *c)
Definition multi.c:521
bool multi_process_timeout(struct multi_context *m, const unsigned int mpp_flags)
Definition multi.c:3725
static int management_callback_kill_by_cn(void *arg, const char *del_cn)
Definition multi.c:3944
static void multi_client_disconnect_script(struct multi_instance *mi)
Definition multi.c:541
static bool stale_route_check_trigger(struct multi_context *m)
Definition multi.c:3804
struct multi_instance * lookup_by_cid(struct multi_context *m, const unsigned long cid)
Definition multi.c:4004
static bool management_kill_by_cid(void *arg, const unsigned long cid, const char *kill_msg)
Definition multi.c:4018
static enum client_connect_return multi_client_connect_call_script(struct multi_context *m, struct multi_instance *mi, bool deferred, uint64_t *option_types_found)
Runs the –client-connect script if one is defined.
Definition multi.c:2203
static void multi_init(struct context *t)
Definition multi.c:276
static void generate_prefix(struct multi_instance *mi)
Definition multi.c:465
static void multi_del_iroutes(struct multi_context *m, struct multi_instance *mi)
Definition multi.c:494
static enum client_connect_return multi_client_connect_call_plugin_v1(struct multi_context *m, struct multi_instance *mi, bool deferred, uint64_t *option_types_found)
Definition multi.c:2023
enum client_connect_return(* multi_client_connect_handler)(struct multi_context *m, struct multi_instance *mi, bool from_deferred, uint64_t *option_types_found)
Definition multi.c:2582
static void management_delete_event(void *arg, event_t event)
Definition multi.c:3994
static uint64_t cid_hash_function(const void *key, const uint8_t hash_key[HASH_KEY_LEN])
Definition multi.c:234
enum client_connect_return multi_client_connect_mda(struct multi_context *m, struct multi_instance *mi, bool deferred, uint64_t *option_types_found)
Definition multi.c:1652
static bool management_client_acc_msg(void *arg, const unsigned long cid, const unsigned int mda_key_id, struct buffer_list *input)
Definition multi.c:4151
static const char * calculate_app_control_extra(struct multi_instance *mi, struct gc_arena *gc)
Definition multi.c:4053
static void multi_uninit(struct multi_context *m)
Definition multi.c:681
const char * multi_instance_string(const struct multi_instance *mi, bool null, struct gc_arena *gc)
Definition multi.c:432
static unsigned int compute_wakeup_sigma(const struct timeval *delta)
Definition multi.c:2950
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:85
static void multi_unicast(struct multi_context *m, const struct buffer *buf, struct multi_instance *mi)
Definition multi.c:2892
static void multi_client_connect_post(struct multi_context *m, struct multi_instance *mi, const char *dc_file, uint64_t *option_types_found)
Definition multi.c:1587
void multi_close_instance(struct multi_context *m, struct multi_instance *mi, bool shutdown)
Definition multi.c:572
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:4073
static void multi_push_restart_schedule_exit(struct multi_context *m, bool next_server)
Definition multi.c:3864
static void multi_top_init(struct context *top)
Definition multi.c:3844
static struct tls_session * lookup_session_by_mda_key_id(struct tls_multi *multi, const unsigned int mda_key_id)
Definition multi.c:4035
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:1997
static enum client_connect_return multi_client_connect_call_plugin_v2(struct multi_context *m, struct multi_instance *mi, bool deferred, uint64_t *option_types_found)
Definition multi.c:2113
static void check_stale_routes(struct multi_context *m)
Definition multi.c:1350
bool multi_check_push_ifconfig_extra_route(const struct options *o, 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:4524
static void multi_process_incoming_link_data(struct multi_context *m, bool floated, struct link_socket *sock)
Process incoming data packet from clients.
Definition multi.c:3388
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:4113
static void multi_client_connect_late_setup(struct multi_context *m, struct multi_instance *mi, const uint64_t option_types_found)
Definition multi.c:2305
static void multi_bcast(struct multi_context *m, const struct buffer *buf, const struct multi_instance *sender_instance, uint16_t vid)
Definition multi.c:2909
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:3966
static void multi_reap_all(const struct multi_context *m)
Definition multi.c:187
static void multi_unlearn_in_addr_t(struct multi_context *m, const struct multi_instance *mi, in_addr_t a)
Definition multi.c:4412
static void multi_print_status(struct multi_context *m, struct status_output *so, const int version)
Definition multi.c:813
void route_quota_exceeded(const struct multi_instance *mi)
Definition multi.c:3758
void ungenerate_prefix(struct multi_instance *mi)
Definition multi.c:482
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:4227
static struct multi_reap * multi_reap_new(uint32_t buckets_per_pass)
Definition multi.c:193
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:1096
void init_management_callback_multi(struct multi_context *m)
Definition multi.c:4199
static void multi_add_mbuf(struct multi_context *m, struct multi_instance *mi, struct mbuf_buffer *mb)
Definition multi.c:2873
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:1924
static void multi_client_connect_post_plugin(struct multi_context *m, struct multi_instance *mi, const struct plugin_return *pr, uint64_t *option_types_found)
Definition multi.c:1613
static uint32_t reap_buckets_per_pass(uint32_t n_buckets)
Definition multi.c:226
static char * management_get_peer_info(void *arg, const unsigned long cid)
Definition multi.c:4181
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:1715
void unlearn_ifconfig_ipv6(struct multi_context *m, struct multi_instance *mi)
Definition multi.c:4456
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:1171
static bool is_exit_restart(int sig)
Definition multi.c:3858
static bool ifconfig_push_constraint_satisfied(const struct context *c)
Definition multi.c:1380
static void set_cc_config(struct multi_instance *mi, struct buffer_list *cc_config)
Definition multi.c:77
static bool cid_compare_function(const void *key1, const void *key2)
Definition multi.c:241
static void multi_client_disconnect_setenv(struct multi_instance *mi)
Definition multi.c:528
void unlearn_ifconfig(struct multi_context *m, struct multi_instance *mi)
Definition multi.c:4444
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:4478
static bool multi_client_generate_tls_keys(struct context *c)
Generates the data channel keys.
Definition multi.c:2282
static enum client_connect_return multi_client_connect_source_ccd(struct multi_context *m, struct multi_instance *mi, bool deferred, uint64_t *option_types_found)
Try to source a dynamic config file from the –client-config-dir directory.
Definition multi.c:2532
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:1217
static bool multi_client_setup_dco_initial(const struct multi_context *m, struct multi_instance *mi, struct gc_arena *gc)
Definition multi.c:2259
static void management_callback_status(void *arg, const int version, struct status_output *so)
Definition multi.c:3922
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:1974
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:1867
static const multi_client_connect_handler client_connect_handlers[]
Definition multi.c:2586
static enum client_connect_return multi_client_connect_script_deferred(struct multi_context *m, struct multi_instance *mi, uint64_t *option_types_found)
Definition multi.c:2162
Header file for server-mode related structures and functions.
client_connect_return
Return values used by the client connect call-back functions.
Definition multi.h:237
@ CC_RET_DEFERRED
Definition multi.h:240
@ CC_RET_FAILED
Definition multi.h:238
@ CC_RET_SKIPPED
Definition multi.h:241
@ CC_RET_SUCCEEDED
Definition multi.h:239
static bool multi_output_queue_ready(const struct multi_context *m, const struct multi_instance *mi)
Definition multi.h:400
#define MULTI_PREFIX_MAX_LENGTH
Definition multi.h:44
#define MULTI_CHECK_SIG(m)
Definition multi.h:642
#define REAP_MIN
Definition multi.h:542
static void multi_route_del(struct multi_route *route)
Definition multi.h:470
static void multi_reap_process(const struct multi_context *m)
Definition multi.h:556
static void route_quota_inc(struct multi_instance *mi)
Definition multi.h:423
void multi_process_incoming_dco(dco_context_t *dco)
Process an incoming DCO message (from kernel space).
#define MULTI_ROUTE_CACHE
Definition multi.h:252
static void clear_prefix(void)
Definition multi.h:523
static bool multi_route_defined(const struct multi_context *m, const struct multi_route *r)
Definition multi.h:479
#define REAP_DIVISOR
Definition multi.h:541
#define MULTI_CACHE_ROUTE_TTL
Definition multi.h:549
#define MPP_CLOSE_ON_SIGNAL
Definition multi.h:287
static void set_prefix(const struct multi_instance *mi)
Definition multi.h:511
#define REAP_MAX
Definition multi.h:543
static void multi_instance_dec_refcount(struct multi_instance *mi)
Definition multi.h:460
static void multi_instance_inc_refcount(struct multi_instance *mi)
Definition multi.h:454
static void multi_get_timeout_instance(struct multi_context *m, struct timeval *dest)
Definition multi.h:584
#define CLIENT_CONNECT_OPT_MASK
Definition multi.h:609
static void multi_set_pending(struct multi_context *m, struct multi_instance *mi)
Definition multi.h:645
#define MULTI_ROUTE_AGEABLE
Definition multi.h:253
#define MPP_RECORD_TOUCH
Definition multi.h:288
#define MULTI_ROUTE_PERMANENT
Definition multi.h:254
#define MPP_PRE_SELECT
Definition multi.h:286
static void multi_process_per_second_timers(struct multi_context *m)
Definition multi.h:565
static bool route_quota_test(const struct multi_instance *mi)
Definition multi.h:436
struct multi_io * multi_io_init(const int maxclients)
Definition multi_io.c:113
void multi_io_process_io(struct multi_context *m)
Definition multi_io.c:570
void multi_io_free(struct multi_io *multi_io)
Definition multi_io.c:134
int multi_io_wait(struct multi_context *m)
Definition multi_io.c:175
void multi_io_delete_event(struct multi_io *multi_io, event_t event)
Definition multi_io.c:782
void multi_io_action(struct multi_context *m, struct multi_instance *mi, int action, bool poll)
Definition multi_io.c:695
#define TA_TIMEOUT
Definition multi_io.h:45
#define CLEAR(x)
Definition basic.h:32
#define M_OPTERR
Definition error.h:101
#define M_CLIENT
Definition error.h:108
static bool check_debug_level(msglvl_t level)
Definition error.h:253
#define M_USAGE
Definition error.h:107
#define M_NONFATAL
Definition error.h:91
#define dmsg(flags,...)
Definition error.h:164
#define msg(flags,...)
Definition error.h:152
unsigned int msglvl_t
Definition error.h:77
#define ASSERT(x)
Definition error.h:221
#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:192
#define MAX_PEER_ID
Definition openvpn.h:526
#define CM_TOP
Definition openvpn.h:454
const char title_string[]
Definition options.c:75
bool has_udp_in_local_list(const struct options *options)
Definition options.c:7884
#define MODE_SERVER
Definition options.h:265
static bool dco_enabled(const struct options *o)
Returns whether the current configuration has dco enabled.
Definition options.h:981
void options_string_import(struct options *options, const char *config, const msglvl_t msglevel, const uint64_t permission_mask, uint64_t *option_types_found, struct env_set *es)
#define OPT_P_COMP
Definition options.h:751
void options_server_import(struct options *o, const char *filename, msglvl_t msglevel, uint64_t permission_mask, uint64_t *option_types_found, struct env_set *es)
const char * time_string(time_t t, tv_usec_t usec, bool show_usec, struct gc_arena *gc)
Definition otime.c:104
struct frequency_limit * frequency_limit_init(int max, int per)
Definition otime.c:137
time_t now
Definition otime.c:33
void frequency_limit_free(struct frequency_limit *f)
Definition otime.c:152
static int openvpn_gettimeofday(struct timeval *tv, void *tz)
Definition otime.h:71
static void tv_add(struct timeval *dest, const struct timeval *src)
Definition otime.h:129
@ 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:690
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:545
const char * platform_gen_path(const char *directory, const char *filename, struct gc_arena *gc)
Put a directory and filename together.
Definition platform.c:622
bool platform_unlink(const char *filename)
Definition platform.c:492
void plugin_return_free(struct plugin_return *pr)
Definition plugin.c:1001
void plugin_return_get_column(const struct plugin_return *src, struct plugin_return *dest, const char *colname)
Definition plugin.c:987
bool plugin_defined(const struct plugin_list *pl, const int type)
Definition plugin.c:919
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:316
bool ifconfig_pool_release(struct ifconfig_pool *pool, ifconfig_pool_handle hand, const bool hard)
Definition pool.c:357
void ifconfig_pool_write(struct ifconfig_pool_persist *persist, const struct ifconfig_pool *pool)
Definition pool.c:720
bool ifconfig_pool_write_trigger(struct ifconfig_pool_persist *persist)
Definition pool.c:577
void ifconfig_pool_free(struct ifconfig_pool *pool)
Definition pool.c:300
void ifconfig_pool_read(struct ifconfig_pool_persist *persist, struct ifconfig_pool *pool)
Definition pool.c:590
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:160
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:1053
bool send_auth_pending_messages(const 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:495
void push_option(struct options *o, const char *opt, msglvl_t msglevel)
Definition push.c:963
void send_restart(struct context *c, const char *kill_msg)
Definition push.c:555
void remove_iroutes_from_push_route_list(struct options *o)
Definition push.c:1214
bool management_callback_send_push_update_by_cid(void *arg, unsigned long cid, const char *options)
Definition push_util.c:358
bool management_callback_send_push_update_broadcast(void *arg, const char *options)
Definition push_util.c:350
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:711
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:388
struct schedule * schedule_init(void)
Definition schedule.c:373
void schedule_free(struct schedule *s)
Definition schedule.c:382
static void schedule_add_entry(struct schedule *s, struct schedule_entry *e, const struct timeval *tv, unsigned int sigma)
Add a struct schedule_entry to the scheduler btree or update an existing entry with a new wakeup time...
Definition schedule.h:98
static bool session_id_defined(const struct session_id *sid1)
Definition session_id.h:53
static uint64_t session_id_hash_function(const void *key, const uint8_t hash_key[HASH_KEY_LEN])
Definition sid_hash.h:81
static bool session_id_hash_equal(const void *sid1, const void *sid2)
Definition sid_hash.h:89
static bool multi_hash_sid_remove(struct multi_context *m, const struct session_id *sid)
Definition sid_hash.h:59
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:582
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:1494
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_tcp(int proto)
returns if the proto is a TCP variant (tcp-server, tcp-client or tcp)
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:4380
bool tls_session_update_crypto_params(struct tls_multi *multi, struct tls_session *session, struct options *options, struct frame *frame, struct frame *frame_fragment, const 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:1730
void tls_session_soft_reset(struct tls_multi *tls_multi)
Definition ssl.c:1858
#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:601
@ CAS_PENDING_DEFERRED
Waiting on an async option import handler.
Definition ssl_common.h:591
@ CAS_WAITING_AUTH
Initial TLS connection established but deferred auth is not yet finished.
Definition ssl_common.h:589
@ CAS_PENDING_DEFERRED_PARTIAL
at least handler succeeded but another is still pending
Definition ssl_common.h:592
@ CAS_PENDING
Options import (Connect script/plugin, ccd,...).
Definition ssl_common.h:590
@ CAS_NOT_CONNECTED
Definition ssl_common.h:588
@ CAS_FAILED
Option import failed or explicitly denied the client.
Definition ssl_common.h:593
@ 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:771
#define SSLF_USERNAME_AS_COMMON_NAME
Definition ssl_common.h:430
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:238
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:217
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:637
Control Channel SSL/Data dynamic negotiation Module This file is split from ssl.h to be able to unit ...
SSL utility functions.
static 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.h:77
bool ssl_verify_username_length(const 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:828
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.
void status_reset(const struct status_output *so)
Definition status.c:147
bool status_trigger(struct status_output *so)
Definition status.c:132
void status_printf(struct status_output *so, const char *format,...)
Definition status.c:226
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:64
void status_flush(struct status_output *so)
Definition status.c:156
bool status_close(struct status_output *so)
Definition status.c:187
Definition argv.h:35
One node in a buffer_list linked list.
Definition buffer.h:2161
struct buffer_entry * next
Pointer to the next node, or NULL.
Definition buffer.h:2163
struct buffer buf
The buffer stored in this list node.
Definition buffer.h:2162
A singly-linked list of buffers, with head/tail pointers for O(1) push.
Definition buffer.h:2168
struct buffer_entry * head
Next item to pop/peek.
Definition buffer.h:2169
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
uint8_t * data
Pointer to the allocated memory.
Definition buffer.h:78
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:76
Detached client connection state.
Definition multi.h:71
uint64_t option_types_found
Definition multi.h:76
char * config_file
The temporary file name that contains the config directives returned by the client-connect script.
Definition multi.h:88
char * deferred_ret_file
The temporary file name that contains the return status of the client-connect script if it exits with...
Definition multi.h:82
unsigned int flags
Definition comp.h:77
int explicit_exit_notification
Definition options.h:152
int fragment
Definition options.h:143
struct ifconfig_pool_persist * ifconfig_pool_persist
Definition openvpn.h:180
struct status_output * status_output
Definition openvpn.h:168
struct tuntap * tuntap
Tun/tap virtual network interface.
Definition openvpn.h:155
bool push_request_received
Definition openvpn.h:398
counter_type link_read_bytes
Definition openvpn.h:249
counter_type link_write_bytes
Definition openvpn.h:252
bool push_ifconfig_ipv6_defined
Definition openvpn.h:405
struct fragment_master * fragment
Definition openvpn.h:235
bool push_ifconfig_defined
Definition openvpn.h:399
counter_type dco_read_bytes
Definition openvpn.h:250
struct man_def_auth_context mda_context
Definition openvpn.h:424
counter_type dco_write_bytes
Definition openvpn.h:253
struct env_set * es
Definition openvpn.h:394
struct tls_multi * tls_multi
TLS state structure for this VPN tunnel.
Definition openvpn.h:300
struct frame frame
Definition openvpn.h:231
struct in6_addr push_ifconfig_ipv6_remote
Definition openvpn.h:408
struct link_socket_actual from
Definition openvpn.h:228
struct frame frame_fragment
Definition openvpn.h:236
int push_ifconfig_ipv6_netbits
Definition openvpn.h:407
struct buffer to_link
Definition openvpn.h:351
struct buffer to_tun
Definition openvpn.h:350
struct in6_addr push_ifconfig_ipv6_local
Definition openvpn.h:406
struct link_socket ** link_sockets
Definition openvpn.h:220
in_addr_t push_ifconfig_local_alias
Definition openvpn.h:403
struct link_socket_info ** link_socket_infos
Definition openvpn.h:221
struct link_socket_actual * to_link_addr
Definition openvpn.h:227
in_addr_t push_ifconfig_remote_netmask
Definition openvpn.h:402
struct buffer buf
Definition openvpn.h:349
struct timeval timeval
Time to next event of timers and similar.
Definition openvpn.h:370
struct event_set * event_set
Definition openvpn.h:213
struct context_buffers * buffers
Definition openvpn.h:341
in_addr_t push_ifconfig_local
Definition openvpn.h:401
Contains all state information for one tunnel.
Definition openvpn.h:445
int mode
Role of this context within the OpenVPN process.
Definition openvpn.h:458
struct multi_context * multi
Pointer to the main P2MP context.
Definition openvpn.h:463
bool did_dco_iroutes
Whether DCO iroutes have been installed.
Definition openvpn.h:484
struct signal_info * sig
Internal error signaling object.
Definition openvpn.h:474
openvpn_net_ctx_t net_ctx
Networking API opaque context.
Definition openvpn.h:472
struct plugin_list * plugins
List of plug-ins.
Definition openvpn.h:476
struct context_2 c2
Level 2 context.
Definition openvpn.h:490
struct env_set * es
Set of environment variables.
Definition openvpn.h:470
struct options options
Options loaded from command line or configuration file.
Definition openvpn.h:446
struct context_1 c1
Level 1 context.
Definition openvpn.h:489
Security parameter state for processing data channel packets.
Definition crypto.h:286
struct key_ctx_bi key_ctx_bi
OpenSSL cipher and HMAC contexts for both sending and receiving directions.
Definition crypto.h:287
int signal_received
Definition multi.h:62
struct timeval wakeup
Definition multi.h:63
union event_arg::@352313260244035237115140212234220346176163201310 u
struct multi_instance * mi
Definition event.h:144
event_arg_t type
Definition event.h:141
struct buffer outgoing
Buffer containing the remaining parts of the fragmented packet being sent.
Definition fragment.h:172
Packet geometry parameters.
Definition mtu.h:113
struct frame::@074234134026241341315172337061247271261307273127 buf
int payload_size
the maximum size that a payload that our buffers can hold from either tun device or network link.
Definition mtu.h:118
int headroom
the headroom in the buffer, this is choosen to allow all potential header to be added before the pack...
Definition mtu.h:124
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
void * value
Definition list.h:41
const void * key
Definition list.h:42
Definition list.h:56
struct iroute_ipv6 * next
Definition route.h:271
unsigned int netbits
Definition route.h:270
struct in6_addr network
Definition route.h:269
in_addr_t network
Definition route.h:262
int netbits
Definition route.h:263
struct iroute * next
Definition route.h:264
Container for bidirectional cipher and HMAC key material.
Definition crypto.h:233
bool initialized
Definition crypto.h:278
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:145
unsigned long cid
Definition manage.h:64
bool(* client_acc_msg)(void *arg, const unsigned long cid, const unsigned int mda_key_id, struct buffer_list *msg)
Definition manage.h:193
char *(* get_peer_info)(void *arg, const unsigned long cid)
Definition manage.h:192
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:187
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:206
bool(* push_update_by_cid)(void *arg, unsigned long cid, const char *options)
Definition manage.h:207
bool(* client_pending_auth)(void *arg, const unsigned long cid, const unsigned int kid, const char *extra, unsigned int timeout)
Definition manage.h:190
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:48
struct buffer buf
Definition mbuf.h:44
struct mbuf_buffer * buffer
Definition mbuf.h:53
struct multi_instance * instance
Definition mbuf.h:54
uint16_t vid
Definition mroute.h:94
uint8_t addr[OPENVPN_ETH_ALEN]
Definition mroute.h:93
uint8_t proto
Definition mroute.h:84
uint8_t type
Definition mroute.h:85
struct mroute_addr::@201361377123046052363312040316220023015150251140::@324142022017217332104337017226063207330251301017 v6
in_port_t port
Definition mroute.h:99
uint8_t len
Definition mroute.h:83
struct mroute_addr::@201361377123046052363312040316220023015150251140::@363302133323255313212060077174173034011260361234 v4
uint8_t netbits
Definition mroute.h:86
unsigned int cache_generation
Definition mroute.h:133
uint8_t net_len[MR_HELPER_NET_LEN]
Definition mroute.h:136
int n_net_len
Definition mroute.h:135
Main OpenVPN server state structure.
Definition multi.h:170
int n_clients
Definition multi.h:204
struct mroute_addr local
Definition multi.h:199
struct schedule * schedule
Definition multi.h:189
uint32_t assigned_peer_ids
Number of peer-ids that are currently assigned.
Definition multi.h:177
struct mbuf_set * mbuf
Set of buffers for passing data channel packets between VPN tunnel instances.
Definition multi.h:190
struct initial_packet_rate_limit * initial_rate_limiter
Definition multi.h:196
struct deferred_signal_schedule_entry deferred_shutdown_signal
Definition multi.h:230
uint32_t max_peerid
highest currently allocated peer-id and maximum allocated/valid index in instances
Definition multi.h:174
struct multi_reap * reaper
Definition multi.h:198
struct multi_io * multi_io
I/O state and events tracker.
Definition multi.h:193
struct hash * hash
VPN tunnel instances indexed by real address of the remote peer.
Definition multi.h:179
struct hash * cid_hash
Definition multi.h:207
unsigned long cid_counter
Definition multi.h:208
struct event_timeout stale_routes_check_et
Definition multi.h:223
int tcp_queue_limit
Definition multi.h:202
struct ifconfig_pool * ifconfig_pool
Definition multi.h:194
struct frequency_limit * new_connection_limiter
Definition multi.h:195
uint32_t max_clients
Definition multi.h:201
struct context top
Storage structure for process-wide configuration.
Definition multi.h:217
int status_file_version
Definition multi.h:203
struct multi_instance * pending
Definition multi.h:211
struct hash * vhash
VPN tunnel instances indexed by virtual address of remote hosts.
Definition multi.h:181
struct multi_instance ** instances
Array of multi_instances with the size of max_clients.
Definition multi.h:171
struct multi_instance ** mpp_touched
Definition multi.h:213
bool enable_c2c
Definition multi.h:200
struct hash * sid_hash
TLS sessions indexed by the peer's session id.
Definition multi.h:183
struct multi_instance * earliest_wakeup
Definition multi.h:212
struct mroute_helper * route_helper
Definition multi.h:197
Server-mode state structure for one single VPN tunnel.
Definition multi.h:102
struct buffer_list * cc_config
Definition multi.h:145
struct client_connect_defer_state client_connect_defer_state
Definition multi.h:152
bool did_cid_hash
Definition multi.h:144
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:147
ifconfig_pool_handle vaddr_handle
Definition multi.h:123
bool did_real_hash
Indicates that the real address/port of the client is hashed in the multi_context m->hash table.
Definition multi.h:136
struct gc_arena gc
Definition multi.h:112
struct in6_addr reporting_addr_ipv6
Definition multi.h:132
struct timeval wakeup
Definition multi.h:120
struct event_arg ev_arg
this struct will store a pointer to either mi or link_socket, depending on the event type,...
Definition multi.h:107
struct context context
The context structure storing state for this VPN tunnel.
Definition multi.h:150
struct session_id sid_hashed_value
If this is multi_instance is hashed in the sid lookup table the session id here is a non-null session...
Definition multi.h:141
int n_clients_delta
Definition multi.h:148
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:249
time_t last_reference
Definition multi.h:258
unsigned int cache_generation
Definition multi.h:257
unsigned int flags
Definition multi.h:255
struct multi_instance * instance
Definition multi.h:250
union openvpn_sockaddr::@051240265251124304325377241125360350250341115171 addr
struct sockaddr_in in4
Definition socket_util.h:43
struct compress_options comp
Definition options.h:410
uint32_t sid_hash_size
Definition options.h:501
int push_ifconfig_ipv6_netbits
Definition options.h:524
int max_routes_per_client
Definition options.h:537
const char * ncp_ciphers_conf
The original ncp_ciphers specified by the user in the configuration.
Definition options.h:577
int status_file_version
Definition options.h:407
in_addr_t push_ifconfig_constraint_network
Definition options.h:519
const char * tmp_dir
Definition options.h:466
unsigned int imported_protocol_flags
Definition options.h:735
int stale_routes_ageing_time
Definition options.h:539
bool duplicate_cn
Definition options.h:528
uint32_t real_hash_size
Definition options.h:499
bool use_peer_id
Whether the data channel uses the DATA_V2 header (peer-id).
Definition options.h:703
in_addr_t ifconfig_pool_netmask
Definition options.h:491
const char * ifconfig_ipv6_local
Definition options.h:329
int cf_max
Definition options.h:530
const char * dev_type
Definition options.h:323
bool push_ifconfig_defined
Definition options.h:514
bool ifconfig_pool_defined
Definition options.h:488
bool vlan_tagging
Definition options.h:724
in_addr_t ifconfig_pool_end
Definition options.h:490
bool ifconfig_ipv6_pool_defined
Definition options.h:495
uint32_t max_clients
Definition options.h:536
const char * client_disconnect_script
Definition options.h:503
int n_bcast_buf
Definition options.h:510
const char * ifconfig_local
Definition options.h:327
struct connection_entry ce
Definition options.h:294
struct iroute_ipv6 * iroutes_ipv6
Definition options.h:513
in_addr_t push_ifconfig_local_alias
Definition options.h:517
int topology
Definition options.h:326
const char * ncp_ciphers
Definition options.h:578
uint32_t virtual_hash_size
Definition options.h:500
const char * learn_address_script
Definition options.h:504
const char * ciphername
Definition options.h:573
int cf_initial_max
Definition options.h:533
int stale_routes_check_interval
Definition options.h:538
uint16_t vlan_pvid
Definition options.h:726
int mode
Definition options.h:266
int ifconfig_ipv6_pool_netbits
Definition options.h:497
in_addr_t push_ifconfig_constraint_netmask
Definition options.h:520
bool enable_ncp_fallback
If defined fall back to ciphername if NCP fails.
Definition options.h:574
in_addr_t push_ifconfig_local
Definition options.h:515
struct gc_arena gc
Definition options.h:258
bool push_ifconfig_constraint_defined
Definition options.h:518
int cf_initial_per
Definition options.h:534
bool force_key_material_export
Definition options.h:710
struct iroute * iroutes
Definition options.h:512
struct in6_addr push_ifconfig_ipv6_remote
Definition options.h:525
const char * client_connect_script
Definition options.h:502
bool push_ifconfig_ipv6_defined
Definition options.h:522
int tcp_queue_limit
Definition options.h:511
const char * override_username
Definition options.h:509
struct in6_addr push_ifconfig_ipv6_local
Definition options.h:523
const char * ifconfig_remote_netmask
Definition options.h:328
bool enable_c2c
Definition options.h:527
int cf_per
Definition options.h:531
in_addr_t ifconfig_pool_start
Definition options.h:489
in_addr_t push_ifconfig_remote_netmask
Definition options.h:516
const char * dev
Definition options.h:322
struct in6_addr ifconfig_ipv6_pool_base
Definition options.h:496
const char * client_config_dir
Definition options.h:506
int ifconfig_ipv6_netbits
Definition options.h:330
struct openvpn_plugin_string_list * list[MAX_PLUGINS]
Definition plugin.h:106
Definition schedule.h:41
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:619
char * auth_token_initial
The first auth-token we sent to a client.
Definition ssl_common.h:691
bool remote_usescomp
remote announced comp-lzo in OCC string
Definition ssl_common.h:713
char * peer_info
A multi-line string of general-purpose info received from peer over control channel.
Definition ssl_common.h:680
char * remote_ciphername
cipher specified in peer's config file
Definition ssl_common.h:712
char * locked_username
The locked username is the username we assume the client is using.
Definition ssl_common.h:657
enum multi_status multi_state
Definition ssl_common.h:640
int acc_max_message_length
the maximum length of an ACC control channel message
Definition ssl_common.h:741
struct tls_session session[TM_SIZE]
Array of tls_session objects representing control channel sessions with the remote peer.
Definition ssl_common.h:721
struct cert_hash_set * locked_cert_hash_set
Definition ssl_common.h:663
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:652
bool use_peer_id
Definition ssl_common.h:709
char * acc_negotiated_protocols
the protocols that are negotiated with the peer.
Definition ssl_common.h:738
char * locked_original_username
The username that client initially used before being overridden by –override-user.
Definition ssl_common.h:661
uint32_t rx_peer_id
Definition ssl_common.h:707
int dco_peer_id
This is the handle that DCO uses to identify this session with the kernel.
Definition ssl_common.h:733
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:384
Security parameter state of a single session within a VPN tunnel.
Definition ssl_common.h:497
struct key_state key[KS_SIZE]
Definition ssl_common.h:532
struct tls_options * opt
Definition ssl_common.h:499
in_addr_t local
Definition tun.h:208
struct in6_addr local_ipv6
Definition tun.h:211
dco_context_t dco
Definition tun.h:247
in_addr_t remote_netmask
Definition tun.h:209
char username[USER_PASS_LEN]
Definition misc.h:70
#define SIGHUP
Definition syshead.h:55
#define SIGINT
Definition syshead.h:56
#define SIGTERM
Definition syshead.h:59
#define SIGUSR1
Definition syshead.h:57
uint32_t in_addr_t
Definition syshead.h:52
#define SIGUSR2
Definition syshead.h:58
static int cleanup(void **state)
struct gc_arena gc
Definition test_ssl.c:138
int dev_type_enum(const char *dev, const char *dev_type)
Definition tun.c:527
#define TUNNEL_TOPOLOGY(tt)
Definition tun.h:185
#define TUNNEL_TYPE(tt)
Definition tun.h:182
int16_t vlan_decapsulate(const struct context *c, struct buffer *buf)
Definition vlan.c:86
bool vlan_is_tagged(const struct buffer *buf)
Definition vlan.c:270