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