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