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