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