OpenVPN
init.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#include "syshead.h"
28
29#ifdef ENABLE_SYSTEMD
30#include <systemd/sd-daemon.h>
31#endif
32
33#include "win32.h"
34#include "init.h"
35#include "run_command.h"
36#include "sig.h"
37#include "occ.h"
38#include "list.h"
39#include "otime.h"
40#include "pool.h"
41#include "gremlin.h"
42#include "occ.h"
43#include "pkcs11.h"
44#include "ps.h"
45#include "lladdr.h"
46#include "ping.h"
47#include "ssl_verify.h"
48#include "ssl_ncp.h"
49#include "tls_crypt.h"
50#include "forward.h"
51#include "auth_token.h"
52#include "mss.h"
53#include "mudp.h"
54#include "dco.h"
55#include "tun_afunix.h"
56#include "schedule.h"
57#include "options_string.h"
58
59#include "memdbg.h"
60
61
62static struct context *static_context; /* GLOBAL */
63static const char *saved_pid_file_name; /* GLOBAL */
64
65/*
66 * Crypto initialization flags
67 */
68#define CF_LOAD_PERSISTED_PACKET_ID (1 << 0)
69#define CF_INIT_TLS_MULTI (1 << 1)
70#define CF_INIT_TLS_AUTH_STANDALONE (1 << 2)
71
72static void do_init_first_time(struct context *c);
73
74static bool do_deferred_p2p_ncp(struct context *c);
75
76static void
78{
79 CLEAR(*c);
80}
81
82static void
84{
85 CLEAR(c->c1);
86}
87
88void
90{
91 CLEAR(c->c2);
92}
93
94void
96{
97 const bool first_time_save = c->first_time;
98 const struct context_persist cpsave = c->persist;
100 c->first_time = first_time_save;
101 c->persist = cpsave;
102}
103
104/*
105 * Pass tunnel endpoint and MTU parms to a user-supplied script.
106 * Used to execute the up/down script/plugins.
107 */
108static void
109run_up_down(const char *command, const struct plugin_list *plugins, int plugin_type,
110 const char *arg,
111#ifdef _WIN32
112 DWORD adapter_index,
113#endif
114 const char *dev_type, int tun_mtu, const char *ifconfig_local,
115 const char *ifconfig_remote, const char *context, const char *signal_text,
116 const char *script_type, struct env_set *es)
117{
118 struct gc_arena gc = gc_new();
119
120 if (signal_text)
121 {
122 setenv_str(es, "signal", signal_text);
123 }
124 setenv_str(es, "script_context", context);
125 setenv_int(es, "tun_mtu", tun_mtu);
126 setenv_str(es, "dev", arg);
127 if (dev_type)
128 {
129 setenv_str(es, "dev_type", dev_type);
130 }
131#ifdef _WIN32
132 setenv_int(es, "dev_idx", adapter_index);
133#endif
134
135 if (!ifconfig_local)
136 {
137 ifconfig_local = "";
138 }
139 if (!ifconfig_remote)
140 {
141 ifconfig_remote = "";
142 }
143 if (!context)
144 {
145 context = "";
146 }
147
148 if (plugin_defined(plugins, plugin_type))
149 {
150 struct argv argv = argv_new();
151 ASSERT(arg);
152 argv_printf(&argv, "%s %d 0 %s %s %s", arg, tun_mtu, ifconfig_local, ifconfig_remote,
153 context);
154
155 if (plugin_call(plugins, plugin_type, &argv, NULL, es) != OPENVPN_PLUGIN_FUNC_SUCCESS)
156 {
157 msg(M_FATAL, "ERROR: up/down plugin call failed");
158 }
159
160 argv_free(&argv);
161 }
162
163 if (command)
164 {
165 struct argv argv = argv_new();
166 ASSERT(arg);
167 setenv_str(es, "script_type", script_type);
168 argv_parse_cmd(&argv, command);
169 argv_printf_cat(&argv, "%s %d 0 %s %s %s", arg, tun_mtu, ifconfig_local, ifconfig_remote,
170 context);
172 openvpn_run_script(&argv, es, S_FATAL, "--up/--down");
173 argv_free(&argv);
174 }
175
176 gc_free(&gc);
177}
178
179/*
180 * Should be called after options->ce is modified at the top
181 * of a SIGUSR1 restart.
182 */
183static void
185{
186 /*
187 * In pull mode, we usually import --ping/--ping-restart parameters from
188 * the server. However we should also set an initial default --ping-restart
189 * for the period of time before we pull the --ping-restart parameter
190 * from the server.
191 */
194 {
197 }
198}
199
200#ifdef ENABLE_MANAGEMENT
201static bool
202management_callback_proxy_cmd(void *arg, const char **p)
203{
204 struct context *c = arg;
205 struct connection_entry *ce = &c->options.ce;
206 struct gc_arena *gc = &c->c2.gc;
207 bool ret = false;
208
209 update_time();
210 if (streq(p[1], "NONE"))
211 {
212 ret = true;
213 }
214 else if (p[2] && p[3])
215 {
216 if (streq(p[1], "HTTP"))
217 {
218 struct http_proxy_options *ho;
219 if (ce->proto != PROTO_TCP && ce->proto != PROTO_TCP_CLIENT)
220 {
221 msg(M_WARN, "HTTP proxy support only works for TCP based connections");
222 return false;
223 }
225 ho->server = string_alloc(p[2], gc);
226 ho->port = string_alloc(p[3], gc);
227 ho->auth_retry = (p[4] && streq(p[4], "nct") ? PAR_NCT : PAR_ALL);
228 ret = true;
229 }
230 else if (streq(p[1], "SOCKS"))
231 {
233 ce->socks_proxy_port = string_alloc(p[3], gc);
234 ret = true;
235 }
236 }
237 else
238 {
239 msg(M_WARN, "Bad proxy command");
240 }
241
242 ce->flags &= ~CE_MAN_QUERY_PROXY;
243
244 return ret;
245}
246
247static bool
249{
250 const struct connection_list *l = c->options.connection_list;
251 struct connection_entry *ce = &c->options.ce;
252 struct gc_arena gc;
253 bool ret = true;
254
255 update_time();
256 if (management)
257 {
258 gc = gc_new();
259 {
260 struct buffer out = alloc_buf_gc(256, &gc);
261 buf_printf(&out, ">PROXY:%u,%s,%s", (l ? l->current : 0) + 1,
262 (proto_is_udp(ce->proto) ? "UDP" : "TCP"), np(ce->remote));
265 }
267 while (ce->flags & CE_MAN_QUERY_PROXY)
268 {
270 if (IS_SIG(c))
271 {
272 ret = false;
273 break;
274 }
275 }
277 gc_free(&gc);
278 }
279
280 return ret;
281}
282
297static bool
298management_callback_send_cc_message(void *arg, const char *command, const char *parameters)
299{
300 struct context *c = (struct context *)arg;
301 size_t len = strlen(command) + 1 + strlen(parameters) + 1;
302 if (len > PUSH_BUNDLE_SIZE)
303 {
304 return false;
305 }
306
307 struct gc_arena gc = gc_new();
308 struct buffer buf = alloc_buf_gc(len, &gc);
309 ASSERT(buf_printf(&buf, "%s", command));
310 if (parameters)
311 {
312 ASSERT(buf_printf(&buf, ",%s", parameters));
313 }
315
316 gc_free(&gc);
317 return status;
318}
319
320static unsigned int
322{
323 ASSERT(arg);
324 const struct context *c = (struct context *)arg;
325 const struct connection_list *l = c->options.connection_list;
326
327 return l->len;
328}
329
330static bool
331management_callback_remote_entry_get(void *arg, unsigned int index, char **remote)
332{
333 ASSERT(arg);
334 ASSERT(remote);
335
336 struct context *c = (struct context *)arg;
337 struct connection_list *l = c->options.connection_list;
338 bool ret = true;
339
340 if (l->len > 0 && index < (unsigned int)l->len)
341 {
342 const struct connection_entry *ce = l->array[index];
343 const char *proto = proto2ascii(ce->proto, ce->af, false);
344 const char *status = (ce->flags & CE_DISABLED) ? "disabled" : "enabled";
345
346 /* space for output including 3 commas and a nul */
347 size_t len =
348 strlen(ce->remote) + strlen(ce->remote_port) + strlen(proto) + strlen(status) + 3 + 1;
349 char *out = malloc(len);
351
352 snprintf(out, len, "%s,%s,%s,%s", ce->remote, ce->remote_port, proto, status);
353 *remote = out;
354 }
355 else
356 {
357 ret = false;
358 msg(M_WARN, "Out of bounds index in management query for remote entry: index = %u", index);
359 }
360
361 return ret;
362}
363
364static bool
365management_callback_remote_cmd(void *arg, const char **p)
366{
367 struct context *c = (struct context *)arg;
368 struct connection_entry *ce = &c->options.ce;
369 int ret = false;
370 if (p[1]
373 {
374 unsigned int flags = 0;
375 if (!strcmp(p[1], "ACCEPT"))
376 {
378 ret = true;
379 }
380 else if (!strcmp(p[1], "SKIP"))
381 {
383 ret = true;
384 c->options.ce_advance_count = (p[2]) ? atoi(p[2]) : 1;
385 }
386 else if (!strcmp(p[1], "MOD") && p[2] && p[3])
387 {
388 if (strlen(p[2]) < RH_HOST_LEN && strlen(p[3]) < RH_PORT_LEN)
389 {
390 struct remote_host_store *rhs = c->options.rh_store;
391 if (!rhs)
392 {
394 c->options.rh_store = rhs;
395 }
396 strncpynt(rhs->host, p[2], RH_HOST_LEN);
397 strncpynt(rhs->port, p[3], RH_PORT_LEN);
398
399 ce->remote = rhs->host;
400 ce->remote_port = rhs->port;
402 ret = true;
403 }
404 }
405 if (ret)
406 {
409 }
410 }
411 return ret;
412}
413
414static bool
416{
417 struct gc_arena gc = gc_new();
418 volatile struct connection_entry *ce = &c->options.ce;
419 int ce_changed = true; /* presume the connection entry will be changed */
420
421 update_time();
422 if (management)
423 {
424 struct buffer out = alloc_buf_gc(256, &gc);
425
426 buf_printf(&out, ">REMOTE:%s,%s,%s", np(ce->remote), ce->remote_port,
427 proto2ascii(ce->proto, ce->af, false));
430
435 {
437 if (IS_SIG(c))
438 {
439 ce_changed = false; /* connection entry have not been set */
440 break;
441 }
442 }
444 }
445 gc_free(&gc);
446
447 if (ce_changed)
448 {
449 /* If it is likely a connection entry was modified,
450 * check what changed in the flags and that it was not skipped
451 */
452 const int flags = ((ce->flags >> CE_MAN_QUERY_REMOTE_SHIFT) & CE_MAN_QUERY_REMOTE_MASK);
454 }
455 return ce_changed;
456}
457#endif /* ENABLE_MANAGEMENT */
458
470static void
472{
473 struct connection_list *l = c->options.connection_list;
474
475 l->current = -1;
476 if (c->options.remote_random)
477 {
478 for (int i = l->len - 1; i > 0; --i)
479 {
480 const int64_t j = get_random() % (i + 1);
481 if (i != j)
482 {
483 struct connection_entry *tmp;
484 tmp = l->array[i];
485 l->array[i] = l->array[j];
486 l->array[j] = tmp;
487 }
488 }
489 }
490}
491
492/*
493 * Clear the remote address list
494 */
495static void
497{
498 if (lsa->remote_list && free)
499 {
500 freeaddrinfo(lsa->remote_list);
501 }
502 lsa->remote_list = NULL;
503 lsa->current_remote = NULL;
504}
505
506/*
507 * Increment to next connection entry
508 */
509static void
511{
512 struct connection_list *l = c->options.connection_list;
513 bool ce_defined;
514 const struct connection_entry *ce;
515 int n_cycles = 0;
516
517 do
518 {
519 ce_defined = true;
520 if (c->options.no_advance && l->current >= 0)
521 {
522 c->options.no_advance = false;
523 }
524 else
525 {
526 /* Check if there is another resolved address to try for
527 * the current connection */
529 && c->c1.link_socket_addrs[0].current_remote->ai_next
531 {
533 c->c1.link_socket_addrs[0].current_remote->ai_next;
534 }
535 else
536 {
537 c->options.advance_next_remote = false;
538 /* FIXME (schwabe) fix the persist-remote-ip option for real,
539 * this is broken probably ever since connection lists and multiple
540 * remote existed
541 */
543 {
544 /* Connection entry addrinfo objects might have been
545 * resolved earlier but the entry itself might have been
546 * skipped by management on the previous loop.
547 * If so, clear the addrinfo objects as close_instance does
548 */
550 {
553 }
554
555 /* close_instance should have cleared the addrinfo objects */
557 ASSERT(c->c1.link_socket_addrs[0].remote_list == NULL);
558 }
559 else
560 {
563 }
564
565 int advance_count = 1;
566
567 /* If previous connection entry was skipped by management client
568 * with a count to advance by, apply it.
569 */
570 if (c->options.ce_advance_count > 0)
571 {
572 advance_count = c->options.ce_advance_count;
573 }
574
575 /*
576 * Increase the number of connection attempts
577 * If this is connect-retry-max * size(l)
578 * OpenVPN will quit
579 */
580
581 c->options.unsuccessful_attempts += advance_count;
582 l->current += advance_count;
583
584 if (l->current >= l->len)
585 {
586 l->current %= l->len;
587 if (++n_cycles >= 2)
588 {
589 msg(M_FATAL, "No usable connection profiles are present");
590 }
591 }
592 }
593 }
594
596 ce = l->array[l->current];
597
598 if (ce->flags & CE_DISABLED)
599 {
600 ce_defined = false;
601 }
602
603 c->options.ce = *ce;
604
605#ifdef ENABLE_MANAGEMENT
607 {
608 /* allow management interface to override connection entry details */
609 ce_defined = ce_management_query_remote(c);
610 if (IS_SIG(c))
611 {
612 break;
613 }
614 }
615 else if (ce_defined && management && management_query_proxy_enabled(management))
616 {
617 ce_defined = ce_management_query_proxy(c);
618 if (IS_SIG(c))
619 {
620 break;
621 }
622 }
623#endif
624 } while (!ce_defined);
625
626 /* Check if this connection attempt would bring us over the limit */
627 int max_attempts = l->len * c->options.connect_retry_max;
628 if (max_attempts > 0 && c->options.unsuccessful_attempts > (unsigned int)max_attempts)
629 {
630 msg(M_FATAL, "All connections have been connect-retry-max (%d) times unsuccessful, exiting",
632 }
634}
635
636/*
637 * Query for private key and auth-user-pass username/passwords
638 */
639void
641{
642 /* Certificate password input */
643 if (c->options.key_pass_file)
644 {
646 }
647
648 /* Auth user/pass input */
650 {
652#ifdef ENABLE_MANAGEMENT
655#else
658#endif
659 }
660}
661
662/*
663 * Initialize/Uninitialize HTTP or SOCKS proxy
664 */
665
666static void
668{
669 if (c->c1.http_proxy_owned && c->c1.http_proxy)
670 {
672 c->c1.http_proxy = NULL;
673 c->c1.http_proxy_owned = false;
674 }
675 if (c->c1.socks_proxy_owned && c->c1.socks_proxy)
676 {
678 c->c1.socks_proxy = NULL;
679 c->c1.socks_proxy_owned = false;
680 }
681}
682
683static void
685{
686 bool did_http = false;
687
689
691 {
693
694 /* Possible HTTP proxy user/pass input */
696 if (c->c1.http_proxy)
697 {
698 did_http = true;
699 c->c1.http_proxy_owned = true;
700 }
701 }
702
703 if (!did_http && c->options.ce.socks_proxy_server)
704 {
705 c->c1.socks_proxy =
708 if (c->c1.socks_proxy)
709 {
710 c->c1.socks_proxy_owned = true;
711 }
712 }
713}
714
715static void
717{
719}
720
721static void
723{
725}
726
727static void
733
734void
736{
738
740
742
744
746
747#if defined(ENABLE_PKCS11)
748 if (c->first_time)
749 {
750 int i;
751 pkcs11_initialize(true, c->options.pkcs11_pin_cache_period);
752 for (i = 0; i < MAX_PARMS && c->options.pkcs11_providers[i] != NULL; i++)
753 {
754 pkcs11_addProvider(
755 c->options.pkcs11_providers[i], c->options.pkcs11_protected_authentication[i],
756 c->options.pkcs11_private_mode[i], c->options.pkcs11_cert_private[i]);
757 }
758 }
759#endif
760
761#if 0 /* test get_user_pass with GET_USER_PASS_NEED_OK flag */
762 {
763 /*
764 * In the management interface, you can okay the request by entering "needok token-insertion-request ok"
765 */
766 struct user_pass up;
767 CLEAR(up);
768 strcpy(up.username, "Please insert your cryptographic token"); /* put the high-level message in up.username */
769 get_user_pass(&up, NULL, "token-insertion-request", GET_USER_PASS_MANAGEMENT|GET_USER_PASS_NEED_OK);
770 msg(M_INFO, "RET:%s", up.password); /* will return the third argument to management interface
771 * 'needok' command, usually 'ok' or 'cancel'. */
772 }
773#endif
774
775#ifdef ENABLE_SYSTEMD
776 /* We can report the PID via getpid() to systemd here as OpenVPN will not
777 * do any fork due to daemon() a future call.
778 * See possibly_become_daemon() [init.c] for more details.
779 */
780 sd_notifyf(0, "READY=1\nSTATUS=Pre-connection initialization successful\nMAINPID=%lu",
781 (unsigned long)getpid());
782#endif
783}
784
785void
787{
788 gc_free(&c->c2.gc);
789 gc_free(&c->options.gc);
790 gc_free(&c->gc);
791}
792
793#if PORT_SHARE
794
795static void
796close_port_share(void)
797{
798 if (port_share)
799 {
800 port_share_close(port_share);
801 port_share = NULL;
802 }
803}
804
805static void
806init_port_share(struct context *c)
807{
808 if (!port_share && (c->options.port_share_host && c->options.port_share_port))
809 {
810 port_share =
811 port_share_open(c->options.port_share_host, c->options.port_share_port,
812 c->c2.frame.buf.payload_size, c->options.port_share_journal_dir);
813 if (port_share == NULL)
814 {
815 msg(M_FATAL, "Fatal error: Port sharing failed");
816 }
817 }
818}
819
820#endif /* if PORT_SHARE */
821
822
823bool
825{
826 /*
827 * Initialize random number seed. random() is only used
828 * when "weak" random numbers are acceptable.
829 * SSL library routines are always used when cryptographically
830 * strong random numbers are required.
831 */
832 struct timeval tv;
833 if (!gettimeofday(&tv, NULL))
834 {
835 const unsigned int seed = (unsigned int)(tv.tv_sec ^ tv.tv_usec);
836 srandom(seed);
837 }
838
839 error_reset(); /* initialize error.c */
840 reset_check_status(); /* initialize status check code in socket.c */
841
842#ifdef _WIN32
843 init_win32();
844#endif
845
846#ifdef OPENVPN_DEBUG_COMMAND_LINE
847 {
848 int i;
849 for (i = 0; i < argc; ++i)
850 {
851 msg(M_INFO, "argv[%d] = '%s'", i, argv[i]);
852 }
853 }
854#endif
855
856 update_time();
857
858 init_ssl_lib();
859
860#ifdef IFCONFIG_POOL_TEST
861 ifconfig_pool_test(0x0A010004, 0x0A0100FF);
862 return false;
863#endif
864
865#ifdef TIME_TEST
866 time_test();
867 return false;
868#endif
869
870#ifdef GEN_PATH_TEST
871 {
872 struct gc_arena gc = gc_new();
873 const char *fn = gen_path("foo", "bar", &gc);
874 printf("%s\n", fn);
875 gc_free(&gc);
876 }
877 return false;
878#endif
879
880#ifdef STATUS_PRINTF_TEST
881 {
882 struct gc_arena gc = gc_new();
883 const char *tmp_file = platform_create_temp_file("/tmp", "foo", &gc);
884 struct status_output *so = status_open(tmp_file, 0, -1, NULL, STATUS_OUTPUT_WRITE);
885 status_printf(so, "%s", "foo");
886 status_printf(so, "%s", "bar");
887 if (!status_close(so))
888 {
889 msg(M_WARN, "STATUS_PRINTF_TEST: %s: write error", tmp_file);
890 }
891 gc_free(&gc);
892 }
893 return false;
894#endif
895
896 return true;
897}
898
899void
901{
902 free_ssl_lib();
903
904#ifdef ENABLE_PKCS11
905 pkcs11_terminate();
906#endif
907
908#if PORT_SHARE
909 close_port_share();
910#endif
911
912#if defined(MEASURE_TLS_HANDSHAKE_STATS)
913 show_tls_performance_stats();
914#endif
915}
916
917void
918init_verb_mute(struct context *c, unsigned int flags)
919{
920 if (flags & IVM_LEVEL_1)
921 {
922 /* set verbosity and mute levels */
926 }
927
928 /* special D_LOG_RW mode */
929 if (flags & IVM_LEVEL_2)
930 {
932 }
933}
934
935/*
936 * Possibly set --dev based on --dev-node.
937 * For example, if --dev-node /tmp/foo/tun, and --dev undefined,
938 * set --dev to tun.
939 */
940void
942{
943 if (!options->dev && options->dev_node)
944 {
945 /* POSIX basename() implementations may modify its arguments */
946 char *dev_node = string_alloc(options->dev_node, NULL);
947 options->dev = basename(dev_node);
948 }
949}
950
951bool
953{
954 /*
955 * OpenSSL info print mode?
956 */
959 {
961 {
963 }
965 {
967 }
969 {
971 }
973 {
976 }
977 if (options->show_curves)
978 {
980 }
981 return true;
982 }
983 return false;
984}
985
986/*
987 * Static pre-shared key generation mode?
988 */
989bool
991{
992 /* should we disable paging? */
993 if (options->mlock && (options->genkey))
994 {
995 platform_mlockall(true);
996 }
997
998 /*
999 * We do not want user to use --genkey with --secret. In the transistion
1000 * phase we for secret.
1001 */
1003 {
1004 msg(M_USAGE, "Using --genkey type with --secret filename is "
1005 "not supported. Use --genkey type filename instead.");
1006 }
1008 {
1009 int nbits_written;
1010 const char *genkey_filename = options->genkey_filename;
1012 {
1013 msg(M_USAGE, "You must provide a filename to either --genkey "
1014 "or --secret, not both");
1015 }
1016
1017 /*
1018 * Copy filename from shared_secret_file to genkey_filename to support
1019 * the old --genkey --secret foo.file syntax.
1020 */
1022 {
1023 msg(M_WARN, "WARNING: Using --genkey --secret filename is "
1024 "DEPRECATED. Use --genkey secret filename instead.");
1025 genkey_filename = options->shared_secret_file;
1026 }
1027
1028 nbits_written = write_key_file(2, genkey_filename);
1029 if (nbits_written < 0)
1030 {
1031 msg(M_FATAL, "Failed to write key file");
1032 }
1033
1034 msg(D_GENKEY | M_NOPREFIX, "Randomly generated %d bit key written to %s", nbits_written,
1036 return true;
1037 }
1039 {
1041 return true;
1042 }
1044 {
1046 {
1047 msg(M_USAGE,
1048 "--genkey tls-crypt-v2-client requires a server key to be set via --tls-crypt-v2 to create a client key");
1049 }
1050
1054 return true;
1055 }
1057 {
1059 return true;
1060 }
1061 else
1062 {
1063 return false;
1064 }
1065}
1066
1067/*
1068 * Persistent TUN/TAP device management mode?
1069 */
1070bool
1072{
1073 if (!options->persist_config)
1074 {
1075 return false;
1076 }
1077
1078 /* sanity check on options for --mktun or --rmtun */
1079 notnull(options->dev, "TUN/TAP device (--dev)");
1082 {
1084 "options --mktun or --rmtun should only be used together with --dev");
1085 }
1086
1087#if defined(ENABLE_DCO)
1088 if (dco_enabled(options))
1089 {
1090 /* creating a DCO interface via --mktun is not supported as it does not
1091 * make much sense. Since DCO is enabled by default, people may run into
1092 * this without knowing, therefore this case should be properly handled.
1093 *
1094 * Disable DCO if --mktun was provided and print a message to let
1095 * user know.
1096 */
1098 {
1099 msg(M_WARN, "Note: --mktun does not support DCO. Creating TUN interface.");
1100 }
1101
1102 options->disable_dco = true;
1103 }
1104#endif
1105
1106#ifdef ENABLE_FEATURE_TUN_PERSIST
1110 {
1111 set_lladdr(ctx, options->dev, options->lladdr, NULL);
1112 }
1113 return true;
1114#else /* ifdef ENABLE_FEATURE_TUN_PERSIST */
1116 "options --mktun and --rmtun are not available on your operating "
1117 "system. Please check 'man tun' (or 'tap'), whether your system "
1118 "supports using 'ifconfig %s create' / 'destroy' to create/remove "
1119 "persistent tunnel interfaces.",
1120 options->dev);
1121#endif
1122 return false;
1123}
1124
1125/*
1126 * Should we become a daemon?
1127 * Return true if we did it.
1128 */
1129bool
1131{
1132 bool ret = false;
1133
1134#ifdef ENABLE_SYSTEMD
1135 /* return without forking if we are running from systemd */
1136 if (sd_notify(0, "READY=0") > 0)
1137 {
1138 return ret;
1139 }
1140#endif
1141
1142 if (options->daemon)
1143 {
1144 /* Don't chdir immediately, but the end of the init sequence, if needed */
1145
1146#if defined(__APPLE__) && defined(__clang__)
1147#pragma clang diagnostic push
1148#pragma clang diagnostic ignored "-Wdeprecated-declarations"
1149#endif
1150 if (daemon(1, options->log) < 0)
1151 {
1152 msg(M_ERR, "daemon() failed or unsupported");
1153 }
1154#if defined(__APPLE__) && defined(__clang__)
1155#pragma clang diagnostic pop
1156#endif
1158 if (options->log)
1159 {
1161 }
1162
1163 ret = true;
1164 }
1165 return ret;
1166}
1167
1168/*
1169 * Actually do UID/GID downgrade, chroot and SELinux context switching, if requested.
1170 */
1171static void
1172do_uid_gid_chroot(struct context *c, bool no_delay)
1173{
1174 static const char why_not[] = "will be delayed because of --client, --pull, or --up-delay";
1175 struct context_0 *c0 = c->c0;
1176
1177 if (c0 && !c0->uid_gid_chroot_set)
1178 {
1179 /* chroot if requested */
1180 if (c->options.chroot_dir)
1181 {
1182 if (no_delay)
1183 {
1185 }
1186 else if (c->first_time)
1187 {
1188 msg(M_INFO, "NOTE: chroot %s", why_not);
1189 }
1190 }
1191
1192 /* set user and/or group if we want to setuid/setgid */
1193 if (c0->uid_gid_specified)
1194 {
1195 if (no_delay)
1196 {
1198 }
1199 else if (c->first_time)
1200 {
1201 msg(M_INFO, "NOTE: UID/GID downgrade %s", why_not);
1202 }
1203 }
1204
1205#ifdef ENABLE_SELINUX
1206 /* Apply a SELinux context in order to restrict what OpenVPN can do
1207 * to _only_ what it is supposed to do after initialization is complete
1208 * (basically just network I/O operations). Doing it after chroot
1209 * requires /proc to be mounted in the chroot (which is annoying indeed
1210 * but doing it before requires more complex SELinux policies.
1211 */
1212 if (c->options.selinux_context)
1213 {
1214 if (no_delay)
1215 {
1216 if (-1 == setcon(c->options.selinux_context))
1217 {
1218 msg(M_ERR, "setcon to '%s' failed; is /proc accessible?",
1219 c->options.selinux_context);
1220 }
1221 else
1222 {
1223 msg(M_INFO, "setcon to '%s' succeeded", c->options.selinux_context);
1224 }
1225 }
1226 else if (c->first_time)
1227 {
1228 msg(M_INFO, "NOTE: setcon %s", why_not);
1229 }
1230 }
1231#endif
1232
1233 /* Privileges are going to be dropped by now (if requested), be sure
1234 * to prevent any future privilege dropping attempts from now on.
1235 */
1236 if (no_delay)
1237 {
1238 c0->uid_gid_chroot_set = true;
1239 }
1240 }
1241}
1242
1243/*
1244 * Return common name in a way that is formatted for
1245 * prepending to msg() output.
1246 */
1247const char *
1249{
1250 struct buffer out = alloc_buf_gc(256, gc);
1251 if (c->c2.tls_multi)
1252 {
1253 buf_printf(&out, "[%s] ", tls_common_name(c->c2.tls_multi, false));
1254 }
1255 return BSTR(&out);
1256}
1257
1258void
1260{
1261#ifdef _WIN32
1263 {
1266 }
1267 else
1268 {
1270
1271 /* put a title on the top window bar */
1273 {
1276 }
1277 }
1278#endif /* ifdef _WIN32 */
1279}
1280
1281void
1283{
1284 c->c2.coarse_timer_wakeup = 0;
1285}
1286
1287/*
1288 * Initialise the server poll timeout timer
1289 * This timer is used in the http/socks proxy setup so it needs to be setup
1290 * before
1291 */
1292static void
1294{
1295 update_time();
1296 if (c->options.ce.connect_timeout)
1297 {
1298 event_timeout_init(&c->c2.server_poll_interval, c->options.ce.connect_timeout, now);
1299 }
1300}
1301
1302/*
1303 * Initialize timers
1304 */
1305static void
1307{
1308 update_time();
1310
1311 /* initialize inactivity timeout */
1312 if (c->options.inactivity_timeout)
1313 {
1314 event_timeout_init(&c->c2.inactivity_interval, c->options.inactivity_timeout, now);
1315 }
1316
1317 /* initialize inactivity timeout */
1318 if (c->options.session_timeout)
1319 {
1320 event_timeout_init(&c->c2.session_interval, c->options.session_timeout, now);
1321 }
1322
1323 /* initialize pings */
1324 if (dco_enabled(&c->options))
1325 {
1326 /* The DCO kernel module will send the pings instead of user space */
1327 event_timeout_clear(&c->c2.ping_rec_interval);
1328 event_timeout_clear(&c->c2.ping_send_interval);
1329 }
1330 else
1331 {
1332 if (c->options.ping_send_timeout)
1333 {
1334 event_timeout_init(&c->c2.ping_send_interval, c->options.ping_send_timeout, 0);
1335 }
1336
1337 if (c->options.ping_rec_timeout)
1338 {
1339 event_timeout_init(&c->c2.ping_rec_interval, c->options.ping_rec_timeout, now);
1340 }
1341 }
1342
1343 /* If the auth-token renewal interval is shorter than reneg-sec, arm
1344 * "auth-token renewal" timer to send additional auth-token to update the
1345 * token on the client more often. If not, this happens automatically
1346 * at renegotiation time, without needing an extra event.
1347 */
1348 if (c->options.auth_token_generate
1349 && c->options.auth_token_renewal < c->options.renegotiate_seconds)
1350 {
1351 event_timeout_init(&c->c2.auth_token_renewal_interval, c->options.auth_token_renewal, now);
1352 }
1353
1354 if (!deferred)
1355 {
1356 /* initialize connection establishment timer */
1357 event_timeout_init(&c->c2.wait_for_connect, 1, now);
1358
1359 /* initialize occ timers */
1360
1361 if (c->options.occ && !TLS_MODE(c) && c->c2.options_string_local
1362 && c->c2.options_string_remote)
1363 {
1364 event_timeout_init(&c->c2.occ_interval, OCC_INTERVAL_SECONDS, now);
1365 }
1366
1367 if (c->options.mtu_test)
1368 {
1369 event_timeout_init(&c->c2.occ_mtu_load_test_interval, OCC_MTU_LOAD_INTERVAL_SECONDS,
1370 now);
1371 }
1372
1373 /* initialize packet_id persistence timer */
1374 if (c->options.packet_id_file)
1375 {
1376 event_timeout_init(&c->c2.packet_id_persist_interval, 60, now);
1377 }
1378
1379 /* initialize tmp_int optimization that limits the number of times we call
1380 * tls_multi_process in the main event loop */
1382 }
1383}
1384
1385/*
1386 * Initialize traffic shaper.
1387 */
1388static void
1390{
1391 /* initialize traffic shaper (i.e. transmit bandwidth limiter) */
1392 if (c->options.shaper)
1393 {
1394 shaper_init(&c->c2.shaper, c->options.shaper);
1395 shaper_msg(&c->c2.shaper);
1396 }
1397}
1398
1399/*
1400 * Allocate route list structures for IPv4 and IPv6
1401 * (we do this for IPv4 even if no --route option has been seen, as other
1402 * parts of OpenVPN might want to fill the route-list with info, e.g. DHCP)
1403 */
1404static void
1406{
1407 if (!c->c1.route_list)
1408 {
1409 ALLOC_OBJ_CLEAR_GC(c->c1.route_list, struct route_list, &c->gc);
1410 }
1411 if (c->options.routes_ipv6 && !c->c1.route_ipv6_list)
1412 {
1413 ALLOC_OBJ_CLEAR_GC(c->c1.route_ipv6_list, struct route_ipv6_list, &c->gc);
1414 }
1415}
1416
1417
1418/*
1419 * Initialize the route list, resolving any DNS names in route
1420 * options and saving routes in the environment.
1421 */
1422static void
1424 const struct link_socket_info *link_socket_info, struct env_set *es,
1425 openvpn_net_ctx_t *ctx)
1426{
1427 const char *gw = NULL;
1429 int metric = 0;
1430
1431 /* if DCO is enabled we have both regular routes and iroutes in the system
1432 * routing table, and normal routes must have a higher metric for that to
1433 * work so that iroutes are always matched first
1434 */
1435 if (dco_enabled(options))
1436 {
1437 metric = DCO_DEFAULT_METRIC;
1438 }
1439
1440 if (dev == DEV_TYPE_TUN && (options->topology == TOP_NET30 || options->topology == TOP_P2P))
1441 {
1443 }
1445 {
1447 }
1449 {
1450 metric = options->route_default_metric;
1451 }
1452
1455 {
1456 /* copy routes to environment */
1458 }
1459}
1460
1461static void
1463 const struct link_socket_info *link_socket_info, struct env_set *es,
1464 openvpn_net_ctx_t *ctx)
1465{
1466 const char *gw = NULL;
1467 int metric = -1; /* no metric set */
1468
1469 /* see explanation in do_init_route_list() */
1470 if (dco_enabled(options))
1471 {
1472 metric = DCO_DEFAULT_METRIC;
1473 }
1474
1475 gw = options->ifconfig_ipv6_remote; /* default GW = remote end */
1477 {
1479 }
1480
1482 {
1483 metric = options->route_default_metric;
1484 }
1485
1486 /* redirect (IPv6) gateway to VPN? if yes, add a few more specifics
1487 */
1489 {
1490 char *opt_list[] = { "::/3", "2000::/4", "3000::/4", "fc00::/7", NULL };
1491 int i;
1492
1493 for (i = 0; opt_list[i]; i++)
1494 {
1498 }
1499 }
1500
1503 {
1504 /* copy routes to environment */
1506 }
1507}
1508
1509
1510/*
1511 * Called after all initialization has been completed.
1512 */
1513void
1514initialization_sequence_completed(struct context *c, const unsigned int flags)
1515{
1516 static const char message[] = "Initialization Sequence Completed";
1517
1518 /* Reset the unsuccessful connection counter on complete initialisation */
1519 c->options.unsuccessful_attempts = 0;
1520
1521 /* If we delayed UID/GID downgrade or chroot, do it now */
1522 do_uid_gid_chroot(c, true);
1523
1524 /* Test if errors */
1525 if (flags & ISC_ERRORS)
1526 {
1527#ifdef _WIN32
1530 msg(M_INFO, "%s With Errors ( see http://openvpn.net/faq.html#dhcpclientserv )", message);
1531#else
1532#ifdef ENABLE_SYSTEMD
1533 sd_notifyf(0, "STATUS=Failed to start up: %s With Errors\nERRNO=1", message);
1534#endif
1535 msg(M_INFO, "%s With Errors", message);
1536#endif
1537 }
1538 else
1539 {
1540#ifdef ENABLE_SYSTEMD
1541 sd_notifyf(0, "STATUS=%s", message);
1542#endif
1543 msg(M_INFO, "%s", message);
1544 }
1545
1546 /* Flag that we initialized */
1547 if ((flags & (ISC_ERRORS | ISC_SERVER)) == 0)
1548 {
1549 c->options.no_advance = true;
1550 }
1551
1552#ifdef _WIN32
1553 fork_register_dns_action(c->c1.tuntap);
1554#endif
1555
1556#ifdef ENABLE_MANAGEMENT
1557 /* Tell management interface that we initialized */
1558 if (management)
1559 {
1560 const in_addr_t *tun_local = NULL;
1561 const struct in6_addr *tun_local6 = NULL;
1562 struct openvpn_sockaddr local, remote;
1563 const struct link_socket_actual *actual;
1564 socklen_t sa_len = sizeof(local);
1565 const char *detail = "SUCCESS";
1566 if (flags & ISC_ERRORS)
1567 {
1568 detail = "ERROR";
1569 }
1570 /* Flag route error only on platforms where trivial "already exists" errors
1571 * are filtered out. Currently this is the case on Windows or if usng netlink.
1572 */
1573#if defined(_WIN32) || defined(ENABLE_SITNL)
1574 else if (flags & ISC_ROUTE_ERRORS)
1575 {
1576 detail = "ROUTE_ERROR";
1577 }
1578#endif
1579
1580 CLEAR(local);
1581 actual = &get_link_socket_info(c)->lsa->actual;
1582 remote = actual->dest;
1583 getsockname(c->c2.link_sockets[0]->sd, &local.addr.sa, &sa_len);
1584#if ENABLE_IP_PKTINFO
1585 if (!addr_defined(&local))
1586 {
1587 switch (local.addr.sa.sa_family)
1588 {
1589 case AF_INET:
1590#if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
1591 local.addr.in4.sin_addr = actual->pi.in4.ipi_spec_dst;
1592#else
1593 local.addr.in4.sin_addr = actual->pi.in4;
1594#endif
1595 break;
1596
1597 case AF_INET6:
1598 local.addr.in6.sin6_addr = actual->pi.in6.ipi6_addr;
1599 break;
1600 }
1601 }
1602#endif
1603
1604 if (c->c1.tuntap)
1605 {
1606 tun_local = &c->c1.tuntap->local;
1607 tun_local6 = &c->c1.tuntap->local_ipv6;
1608 }
1609 management_set_state(management, OPENVPN_STATE_CONNECTED, detail, tun_local, tun_local6,
1610 &local, &remote);
1611 if (tun_local)
1612 {
1614 }
1615 }
1616#endif /* ifdef ENABLE_MANAGEMENT */
1617}
1618
1623static bool
1624route_noexec_enabled(const struct options *o, const struct tuntap *tt)
1625{
1626 return o->route_noexec || (tt && tt->backend_driver == DRIVER_AFUNIX)
1627 || (tt && tt->backend_driver == DRIVER_NULL);
1628}
1629
1630/*
1631 * Possibly add routes and/or call route-up script
1632 * based on options.
1633 */
1634bool
1636 struct route_ipv6_list *route_ipv6_list, const struct tuntap *tt,
1637 const struct plugin_list *plugins, struct env_set *es, openvpn_net_ctx_t *ctx)
1638{
1639 bool ret = true;
1641 {
1644 }
1645#ifdef ENABLE_MANAGEMENT
1646 if (management)
1647 {
1649 }
1650#endif
1651
1652 if (plugin_defined(plugins, OPENVPN_PLUGIN_ROUTE_UP))
1653 {
1654 if (plugin_call(plugins, OPENVPN_PLUGIN_ROUTE_UP, NULL, NULL, es)
1655 != OPENVPN_PLUGIN_FUNC_SUCCESS)
1656 {
1657 msg(M_WARN, "WARNING: route-up plugin call failed");
1658 }
1659 }
1660
1661 if (options->route_script)
1662 {
1663 struct argv argv = argv_new();
1664 setenv_str(es, "script_type", "route-up");
1666 openvpn_run_script(&argv, es, 0, "--route-up");
1667 argv_free(&argv);
1668 }
1669
1670#ifdef _WIN32
1671 if (options->show_net_up)
1672 {
1675 }
1676 else if (check_debug_level(D_SHOW_NET))
1677 {
1680 }
1681#endif
1682 return ret;
1683}
1684
1685/*
1686 * initialize tun/tap device object
1687 */
1688static void
1690{
1696 c->c2.es, &c->net_ctx, c->c1.tuntap);
1697
1699 {
1700 /* Using AF_UNIX trumps using DCO */
1702 }
1703 else if (is_dev_type(c->options.dev, c->options.dev_type, "null"))
1704 {
1706 }
1707#ifdef _WIN32
1708 else
1709 {
1711 }
1712#else
1713 else if (dco_enabled(&c->options))
1714 {
1716 }
1717 else
1718 {
1720 }
1721#endif
1722
1724
1725 c->c1.tuntap_owned = true;
1726}
1727
1728/*
1729 * Open tun/tap device, ifconfig, call up script, etc.
1730 */
1731
1732
1733static bool
1735{
1736 if (tt && tt->backend_driver == DRIVER_AFUNIX)
1737 {
1738 return false;
1739 }
1740#ifdef TARGET_ANDROID
1741 return false;
1742#else
1743 return is_tun_type_set(tt);
1744#endif
1745}
1746
1755static void
1757{
1758#if defined(_WIN32)
1759 /* Fortify 'redirect-gateway block-local' with firewall rules? */
1760 bool block_local = block_local_needed(c->c1.route_list);
1761
1762 if (c->options.block_outside_dns || block_local)
1763 {
1764 BOOL dns_only = !block_local;
1765 if (!win_wfp_block(c->c1.tuntap->adapter_index, c->options.msg_channel, dns_only))
1766 {
1767 msg(M_FATAL, "WFP: initialization failed");
1768 }
1769 }
1770#endif
1771}
1772
1781static void
1782del_wfp_block(struct context *c, unsigned long adapter_index)
1783{
1784#if defined(_WIN32)
1786 {
1787 if (!win_wfp_uninit(adapter_index, c->options.msg_channel))
1788 {
1789 msg(M_FATAL, "WFP: deinitialization failed");
1790 }
1791 }
1792#endif
1793}
1794
1800static bool
1802{
1803 return c->options.ifconfig_noexec
1804 || (c->c1.tuntap && c->c1.tuntap->backend_driver == DRIVER_AFUNIX)
1805 || (c->c1.tuntap && c->c1.tuntap->backend_driver == DRIVER_NULL);
1806}
1807
1808static void
1810{
1811 struct tuntap *tt = c->c1.tuntap;
1812
1813 if (tt->backend_driver == DRIVER_NULL)
1814 {
1816 }
1817 else if (tt->backend_driver == DRIVER_AFUNIX)
1818 {
1819 open_tun_afunix(&c->options, c->c2.frame.tun_mtu, tt, c->c2.es);
1820 }
1821 else
1822 {
1824 }
1825 msg(M_INFO, "%s device [%s] opened", print_tun_backend_driver(tt->backend_driver),
1826 tt->actual_name);
1827}
1828
1829
1830static bool
1831do_open_tun(struct context *c, int *error_flags)
1832{
1833 struct gc_arena gc = gc_new();
1834 bool ret = false;
1835 *error_flags = 0;
1836
1837 if (!can_preserve_tun(c->c1.tuntap))
1838 {
1839#ifdef TARGET_ANDROID
1840 /* If we emulate persist-tun on android we still have to open a new tun and
1841 * then close the old */
1842 int oldtunfd = -1;
1843 if (c->c1.tuntap)
1844 {
1845 oldtunfd = c->c1.tuntap->fd;
1846 free(c->c1.tuntap);
1847 c->c1.tuntap = NULL;
1848 c->c1.tuntap_owned = false;
1849 }
1850#endif
1851
1852 /* initialize (but do not open) tun/tap object, this also sets
1853 * the backend driver type */
1854 do_init_tun(c);
1855
1856 /* inherit the dco context from the tuntap object */
1857 if (c->c2.tls_multi)
1858 {
1859 c->c2.tls_multi->dco = &c->c1.tuntap->dco;
1860 }
1861
1862#ifdef _WIN32
1863 /* store (hide) interactive service handle in tuntap_options */
1865 msg(D_ROUTE, "interactive service msg_channel=%" PRIuPTR, (intptr_t)c->options.msg_channel);
1866#endif
1867
1868 /* allocate route list structure */
1870
1871 /* parse and resolve the route option list */
1872 ASSERT(c->c2.link_sockets[0]);
1873 if (c->options.routes && c->c1.route_list)
1874 {
1876 c->c2.es, &c->net_ctx);
1877 }
1879 {
1881 &c->c2.link_sockets[0]->info, c->c2.es, &c->net_ctx);
1882 }
1883
1884 /* do ifconfig */
1886 {
1887 /* guess actual tun/tap unit number that will be returned
1888 * by open_tun */
1889 const char *guess =
1891 do_ifconfig(c->c1.tuntap, guess, c->c2.frame.tun_mtu, c->c2.es, &c->net_ctx);
1892 }
1893
1894 /* possibly add routes */
1896 {
1897 /* Ignore route_delay, would cause ROUTE_BEFORE_TUN to be ignored */
1899 c->c1.tuntap, c->plugins, c->c2.es, &c->net_ctx);
1900 *error_flags |= (status ? 0 : ISC_ROUTE_ERRORS);
1901 }
1902#ifdef TARGET_ANDROID
1903 /* Store the old fd inside the fd so open_tun can use it */
1904 c->c1.tuntap->fd = oldtunfd;
1905#endif
1906
1907 if (dco_enabled(&c->options))
1908 {
1909 ovpn_dco_init(c);
1910 }
1911
1912 /* open the tun device */
1914
1915 /* set the hardware address */
1916 if (c->options.lladdr)
1917 {
1919 }
1920
1921 /* do ifconfig */
1923 {
1925 &c->net_ctx);
1926 }
1927
1928 run_dns_up_down(true, &c->options, c->c1.tuntap, &c->persist.duri);
1929
1930 /* run the up script */
1931 run_up_down(c->options.up_script, c->plugins, OPENVPN_PLUGIN_UP, c->c1.tuntap->actual_name,
1932#ifdef _WIN32
1934#endif
1938 NULL, "up", c->c2.es);
1939
1940 add_wfp_block(c);
1941
1942 /* possibly add routes */
1944 {
1946 c->c1.tuntap, c->plugins, c->c2.es, &c->net_ctx);
1947 *error_flags |= (status ? 0 : ISC_ROUTE_ERRORS);
1948 }
1949
1950 ret = true;
1951 static_context = c;
1952 }
1953 else
1954 {
1955 msg(M_INFO, "Preserving previous TUN/TAP instance: %s", c->c1.tuntap->actual_name);
1956
1957 /* explicitly set the ifconfig_* env vars */
1959
1960 run_dns_up_down(true, &c->options, c->c1.tuntap, &c->persist.duri);
1961
1962 /* run the up script if user specified --up-restart */
1963 if (c->options.up_restart)
1964 {
1965 run_up_down(c->options.up_script, c->plugins, OPENVPN_PLUGIN_UP,
1966 c->c1.tuntap->actual_name,
1967#ifdef _WIN32
1969#endif
1973 "restart", NULL, "up", c->c2.es);
1974 }
1975
1976 add_wfp_block(c);
1977 }
1978 gc_free(&gc);
1979 return ret;
1980}
1981
1982/*
1983 * Close TUN/TAP device
1984 */
1985
1986static void
1988{
1989 msg(D_CLOSE, "Closing %s interface", print_tun_backend_driver(c->c1.tuntap->backend_driver));
1990
1991 if (c->c1.tuntap)
1992 {
1994 {
1995 undo_ifconfig(c->c1.tuntap, &c->net_ctx);
1996 }
1998 {
2000 }
2001 else if (c->c1.tuntap->backend_driver == DRIVER_NULL)
2002 {
2003 free(c->c1.tuntap->actual_name);
2004 free(c->c1.tuntap);
2005 }
2006 else
2007 {
2008 close_tun(c->c1.tuntap, &c->net_ctx);
2009 }
2010 c->c1.tuntap = NULL;
2011 }
2012 c->c1.tuntap_owned = false;
2014}
2015
2016static void
2017do_close_tun(struct context *c, bool force)
2018{
2019 /* With dco-win we open tun handle in the very beginning.
2020 * In case when tun wasn't opened - like we haven't connected,
2021 * we still need to close tun handle
2022 */
2024 {
2026 return;
2027 }
2028
2029 if (!c->c1.tuntap || !c->c1.tuntap_owned)
2030 {
2031 return;
2032 }
2033
2034 struct gc_arena gc = gc_new();
2035 const char *tuntap_actual = string_alloc(c->c1.tuntap->actual_name, &gc);
2036 const in_addr_t local = c->c1.tuntap->local;
2037 const in_addr_t remote_netmask = c->c1.tuntap->remote_netmask;
2038 unsigned long adapter_index = 0;
2039#ifdef _WIN32
2040 adapter_index = c->c1.tuntap->adapter_index;
2041#endif
2042
2043 run_dns_up_down(false, &c->options, c->c1.tuntap, &c->persist.duri);
2044
2045 if (force || !(c->sig->signal_received == SIGUSR1 && c->options.persist_tun))
2046 {
2047 static_context = NULL;
2048
2049#ifdef ENABLE_MANAGEMENT
2050 /* tell management layer we are about to close the TUN/TAP device */
2051 if (management)
2052 {
2054 management_up_down(management, "DOWN", c->c2.es);
2055 }
2056#endif
2057
2058 /* delete any routes we added */
2059 if (c->c1.route_list || c->c1.route_ipv6_list)
2060 {
2061 run_up_down(c->options.route_predown_script, c->plugins, OPENVPN_PLUGIN_ROUTE_PREDOWN,
2062 tuntap_actual,
2063#ifdef _WIN32
2064 adapter_index,
2065#endif
2066 NULL, c->c2.frame.tun_mtu, print_in_addr_t(local, IA_EMPTY_IF_UNDEF, &gc),
2067 print_in_addr_t(remote_netmask, IA_EMPTY_IF_UNDEF, &gc), "init",
2069 "route-pre-down", c->c2.es);
2070
2072 ROUTE_OPTION_FLAGS(&c->options), c->c2.es, &c->net_ctx);
2073 }
2074
2075 /* actually close tun/tap device based on --down-pre flag */
2076 if (!c->options.down_pre)
2077 {
2079 }
2080
2081 /* Run the down script -- note that it will run at reduced
2082 * privilege if, for example, "--user" was used. */
2083 run_up_down(c->options.down_script, c->plugins, OPENVPN_PLUGIN_DOWN, tuntap_actual,
2084#ifdef _WIN32
2085 adapter_index,
2086#endif
2087 NULL, c->c2.frame.tun_mtu, print_in_addr_t(local, IA_EMPTY_IF_UNDEF, &gc),
2088 print_in_addr_t(remote_netmask, IA_EMPTY_IF_UNDEF, &gc), "init",
2090 c->c2.es);
2091
2092 del_wfp_block(c, adapter_index);
2093
2094 /* actually close tun/tap device based on --down-pre flag */
2095 if (c->options.down_pre)
2096 {
2098 }
2099 }
2100 else
2101 {
2102 /* run the down script on this restart if --up-restart was specified */
2103 if (c->options.up_restart)
2104 {
2105 run_up_down(c->options.down_script, c->plugins, OPENVPN_PLUGIN_DOWN, tuntap_actual,
2106#ifdef _WIN32
2107 adapter_index,
2108#endif
2109 NULL, c->c2.frame.tun_mtu, print_in_addr_t(local, IA_EMPTY_IF_UNDEF, &gc),
2110 print_in_addr_t(remote_netmask, IA_EMPTY_IF_UNDEF, &gc), "restart",
2112 c->c2.es);
2113 }
2114
2115 del_wfp_block(c, adapter_index);
2116 }
2117 gc_free(&gc);
2118}
2119
2120void
2122{
2123 struct context *c = static_context;
2124 if (c)
2125 {
2126 static_context = NULL;
2127 do_close_tun(c, true);
2128 }
2129}
2130
2131/*
2132 * Handle delayed tun/tap interface bringup due to --up-delay or --pull
2133 */
2134
2139static bool
2141{
2142 const struct sha256_digest zero = { { 0 } };
2143 return memcmp(a, b, sizeof(struct sha256_digest))
2144 || !memcmp(a, &zero, sizeof(struct sha256_digest));
2145}
2146
2152static void
2153add_delim_if_non_empty(struct buffer *buf, const char *header)
2154{
2155 if (BLENZ(buf) > strlen(header))
2156 {
2157 buf_printf(buf, ", ");
2158 }
2159}
2160
2161
2166static void
2168{
2169 struct options *o = &c->options;
2170
2171 struct buffer out;
2172 uint8_t line[1024] = { 0 };
2173 buf_set_write(&out, line, sizeof(line));
2174
2175
2176 if (cipher_kt_mode_aead(o->ciphername))
2177 {
2178 buf_printf(&out, "Data Channel: cipher '%s'", cipher_kt_name(o->ciphername));
2179 }
2180 else
2181 {
2182 buf_printf(&out, "Data Channel: cipher '%s', auth '%s'", cipher_kt_name(o->ciphername),
2183 md_kt_name(o->authname));
2184 }
2185
2186 if (c->c2.tls_multi && c->c2.tls_multi->use_peer_id)
2187 {
2188 buf_printf(&out, ", rx-peer-id: %u, tx-peer-id: %u", c->c2.tls_multi->rx_peer_id,
2189 c->c2.tls_multi->tx_peer_id);
2190 }
2191
2192#ifdef USE_COMP
2193 if (c->c2.comp_context)
2194 {
2195 buf_printf(&out, ", compression: '%s'", c->c2.comp_context->alg.name);
2196 }
2197#endif
2198
2199 msg(D_HANDSHAKE, "%s", BSTR(&out));
2200
2201 buf_clear(&out);
2202
2203 const char *header = "Timers: ";
2204
2205 buf_printf(&out, "%s", header);
2206
2207 if (o->ping_send_timeout)
2208 {
2209 buf_printf(&out, "ping %d", o->ping_send_timeout);
2210 }
2211
2212 if (o->ping_rec_timeout_action != PING_UNDEF)
2213 {
2214 /* yes unidirectional ping is possible .... */
2215 add_delim_if_non_empty(&out, header);
2216
2217 if (o->ping_rec_timeout_action == PING_EXIT)
2218 {
2219 buf_printf(&out, "ping-exit %d", o->ping_rec_timeout);
2220 }
2221 else
2222 {
2223 buf_printf(&out, "ping-restart %d", o->ping_rec_timeout);
2224 }
2225 }
2226
2227 if (o->inactivity_timeout)
2228 {
2229 add_delim_if_non_empty(&out, header);
2230
2231 buf_printf(&out, "inactive %d", o->inactivity_timeout);
2232 if (o->inactivity_minimum_bytes)
2233 {
2234 buf_printf(&out, " %" PRIu64, o->inactivity_minimum_bytes);
2235 }
2236 }
2237
2238 if (o->session_timeout)
2239 {
2240 add_delim_if_non_empty(&out, header);
2241 buf_printf(&out, "session-timeout %d", o->session_timeout);
2242 }
2243
2244 if (BLENZ(&out) > strlen(header))
2245 {
2246 msg(D_HANDSHAKE, "%s", BSTR(&out));
2247 }
2248
2249 buf_clear(&out);
2250 header = "Protocol options: ";
2251 buf_printf(&out, "%s", header);
2252
2253 if (c->options.ce.explicit_exit_notification)
2254 {
2255 buf_printf(&out, "explicit-exit-notify %d", c->options.ce.explicit_exit_notification);
2256 }
2257 if (c->options.imported_protocol_flags)
2258 {
2259 add_delim_if_non_empty(&out, header);
2260
2261 buf_printf(&out, "protocol-flags");
2262
2263 if (o->imported_protocol_flags & CO_USE_CC_EXIT_NOTIFY)
2264 {
2265 buf_printf(&out, " cc-exit");
2266 }
2267 if (o->imported_protocol_flags & CO_USE_TLS_KEY_MATERIAL_EXPORT)
2268 {
2269 buf_printf(&out, " tls-ekm");
2270 }
2271 if (o->imported_protocol_flags & CO_USE_DYNAMIC_TLS_CRYPT)
2272 {
2273 buf_printf(&out, " dyn-tls-crypt");
2274 }
2275 if (o->imported_protocol_flags & CO_EPOCH_DATA_KEY_FORMAT)
2276 {
2277 buf_printf(&out, " aead-epoch");
2278 }
2279 }
2280
2281 if (BLENZ(&out) > strlen(header))
2282 {
2283 msg(D_HANDSHAKE, "%s", BSTR(&out));
2284 }
2285}
2286
2287
2295static bool
2297{
2298 struct frame *frame_fragment = NULL;
2299#ifdef ENABLE_FRAGMENT
2300 if (c->options.ce.fragment)
2301 {
2302 frame_fragment = &c->c2.frame_fragment;
2303 }
2304#endif
2305
2306 /* The peer-id can also be negotiated without being pushed, so sync the
2307 * option before the frame is recalculated: it decides whether the
2308 * DATA_V2 header is accounted for */
2310
2313 frame_fragment, get_link_socket_info(c),
2314 &c->c1.tuntap->dco))
2315 {
2316 msg(D_TLS_ERRORS, "OPTIONS ERROR: failed to import crypto options");
2317 return false;
2318 }
2319
2320 return true;
2321}
2322
2323bool
2324do_up(struct context *c, bool pulled_options, uint64_t option_types_found)
2325{
2326 int error_flags = 0;
2327 if (!c->c2.do_up_ran)
2328 {
2330
2331 if (pulled_options)
2332 {
2333 if (!do_deferred_options(c, option_types_found, false))
2334 {
2335 msg(D_PUSH_ERRORS, "ERROR: Failed to apply push options");
2336 return false;
2337 }
2338 }
2339
2340 /* if --up-delay specified, open tun, do ifconfig, and run up script now */
2341 if (c->options.up_delay || PULL_DEFINED(&c->options))
2342 {
2343 c->c2.did_open_tun = do_open_tun(c, &error_flags);
2344 update_time();
2345
2346 /*
2347 * Was tun interface object persisted from previous restart iteration,
2348 * and if so did pulled options string change from previous iteration?
2349 */
2350 if (!c->c2.did_open_tun && PULL_DEFINED(&c->options) && c->c1.tuntap
2353 {
2354 /* if so, close tun, delete routes, then reinitialize tun and add routes */
2355 msg(M_INFO,
2356 "NOTE: Pulled options changed on restart, will need to close and reopen TUN/TAP device.");
2357
2358 bool tt_dco_win = tuntap_is_dco_win(c->c1.tuntap);
2359 do_close_tun(c, true);
2360
2361 if (tt_dco_win)
2362 {
2363 msg(M_NONFATAL, "dco-win doesn't yet support reopening TUN device");
2364 /* prevent link_socket_close() from closing handle with WinSock API */
2366 return false;
2367 }
2368 else
2369 {
2371 c->c2.did_open_tun = do_open_tun(c, &error_flags);
2372 update_time();
2373 }
2374 }
2375 }
2376 }
2377
2378 /* This part needs to be run in p2p mode (without pull) when the client
2379 * reconnects to setup various things (like DCO and NCP cipher) that
2380 * might have changed from the previous connection.
2381 */
2382 if (!c->c2.do_up_ran
2384 {
2385 if (c->mode == MODE_POINT_TO_POINT)
2386 {
2387 /* ovpn-dco requires adding the peer now, before any option can be set,
2388 * but *after* having parsed the pushed peer-id in do_deferred_options()
2389 */
2390 int ret = dco_p2p_add_new_peer(c);
2391 if (ret < 0)
2392 {
2393 msg(D_DCO, "Cannot add peer to DCO: %s (%d)", strerror(-ret), ret);
2394 return false;
2395 }
2396 }
2397
2398 /* do_deferred_options_part2() and do_deferred_p2p_ncp() *must* be
2399 * invoked after open_tun().
2400 * This is required by DCO because we must have created the interface
2401 * and added the peer before we can fiddle with the keys or any other
2402 * data channel per-peer setting.
2403 */
2404 if (pulled_options)
2405 {
2407 {
2408 return false;
2409 }
2410 }
2411 else
2412 {
2413 if (c->mode == MODE_POINT_TO_POINT)
2414 {
2415 if (!do_deferred_p2p_ncp(c))
2416 {
2417 msg(D_TLS_ERRORS, "ERROR: Failed to apply P2P negotiated protocol options");
2418 return false;
2419 }
2420 }
2421 }
2422
2423 if (c->c2.did_open_tun)
2424 {
2426
2427 /* if --route-delay was specified, start timer */
2429 {
2434 }
2435 else
2436 {
2437 /* client/p2p --route-delay undefined */
2438 initialization_sequence_completed(c, error_flags);
2439 }
2440 }
2441 else if (c->options.mode == MODE_POINT_TO_POINT)
2442 {
2443 /* client/p2p restart with --persist-tun */
2444 initialization_sequence_completed(c, error_flags);
2445 }
2446
2448
2449 c->c2.do_up_ran = true;
2450 if (c->c2.tls_multi)
2451 {
2453 }
2454 }
2455 return true;
2456}
2457
2458bool
2459do_update(struct context *c, uint64_t option_types_found)
2460{
2461 /* Not necessary since to receive the update the openvpn
2462 * instance must be up and running but just in case
2463 */
2464 if (!c->c2.do_up_ran)
2465 {
2466 return false;
2467 }
2468
2469 bool tt_dco_win = tuntap_is_dco_win(c->c1.tuntap);
2470 if (tt_dco_win)
2471 {
2472 msg(M_NONFATAL, "dco-win doesn't yet support reopening TUN device");
2473 return false;
2474 }
2475
2476 if (!do_deferred_options(c, option_types_found, true))
2477 {
2478 msg(D_PUSH_ERRORS, "ERROR: Failed to apply push options");
2479 return false;
2480 }
2481
2482 do_close_tun(c, true);
2483
2485 int error_flags = 0;
2486 c->c2.did_open_tun = do_open_tun(c, &error_flags);
2487 update_time();
2488
2489 if (c->c2.did_open_tun)
2490 {
2491 /* if --route-delay was specified, start timer */
2493 {
2498 }
2499
2500 initialization_sequence_completed(c, error_flags);
2501 }
2502
2504
2505 return true;
2506}
2507
2508/*
2509 * These are the option categories which will be accepted by pull.
2510 */
2511uint64_t
2513{
2518
2519 if (!c->options.route_nopull)
2520 {
2521 flags |= (OPT_P_ROUTE | OPT_P_DHCPDNS);
2522 }
2523
2524 return flags;
2525}
2526
2527static bool
2529{
2530 if (!c->c2.tls_multi)
2531 {
2532 return true;
2533 }
2534
2536
2538
2539 const char *ncp_cipher =
2541
2542 if (ncp_cipher)
2543 {
2544 c->options.ciphername = ncp_cipher;
2545 }
2546 else if (!c->options.enable_ncp_fallback)
2547 {
2548 msg(D_TLS_ERRORS, "ERROR: failed to negotiate cipher with peer and "
2549 "--data-ciphers-fallback not enabled. No usable "
2550 "data channel cipher");
2551 return false;
2552 }
2553
2554 struct frame *frame_fragment = NULL;
2555#ifdef ENABLE_FRAGMENT
2556 if (c->options.ce.fragment)
2557 {
2558 frame_fragment = &c->c2.frame_fragment;
2559 }
2560#endif
2561
2563 frame_fragment, get_link_socket_info(c),
2564 &c->c1.tuntap->dco))
2565 {
2566 msg(D_TLS_ERRORS, "ERROR: failed to set crypto cipher");
2567 return false;
2568 }
2569 return true;
2570}
2571
2572bool
2573do_deferred_options(struct context *c, const uint64_t found, const bool is_update)
2574{
2575 if (found & OPT_P_MESSAGES)
2576 {
2578 msg(D_PUSH, "OPTIONS IMPORT: --verb and/or --mute level changed");
2579 }
2580 if (found & OPT_P_TIMER)
2581 {
2582 do_init_timers(c, true);
2583 msg(D_PUSH_DEBUG, "OPTIONS IMPORT: timers and/or timeouts modified");
2584 }
2585
2586 if (found & OPT_P_EXPLICIT_NOTIFY)
2587 {
2588 /* Client side, so just check the first link_socket */
2591 {
2592 msg(D_PUSH, "OPTIONS IMPORT: --explicit-exit-notify can only be used with --proto udp");
2594 }
2595 else
2596 {
2597 msg(D_PUSH_DEBUG, "OPTIONS IMPORT: explicit notify parm(s) modified");
2598 }
2599 }
2600
2601 if (found & OPT_P_COMP)
2602 {
2604 {
2605 msg(D_PUSH_ERRORS, "OPTIONS ERROR: server pushed compression "
2606 "settings that are not allowed and will result "
2607 "in a non-working connection. "
2608 "See also allow-compression in the manual.");
2609 return false;
2610 }
2611#ifdef USE_COMP
2612 msg(D_PUSH_DEBUG, "OPTIONS IMPORT: compression parms modified");
2613 comp_uninit(c->c2.comp_context);
2614 c->c2.comp_context = comp_init(&c->options.comp);
2615#endif
2616 }
2617
2618 if (found & OPT_P_SHAPER)
2619 {
2620 msg(D_PUSH, "OPTIONS IMPORT: traffic shaper enabled");
2622 }
2623
2624 if (found & OPT_P_SOCKBUF)
2625 {
2626 msg(D_PUSH, "OPTIONS IMPORT: --sndbuf/--rcvbuf options modified");
2627
2628 for (int i = 0; i < c->c1.link_sockets_num; i++)
2629 {
2631 c->options.sndbuf);
2632 }
2633 }
2634
2635 if (found & OPT_P_PERSIST)
2636 {
2637 msg(D_PUSH, "OPTIONS IMPORT: --persist options modified");
2638 }
2639 if (found & OPT_P_UP)
2640 {
2641 msg(D_PUSH, "OPTIONS IMPORT: --ifconfig/up options modified");
2642 }
2643 if (found & OPT_P_ROUTE)
2644 {
2645 msg(D_PUSH, "OPTIONS IMPORT: route options modified");
2646 }
2647 if (found & OPT_P_ROUTE_EXTRAS)
2648 {
2649 msg(D_PUSH, "OPTIONS IMPORT: route-related options modified");
2650 }
2651 if (found & OPT_P_DHCPDNS)
2652 {
2653 msg(D_PUSH, "OPTIONS IMPORT: --ip-win32 and/or --dhcp-option options modified");
2654 }
2655 if (found & OPT_P_SETENV)
2656 {
2657 msg(D_PUSH, "OPTIONS IMPORT: environment modified");
2658 }
2659
2660 if (found & OPT_P_PEER_ID)
2661 {
2662 msg(D_PUSH_DEBUG, "OPTIONS IMPORT: peer-id set");
2663 c->c2.tls_multi->use_peer_id = true;
2666 }
2667
2668 /* process (potentially) pushed options */
2669 if (c->options.pull)
2670 {
2671 /* On PUSH_UPDATE, NCP related flags are never updated, and so the code
2672 * would assume "no cipher pushed = NCP failed" - so, don't call it on
2673 * updates */
2674 if (!is_update && !check_pull_client_ncp(c, found))
2675 {
2676 return false;
2677 }
2678
2679 /* Check if pushed options are compatible with DCO, if enabled */
2681 {
2682 msg(D_PUSH_ERRORS, "OPTIONS ERROR: pushed options are incompatible "
2683 "with data channel offload. Use --disable-dco to connect to "
2684 "this server");
2685 return false;
2686 }
2687 }
2688
2689 /* Ensure that for epoch data format is only enabled if also data v2
2690 * is enabled */
2692 bool datav2_enabled = c->c2.tls_multi->use_peer_id && c->c2.tls_multi->tx_peer_id < MAX_PEER_ID;
2693
2694 if (epoch_data && !datav2_enabled)
2695 {
2696 msg(D_PUSH_ERRORS, "OPTIONS ERROR: Epoch key data format tag requires "
2697 "data v2 (peer-id) to be enabled.");
2698 return false;
2699 }
2700
2701
2702 if (found & OPT_P_PUSH_MTU)
2703 {
2704 /* MTU has changed, check that the pushed MTU is small enough to
2705 * be able to change it */
2706 msg(D_PUSH, "OPTIONS IMPORT: tun-mtu set to %d", c->options.ce.tun_mtu);
2707
2708 struct frame *frame = &c->c2.frame;
2709
2711 {
2713 "Server-pushed tun-mtu is too large, please add "
2714 "tun-mtu-max %d in the client configuration",
2715 c->options.ce.tun_mtu);
2716 }
2718 }
2719
2720 return true;
2721}
2722
2723/*
2724 * Possible hold on initialization, holdtime is the
2725 * time OpenVPN would wait without management
2726 */
2727static bool
2728do_hold(int holdtime)
2729{
2730#ifdef ENABLE_MANAGEMENT
2731 if (management)
2732 {
2733 /* block until management hold is released */
2734 if (management_hold(management, holdtime))
2735 {
2736 return true;
2737 }
2738 }
2739#endif
2740 return false;
2741}
2742
2743/*
2744 * Sleep before restart.
2745 */
2746static void
2748{
2749 int sec = 2;
2750 int backoff = 0;
2751
2752 switch (c->mode)
2753 {
2754 case CM_TOP:
2755 sec = 1;
2756 break;
2757
2758 case CM_CHILD_UDP:
2759 case CM_CHILD_TCP:
2761 break;
2762 }
2763
2764#ifdef ENABLE_DEBUG
2765 if (GREMLIN_CONNECTION_FLOOD_LEVEL(c->options.gremlin))
2766 {
2767 sec = 0;
2768 }
2769#endif
2770
2772 {
2773 sec = 10;
2774 }
2775
2776 /* Slow down reconnection after 5 retries per remote -- for TCP client or UDP tls-client only */
2777 if (c->mode == CM_CHILD_TCP || (c->options.ce.proto == PROTO_UDP && c->options.tls_client))
2778 {
2779 backoff = (c->options.unsuccessful_attempts / c->options.connection_list->len) - 4;
2780 if (backoff > 0)
2781 {
2782 /* sec is less than 2^16; we can left shift it by up to 15 bits without overflow */
2783 sec = max_int(sec, 1) << min_int(backoff, 15);
2784 }
2786 {
2787 sec = max_int(sec, c->options.server_backoff_time);
2789 }
2790
2792 {
2794 }
2795 }
2796
2798 {
2800 }
2801 else if (c->persist.restart_sleep_seconds == -1)
2802 {
2803 sec = 0;
2804 }
2806
2807 /* do management hold on context restart, i.e. second, third, fourth, etc. initialization */
2808 if (do_hold(sec))
2809 {
2810 sec = 0;
2811 }
2812
2813 if (sec)
2814 {
2815 msg(D_RESTART, "Restart pause, %d second(s)", sec);
2816 management_sleep(sec);
2817 }
2818}
2819
2820/*
2821 * Do a possible pause on context_2 initialization.
2822 */
2823static void
2825{
2826 if (!c->first_time)
2827 {
2829 }
2830 else
2831 {
2832 do_hold(0); /* do management hold on first context initialization */
2833 }
2834}
2835
2836static int
2837get_frame_mtu(struct context *c, const struct options *o)
2838{
2839 size_t mtu;
2840
2841 if (o->ce.link_mtu_defined)
2842 {
2844 /* if we have a link mtu defined we calculate what the old code
2845 * would have come up with as tun-mtu */
2846 size_t overhead = frame_calculate_protocol_header_size(&c->c1.ks.key_type, o, true);
2847 mtu = o->ce.link_mtu - overhead;
2848 }
2849 else
2850 {
2852 mtu = o->ce.tun_mtu;
2853 }
2854
2855 if (mtu < TUN_MTU_MIN)
2856 {
2857 msg(M_WARN, "TUN MTU value (%zu) must be at least %d", mtu, TUN_MTU_MIN);
2858 frame_print(&c->c2.frame, M_FATAL, "MTU is too small");
2859 }
2860 if (mtu > TUN_MTU_MAX)
2861 {
2862 msg(M_WARN, "TUN MTU value (%zu) clamped to %d", mtu, TUN_MTU_MAX);
2863 mtu = TUN_MTU_MAX;
2864 }
2865 return (int)mtu;
2866}
2867
2868/*
2869 * Finalize MTU parameters based on command line or config file options.
2870 */
2871static void
2872frame_finalize_options(struct context *c, const struct options *o)
2873{
2874 if (!o)
2875 {
2876 o = &c->options;
2877 }
2878
2879 struct frame *frame = &c->c2.frame;
2880
2881 frame->tun_mtu = get_frame_mtu(c, o);
2883
2884 /* max mtu needs to be at least as large as the tun mtu */
2886
2887 /* We always allow at least 1600 MTU packets to be received in our buffer
2888 * space to allow server to push "baby giant" MTU sizes */
2890
2891 unsigned int payload_size = frame->tun_max_mtu;
2892
2893 /* we need to be also large enough to hold larger control channel packets
2894 * if configured */
2896
2897 /* The extra tun needs to be added to the payload size */
2898 if (o->ce.tun_mtu_defined)
2899 {
2901 }
2902
2903 /* Add 32 byte of extra space in the buffer to account for small errors
2904 * in the calculation */
2905 payload_size += 32;
2906
2907
2908 /* the space that is reserved before the payload to add extra headers to it
2909 * we always reserve the space for the worst case */
2910 unsigned int headroom = 0;
2911
2912 /* includes IV and packet ID */
2914
2915 /* peer id + opcode */
2916 headroom += 4;
2917
2918 /* socks proxy header */
2920
2921 /* compression header and fragment header (part of the encrypted payload) */
2922 headroom += 1 + 1;
2923
2924 /* Round up headroom to the next multiple of 4 to ensure alignment */
2925 headroom = (headroom + 3) & ~3;
2926
2927 /* Add the headroom to the payloadsize as a received (IP) packet can have
2928 * all the extra headers in it */
2930
2931 /* the space after the payload, this needs some extra buffer space for
2932 * encryption so headroom is probably too much but we do not really care
2933 * the few extra bytes */
2934 unsigned int tailroom = headroom;
2935
2936#ifdef USE_COMP
2938 "MTU: adding %u buffer tailroom for compression for %u "
2939 "bytes of payload",
2940 COMP_EXTRA_BUFFER(payload_size), payload_size);
2941 tailroom += COMP_EXTRA_BUFFER(payload_size);
2942#endif
2943
2947}
2948
2949/*
2950 * Free a key schedule, including OpenSSL components.
2951 */
2952static void
2953key_schedule_free(struct key_schedule *ks, bool free_ssl_ctx)
2954{
2956 if (tls_ctx_initialised(ks->ssl_ctx) && free_ssl_ctx)
2957 {
2958 tls_ctx_free(ks->ssl_ctx);
2959 free(ks->ssl_ctx);
2961 }
2962 CLEAR(*ks);
2963}
2964
2965static void
2966init_crypto_pre(struct context *c, const unsigned int flags)
2967{
2968 if (c->options.engine)
2969 {
2971 }
2972
2973 if (flags & CF_LOAD_PERSISTED_PACKET_ID)
2974 {
2975 /* load a persisted packet-id for cross-session replay-protection */
2976 if (c->options.packet_id_file)
2977 {
2979 }
2980 }
2981}
2982
2983
2984static void
2986{
2987 const struct options *options = &c->options;
2989
2990 init_crypto_pre(c, 0);
2991
2993
2994 /* Initialize packet ID tracking */
2996 "STATIC", 0);
2997
2999
3000 /* Init cipher and hash algorithm */
3002 options->test_crypto, true);
3003
3005 "test crypto key");
3006
3007 /* Get key schedule */
3009}
3010
3011/*
3012 * Static Key Mode (using a pre-shared key)
3013 */
3014static void
3015do_init_crypto_static(struct context *c, const unsigned int flags)
3016{
3017 const struct options *options = &c->options;
3019
3020 init_crypto_pre(c, flags);
3021
3022 /* Initialize flags */
3024 {
3026 }
3027
3028 /* Initialize packet ID tracking */
3030 "STATIC", 0);
3034
3036 {
3037 /* Get cipher & hash algorithms */
3039 options->test_crypto, true);
3040
3041 /* Read cipher and hmac keys from shared secret file */
3044 options->key_direction, "Static Key Encryption", "secret", NULL);
3045 }
3046 else
3047 {
3048 msg(M_INFO, "Re-using pre-shared static key");
3049 }
3050
3051 /* Get key schedule */
3053}
3054
3055/*
3056 * Initialize the tls-auth/crypt key context
3057 */
3058static void
3060{
3061 const struct options *options = &c->options;
3062
3063 /* TLS handshake authentication (--tls-auth) */
3065 {
3066 /* Initialize key_type for tls-auth with auth only */
3068 c->c1.ks.tls_auth_key_type.cipher = "none";
3070 if (!md_valid(options->authname))
3071 {
3072 msg(M_FATAL,
3073 "ERROR: tls-auth enabled, but no valid --auth "
3074 "algorithm specified ('%s')",
3075 options->authname);
3076 }
3077
3080 options->ce.key_direction, "Control Channel Authentication",
3081 "tls-auth", &c->c1.ks.original_wrap_keydata);
3082 }
3083
3084 /* TLS handshake encryption+authentication (--tls-crypt) */
3086 {
3090 }
3091
3092 /* tls-crypt with client-specific keys (--tls-crypt-v2) */
3094 {
3095 if (options->tls_server)
3096 {
3100 }
3101 else
3102 {
3106 }
3107 /* We have to ensure that the loaded tls-crypt key is small enough
3108 * to fit into the initial hard reset v3 packet */
3109 int wkc_len = buf_len(&c->c1.ks.tls_crypt_v2_wkc);
3110
3111 /* empty ACK/message id, tls-crypt, Opcode, UDP, ipv6 */
3112 int required_size = 5 + wkc_len + tls_crypt_buf_overhead() + 1 + 8 + 40;
3113
3114 if (required_size > c->options.ce.tls_mtu)
3115 {
3116 msg(M_WARN,
3117 "ERROR: tls-crypt-v2 client key too large to work with "
3118 "requested --max-packet-size %d, requires at least "
3119 "--max-packet-size %d. Packets will ignore requested "
3120 "maximum packet size",
3121 c->options.ce.tls_mtu, required_size);
3122 }
3123 }
3124}
3125
3126/*
3127 * Initialize the persistent component of OpenVPN's TLS mode,
3128 * which is preserved across SIGUSR1 resets.
3129 */
3130static void
3132{
3133 const struct options *options = &c->options;
3134
3136 {
3137 /*
3138 * Initialize the OpenSSL library's global
3139 * SSL context.
3140 */
3141 ASSERT(NULL == c->c1.ks.ssl_ctx);
3144 {
3145 switch (auth_retry_get())
3146 {
3147 case AR_NONE:
3148 msg(M_FATAL, "Error: private key password verification failed");
3149 break;
3150
3151 case AR_INTERACT:
3152 ssl_purge_auth(false);
3153 /* Intentional [[fallthrough]]; */
3154
3155 case AR_NOINTERACT:
3156 /* SOFT-SIGUSR1 -- Password failure error */
3157 register_signal(c->sig, SIGUSR1, "private-key-password-failure");
3158 break;
3159
3160 default:
3161 ASSERT(0);
3162 }
3163 return;
3164 }
3165
3166 /*
3167 * BF-CBC is allowed to be used only when explicitly configured
3168 * as NCP-fallback or when NCP has been disabled or explicitly
3169 * allowed in the in ncp_ciphers list.
3170 * In all other cases do not attempt to initialize BF-CBC as it
3171 * may not even be supported by the underlying SSL library.
3172 *
3173 * Therefore, the key structure has to be initialized when:
3174 * - any non-BF-CBC cipher was selected; or
3175 * - BF-CBC is selected, NCP is enabled and fallback is enabled
3176 * (BF-CBC will be the fallback).
3177 * - BF-CBC is in data-ciphers and we negotiate to use BF-CBC:
3178 * If the negotiated cipher and options->ciphername are the
3179 * same we do not reinit the cipher
3180 *
3181 * Note that BF-CBC will still be part of the OCC string to retain
3182 * backwards compatibility with older clients.
3183 */
3184 const char *ciphername = options->ciphername;
3185 if (streq(options->ciphername, "BF-CBC")
3188 {
3189 ciphername = "none";
3190 }
3191
3192 /* Do not warn if the cipher is used only in OCC */
3193 bool warn = options->enable_ncp_fallback;
3195
3196 /* initialize tls-auth/crypt/crypt-v2 key */
3198
3199 /* initialise auth-token crypto support */
3201 {
3204 }
3205
3206#if 0 /* was: #if ENABLE_INLINE_FILES -- Note that enabling this code will break restarts */
3208 {
3210 c->options.priv_key_file_inline = NULL;
3211 }
3212#endif
3213 }
3214 else
3215 {
3216 msg(D_INIT_MEDIUM, "Re-using SSL/TLS context");
3217
3218 /*
3219 * tls-auth/crypt key can be configured per connection block, therefore
3220 * we must reload it as it may have changed
3221 */
3223 }
3224}
3225
3226static void
3227do_init_crypto_tls(struct context *c, const unsigned int flags)
3228{
3229 const struct options *options = &c->options;
3230 struct tls_options to;
3231 bool packet_id_long_form;
3232
3235
3236 init_crypto_pre(c, flags);
3237
3238 /* Make sure we are either a TLS client or server but not both */
3240
3241 /* initialize persistent component */
3243 if (IS_SIG(c))
3244 {
3245 return;
3246 }
3247
3248 /* In short form, unique datagram identifier is 32 bits, in long form 64 bits */
3249 packet_id_long_form = cipher_kt_mode_ofb_cfb(c->c1.ks.key_type.cipher);
3250
3251 /* Set all command-line TLS-related options */
3252 CLEAR(to);
3253
3255 {
3257 }
3258
3260 if (packet_id_long_form)
3261 {
3263 }
3264
3265 to.ssl_ctx = c->c1.ks.ssl_ctx;
3266 to.key_type = c->c1.ks.key_type;
3278 {
3279 /* Add 10% jitter to reneg-sec by default (server side only) */
3280 int jitter_max = max_int(options->renegotiate_seconds / 10, 1);
3281 int auto_jitter = options->mode != MODE_SERVER ? 0 : (int)(get_random() % jitter_max);
3283 }
3284 else
3285 {
3286 /* Add user-specified jitter to reneg-sec */
3288 to.renegotiate_seconds = options->renegotiate_seconds - (int)(get_random() % jitter_max);
3289 }
3291 to.mode = options->mode;
3292 to.pull = options->pull;
3293 if (options->push_peer_info) /* all there is */
3294 {
3295 to.push_peer_info_detail = 3;
3296 }
3297 else if (options->pull) /* pull clients send some details */
3298 {
3299 to.push_peer_info_detail = 2;
3300 }
3301 else if (options->mode == MODE_SERVER) /* server: no peer info at all */
3302 {
3303 to.push_peer_info_detail = 0;
3304 }
3305 else /* default: minimal info to allow NCP in P2P mode */
3306 {
3307 to.push_peer_info_detail = 1;
3308 }
3309
3310 /* Check if the DCO drivers support the epoch data format */
3311 if (dco_enabled(options))
3312 {
3314 }
3315 else
3316 {
3317 to.data_epoch_supported = true;
3318 }
3319
3320 /* should we not xmit any packets until we get an initial
3321 * response from client? */
3322 if (to.server && (c->mode == CM_CHILD_TCP || (c->mode == CM_P2P && options->ce.proto == PROTO_TCP_SERVER)))
3323 {
3324 to.xmit_hold = true;
3325 }
3326
3334 memcpy(to.remote_cert_ku, options->remote_cert_ku, sizeof(to.remote_cert_ku));
3341 to.es = c->c2.es;
3342 to.net_ctx = &c->net_ctx;
3343
3344#ifdef ENABLE_DEBUG
3345 to.gremlin = c->options.gremlin;
3346#endif
3347
3348 to.plugins = c->plugins;
3349
3350#ifdef ENABLE_MANAGEMENT
3351 to.mda_context = &c->c2.mda_context;
3352#endif
3353
3357 to.tmp_dir = options->tmp_dir;
3360 {
3362 }
3371
3373
3374#ifdef ENABLE_MANAGEMENT
3375 to.sci = &options->sc_info;
3376#endif
3377
3378#ifdef USE_COMP
3379 to.comp_options = options->comp;
3380#endif
3381
3383 {
3385 if (to.ekm_size < 16 || to.ekm_size > 4095)
3386 {
3387 to.ekm_size = 0;
3388 }
3389
3391 to.ekm_label_size = strlen(to.ekm_label);
3392 }
3393 else
3394 {
3395 to.ekm_size = 0;
3396 }
3397
3398 /* TLS handshake authentication (--tls-auth) */
3400 {
3401 to.tls_wrap.mode = TLS_WRAP_AUTH;
3402 }
3403
3404 /* TLS handshake encryption (--tls-crypt) */
3406 {
3407 to.tls_wrap.mode = TLS_WRAP_CRYPT;
3408 }
3409
3410 if (to.tls_wrap.mode == TLS_WRAP_AUTH || to.tls_wrap.mode == TLS_WRAP_CRYPT)
3411 {
3416 }
3417
3419 {
3420 to.tls_crypt_v2 = true;
3422
3423 if (options->tls_server)
3424 {
3429 {
3431 }
3432 }
3433 }
3434
3435 /* let the TLS engine know if keys have to be installed in DCO or not */
3437
3438 /*
3439 * Initialize OpenVPN's master TLS-mode object.
3440 */
3441 if (flags & CF_INIT_TLS_MULTI)
3442 {
3443 c->c2.tls_multi = tls_multi_init(&to);
3444 /* inherit the dco context from the tuntap object */
3445 if (c->c1.tuntap)
3446 {
3447 c->c2.tls_multi->dco = &c->c1.tuntap->dco;
3448 }
3449 }
3450
3451 if (flags & CF_INIT_TLS_AUTH_STANDALONE)
3452 {
3455 }
3456}
3457
3458static void
3460{
3461 if (c->c2.tls_multi)
3462 {
3465 frame_print(&c->c2.tls_multi->opt.frame, D_MTU_INFO, "Control Channel MTU parms");
3466
3467 /* Keep the max mtu also in the frame of tls multi so it can access
3468 * it in push_peer_info */
3470 }
3471 if (c->c2.tls_auth_standalone)
3472 {
3474 c->options.ce.tls_mtu);
3475 frame_print(&c->c2.tls_auth_standalone->frame, D_MTU_INFO, "TLS-Auth MTU parms");
3478 }
3479}
3480
3481/*
3482 * No encryption or authentication.
3483 */
3484static void
3486{
3488
3489 /* Initialise key_type with auth/cipher "none", so the key_type struct is
3490 * valid */
3491 init_key_type(&c->c1.ks.key_type, "none", "none", c->options.test_crypto, true);
3492
3493 msg(M_WARN, "******* WARNING *******: All encryption and authentication features "
3494 "disabled -- All data will be tunnelled as clear text and will not be "
3495 "protected against man-in-the-middle changes. "
3496 "PLEASE DO RECONSIDER THIS CONFIGURATION!");
3497}
3498
3499static void
3500do_init_crypto(struct context *c, const unsigned int flags)
3501{
3503 {
3504 do_init_crypto_static(c, flags);
3505 }
3506 else if (c->options.tls_server || c->options.tls_client)
3507 {
3508 do_init_crypto_tls(c, flags);
3509 }
3510 else /* no encryption or authentication. */
3511 {
3513 }
3514}
3515
3516static void
3518{
3519 /*
3520 * Adjust frame size based on the --tun-mtu-extra parameter.
3521 */
3523 {
3525 }
3526
3527 /*
3528 * Fill in the blanks in the frame parameters structure,
3529 * make sure values are rational, etc.
3530 */
3531 frame_finalize_options(c, NULL);
3532
3533
3534#if defined(ENABLE_FRAGMENT)
3535 /*
3536 * MTU advisories
3537 */
3538 if (c->options.ce.fragment && c->options.mtu_test)
3539 {
3540 msg(M_WARN,
3541 "WARNING: using --fragment and --mtu-test together may produce an inaccurate MTU test result");
3542 }
3543#endif
3544
3545#ifdef ENABLE_FRAGMENT
3546 if (c->options.ce.fragment > 0 && c->options.ce.mssfix > c->options.ce.fragment)
3547 {
3548 msg(M_WARN,
3549 "WARNING: if you use --mssfix and --fragment, you should "
3550 "set --fragment (%d) larger or equal than --mssfix (%d)",
3552 }
3553 if (c->options.ce.fragment > 0 && c->options.ce.mssfix > 0
3555 {
3556 msg(M_WARN, "WARNING: if you use --mssfix and --fragment, you should "
3557 "use the \"mtu\" flag for both or none of of them.");
3558 }
3559#endif
3560}
3561
3562static void
3564{
3565 const struct options *o = &c->options;
3566
3567 if (o->ping_send_timeout && !o->ping_rec_timeout)
3568 {
3569 msg(M_WARN, "WARNING: --ping should normally be used with --ping-restart or --ping-exit");
3570 }
3571
3572 if (o->username || o->groupname || o->chroot_dir
3573#ifdef ENABLE_SELINUX
3574 || o->selinux_context
3575#endif
3576 )
3577 {
3578 if (!o->persist_tun)
3579 {
3580 msg(M_WARN,
3581 "WARNING: you are using user/group/chroot/setcon without persist-tun -- this may cause restarts to fail");
3582 }
3583 }
3584
3585 if (o->chroot_dir && !(o->username && o->groupname))
3586 {
3587 msg(M_WARN,
3588 "WARNING: you are using chroot without specifying user and group -- this may cause the chroot jail to be insecure");
3589 }
3590
3591 if (o->pull && o->ifconfig_local && c->first_time)
3592 {
3593 msg(M_WARN,
3594 "WARNING: using --pull/--client and --ifconfig together is probably not what you want");
3595 }
3596
3598 {
3599 msg(M_WARN,
3600 "NOTE: when bridging your LAN adapter with the TAP adapter, note that the new bridge adapter will often take on its own IP address that is different from what the LAN adapter was previously set to");
3601 }
3602
3603 if (o->mode == MODE_SERVER)
3604 {
3605 if (o->duplicate_cn && o->client_config_dir)
3606 {
3607 msg(M_WARN,
3608 "WARNING: using --duplicate-cn and --client-config-dir together is probably not what you want");
3609 }
3611 {
3612 msg(M_WARN, "WARNING: --ifconfig-pool-persist will not work with --duplicate-cn");
3613 }
3614 if (!o->keepalive_ping || !o->keepalive_timeout)
3615 {
3616 msg(M_WARN, "WARNING: --keepalive option is missing from server config");
3617 }
3618 }
3619
3620 if (o->tls_server)
3621 {
3623 }
3626 && !(o->verify_hash_depth == 0 && o->verify_hash))
3627 {
3628 msg(M_WARN,
3629 "WARNING: No server certificate verification method has been enabled. See http://openvpn.net/howto.html#mitm for more info.");
3630 }
3631 if (o->ns_cert_type)
3632 {
3633 msg(M_WARN, "WARNING: --ns-cert-type is DEPRECATED. Use --remote-cert-tls instead.");
3634 }
3635
3636 /* If a script is used, print appropriate warnings */
3637 if (o->user_script_used)
3638 {
3640 {
3641 msg(M_WARN,
3642 "NOTE: the current --script-security setting may allow this configuration to call user-defined scripts");
3643 }
3644 else if (script_security() >= SSEC_PW_ENV)
3645 {
3646 msg(M_WARN,
3647 "WARNING: the current --script-security setting may allow passwords to be passed to scripts via environmental variables");
3648 }
3649 else
3650 {
3651 msg(M_WARN,
3652 "NOTE: starting with " PACKAGE_NAME
3653 " 2.1, '--script-security 2' or higher is required to call user-defined scripts or executables");
3654 }
3655 }
3656}
3657
3658struct context_buffers *
3660{
3661 struct context_buffers *b;
3662
3664
3665 size_t buf_size = BUF_SIZE(frame);
3666
3667 b->read_link_buf = alloc_buf(buf_size);
3668 b->read_tun_buf = alloc_buf(buf_size);
3669
3670 b->aux_buf = alloc_buf(buf_size);
3671
3672 b->encrypt_buf = alloc_buf(buf_size);
3673 b->decrypt_buf = alloc_buf(buf_size);
3674
3675#ifdef USE_COMP
3676 b->compress_buf = alloc_buf(buf_size);
3677 b->decompress_buf = alloc_buf(buf_size);
3678#endif
3679
3680 return b;
3681}
3682
3683void
3685{
3686 if (b)
3687 {
3690 free_buf(&b->aux_buf);
3691
3692#ifdef USE_COMP
3693 free_buf(&b->compress_buf);
3694 free_buf(&b->decompress_buf);
3695#endif
3696
3697 free_buf(&b->encrypt_buf);
3698 free_buf(&b->decrypt_buf);
3699
3700 free(b);
3701 }
3702}
3703
3704/*
3705 * Now that we know all frame parameters, initialize
3706 * our buffers.
3707 */
3708static void
3710{
3712 c->c2.buffers_owned = true;
3713}
3714
3715#ifdef ENABLE_FRAGMENT
3716/*
3717 * Fragmenting code has buffers to initialize
3718 * once frame parameters are known.
3719 */
3720static void
3722{
3724
3725 /*
3726 * Set frame parameter for fragment code. This is necessary because
3727 * the fragmentation code deals with payloads which have already been
3728 * passed through the compression code.
3729 */
3730 c->c2.frame_fragment = c->c2.frame;
3731
3735}
3736#endif
3737
3738/*
3739 * Allocate our socket object.
3740 */
3741static void
3743{
3744 ASSERT(!c->c2.link_sockets);
3745
3747
3748 for (int i = 0; i < c->c1.link_sockets_num; i++)
3749 {
3751 }
3752 c->c2.link_socket_owned = true;
3753}
3754
3755/*
3756 * bind TCP/UDP sockets
3757 */
3758static void
3760{
3761 for (int i = 0; i < c->c1.link_sockets_num; i++)
3762 {
3763 int mode = LS_MODE_DEFAULT;
3764
3765 /* mode allows CM_CHILD_TCP
3766 * instances to inherit acceptable fds
3767 * from a top-level parent */
3768 if (c->options.mode == MODE_SERVER)
3769 {
3770 /* initializing listening socket */
3771 if (c->mode == CM_TOP)
3772 {
3773 mode = LS_MODE_TCP_LISTEN;
3774 }
3775 /* initializing socket to client */
3776 else if (c->mode == CM_CHILD_TCP)
3777 {
3779 }
3780 }
3781
3782 /* init each socket with its specific args */
3783 link_socket_init_phase1(c, i, mode);
3784 }
3785}
3786
3787/*
3788 * finalize TCP/UDP sockets
3789 */
3790static void
3792{
3793 for (int i = 0; i < c->c1.link_sockets_num; i++)
3794 {
3796 }
3797}
3798
3799/*
3800 * Print MTU INFO
3801 */
3802static void
3804{
3805 frame_print(&c->c2.frame, D_MTU_INFO, "Data Channel MTU parms");
3806#ifdef ENABLE_FRAGMENT
3807 if (c->c2.fragment)
3808 {
3809 frame_print(&c->c2.frame_fragment, D_MTU_INFO, "Fragmentation MTU parms");
3810 }
3811#endif
3812}
3813
3814/*
3815 * Get local and remote options compatibility strings.
3816 */
3817static void
3819{
3820 struct gc_arena gc = gc_new();
3821
3823 options_string(&c->options, &c->c2.frame, c->c1.tuntap, &c->net_ctx, false, &gc);
3825 options_string(&c->options, &c->c2.frame, c->c1.tuntap, &c->net_ctx, true, &gc);
3826
3827 msg(D_SHOW_OCC, "Local Options String (VER=%s): '%s'",
3829 msg(D_SHOW_OCC, "Expected Remote Options String (VER=%s): '%s'",
3831
3832 if (c->c2.tls_multi)
3833 {
3836 }
3837
3838 gc_free(&gc);
3839}
3840
3841/*
3842 * These things can only be executed once per program instantiation.
3843 * Set up for possible UID/GID downgrade, but don't do it yet.
3844 * Daemonize if requested.
3845 */
3846static void
3848{
3849 if (c->first_time && !c->c0)
3850 {
3851 struct context_0 *c0;
3852
3853 ALLOC_OBJ_CLEAR_GC(c->c0, struct context_0, &c->gc);
3854 c0 = c->c0;
3855
3856 /* get user and/or group that we want to setuid/setgid to,
3857 * sets also platform_x_state */
3858 bool group_defined = platform_group_get(c->options.groupname, &c0->platform_state_group);
3859 bool user_defined = platform_user_get(c->options.username, &c0->platform_state_user);
3860
3861 c0->uid_gid_specified = user_defined || group_defined;
3862
3863 /* fork the dns script runner to preserve root? */
3864 c->persist.duri.required = user_defined;
3865
3866 /* perform postponed chdir if --daemon */
3867 if (c->did_we_daemonize && c->options.cd_dir == NULL)
3868 {
3869 platform_chdir("/");
3870 }
3871
3872 /* should we change scheduling priority? */
3874 }
3875}
3876
3877/*
3878 * free buffers
3879 */
3880static void
3882{
3883 if (c->c2.buffers_owned)
3884 {
3886 c->c2.buffers = NULL;
3887 c->c2.buffers_owned = false;
3888 }
3889}
3890
3891/*
3892 * close TLS
3893 */
3894static void
3896{
3897 if (c->c2.tls_multi)
3898 {
3899 tls_multi_free(c->c2.tls_multi, true);
3900 c->c2.tls_multi = NULL;
3901 }
3902
3903 /* free options compatibility strings */
3904 free(c->c2.options_string_local);
3905 free(c->c2.options_string_remote);
3906
3908
3909 if (c->c2.pulled_options_state)
3910 {
3913 }
3914
3916}
3917
3918/*
3919 * Free key schedules
3920 */
3921static void
3922do_close_free_key_schedule(struct context *c, bool free_ssl_ctx)
3923{
3924 /*
3925 * always free the tls_auth/crypt key. The key will
3926 * be reloaded from memory (pre-cached)
3927 */
3930 CLEAR(c->c1.ks.tls_wrap_key);
3933
3934 if (!(c->sig->signal_received == SIGUSR1))
3935 {
3936 key_schedule_free(&c->c1.ks, free_ssl_ctx);
3937 }
3938}
3939
3940/*
3941 * Close TCP/UDP connection
3942 */
3943static void
3945{
3946 if (c->c2.link_sockets && c->c2.link_socket_owned)
3947 {
3948 for (int i = 0; i < c->c1.link_sockets_num; i++)
3949 {
3950 /* in dco-win case, link socket is a tun handle which is
3951 * closed in do_close_tun(). Set it to UNDEFINED so
3952 * we won't use WinSock API to close it. */
3953 if (tuntap_is_dco_win(c->c1.tuntap))
3954 {
3956 }
3957
3959 }
3960 c->c2.link_sockets = NULL;
3961 }
3962
3963
3964 /* Preserve the resolved list of remote if the user request to or if we want
3965 * reconnect to the same host again or there are still addresses that need
3966 * to be tried */
3967 if (!(c->sig->signal_received == SIGUSR1
3968 && ((c->options.persist_remote_ip)
3969 || (c->sig->source != SIG_SOURCE_HARD
3971 && c->c1.link_socket_addrs[0].current_remote->ai_next)
3972 || c->options.no_advance)))))
3973 {
3975 }
3976
3977 /* Clear the remote actual address when persist_remote_ip is not in use */
3979 {
3980 for (int i = 0; i < c->c1.link_sockets_num; i++)
3981 {
3983 }
3984 }
3985
3987 {
3988 for (int i = 0; i < c->c1.link_sockets_num; i++)
3989 {
3991 {
3992 freeaddrinfo(c->c1.link_socket_addrs[i].bind_local);
3993 }
3994
3995 c->c1.link_socket_addrs[i].bind_local = NULL;
3996 }
3997 }
3998}
3999
4000/*
4001 * Close packet-id persistence file
4002 */
4003static void
4013
4014#ifdef ENABLE_FRAGMENT
4015/*
4016 * Close fragmentation handler.
4017 */
4018static void
4020{
4021 if (c->c2.fragment)
4022 {
4024 c->c2.fragment = NULL;
4025 }
4026}
4027#endif
4028
4029/*
4030 * Open and close our event objects.
4031 */
4032
4033static void
4034do_event_set_init(struct context *c, bool need_us_timeout)
4035{
4036 unsigned int flags = 0;
4037
4039
4040 flags |= EVENT_METHOD_FAST;
4041
4042 if (need_us_timeout)
4043 {
4044 flags |= EVENT_METHOD_US_TIMEOUT;
4045 }
4046
4047 c->c2.event_set = event_set_init(&c->c2.event_set_max, flags);
4048 c->c2.event_set_owned = true;
4049}
4050
4051static void
4053{
4054 if (c->c2.event_set && c->c2.event_set_owned)
4055 {
4057 c->c2.event_set = NULL;
4058 c->c2.event_set_owned = false;
4059 }
4060}
4061
4062/*
4063 * Open and close --status file
4064 */
4065
4066static void
4068{
4069 if (!c->c1.status_output)
4070 {
4071 c->c1.status_output =
4074 c->c1.status_output_owned = true;
4075 }
4076}
4077
4078static void
4080{
4081 if (!(c->sig->signal_received == SIGUSR1))
4082 {
4084 {
4086 c->c1.status_output = NULL;
4087 c->c1.status_output_owned = false;
4088 }
4089 }
4090}
4091
4092/*
4093 * Handle ifconfig-pool persistence object.
4094 */
4095static void
4106
4107static void
4109{
4110 if (!(c->sig->signal_received == SIGUSR1))
4111 {
4113 {
4115 c->c1.ifconfig_pool_persist = NULL;
4117 }
4118 }
4119}
4120
4121/*
4122 * Inherit environmental variables
4123 */
4124
4125static void
4126do_inherit_env(struct context *c, const struct env_set *src)
4127{
4128 c->c2.es = env_set_create(NULL);
4129 c->c2.es_owned = true;
4130 env_set_inherit(c->c2.es, src);
4131}
4132
4133static void
4135{
4136 if (c->c2.es && c->c2.es_owned)
4137 {
4138 env_set_destroy(c->c2.es);
4139 c->c2.es = NULL;
4140 c->c2.es_owned = false;
4141 }
4142}
4143
4144static void
4146{
4147 if (c->options.tls_exit)
4148 {
4150 }
4151 else
4152 {
4154 }
4155}
4156
4157#ifdef ENABLE_PLUGIN
4158
4159void
4161{
4162 if (c->options.plugin_list && !c->plugins)
4163 {
4165 c->plugins_owned = true;
4166 }
4167}
4168
4169void
4170open_plugins(struct context *c, const bool import_options, int init_point)
4171{
4172 if (c->plugins && c->plugins_owned)
4173 {
4174 if (import_options)
4175 {
4176 struct plugin_return pr, config;
4177 plugin_return_init(&pr);
4178 plugin_list_open(c->plugins, c->options.plugin_list, &pr, c->c2.es, init_point);
4179 plugin_return_get_column(&pr, &config, "config");
4180 if (plugin_return_defined(&config))
4181 {
4182 int i;
4183 for (i = 0; i < config.n; ++i)
4184 {
4185 uint64_t option_types_found = 0;
4186 if (config.list[i] && config.list[i]->value)
4187 {
4189 &c->options, config.list[i]->value, D_IMPORT_ERRORS | M_OPTERR,
4190 OPT_P_DEFAULT & ~OPT_P_PLUGIN, &option_types_found, c->es);
4191 }
4192 }
4193 }
4194 plugin_return_free(&pr);
4195 }
4196 else
4197 {
4198 plugin_list_open(c->plugins, c->options.plugin_list, NULL, c->c2.es, init_point);
4199 }
4200 }
4201}
4202
4203static void
4205{
4206 if (c->plugins && c->plugins_owned && !(c->sig->signal_received == SIGUSR1))
4207 {
4209 c->plugins = NULL;
4210 c->plugins_owned = false;
4211 }
4212}
4213
4214static void
4215do_inherit_plugins(struct context *c, const struct context *src)
4216{
4217 if (!c->plugins && src->plugins)
4218 {
4220 c->plugins_owned = true;
4221 }
4222}
4223
4224#endif /* ifdef ENABLE_PLUGIN */
4225
4226#ifdef ENABLE_MANAGEMENT
4227
4228static void
4229management_callback_status_p2p(void *arg, const int version, struct status_output *so)
4230{
4231 struct context *c = (struct context *)arg;
4232 print_status(c, so);
4233}
4234
4235void
4236management_show_net_callback(void *arg, const msglvl_t msglevel)
4237{
4238#ifdef _WIN32
4239 show_routes(msglevel);
4240 show_adapters(msglevel);
4241 msg(msglevel, "END");
4242#else
4243 msg(msglevel, "ERROR: Sorry, this command is currently only implemented on Windows");
4244#endif
4245}
4246
4247#ifdef TARGET_ANDROID
4248int
4249management_callback_network_change(void *arg, bool samenetwork)
4250{
4251 /* Check if the client should translate the network change to a SIGUSR1 to
4252 * reestablish the connection or just reprotect the socket
4253 *
4254 * At the moment just assume that, for all settings that use pull (not
4255 * --static) and are not using peer-id reestablishing the connection is
4256 * required (unless the network is the same)
4257 *
4258 * The function returns -1 on invalid fd and -2 if the socket cannot be
4259 * reused. On the -2 return value the man_network_change function triggers
4260 * a SIGUSR1 to force a reconnect.
4261 */
4262
4263 int socketfd = -1;
4264 struct context *c = (struct context *)arg;
4265 if (!c->c2.link_sockets || !c->c2.link_sockets[0])
4266 {
4267 return -1;
4268 }
4269 if (c->c2.link_sockets[0]->sd == SOCKET_UNDEFINED)
4270 {
4271 return -1;
4272 }
4273
4274 /* On some newer Android handsets, changing to a different network
4275 * often does not trigger a TCP reset but continue using the old
4276 * connection (e.g. using mobile connection when WiFi becomes available */
4277 const struct link_socket_info *lsi = get_link_socket_info(c);
4278 if (lsi && proto_is_tcp(lsi->proto) && !samenetwork)
4279 {
4280 return -2;
4281 }
4282
4283 socketfd = c->c2.link_sockets[0]->sd;
4284 if (!c->options.pull || c->c2.tls_multi->use_peer_id || samenetwork)
4285 {
4286 return socketfd;
4287 }
4288 else
4289 {
4290 return -2;
4291 }
4292}
4293#endif /* ifdef TARGET_ANDROID */
4294
4295#endif /* ifdef ENABLE_MANAGEMENT */
4296
4297void
4299{
4300#ifdef ENABLE_MANAGEMENT
4301 if (management)
4302 {
4303 struct management_callback cb;
4304 CLEAR(cb);
4305 cb.arg = c;
4311#ifdef TARGET_ANDROID
4312 cb.network_change = management_callback_network_change;
4313#endif
4317 }
4318#endif
4319}
4320
4321#ifdef ENABLE_MANAGEMENT
4322
4323void
4325{
4326 if (!management)
4327 {
4329 }
4330}
4331
4332bool
4334{
4335 /* initialize management layer */
4336 if (management)
4337 {
4338 if (c->options.management_addr)
4339 {
4340 unsigned int flags = c->options.management_flags;
4341 if (c->options.mode == MODE_SERVER)
4342 {
4343 flags |= MF_SERVER;
4344 }
4345 if (management_open(
4351 {
4353 NULL);
4354 }
4355
4356 /* initial management hold, called early, before first context initialization */
4357 do_hold(0);
4358 if (IS_SIG(c))
4359 {
4360 msg(M_WARN, "Signal received from management interface, exiting");
4361 return false;
4362 }
4363 }
4364 else
4365 {
4367 }
4368 }
4369 return true;
4370}
4371
4372void
4374{
4375 if (management)
4376 {
4378 management = NULL;
4379 }
4380}
4381
4382#endif /* ifdef ENABLE_MANAGEMENT */
4383
4384
4385void
4387{
4388#ifdef ENABLE_MANAGEMENT
4389 if (management)
4390 {
4392 }
4393#endif
4394}
4395
4396void
4398{
4399#ifdef ENABLE_MANAGEMENT
4400 if (management)
4401 {
4403 }
4404#endif
4405}
4406
4407/*
4408 * Initialize a tunnel instance.
4409 */
4410static void
4411init_instance(struct context *c, const struct env_set *env, const unsigned int flags)
4412{
4413 const struct options *options = &c->options;
4414 const bool child = (c->mode == CM_CHILD_TCP || c->mode == CM_CHILD_UDP);
4415
4416 /* init garbage collection level */
4417 gc_init(&c->c2.gc);
4418
4419 /* inherit environmental variables */
4420 if (env)
4421 {
4422 do_inherit_env(c, env);
4423 }
4424
4425 if (c->mode == CM_P2P)
4426 {
4428 }
4429
4430 /* possible sleep or management hold if restart */
4431 if (c->mode == CM_P2P || c->mode == CM_TOP)
4432 {
4434 if (IS_SIG(c))
4435 {
4436 goto sig;
4437 }
4438 }
4439
4441 {
4442 do_preresolve(c);
4443 if (IS_SIG(c))
4444 {
4445 goto sig;
4446 }
4447 }
4448
4449 /* Resets all values to the initial values from the config where needed */
4450 pre_connect_restore(&c->options, &c->c2.gc);
4451
4452 /* map in current connection entry */
4454
4455 /* should we disable paging? */
4456 if (c->first_time && options->mlock)
4457 {
4458 platform_mlockall(true);
4459 }
4460
4461 /* get passwords if undefined */
4462 if (auth_retry_get() == AR_INTERACT)
4463 {
4465 }
4466
4467 /* initialize context level 2 --verb/--mute parms */
4469
4470 /* set error message delay for non-server modes */
4471 if (c->mode == CM_P2P)
4472 {
4474 }
4475
4476 /* warn about inconsistent options */
4477 if (c->mode == CM_P2P || c->mode == CM_TOP)
4478 {
4480 }
4481
4482#ifdef ENABLE_PLUGIN
4483 /* initialize plugins */
4484 if (c->mode == CM_P2P || c->mode == CM_TOP)
4485 {
4486 open_plugins(c, false, OPENVPN_PLUGIN_INIT_PRE_DAEMON);
4487 }
4488#endif
4489
4490 /* should we throw a signal on TLS errors? */
4492
4493 /* open --status file */
4494 if (c->mode == CM_P2P || c->mode == CM_TOP)
4495 {
4497 }
4498
4499 /* open --ifconfig-pool-persist file */
4500 if (c->mode == CM_TOP)
4501 {
4503 }
4504
4505 /* reset OCC state */
4506 if (c->mode == CM_P2P || child)
4507 {
4508 c->c2.occ_op = occ_reset_op();
4509 }
4510
4511 /* our wait-for-i/o objects, different for posix vs. win32 */
4512 if (c->mode == CM_P2P || c->mode == CM_TOP)
4513 {
4515 }
4516 else if (c->mode == CM_CHILD_TCP)
4517 {
4518 do_event_set_init(c, false);
4519 }
4520
4521 /* initialize HTTP or SOCKS proxy object at scope level 2 */
4522 init_proxy(c);
4523
4524 /* allocate our socket object */
4525 if (c->mode == CM_P2P || c->mode == CM_TOP || c->mode == CM_CHILD_TCP)
4526 {
4528 }
4529
4530#ifdef ENABLE_FRAGMENT
4531 /* initialize internal fragmentation object */
4532 if (options->ce.fragment && (c->mode == CM_P2P || child))
4533 {
4534 c->c2.fragment = fragment_init(&c->c2.frame);
4535 }
4536#endif
4537
4538 /* init crypto layer */
4539 {
4540 unsigned int crypto_flags = 0;
4541 if (c->mode == CM_TOP)
4542 {
4543 crypto_flags = CF_INIT_TLS_AUTH_STANDALONE;
4544 }
4545 else if (c->mode == CM_P2P)
4546 {
4548 }
4549 else if (child)
4550 {
4551 crypto_flags = CF_INIT_TLS_MULTI;
4552 }
4553 do_init_crypto(c, crypto_flags);
4554 if (IS_SIG(c) && !child)
4555 {
4556 goto sig;
4557 }
4558 }
4559
4560#ifdef USE_COMP
4561 /* initialize compression library. */
4562 if (comp_enabled(&options->comp) && (c->mode == CM_P2P || child))
4563 {
4564 c->c2.comp_context = comp_init(&options->comp);
4565 }
4566#endif
4567
4568 /* initialize MTU variables */
4569 do_init_frame(c);
4570
4571 /* initialize TLS MTU variables */
4573
4574 /* init workspace buffers whose size is derived from frame size */
4575 if (c->mode == CM_P2P || c->mode == CM_CHILD_TCP)
4576 {
4577 do_init_buffers(c);
4578 }
4579
4580#ifdef ENABLE_FRAGMENT
4581 /* initialize internal fragmentation capability with known frame size */
4582 if (options->ce.fragment && (c->mode == CM_P2P || child))
4583 {
4585 }
4586#endif
4587
4588 /* bind the TCP/UDP socket */
4589 if (c->mode == CM_P2P || c->mode == CM_TOP || c->mode == CM_CHILD_TCP)
4590 {
4592 }
4593
4594 /* initialize tun/tap device object,
4595 * open tun/tap device, ifconfig, run up script, etc. */
4596 if (!(options->up_delay || PULL_DEFINED(options)) && (c->mode == CM_P2P || c->mode == CM_TOP))
4597 {
4598 int error_flags = 0;
4599 c->c2.did_open_tun = do_open_tun(c, &error_flags);
4600 }
4601
4602 /* print MTU info */
4604
4605 /* get local and remote options compatibility strings */
4606 if (c->mode == CM_P2P || child)
4607 {
4609 }
4610
4611 /* initialize output speed limiter */
4612 if (c->mode == CM_P2P)
4613 {
4615 }
4616
4617 /* do one-time inits, and possibly become a daemon here */
4619
4620#ifdef ENABLE_PLUGIN
4621 /* initialize plugins */
4622 if (c->mode == CM_P2P || c->mode == CM_TOP)
4623 {
4624 open_plugins(c, false, OPENVPN_PLUGIN_INIT_POST_DAEMON);
4625 }
4626#endif
4627
4628 /* initialise connect timeout timer */
4630
4631 /* finalize the TCP/UDP socket */
4632 if (c->mode == CM_P2P || c->mode == CM_TOP || c->mode == CM_CHILD_TCP)
4633 {
4635
4636
4637 /* Update dynamic frame calculation as exact transport socket information
4638 * (IP vs IPv6) may be only available after socket phase2 has finished.
4639 * This is only needed for --static or no crypto, NCP will recalculate this
4640 * in tls_session_update_crypto_params (P2MP) */
4641 for (int i = 0; i < c->c1.link_sockets_num; i++)
4642 {
4644 &c->c2.link_sockets[i]->info);
4645 }
4646 }
4647
4648 /*
4649 * Actually do UID/GID downgrade, and chroot, if requested.
4650 * May be delayed by --client, --pull, or --up-delay.
4651 */
4653
4654 /* initialize timers */
4655 if (c->mode == CM_P2P || child)
4656 {
4657 do_init_timers(c, false);
4658 }
4659
4660#ifdef ENABLE_PLUGIN
4661 /* initialize plugins */
4662 if (c->mode == CM_P2P || c->mode == CM_TOP)
4663 {
4664 open_plugins(c, false, OPENVPN_PLUGIN_INIT_POST_UID_CHANGE);
4665 }
4666#endif
4667
4668#if PORT_SHARE
4669 /* share OpenVPN port with foreign (such as HTTPS) server */
4670 if (c->first_time && (c->mode == CM_P2P || c->mode == CM_TOP))
4671 {
4672 init_port_share(c);
4673 }
4674#endif
4675
4676 /* Check for signals */
4677 if (IS_SIG(c))
4678 {
4679 goto sig;
4680 }
4681
4682 return;
4683
4684sig:
4685 if (!c->sig->signal_text)
4686 {
4687 c->sig->signal_text = "init_instance";
4688 }
4689 close_context(c, -1, flags);
4690 return;
4691}
4692
4693/*
4694 * Initialize a tunnel instance, handle pre and post-init
4695 * signal settings.
4696 */
4697void
4698init_instance_handle_signals(struct context *c, const struct env_set *env, const unsigned int flags)
4699{
4701 init_instance(c, env, flags);
4703
4704 /*
4705 * This is done so that signals thrown during
4706 * initialization can bring us back to
4707 * a management hold.
4708 */
4709 if (IS_SIG(c))
4710 {
4711 remap_signal(c);
4713 }
4714}
4715
4716/*
4717 * Close a tunnel instance.
4718 */
4719void
4721{
4722 /* close event objects */
4724
4725 if (c->mode == CM_P2P || c->mode == CM_CHILD_TCP || c->mode == CM_CHILD_UDP
4726 || c->mode == CM_TOP)
4727 {
4728#ifdef USE_COMP
4729 if (c->c2.comp_context)
4730 {
4731 comp_uninit(c->c2.comp_context);
4732 c->c2.comp_context = NULL;
4733 }
4734#endif
4735
4736 /* free buffers */
4738
4739 /* close peer for DCO if enabled, needs peer-id so must be done before
4740 * closing TLS contexts */
4741 dco_remove_peer(c);
4742
4743 /* close TLS */
4744 do_close_tls(c);
4745
4746 /* free key schedules */
4747 do_close_free_key_schedule(c, (c->mode == CM_P2P || c->mode == CM_TOP));
4748
4749 /* close TCP/UDP connection */
4751
4752 /* close TUN/TAP device */
4753 do_close_tun(c, false);
4754
4755#ifdef ENABLE_MANAGEMENT
4756 if (management)
4757 {
4759 }
4760#endif
4761
4762#ifdef ENABLE_PLUGIN
4763 /* call plugin close functions and unload */
4765#endif
4766
4767 /* close packet-id persistence file */
4769
4770 /* close --status file */
4772
4773#ifdef ENABLE_FRAGMENT
4774 /* close fragmentation handler */
4776#endif
4777
4778 /* close --ifconfig-pool-persist obj */
4780
4781 /* free up environmental variable store */
4783
4784 /* close HTTP or SOCKS proxy */
4785 uninit_proxy(c);
4786
4787 /* garbage collect */
4788 gc_free(&c->c2.gc);
4789 }
4790}
4791
4792void
4793inherit_context_child(struct context *dest, const struct context *src, struct link_socket *sock)
4794{
4795 CLEAR(*dest);
4796
4797 /* proto_is_dgram will ASSERT(0) if proto is invalid */
4799
4800 dest->gc = gc_new();
4801
4802 ALLOC_OBJ_CLEAR_GC(dest->sig, struct signal_info, &dest->gc);
4803
4804 /* c1 init */
4806 dest->c1.link_sockets_num = 1;
4808
4809 dest->c1.ks.key_type = src->c1.ks.key_type;
4810 /* inherit SSL context */
4811 dest->c1.ks.ssl_ctx = src->c1.ks.ssl_ctx;
4812 dest->c1.ks.tls_wrap_key = src->c1.ks.tls_wrap_key;
4815 /* inherit pre-NCP ciphers */
4816 dest->options.ciphername = src->options.ciphername;
4817 dest->options.authname = src->options.authname;
4818
4819 /* inherit auth-token */
4820 dest->c1.ks.auth_token_key = src->c1.ks.auth_token_key;
4821
4822 /* options */
4823 dest->options = src->options;
4824 dest->options.ce.proto = sock->info.proto;
4825 options_detach(&dest->options);
4826
4827 dest->c2.event_set = src->c2.event_set;
4828
4829 if (dest->mode == CM_CHILD_TCP)
4830 {
4831 /*
4832 * The CM_TOP context does the socket listen(),
4833 * and the CM_CHILD_TCP context does the accept().
4834 */
4835 dest->c2.accept_from = sock;
4836 }
4837
4838#ifdef ENABLE_PLUGIN
4839 /* inherit plugins */
4840 do_inherit_plugins(dest, src);
4841#endif
4842
4843 /* context init */
4844
4845 /* inherit tun/tap interface object now as it may be required
4846 * to initialize the DCO context in init_instance()
4847 */
4848 dest->c1.tuntap = src->c1.tuntap;
4849
4850 /* UDP inherits some extra things which TCP does not */
4851 if (dest->mode == CM_CHILD_UDP)
4852 {
4853 ASSERT(!dest->c2.link_sockets);
4854 ASSERT(dest->options.ce.local_list);
4855
4856 /* inherit buffers */
4857 dest->c2.buffers = src->c2.buffers;
4858
4859 ALLOC_ARRAY_GC(dest->c2.link_sockets, struct link_socket *, 1, &dest->gc);
4860
4861 /* inherit parent link_socket and tuntap */
4862 dest->c2.link_sockets[0] = sock;
4863
4864 ALLOC_ARRAY_GC(dest->c2.link_socket_infos, struct link_socket_info *, 1, &dest->gc);
4865 ALLOC_OBJ_GC(dest->c2.link_socket_infos[0], struct link_socket_info, &dest->gc);
4866 *dest->c2.link_socket_infos[0] = sock->info;
4867
4868 /* locally override some link_socket_info fields */
4869 dest->c2.link_socket_infos[0]->lsa = &dest->c1.link_socket_addrs[0];
4871 }
4872
4874 if (IS_SIG(dest))
4875 {
4876 return;
4877 }
4878}
4879
4880void
4881inherit_context_top(struct context *dest, const struct context *src)
4882{
4883 /* copy parent */
4884 *dest = *src;
4885
4886 /*
4887 * CM_TOP_CLONE will prevent close_instance from freeing or closing
4888 * resources owned by the parent.
4889 *
4890 * Also note that CM_TOP_CLONE context objects are
4891 * closed by multi_top_free in multi.c.
4892 */
4893 dest->mode = CM_TOP_CLONE;
4894
4895 dest->first_time = false;
4896 dest->c0 = NULL;
4897
4898 options_detach(&dest->options);
4899 gc_detach(&dest->gc);
4900 gc_detach(&dest->c2.gc);
4901
4902 /* detach plugins */
4903 dest->plugins_owned = false;
4904
4905 dest->c2.tls_multi = NULL;
4906
4907 /* detach c1 ownership */
4908 dest->c1.tuntap_owned = false;
4909 dest->c1.status_output_owned = false;
4910 dest->c1.ifconfig_pool_persist_owned = false;
4911
4912 /* detach c2 ownership */
4913 dest->c2.event_set_owned = false;
4914 dest->c2.link_socket_owned = false;
4915 dest->c2.buffers_owned = false;
4916 dest->c2.es_owned = false;
4917
4918 dest->c2.event_set = NULL;
4919 do_event_set_init(dest, false);
4920
4921#ifdef USE_COMP
4922 dest->c2.comp_context = NULL;
4923#endif
4924}
4925
4926void
4927close_context(struct context *c, int sig, unsigned int flags)
4928{
4929 ASSERT(c);
4930 ASSERT(c->sig);
4931
4932 if (sig >= 0)
4933 {
4934 register_signal(c->sig, sig, "close_context");
4935 }
4936
4937 if (c->sig->signal_received == SIGUSR1)
4938 {
4939 if ((flags & CC_USR1_TO_HUP)
4940 || (c->sig->source == SIG_SOURCE_HARD && (flags & CC_HARD_USR1_TO_HUP)))
4941 {
4942 register_signal(c->sig, SIGHUP, "close_context usr1 to hup");
4943 }
4944 }
4945
4946 if (!(flags & CC_NO_CLOSE))
4947 {
4948 close_instance(c);
4949 }
4950
4951 if (flags & CC_GC_FREE)
4952 {
4953 context_gc_free(c);
4954 }
4955}
4956
4957/* Write our PID to a file */
4958void
4959write_pid_file(const char *filename, const char *chroot_dir)
4960{
4961 if (filename)
4962 {
4963 unsigned int pid = 0;
4964 FILE *fp = platform_fopen(filename, "w");
4965 if (!fp)
4966 {
4967 msg(M_ERR, "Open error on pid file %s", filename);
4968 return;
4969 }
4970
4971 pid = platform_getpid();
4972 fprintf(fp, "%u\n", pid);
4973 if (fclose(fp))
4974 {
4975 msg(M_ERR, "Close error on pid file %s", filename);
4976 }
4977
4978 /* remember file name so it can be deleted "out of context" later */
4979 /* (the chroot case is more complex and not handled today) */
4980 if (!chroot_dir)
4981 {
4982 saved_pid_file_name = strdup(filename);
4984 {
4985 msg(M_FATAL, "Failed allocate memory saved_pid_file_name");
4986 }
4987 }
4988 }
4989}
4990
4991/* remove PID file on exit, called from openvpn_exit() */
4992void
4994{
4996 {
4998 }
4999}
5000
5001
5002/*
5003 * Do a loopback test
5004 * on the crypto subsystem.
5005 */
5006void
5008{
5009 /* print version number */
5010 msg(M_INFO, "%s", title_string);
5011 const struct options *options = &c->options;
5012
5015 context_init_1(c);
5018
5020
5022
5023 key_schedule_free(&c->c1.ks, true);
5025
5026 context_gc_free(c);
5027}
void argv_msg(const msglvl_t msglevel, const struct argv *a)
Write the arguments stored in a struct argv via the msg() command.
Definition argv.c:242
void argv_parse_cmd(struct argv *argres, const char *cmdstr)
Parses a command string, tokenizes it and puts each element into a separate struct argv argument slot...
Definition argv.c:481
void argv_free(struct argv *a)
Frees all memory allocations allocated by the struct argv related functions.
Definition argv.c:101
bool argv_printf(struct argv *argres, const char *format,...)
printf() variant which populates a struct argv.
Definition argv.c:438
bool argv_printf_cat(struct argv *argres, const char *format,...)
printf() inspired argv concatenation.
Definition argv.c:462
struct argv argv_new(void)
Allocates a new struct argv and ensures it is initialised.
Definition argv.c:87
void auth_token_write_server_key_file(const char *filename)
Generate a auth-token server secret key, and write to file.
Definition auth_token.c:136
void auth_token_init_secret(struct key_ctx *key_ctx, const char *key_file, bool key_inline)
Loads an HMAC secret from a file or if no file is present generates a epheremal secret for the run ti...
Definition auth_token.c:142
void free_buf(struct buffer *buf)
Free the memory allocated for a buffer.
Definition buffer.c:169
void buf_clear(struct buffer *buf)
Zeroise and reset a buffer.
Definition buffer.c:148
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
Definition buffer.c:226
void string_clear(char *str)
Securely clear a null-terminated string.
Definition buffer.c:649
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
struct buffer alloc_buf(size_t size)
Allocate a buffer of the given size.
Definition buffer.c:60
char * string_alloc(const char *str, struct gc_arena *gc)
Duplicate a string, allocating memory under garbage collection.
Definition buffer.c:616
static void gc_detach(struct gc_arena *a)
Detach all allocations from an arena without freeing them.
Definition buffer.h:1885
#define BSTR(buf)
Return the buffer content pointer cast to char *.
Definition buffer.h:151
#define ALLOC_ARRAY_GC(dptr, type, n, gc)
Allocate a garbage-collected array of n elements of the given type.
Definition buffer.h:2003
#define ALLOC_ARRAY_CLEAR_GC(dptr, type, n, gc)
Allocate and zero-initialise a garbage-collected array of n elements.
Definition buffer.h:2029
static void gc_init(struct gc_arena *a)
Initialise a garbage collection arena to an empty state.
Definition buffer.h:1870
static void buf_set_write(struct buffer *buf, uint8_t *data, int size)
Initialise a buffer with an externally provided writable memory region.
Definition buffer.h:594
static int buf_len(const struct buffer *buf)
Return the length of the buffer content.
Definition buffer.h:438
#define ALLOC_OBJ_CLEAR_GC(dptr, type, gc)
Allocate and zero-initialise a garbage-collected object of the given type.
Definition buffer.h:2070
#define ALLOC_OBJ_GC(dptr, type, gc)
Allocate a garbage-collected object of the given type (uninitialised).
Definition buffer.h:2058
#define BLENZ(buf)
Return the length of the buffer content as a size_t.
Definition buffer.h:147
static void strncpynt(char *dest, const char *src, size_t maxlen)
Like strncpy() but always null-terminates the destination.
Definition buffer.h:646
static void check_malloc_return(void *p)
Abort if a memory allocation returned NULL.
Definition buffer.h:2082
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1912
#define ALLOC_OBJ_CLEAR(dptr, type)
Allocate and zero-initialise memory for a single object of the given type.
Definition buffer.h:1974
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Definition buffer.h:1896
#define PUSH_BUNDLE_SIZE
Definition common.h:89
bool check_compression_settings_valid(struct compress_options *info, msglvl_t msglevel)
Checks if the compression settings are valid.
Definition comp.c:162
char * basename(char *filename)
int daemon(int nochdir, int noclose)
void free_key_ctx_bi(struct key_ctx_bi *ctx)
Definition crypto.c:1100
void init_key_type(struct key_type *kt, const char *ciphername, const char *authname, bool tls_mode, bool warn)
Initialize a key_type structure with.
Definition crypto.c:875
int write_key_file(const int nkeys, const char *filename)
Write nkeys 1024-bits keys to file.
Definition crypto.c:1556
unsigned int crypto_max_overhead(void)
Return the worst-case OpenVPN crypto overhead (in bytes)
Definition crypto.c:850
int64_t get_random(void)
an analogue to the random() function, but use prng_bytes and also int64_t instead of long to avoid LL...
Definition crypto.c:1737
void test_crypto(struct crypto_options *co, struct frame *frame)
Definition crypto.c:1199
void generate_test_crypto_random_key(const struct key_type *key_type, struct key_ctx_bi *ctx, const char *key_name)
Generate a random key and initialise ctx to be used the in the crypto random test.
Definition crypto.c:1329
void crypto_read_openvpn_key(const struct key_type *key_type, struct key_ctx_bi *ctx, const char *key_file, bool key_inline, const int key_direction, const char *key_name, const char *opt_name, struct key2 *keydata)
Definition crypto.c:1290
void free_key_ctx(struct key_ctx *ctx)
Definition crypto.c:1081
#define CO_PACKET_ID_LONG_FORM
Bit-flag indicating whether to use OpenVPN's long packet ID format.
Definition crypto.h:347
#define CO_USE_TLS_KEY_MATERIAL_EXPORT
Bit-flag indicating that data channel key derivation is done using TLS keying material export [RFC570...
Definition crypto.h:359
#define CO_USE_DYNAMIC_TLS_CRYPT
Bit-flag indicating that renegotiations are using tls-crypt with a TLS-EKM derived key.
Definition crypto.h:375
#define CO_EPOCH_DATA_KEY_FORMAT
Bit-flag indicating the epoch the data format.
Definition crypto.h:379
#define CO_MUTE_REPLAY_WARNINGS
Bit-flag indicating not to display replay warnings.
Definition crypto.h:356
#define CO_FORCE_TLSCRYPTV2_COOKIE
Bit-flag indicating that we do not allow clients that do not support resending the wrapped client key...
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:371
static bool key_ctx_bi_defined(const struct key_ctx_bi *key)
Definition crypto.h:658
void show_available_engines(void)
bool cipher_kt_mode_aead(const char *ciphername)
Check if the supplied cipher is a supported AEAD mode cipher.
void show_available_ciphers(void)
bool md_valid(const char *digest)
Return if a message digest parameters is valid given the name of the digest.
bool cipher_kt_mode_ofb_cfb(const char *ciphername)
Check if the supplied cipher is a supported OFB or CFB mode cipher.
const char * md_kt_name(const char *mdname)
Retrieve a string describing the digest digest (e.g.
const char * cipher_kt_name(const char *ciphername)
Retrieve a normalised string describing the cipher (e.g.
void crypto_init_lib_engine(const char *engine_name)
void md_ctx_cleanup(md_ctx_t *ctx)
void show_available_digests(void)
void md_ctx_free(md_ctx_t *ctx)
#define DCO_DEFAULT_METRIC
Definition dco.h:47
static bool dco_supports_epoch_data(struct context *c)
Definition dco.h:387
static void dco_remove_peer(struct context *c)
Definition dco.h:348
static bool dco_check_pull_options(msglvl_t msglevel, const struct options *o)
Definition dco.h:286
static bool ovpn_dco_init(struct context *c)
Definition dco.h:292
static int dco_p2p_add_new_peer(struct context *c)
Definition dco.h:335
void run_dns_up_down(bool up, struct options *o, const struct tuntap *tt, struct dns_updown_runner_info *duri)
Invokes the action associated with bringing DNS up or down.
Definition dns.c:858
void env_set_destroy(struct env_set *es)
Definition env_set.c:166
void setenv_int(struct env_set *es, const char *name, int value)
Definition env_set.c:291
void setenv_str(struct env_set *es, const char *name, const char *value)
Definition env_set.c:307
void env_set_inherit(struct env_set *es, const struct env_set *src)
Definition env_set.c:262
struct env_set * env_set_create(struct gc_arena *gc)
Definition env_set.c:156
#define D_MTU_DEBUG
Definition errlevel.h:125
#define D_SHOW_OCC
Definition errlevel.h:150
#define D_PUSH
Definition errlevel.h:82
#define D_SHOW_NET
Definition errlevel.h:131
#define P2P_ERROR_DELAY_MS
Definition errlevel.h:40
#define D_RESTART
Definition errlevel.h:81
#define D_IMPORT_ERRORS
Definition errlevel.h:63
#define D_CLOSE
Definition errlevel.h:72
#define D_PUSH_DEBUG
Definition errlevel.h:149
#define D_DCO
Definition errlevel.h:93
#define D_HANDSHAKE
Definition errlevel.h:71
#define D_GENKEY
Definition errlevel.h:78
#define D_MTU_INFO
Definition errlevel.h:104
#define D_PUSH_ERRORS
Definition errlevel.h:66
#define D_INIT_MEDIUM
Definition errlevel.h:103
#define D_TLS_ERRORS
Definition errlevel.h:58
#define D_READ_WRITE
Definition errlevel.h:166
#define M_INFO
Definition errlevel.h:54
#define D_LOG_RW
Definition errlevel.h:109
#define D_ROUTE
Definition errlevel.h:79
#define D_LINK_ERRORS
Definition errlevel.h:56
struct event_set * event_set_init(int *maxevents, unsigned int flags)
Definition event.c:1187
#define EVENT_METHOD_FAST
Definition event.h:79
#define EVENT_METHOD_US_TIMEOUT
Definition event.h:78
static void event_free(struct event_set *es)
Definition event.h:159
bool send_control_channel_string(struct context *c, const char *str, msglvl_t msglevel)
Definition forward.c:398
Interface functions to the internal and external multiplexers.
static struct link_socket_info * get_link_socket_info(struct context *c)
Definition forward.h:352
struct tls_auth_standalone * tls_auth_standalone_init(struct tls_options *tls_options, struct gc_arena *gc)
Definition ssl.c:1215
void tls_init_control_channel_frame_parameters(struct frame *frame, int tls_mtu)
Definition ssl.c:150
void tls_multi_free(struct tls_multi *multi, bool clear)
Cleanup a tls_multi structure and free associated memory allocations.
Definition ssl.c:1267
struct tls_multi * tls_multi_init(struct tls_options *tls_options)
Allocate and initialize a tls_multi structure.
Definition ssl.c:1174
void tls_multi_init_finalize(struct tls_multi *multi, int tls_mtu)
Finalize initialization of a tls_multi structure.
Definition ssl.c:1192
void tls_auth_standalone_free(struct tls_auth_standalone *tas)
Frees a standalone tls-auth verification object.
Definition ssl.c:1240
#define TM_ACTIVE
Active tls_session.
Definition ssl_common.h:544
void tls_multi_init_set_options(struct tls_multi *multi, const char *local, const char *remote)
Definition ssl.c:1256
void fragment_frame_init(struct fragment_master *f, const struct frame *frame)
Allocate internal packet buffers for a fragment_master structure.
Definition fragment.c:125
struct fragment_master * fragment_init(struct frame *frame)
Allocate and initialize a fragment_master structure.
Definition fragment.c:92
void fragment_free(struct fragment_master *f)
Free a fragment_master structure and its internal packet buffers.
Definition fragment.c:116
void tls_crypt_init_key(struct key_ctx_bi *key, struct key2 *keydata, const char *key_file, bool key_inline, bool tls_server)
Initialize a key_ctx_bi structure for use with --tls-crypt.
Definition tls_crypt.c:62
void tls_crypt_v2_init_server_key(struct key_ctx *key_ctx, bool encrypt, const char *key_file, bool key_inline)
Initialize a tls-crypt-v2 server key (used to encrypt/decrypt client keys).
Definition tls_crypt.c:339
void tls_crypt_v2_write_client_key_file(const char *filename, const char *b64_metadata, const char *server_key_file, bool server_key_inline)
Generate a tls-crypt-v2 client key, and write to file.
Definition tls_crypt.c:704
void tls_crypt_v2_write_server_key_file(const char *filename)
Generate a tls-crypt-v2 server key, and write to file.
Definition tls_crypt.c:698
int tls_crypt_buf_overhead(void)
Returns the maximum overhead (in bytes) added to the destination buffer by tls_crypt_wrap().
Definition tls_crypt.c:56
void tls_crypt_v2_init_client_key(struct key_ctx_bi *key, struct key2 *original_key, struct buffer *wkc_buf, const char *key_file, bool key_inline)
Initialize a tls-crypt-v2 client key.
Definition tls_crypt.c:316
void uninit_management_callback(void)
Definition init.c:4386
static void uninit_proxy(struct context *c)
Definition init.c:722
bool open_management(struct context *c)
Definition init.c:4333
static void do_init_first_time(struct context *c)
Definition init.c:3847
static void do_init_route_list(const struct options *options, struct route_list *route_list, const struct link_socket_info *link_socket_info, struct env_set *es, openvpn_net_ctx_t *ctx)
Definition init.c:1423
static void do_init_tls_wrap_key(struct context *c)
Definition init.c:3059
static bool management_callback_remote_cmd(void *arg, const char **p)
Definition init.c:365
bool do_genkey(const struct options *options)
Definition init.c:990
static void next_connection_entry(struct context *c)
Definition init.c:510
static bool can_preserve_tun(struct tuntap *tt)
Definition init.c:1734
static void do_init_crypto_test(struct context *c)
Definition init.c:2985
void initialization_sequence_completed(struct context *c, const unsigned int flags)
Definition init.c:1514
void open_plugins(struct context *c, const bool import_options, int init_point)
Definition init.c:4170
bool do_update(struct context *c, uint64_t option_types_found)
A simplified version of the do_up() function.
Definition init.c:2459
static void tls_print_deferred_options_results(struct context *c)
Prints the results of options imported for the data channel.
Definition init.c:2167
static const char * saved_pid_file_name
Definition init.c:63
void init_verb_mute(struct context *c, unsigned int flags)
Definition init.c:918
static void do_uid_gid_chroot(struct context *c, bool no_delay)
Definition init.c:1172
const char * format_common_name(struct context *c, struct gc_arena *gc)
Definition init.c:1248
static void do_close_link_socket(struct context *c)
Definition init.c:3944
static unsigned int management_callback_remote_entry_count(void *arg)
Definition init.c:321
static void do_signal_on_tls_errors(struct context *c)
Definition init.c:4145
static void do_init_crypto_static(struct context *c, const unsigned int flags)
Definition init.c:3015
static void key_schedule_free(struct key_schedule *ks, bool free_ssl_ctx)
Definition init.c:2953
static void do_init_tun(struct context *c)
Definition init.c:1689
static void do_option_warnings(struct context *c)
Definition init.c:3563
static void do_link_socket_addr_new(struct context *c)
Definition init.c:728
void close_instance(struct context *c)
Definition init.c:4720
static void do_init_route_ipv6_list(const struct options *options, struct route_ipv6_list *route_ipv6_list, const struct link_socket_info *link_socket_info, struct env_set *es, openvpn_net_ctx_t *ctx)
Definition init.c:1462
static void do_init_frame(struct context *c)
Definition init.c:3517
void persist_client_stats(struct context *c)
Definition init.c:4397
void inherit_context_top(struct context *dest, const struct context *src)
Definition init.c:4881
static bool ce_management_query_proxy(struct context *c)
Definition init.c:248
static void do_init_crypto(struct context *c, const unsigned int flags)
Definition init.c:3500
static void do_init_frame_tls(struct context *c)
Definition init.c:3459
static void do_init_traffic_shaper(struct context *c)
Definition init.c:1389
static bool route_noexec_enabled(const struct options *o, const struct tuntap *tt)
Determine if external route commands should be executed based on configured options and backend drive...
Definition init.c:1624
static bool ifconfig_noexec_enabled(const struct context *c)
Determines if ifconfig execution should be disabled because of a.
Definition init.c:1801
static void clear_remote_addrlist(struct link_socket_addr *lsa, bool free)
Definition init.c:496
static void do_print_data_channel_mtu_parms(struct context *c)
Definition init.c:3803
void init_plugins(struct context *c)
Definition init.c:4160
void free_context_buffers(struct context_buffers *b)
Definition init.c:3684
static void init_crypto_pre(struct context *c, const unsigned int flags)
Definition init.c:2966
static void do_init_timers(struct context *c, bool deferred)
Definition init.c:1306
static void context_clear(struct context *c)
Definition init.c:77
static void context_clear_1(struct context *c)
Definition init.c:83
#define CF_LOAD_PERSISTED_PACKET_ID
Definition init.c:68
static void do_alloc_route_list(struct context *c)
Definition init.c:1405
static void do_close_status_output(struct context *c)
Definition init.c:4079
static void do_event_set_init(struct context *c, bool need_us_timeout)
Definition init.c:4034
static struct context * static_context
Definition init.c:62
static void do_init_fragment(struct context *c)
Definition init.c:3721
#define CF_INIT_TLS_MULTI
Definition init.c:69
void context_init_1(struct context *c)
Definition init.c:735
void do_test_crypto(struct context *c)
Definition init.c:5007
void pre_setup(const struct options *options)
Definition init.c:1259
static void do_link_socket_new(struct context *c)
Definition init.c:3742
static void do_close_free_key_schedule(struct context *c, bool free_ssl_ctx)
Definition init.c:3922
static bool management_callback_proxy_cmd(void *arg, const char **p)
Definition init.c:202
static void del_wfp_block(struct context *c, unsigned long adapter_index)
Remove any WFP block filters previously added.
Definition init.c:1782
void reset_coarse_timers(struct context *c)
Definition init.c:1282
static void do_open_ifconfig_pool_persist(struct context *c)
Definition init.c:4096
static void do_close_ifconfig_pool_persist(struct context *c)
Definition init.c:4108
static void do_close_tun(struct context *c, bool force)
Definition init.c:2017
void init_management(void)
Definition init.c:4324
void uninit_static(void)
Definition init.c:900
void init_instance_handle_signals(struct context *c, const struct env_set *env, const unsigned int flags)
Definition init.c:4698
static void do_init_crypto_tls(struct context *c, const unsigned int flags)
Definition init.c:3227
void write_pid_file(const char *filename, const char *chroot_dir)
Definition init.c:4959
void context_gc_free(struct context *c)
Definition init.c:786
static void socket_restart_pause(struct context *c)
Definition init.c:2747
static void do_open_status_output(struct context *c)
Definition init.c:4067
void init_options_dev(struct options *options)
Definition init.c:941
static void do_close_free_buf(struct context *c)
Definition init.c:3881
void init_query_passwords(const struct context *c)
Query for private key and auth-user-pass username/passwords.
Definition init.c:640
void inherit_context_child(struct context *dest, const struct context *src, struct link_socket *sock)
Definition init.c:4793
static void do_compute_occ_strings(struct context *c)
Definition init.c:3818
void context_clear_2(struct context *c)
Definition init.c:89
static void do_close_fragment(struct context *c)
Definition init.c:4019
static void update_options_ce_post(struct options *options)
Definition init.c:184
static void do_init_crypto_none(struct context *c)
Definition init.c:3485
static void management_callback_status_p2p(void *arg, const int version, struct status_output *so)
Definition init.c:4229
uint64_t pull_permission_mask(const struct context *c)
Definition init.c:2512
void remove_pid_file(void)
Definition init.c:4993
static void add_delim_if_non_empty(struct buffer *buf, const char *header)
Helper function for tls_print_deferred_options_results Adds the ", " delimitor if there already some ...
Definition init.c:2153
bool print_openssl_info(const struct options *options)
Definition init.c:952
void close_context(struct context *c, int sig, unsigned int flags)
Definition init.c:4927
static bool ce_management_query_remote(struct context *c)
Definition init.c:415
static void open_tun_backend(struct context *c)
Definition init.c:1809
static void do_close_event_set(struct context *c)
Definition init.c:4052
bool do_persist_tuntap(struct options *options, openvpn_net_ctx_t *ctx)
Definition init.c:1071
static bool do_hold(int holdtime)
Definition init.c:2728
void tun_abort(void)
Definition init.c:2121
static void do_init_server_poll_timeout(struct context *c)
Definition init.c:1293
static void do_close_plugins(struct context *c)
Definition init.c:4204
static void do_init_socket_phase1(struct context *c)
Definition init.c:3759
bool do_deferred_options(struct context *c, const uint64_t found, const bool is_update)
Definition init.c:2573
static int get_frame_mtu(struct context *c, const struct options *o)
Definition init.c:2837
static void init_instance(struct context *c, const struct env_set *env, const unsigned int flags)
Definition init.c:4411
bool init_static(void)
Definition init.c:824
static void do_init_crypto_tls_c1(struct context *c)
Definition init.c:3131
static void frame_finalize_options(struct context *c, const struct options *o)
Definition init.c:2872
static void uninit_proxy_dowork(struct context *c)
Definition init.c:667
static bool options_hash_changed_or_zero(const struct sha256_digest *a, const struct sha256_digest *b)
Helper for do_up().
Definition init.c:2140
static void add_wfp_block(struct context *c)
Add WFP filters to block traffic to local networks.
Definition init.c:1756
static void do_close_tun_simple(struct context *c)
Definition init.c:1987
static void do_inherit_plugins(struct context *c, const struct context *src)
Definition init.c:4215
static void run_up_down(const char *command, const struct plugin_list *plugins, int plugin_type, const char *arg, DWORD adapter_index, const char *dev_type, int tun_mtu, const char *ifconfig_local, const char *ifconfig_remote, const char *context, const char *signal_text, const char *script_type, struct env_set *es)
Definition init.c:109
static void do_init_socket_phase2(struct context *c)
Definition init.c:3791
bool possibly_become_daemon(const struct options *options)
Definition init.c:1130
static bool management_callback_remote_entry_get(void *arg, unsigned int index, char **remote)
Definition init.c:331
static void init_connection_list(struct context *c)
Initialize and possibly randomize the connection list.
Definition init.c:471
static void init_proxy_dowork(struct context *c)
Definition init.c:684
static void do_close_packet_id(struct context *c)
Definition init.c:4004
static void do_startup_pause(struct context *c)
Definition init.c:2824
static bool do_deferred_p2p_ncp(struct context *c)
Definition init.c:2528
#define CF_INIT_TLS_AUTH_STANDALONE
Definition init.c:70
static bool do_deferred_options_part2(struct context *c)
This function is expected to be invoked after open_tun() was performed.
Definition init.c:2296
bool do_up(struct context *c, bool pulled_options, uint64_t option_types_found)
Definition init.c:2324
static void do_env_set_destroy(struct context *c)
Definition init.c:4134
static void do_init_buffers(struct context *c)
Definition init.c:3709
static void init_proxy(struct context *c)
Definition init.c:716
void init_management_callback_p2p(struct context *c)
Definition init.c:4298
static bool management_callback_send_cc_message(void *arg, const char *command, const char *parameters)
This method sends a custom control channel message.
Definition init.c:298
bool do_route(const struct options *options, struct route_list *route_list, struct route_ipv6_list *route_ipv6_list, const struct tuntap *tt, const struct plugin_list *plugins, struct env_set *es, openvpn_net_ctx_t *ctx)
Definition init.c:1635
static void do_inherit_env(struct context *c, const struct env_set *src)
Definition init.c:4126
void context_clear_all_except_first_time(struct context *c)
Definition init.c:95
struct context_buffers * init_context_buffers(const struct frame *frame)
Definition init.c:3659
static bool do_open_tun(struct context *c, int *error_flags)
Definition init.c:1831
static void do_close_tls(struct context *c)
Definition init.c:3895
void close_management(void)
Definition init.c:4373
void management_show_net_callback(void *arg, const msglvl_t msglevel)
Definition init.c:4236
#define ISC_ERRORS
Definition init.h:120
#define CC_GC_FREE
Definition init.h:109
#define IVM_LEVEL_2
Definition init.h:45
#define ISC_ROUTE_ERRORS
Definition init.h:122
#define IVM_LEVEL_1
Definition init.h:44
#define CC_USR1_TO_HUP
Definition init.h:110
#define BASE_N_EVENTS
Definition init.h:32
#define CC_HARD_USR1_TO_HUP
Definition init.h:111
#define ISC_SERVER
Definition init.h:121
#define CC_NO_CLOSE
Definition init.h:112
static int min_int(int x, int y)
Definition integer.h:105
static int max_int(int x, int y)
Definition integer.h:92
static unsigned int max_uint(unsigned int x, unsigned int y)
Definition integer.h:53
static SERVICE_STATUS status
Definition interactive.c:52
void interval_init(struct interval *top, int horizon, int refresh)
Definition interval.c:34
static void event_timeout_init(struct event_timeout *et, interval_t n, const time_t last)
Initialises a timer struct.
Definition interval.h:172
static void event_timeout_clear(struct event_timeout *et)
Clears the timeout and reset all values to 0.
Definition interval.h:153
int set_lladdr(openvpn_net_ctx_t *ctx, const char *ifname, const char *lladdr, const struct env_set *es)
Definition lladdr.c:51
void management_pre_tunnel_close(struct management *man)
Definition manage.c:3201
void management_notify_client_close(struct management *management, struct man_def_auth_context *mdac, const struct env_set *es)
Definition manage.c:3119
void management_clear_callback(struct management *man)
Definition manage.c:2873
bool management_hold(struct management *man, int holdtime)
Definition manage.c:3932
struct management * management_init(void)
Definition manage.c:2803
void management_event_loop_n_seconds(struct management *man, int sec)
Definition manage.c:3555
void management_close(struct management *man)
Definition manage.c:2856
void management_set_state(struct management *man, const int state, const char *detail, const in_addr_t *tun_local_ip, const struct in6_addr *tun_local_ip6, const struct openvpn_sockaddr *local, const struct openvpn_sockaddr *remote)
Definition manage.c:2882
bool management_open(struct management *man, const char *addr, const char *port, const char *pass_file, const char *client_user, const char *client_group, const int log_history_cache, const int echo_buffer_size, const int state_buffer_size, const int remap_sigusr1, const unsigned int flags)
Definition manage.c:2817
void management_notify_generic(struct management *man, const char *str)
Definition manage.c:3033
void man_persist_client_stats(struct management *man, struct context *c)
Definition manage.c:4326
void management_set_callback(struct management *man, const struct management_callback *cb)
Definition manage.c:2866
void management_up_down(struct management *man, const char *updown, const struct env_set *es)
Definition manage.c:3017
void management_sleep(const int n)
A sleep function that services the management layer for n seconds rather than doing nothing.
Definition manage.c:4238
void management_post_tunnel_open(struct management *man, const in_addr_t tun_local_ip)
Definition manage.c:3178
static bool management_query_remote_enabled(const struct management *man)
Definition manage.h:427
#define OPENVPN_STATE_CONNECTING
Definition manage.h:451
static bool management_query_proxy_enabled(const struct management *man)
Definition manage.h:433
#define OPENVPN_STATE_CONNECTED
Definition manage.h:454
#define MF_SERVER
Definition manage.h:27
void set_std_files_to_null(bool stdin_only)
Definition misc.c:55
#define GET_USER_PASS_MANAGEMENT
Definition misc.h:113
#define GET_USER_PASS_NEED_OK
Definition misc.h:116
static bool get_user_pass(struct user_pass *up, const char *auth_file, const char *prefix, const unsigned int flags)
Retrieves the user credentials from various sources depending on the flags.
Definition misc.h:155
void frame_calculate_dynamic(struct frame *frame, struct key_type *kt, const struct options *options, struct link_socket_info *lsi)
Set the –mssfix option.
Definition mss.c:317
size_t frame_calculate_protocol_header_size(const struct key_type *kt, const struct options *options, bool occ)
Calculates the size of the OpenVPN protocol header.
Definition mtu.c:69
void frame_print(const struct frame *frame, msglvl_t msglevel, const char *prefix)
Definition mtu.c:198
#define TUN_MTU_MAX_MIN
Definition mtu.h:79
#define BUF_SIZE(f)
Definition mtu.h:188
#define TUN_MTU_MAX
Maximum MTU we accept for MTU related options.
Definition mtu.h:74
#define TUN_MTU_MIN
Definition mtu.h:59
#define SOCKS_UDPv4_HEADROOM
Definition mtu.h:106
static const char * np(const char *str)
Definition multi-auth.c:146
void * openvpn_net_ctx_t
Definition networking.h:38
#define OCC_MTU_LOAD_INTERVAL_SECONDS
Definition occ.h:61
static int occ_reset_op(void)
Definition occ.h:101
#define OCC_INTERVAL_SECONDS
Definition occ.h:45
#define CLEAR(x)
Definition basic.h:32
void error_reset(void)
Definition error.c:157
bool set_mute_cutoff(const int cutoff)
Definition error.c:119
void set_check_status(unsigned int info_level, unsigned int verbose_level)
Definition error.c:611
void reset_check_status(void)
Definition error.c:604
bool set_debug_level(const int level, const unsigned int flags)
Definition error.c:103
#define M_OPTERR
Definition error.h:101
static bool check_debug_level(msglvl_t level)
Definition error.h:251
#define SDL_CONSTRAIN
Definition error.h:201
#define M_NOPREFIX
Definition error.h:98
#define M_USAGE
Definition error.h:107
#define M_FATAL
Definition error.h:90
static void set_check_status_error_delay(unsigned int milliseconds)
Definition error.h:317
#define M_NONFATAL
Definition error.h:91
#define M_ERR
Definition error.h:106
#define msg(flags,...)
Definition error.h:152
unsigned int msglvl_t
Definition error.h:77
#define ASSERT(x)
Definition error.h:219
#define M_WARN
Definition error.h:92
#define TLS_MODE(c)
Definition openvpn.h:539
static void packet_id_persist_init(struct packet_id_persist *p)
Definition openvpn.h:87
#define CM_P2P
Definition openvpn.h:479
#define CM_TOP_CLONE
Definition openvpn.h:481
#define CM_CHILD_TCP
Definition openvpn.h:483
#define CM_CHILD_UDP
Definition openvpn.h:482
#define MAX_PEER_ID
Definition openvpn.h:550
#define CM_TOP
Definition openvpn.h:480
void options_detach(struct options *o)
Definition options.c:1277
void pre_connect_restore(struct options *o, struct gc_arena *gc)
Definition options.c:2505
const char title_string[]
Definition options.c:73
int auth_retry_get(void)
Definition options.c:3401
void notnull(const char *arg, const char *description)
Definition options.c:3541
#define MODE_POINT_TO_POINT
Definition options.h:264
#define OPT_P_UP
Definition options.h:732
#define CE_MAN_QUERY_REMOTE_QUERY
Definition options.h:158
#define OPT_P_NCP
Negotiable crypto parameters.
Definition options.h:743
#define OPT_P_ECHO
Definition options.h:751
#define MODE_SERVER
Definition options.h:265
#define streq(x, y)
Definition options.h:726
#define OPT_P_EXPLICIT_NOTIFY
Definition options.h:750
#define CE_MAN_QUERY_REMOTE_SKIP
Definition options.h:161
#define AR_INTERACT
Definition options.h:940
#define OPT_P_SHAPER
Definition options.h:737
static bool dco_enabled(const struct options *o)
Returns whether the current configuration has dco enabled.
Definition options.h:961
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_SOCKFLAGS
Definition options.h:757
#define SHAPER_DEFINED(opt)
Definition options.h:774
#define CE_MAN_QUERY_REMOTE_MOD
Definition options.h:160
#define CE_MAN_QUERY_PROXY
Definition options.h:156
#define OPT_P_MESSAGES
Definition options.h:742
#define OPT_P_SETENV
Definition options.h:736
#define OPT_P_SOCKBUF
Definition options.h:756
#define CE_MAN_QUERY_REMOTE_MASK
Definition options.h:162
#define OPT_P_PLUGIN
Definition options.h:755
#define OPT_P_TIMER
Definition options.h:738
#define PING_RESTART
Definition options.h:356
#define RH_PORT_LEN
Definition options.h:235
#define OPT_P_DEFAULT
Definition options.h:764
#define CE_MAN_QUERY_REMOTE_SHIFT
Definition options.h:163
#define OPT_P_DHCPDNS
Definition options.h:734
#define OPT_P_PULL_MODE
Definition options.h:754
@ GENKEY_AUTH_TOKEN
Definition options.h:244
@ GENKEY_SECRET
Definition options.h:241
@ GENKEY_TLS_CRYPTV2_SERVER
Definition options.h:243
@ GENKEY_TLS_CRYPTV2_CLIENT
Definition options.h:242
#define OPT_P_PUSH_MTU
Definition options.h:761
#define AR_NONE
Definition options.h:939
#define AR_NOINTERACT
Definition options.h:941
#define RH_HOST_LEN
Definition options.h:233
#define OPT_P_PERSIST
Definition options.h:739
#define MAX_PARMS
Definition options.h:51
#define PING_UNDEF
Definition options.h:354
#define CE_MAN_QUERY_REMOTE_ACCEPT
Definition options.h:159
#define PULL_DEFINED(opt)
Definition options.h:766
#define ROUTE_OPTION_FLAGS(o)
Definition options.h:769
#define PING_EXIT
Definition options.h:355
#define OPT_P_COMP
Definition options.h:741
#define OPT_P_ROUTE_EXTRAS
Definition options.h:753
#define OPT_P_PEER_ID
Definition options.h:759
#define OPT_P_ROUTE
Definition options.h:733
#define CE_DISABLED
Definition options.h:155
const char * options_string_version(const char *s, struct gc_arena *gc)
char * options_string(const struct options *o, const struct frame *frame, struct tuntap *tt, openvpn_net_ctx_t *ctx, bool remote, struct gc_arena *gc)
Build an options string to represent data channel encryption options.
time_t now
Definition otime.c:33
static void update_time(void)
Definition otime.h:84
void time_test(void)
void packet_id_persist_save(struct packet_id_persist *p)
Definition packet_id.c:508
void packet_id_persist_load_obj(const struct packet_id_persist *p, struct packet_id *pid)
Definition packet_id.c:549
void packet_id_init(struct packet_id *p, int seq_backtrack, int time_backtrack, const char *name, int unit)
Definition packet_id.c:96
void packet_id_persist_close(struct packet_id_persist *p)
Definition packet_id.c:450
void packet_id_free(struct packet_id *p)
Definition packet_id.c:126
void packet_id_persist_load(struct packet_id_persist *p, const char *filename)
Definition packet_id.c:464
#define PRE_PULL_INITIAL_PING_RESTART
Definition ping.h:32
unsigned int platform_getpid(void)
Definition platform.c:333
const char * platform_create_temp_file(const char *directory, const char *prefix, struct gc_arena *gc)
Create a temporary file in directory, returns the filename of the created file.
Definition platform.c:540
void platform_user_group_set(const struct platform_state_user *user_state, const struct platform_state_group *group_state, struct context *c)
Definition platform.c:218
void platform_nice(int niceval)
Definition platform.c:311
bool platform_user_get(const char *username, struct platform_state_user *state)
Definition platform.c:80
bool platform_unlink(const char *filename)
Definition platform.c:487
FILE * platform_fopen(const char *path, const char *mode)
Definition platform.c:500
int platform_chdir(const char *dir)
Definition platform.c:392
void platform_mlockall(bool print_msg)
Definition platform.c:344
void platform_chroot(const char *path)
Definition platform.c:54
bool platform_group_get(const char *groupname, struct platform_state_group *state)
Definition platform.c:124
void plugin_list_close(struct plugin_list *pl)
Definition plugin.c:869
void plugin_return_free(struct plugin_return *pr)
Definition plugin.c:986
struct plugin_list * plugin_list_inherit(const struct plugin_list *src)
Definition plugin.c:690
struct plugin_list * plugin_list_init(const struct plugin_option_list *list)
Definition plugin.c:764
void plugin_return_get_column(const struct plugin_return *src, struct plugin_return *dest, const char *colname)
Definition plugin.c:972
bool plugin_defined(const struct plugin_list *pl, const int type)
Definition plugin.c:904
void plugin_list_open(struct plugin_list *pl, const struct plugin_option_list *list, struct plugin_return *pr, const struct env_set *es, const int init_point)
Definition plugin.c:774
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
struct ifconfig_pool_persist * ifconfig_pool_persist_init(const char *filename, int refresh_freq)
Definition pool.c:538
void ifconfig_pool_persist_close(struct ifconfig_pool_persist *persist)
Definition pool.c:560
#define TOP_NET30
Definition proto.h:41
#define DEV_TYPE_TUN
Definition proto.h:35
#define TOP_P2P
Definition proto.h:42
void http_proxy_close(struct http_proxy_info *hp)
Definition proxy.c:524
struct http_proxy_info * http_proxy_new(const struct http_proxy_options *o)
Definition proxy.c:479
struct http_proxy_options * init_http_proxy_options_once(struct http_proxy_options **hpo, struct gc_arena *gc)
Definition proxy.c:44
#define PAR_NCT
Definition proxy.h:51
#define PAR_ALL
Definition proxy.h:50
bool add_routes(struct route_list *rl, struct route_ipv6_list *rl6, const struct tuntap *tt, unsigned int flags, const struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:1090
void setenv_routes_ipv6(struct env_set *es, const struct route_ipv6_list *rl6)
Definition route.c:1372
bool block_local_needed(const struct route_list *rl)
Get the decision whether to block traffic to local networks while the VPN is connected.
Definition route.c:585
bool init_route_ipv6_list(struct route_ipv6_list *rl6, const struct route_ipv6_option_list *opt6, const char *remote_endpoint, int default_metric, const struct in6_addr *remote_host_ipv6, struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:739
void add_route_ipv6_to_option_list(struct route_ipv6_option_list *l, const char *prefix, const char *gateway, const char *metric, int table_id)
Definition route.c:496
bool init_route_list(struct route_list *rl, const struct route_option_list *opt, const char *remote_endpoint, int default_metric, in_addr_t remote_host, struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:593
void delete_routes(struct route_list *rl, struct route_ipv6_list *rl6, const struct tuntap *tt, unsigned int flags, const struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:1155
void show_routes(msglvl_t msglevel)
Definition route.c:3031
void setenv_routes(struct env_set *es, const struct route_list *rl)
Definition route.c:1335
static int route_did_redirect_default_gateway(const struct route_list *rl)
Definition route.h:411
#define RG_REROUTE_GW
Definition route.h:91
int script_security(void)
Definition run_command.c:42
#define S_FATAL
Definition run_command.h:50
#define SSEC_SCRIPTS
allow calling of built-in programs and user-defined scripts
Definition run_command.h:35
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
#define SSEC_PW_ENV
allow calling of built-in programs and user-defined scripts that may receive a password as an environ...
Definition run_command.h:38
void shaper_msg(struct shaper *s)
Definition shaper.c:87
static void shaper_init(struct shaper *s, int bytes_per_second)
Definition shaper.h:85
void post_init_signal_catch(void)
Definition sig.c:421
void print_status(struct context *c, struct status_output *so)
Definition sig.c:478
void pre_init_signal_catch(void)
Definition sig.c:392
void remap_signal(struct context *c)
Definition sig.c:588
const char * signal_description(const int signum, const char *sigtext)
Definition sig.c:105
void restore_signal_state(void)
Definition sig.c:460
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
#define SIG_SOURCE_HARD
Definition sig.h:30
static void siphash_key_init(uint8_t *key)
Initialises a SIPHASH key with a random value.
Definition siphash.h:85
void link_socket_init_phase1(struct context *c, int sock_index, int mode)
Definition socket.c:1300
void link_socket_init_phase2(struct context *c, struct link_socket *sock)
Definition socket.c:1655
void link_socket_update_buffer_sizes(struct link_socket *sock, int rcvbuf, int sndbuf)
Definition socket.c:520
const struct in6_addr * link_socket_current_remote_ipv6(const struct link_socket_info *info)
Definition socket.c:1958
void link_socket_close(struct link_socket *sock)
Definition socket.c:1765
in_addr_t link_socket_current_remote(const struct link_socket_info *info)
Definition socket.c:1924
void do_preresolve(struct context *c)
Definition socket.c:323
struct link_socket * link_socket_new(void)
Definition socket.c:1286
#define LS_MODE_TCP_ACCEPT_FROM
Definition socket.h:200
#define LS_MODE_DEFAULT
Definition socket.h:198
#define LS_MODE_TCP_LISTEN
Definition socket.h:199
const char * proto2ascii(int proto, sa_family_t af, bool display_form)
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_udp(int proto)
Returns if the protocol being used is UDP.
@ PROTO_UDP
@ PROTO_TCP
@ PROTO_TCP_CLIENT
@ PROTO_TCP_SERVER
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)
static bool addr_defined(const struct openvpn_sockaddr *addr)
struct socks_proxy_info * socks_proxy_new(const char *server, const char *port, const char *authfile)
Definition socks.c:51
void socks_proxy_close(struct socks_proxy_info *sp)
Definition socks.c:78
void ssl_purge_auth(const bool auth_user_pass_only)
Definition ssl.c:395
struct tls_root_ctx * init_ssl(const struct options *options, bool in_chroot)
Build master SSL context object that serves for the whole of OpenVPN instantiation.
Definition ssl.c:525
void auth_user_pass_setup(const char *auth_file, bool is_inline, bool username_only, const struct static_challenge_info *sci)
Definition ssl.c:302
void pem_password_setup(const char *auth_file)
Definition ssl.c:257
void init_ssl_lib(void)
Definition ssl.c:234
bool tls_session_update_crypto_params(struct tls_multi *multi, struct tls_session *session, struct options *options, struct frame *frame, struct frame *frame_fragment, struct link_socket_info *lsi, dco_context_t *dco)
Update TLS session crypto parameters (cipher and auth) and derive data channel keys based on the supp...
Definition ssl.c:1672
void free_ssl_lib(void)
Definition ssl.c:242
void enable_auth_user_pass(void)
Definition ssl.c:296
void show_available_tls_ciphers(const char *cipher_list, const char *cipher_list_tls13, const char *tls_cert_profile)
Definition ssl.c:4235
#define TLS_MULTI_HORIZON
Definition ssl.h:61
#define TLS_MULTI_REFRESH
Definition ssl.h:60
void tls_ctx_free(struct tls_root_ctx *ctx)
Frees the library-specific TLSv1 context.
void show_available_curves(void)
Show the available elliptic curves in the crypto library.
bool tls_ctx_initialised(struct tls_root_ctx *ctx)
Checks whether the given TLS context is initialised.
@ CAS_CONNECT_DONE
Definition ssl_common.h:593
@ CAS_RECONNECT_PENDING
session has already successful established (CAS_CONNECT_DONE) but has a reconnect and needs to redo s...
Definition ssl_common.h:592
bool check_pull_client_ncp(struct context *c, const uint64_t found)
Checks whether the cipher negotiation is in an acceptable state and we continue to connect or should ...
Definition ssl_ncp.c:302
bool tls_item_in_cipher_list(const char *item, const char *list)
Return true iff item is present in the colon-separated zero-terminated cipher list.
Definition ssl_ncp.c:197
const char * get_p2p_ncp_cipher(struct tls_session *session, const char *peer_info, struct gc_arena *gc)
Determines the best common cipher from both peers IV_CIPHER lists.
Definition ssl_ncp.c:347
Control Channel SSL/Data dynamic negotiation Module This file is split from ssl.h to be able to unit ...
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.
#define VERIFY_X509_NONE
Definition ssl_verify.h:68
#define NS_CERT_CHECK_SERVER
Do not perform Netscape certificate type verification.
Definition ssl_verify.h:252
void status_printf(struct status_output *so, const char *format,...)
Definition status.c:212
struct status_output * status_open(const char *filename, const int refresh_freq, const int msglevel, const struct virtual_output *vout, const unsigned int flags)
Definition status.c:59
bool status_close(struct status_output *so)
Definition status.c:178
#define STATUS_OUTPUT_WRITE
Definition status.h:51
Definition argv.h:35
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:76
Definition options.h:109
struct local_list * local_list
Definition options.h:110
int tun_mtu_max
Definition options.h:131
int connect_retry_seconds
Definition options.h:121
bool tls_crypt_v2_force_cookie
Definition options.h:181
int link_mtu
Definition options.h:136
bool link_mtu_defined
Definition options.h:137
int tun_mtu_extra
Definition options.h:134
int connect_retry_seconds_max
Definition options.h:122
int mssfix
Definition options.h:146
const char * tls_crypt_file
Definition options.h:172
const char * tls_crypt_v2_file
Definition options.h:177
bool tun_mtu_extra_defined
Definition options.h:135
const char * remote
Definition options.h:116
const char * socks_proxy_port
Definition options.h:126
bool mssfix_encap
Definition options.h:148
struct http_proxy_options * http_proxy_options
Definition options.h:124
bool tls_crypt_file_inline
Definition options.h:173
bool tls_auth_file_inline
Definition options.h:168
bool tun_mtu_defined
Definition options.h:133
int tls_mtu
Definition options.h:138
int explicit_exit_notification
Definition options.h:152
const char * socks_proxy_authfile
Definition options.h:127
const char * remote_port
Definition options.h:115
bool fragment_encap
Definition options.h:144
const char * socks_proxy_server
Definition options.h:125
int fragment
Definition options.h:143
int proto
Definition options.h:111
sa_family_t af
Definition options.h:112
const char * tls_auth_file
Definition options.h:167
int tun_mtu
Definition options.h:129
int key_direction
Definition options.h:169
bool tls_crypt_v2_file_inline
Definition options.h:178
unsigned int flags
Definition options.h:164
struct connection_entry ** array
Definition options.h:206
Level 0 context containing information related to the OpenVPN process.
Definition openvpn.h:138
struct platform_state_group platform_state_group
Definition openvpn.h:144
struct platform_state_user platform_state_user
Definition openvpn.h:143
bool uid_gid_chroot_set
Definition openvpn.h:142
bool uid_gid_specified
Definition openvpn.h:140
struct key_schedule ks
Definition openvpn.h:165
struct ifconfig_pool_persist * ifconfig_pool_persist
Definition openvpn.h:198
bool http_proxy_owned
Definition openvpn.h:191
struct status_output * status_output
Definition openvpn.h:186
struct route_list * route_list
List of routing information.
Definition openvpn.h:178
struct link_socket_addr * link_socket_addrs
Local and remote addresses on the external network.
Definition openvpn.h:160
struct sha256_digest pulled_options_digest_save
Hash of option strings received from the remote OpenVPN server.
Definition openvpn.h:202
int link_sockets_num
Definition openvpn.h:159
bool status_output_owned
Definition openvpn.h:187
struct route_ipv6_list * route_ipv6_list
Definition openvpn.h:183
struct packet_id_persist pid_persist
Definition openvpn.h:171
struct http_proxy_info * http_proxy
Definition openvpn.h:190
bool socks_proxy_owned
Definition openvpn.h:195
bool tuntap_owned
Whether the tun/tap interface should be cleaned up when this context is cleaned up.
Definition openvpn.h:174
bool ifconfig_pool_persist_owned
Definition openvpn.h:199
struct socks_proxy_info * socks_proxy
Definition openvpn.h:194
struct tuntap * tuntap
Tun/tap virtual network interface.
Definition openvpn.h:173
char * options_string_local
Definition openvpn.h:297
struct fragment_master * fragment
Definition openvpn.h:253
bool do_up_ran
Definition openvpn.h:411
char * options_string_remote
Definition openvpn.h:298
struct event_timeout route_wakeup_expire
Definition openvpn.h:384
bool did_open_tun
Definition openvpn.h:387
md_ctx_t * pulled_options_state
Definition openvpn.h:441
bool es_owned
Definition openvpn.h:421
struct man_def_auth_context mda_context
Definition openvpn.h:450
const struct link_socket * accept_from
Definition openvpn.h:243
struct tls_auth_standalone * tls_auth_standalone
TLS state structure required for the initial authentication of a client's connection attempt.
Definition openvpn.h:327
int occ_op
Definition openvpn.h:300
struct env_set * es
Definition openvpn.h:420
bool link_socket_owned
Definition openvpn.h:241
struct tls_multi * tls_multi
TLS state structure for this VPN tunnel.
Definition openvpn.h:324
struct frame frame
Definition openvpn.h:249
struct frame frame_fragment
Definition openvpn.h:254
struct crypto_options crypto_options
Security parameters and crypto state used by the Data Channel Crypto module to process data channel p...
Definition openvpn.h:349
bool buffers_owned
Definition openvpn.h:368
struct link_socket ** link_sockets
Definition openvpn.h:238
struct link_socket_info ** link_socket_infos
Definition openvpn.h:239
uint8_t session_id_key[SIPHASH_KEY_SIZE]
the siphash secret we use to generate and verify our syn cookie like session ids from the server.
Definition openvpn.h:339
bool log_rw
Definition openvpn.h:380
int event_set_max
Definition openvpn.h:232
struct gc_arena gc
Garbage collection arena for allocations done in the level 2 scope of this context_2 structure.
Definition openvpn.h:226
struct sha256_digest pulled_options_digest
Definition openvpn.h:442
struct event_set * event_set
Definition openvpn.h:231
struct context_buffers * buffers
Definition openvpn.h:367
struct event_timeout route_wakeup
Definition openvpn.h:383
int tls_exit_signal
Definition openvpn.h:347
bool event_set_owned
Definition openvpn.h:233
struct buffer read_link_buf
Definition openvpn.h:114
struct buffer encrypt_buf
Definition openvpn.h:101
struct buffer read_tun_buf
Definition openvpn.h:115
struct buffer decrypt_buf
Definition openvpn.h:102
struct buffer aux_buf
Definition openvpn.h:98
int restart_sleep_seconds
Definition openvpn.h:123
struct dns_updown_runner_info duri
Definition openvpn.h:124
Contains all state information for one tunnel.
Definition openvpn.h:471
int mode
Role of this context within the OpenVPN process.
Definition openvpn.h:484
struct context_0 * c0
Level 0 context.
Definition openvpn.h:512
bool did_we_daemonize
Whether demonization has already taken place.
Definition openvpn.h:507
bool first_time
True on the first iteration of OpenVPN's main loop.
Definition openvpn.h:475
struct signal_info * sig
Internal error signaling object.
Definition openvpn.h:500
openvpn_net_ctx_t net_ctx
Networking API opaque context.
Definition openvpn.h:498
struct plugin_list * plugins
List of plug-ins.
Definition openvpn.h:502
struct context_2 c2
Level 2 context.
Definition openvpn.h:514
struct env_set * es
Set of environment variables.
Definition openvpn.h:496
struct options options
Options loaded from command line or configuration file.
Definition openvpn.h:472
bool plugins_owned
Whether the plug-ins should be cleaned up when this context is cleaned up.
Definition openvpn.h:503
struct gc_arena gc
Garbage collection arena for allocations done in the scope of this context structure.
Definition openvpn.h:492
struct context_1 c1
Level 1 context.
Definition openvpn.h:513
struct context_persist persist
Persistent context.
Definition openvpn.h:510
unsigned int flags
Bit-flags determining behavior of security operation functions.
Definition crypto.h:386
struct packet_id_persist * pid_persist
Persistent packet ID state for keeping state between successive OpenVPN process startups.
Definition crypto.h:342
struct key_ctx_bi key_ctx_bi
OpenSSL cipher and HMAC contexts for both sending and receiving directions.
Definition crypto.h:294
struct packet_id packet_id
Current packet ID state for both sending and receiving directions.
Definition crypto.h:333
Packet geometry parameters.
Definition mtu.h:113
int tun_mtu
the (user) configured tun-mtu.
Definition mtu.h:147
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 tun_max_mtu
the maximum tun-mtu size the buffers are are sized for.
Definition mtu.h:157
int extra_tun
Maximum number of bytes in excess of the tun/tap MTU that might be read from or written to the virtua...
Definition mtu.h:161
int headroom
the headroom in the buffer, this is choosen to allow all potential header to be added before the pack...
Definition mtu.h:124
struct frame::@8 buf
int tailroom
the tailroom in the buffer.
Definition mtu.h:128
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
const char * port
Definition proxy.h:47
const char * server
Definition proxy.h:46
struct buffer tls_crypt_v2_wkc
Wrapped client key.
Definition openvpn.h:73
struct key2 original_wrap_keydata
original tls-crypt key preserved to xored into the tls_crypt renegotiation key
Definition openvpn.h:71
struct key_type key_type
Definition openvpn.h:58
struct key_ctx auth_token_key
Definition openvpn.h:74
struct key_type tls_auth_key_type
Definition openvpn.h:67
struct key_ctx_bi tls_wrap_key
Definition openvpn.h:68
struct key_ctx_bi static_key
Definition openvpn.h:61
struct tls_root_ctx * ssl_ctx
Definition openvpn.h:64
struct key_ctx tls_crypt_v2_server_key
Definition openvpn.h:72
const char * cipher
const name of the cipher
Definition crypto.h:142
const char * digest
Message digest static parameters.
Definition crypto.h:143
const char * special_state_msg
Definition manage.h:232
void(* status)(void *arg, const int version, struct status_output *so)
Definition manage.h:178
bool(* remote_entry_get)(void *arg, unsigned int index, char **remote)
Definition manage.h:198
unsigned int(* remote_entry_count)(void *arg)
Definition manage.h:197
bool(* send_cc_message)(void *arg, const char *message, const char *parameter)
Definition manage.h:184
bool(* proxy_cmd)(void *arg, const char **p)
Definition manage.h:192
void(* show_net)(void *arg, const msglvl_t msglevel)
Definition manage.h:179
unsigned int flags
Definition manage.h:176
bool(* remote_cmd)(void *arg, const char **p)
Definition manage.h:193
struct man_persist persist
Definition manage.h:335
union openvpn_sockaddr::@27 addr
struct sockaddr sa
Definition socket_util.h:42
struct sockaddr_in in4
Definition socket_util.h:43
struct sockaddr_in6 in6
Definition socket_util.h:44
int rcvbuf
Definition options.h:413
bool resolve_in_advance
Definition options.h:368
bool route_nopull
Definition options.h:437
const char * genkey_extra_data
Definition options.h:290
struct compress_options comp
Definition options.h:410
bool persist_config
Definition options.h:278
struct connection_list * connection_list
Definition options.h:295
const char * management_port
Definition options.h:447
const char * ifconfig_ipv6_remote
Definition options.h:331
int server_backoff_time
Definition options.h:310
int auth_token_renewal
Definition options.h:545
const char * tmp_dir
Definition options.h:466
bool push_peer_info
Definition options.h:681
bool daemon
Definition options.h:391
int route_default_metric
Definition options.h:429
int renegotiate_seconds_min
Definition options.h:647
const char * auth_token_secret_file
Definition options.h:546
unsigned int imported_protocol_flags
Definition options.h:723
const char * tls_export_peer_cert_dir
Definition options.h:612
bool crl_file_inline
Definition options.h:616
const char * down_script
Definition options.h:386
hash_algo_type verify_hash_algo
Definition options.h:622
int replay_time
Definition options.h:586
int management_state_buffer_size
Definition options.h:451
bool duplicate_cn
Definition options.h:527
int management_echo_buffer_size
Definition options.h:450
bool show_net_up
Definition options.h:694
bool verify_hash_no_ca
Definition options.h:624
bool use_peer_id
Whether the data channel uses the DATA_V2 header (peer-id).
Definition options.h:703
unsigned remote_cert_ku[MAX_PARMS]
Definition options.h:619
bool server_bridge_defined
Definition options.h:481
const char * keying_material_exporter_label
Definition options.h:707
const char * status_file
Definition options.h:406
unsigned int ssl_flags
Definition options.h:625
bool route_noexec
Definition options.h:430
bool ifconfig_nowarn
Definition options.h:333
const char * remote_cert_eku
Definition options.h:620
int tls_timeout
Definition options.h:641
bool test_crypto
Definition options.h:588
bool up_delay
Definition options.h:389
bool server_bridge_proxy_dhcp
Definition options.h:479
const char * authname
Definition options.h:581
const char * exit_event_name
Definition options.h:692
const char * ifconfig_ipv6_local
Definition options.h:329
int replay_window
Definition options.h:585
int mute
Definition options.h:400
bool auth_user_pass_verify_script_via_file
Definition options.h:541
const char * dev_type
Definition options.h:323
int persist_mode
Definition options.h:279
int ifconfig_pool_persist_refresh_freq
Definition options.h:493
bool show_digests
Definition options.h:283
const char * up_script
Definition options.h:385
int ce_advance_count
Definition options.h:306
bool single_session
Definition options.h:679
struct remote_host_store * rh_store
Definition options.h:316
int verify_hash_depth
Definition options.h:623
bool route_delay_defined
Definition options.h:433
const char * packet_id_file
Definition options.h:587
const char * tls_crypt_v2_file
Definition options.h:669
int management_log_history_cache
Definition options.h:449
uint32_t peer_id
Definition options.h:704
struct route_option_list * routes
Definition options.h:434
int keepalive_timeout
Definition options.h:343
bool block_outside_dns
Definition options.h:696
bool tls_exit
Definition options.h:683
bool show_engines
Definition options.h:284
HANDLE msg_channel
Definition options.h:691
const char * key_pass_file
Definition options.h:281
bool mute_replay_warnings
Definition options.h:584
unsigned int unsuccessful_attempts
Definition options.h:304
int handshake_window
Definition options.h:651
const char * ifconfig_local
Definition options.h:327
char * x509_username_field[MAX_PARMS]
Definition options.h:654
struct connection_entry ce
Definition options.h:294
bool user_script_used
Definition options.h:387
bool show_tls_ciphers
Definition options.h:285
struct tuntap_options tuntap_options
Definition options.h:371
struct verify_hash_list * verify_hash
Definition options.h:621
const char * tls_cert_profile
Definition options.h:609
int64_t renegotiate_packets
Definition options.h:645
unsigned int management_flags
Definition options.h:459
const char * route_default_gateway
Definition options.h:426
bool exit_event_initial_state
Definition options.h:693
struct static_challenge_info sc_info
Definition options.h:568
bool auth_token_call_auth
Definition options.h:543
int topology
Definition options.h:326
bool disable_dco
Definition options.h:374
const char * ncp_ciphers
Definition options.h:580
bool genkey
Definition options.h:287
const char * ciphername
Definition options.h:575
const char * auth_user_pass_file
Definition options.h:560
int tls_crypt_v2_max_age
Definition options.h:676
const char * username
Definition options.h:377
struct plugin_option_list * plugin_list
Definition options.h:462
int auth_token_lifetime
Definition options.h:544
int ns_cert_type
Definition options.h:618
const char * tls_crypt_v2_verify_script
Definition options.h:674
int mode
Definition options.h:266
bool tls_server
Definition options.h:591
const char * auth_user_pass_verify_script
Definition options.h:540
int connect_retry_max
Definition options.h:293
bool pull
Definition options.h:556
bool show_curves
Definition options.h:286
const char * route_ipv6_default_gateway
Definition options.h:427
bool tls_client
Definition options.h:592
bool auth_token_generate
Definition options.h:542
bool priv_key_file_inline
Definition options.h:603
const char * tls_verify
Definition options.h:611
const char * crl_file
Definition options.h:615
int ping_rec_timeout_action
Definition options.h:357
bool auth_user_pass_file_inline
Definition options.h:561
bool show_ciphers
Definition options.h:282
bool enable_ncp_fallback
If defined fall back to ciphername if NCP fails.
Definition options.h:576
const char * route_predown_script
Definition options.h:425
int route_delay_window
Definition options.h:432
bool mlock
Definition options.h:340
int sndbuf
Definition options.h:414
struct gc_arena gc
Definition options.h:258
bool down_pre
Definition options.h:388
bool persist_tun
Definition options.h:359
int route_default_table_id
Definition options.h:428
bool auth_token_secret_file_inline
Definition options.h:547
const char * config
Definition options.h:261
int keying_material_exporter_length
Definition options.h:708
bool mtu_test
Definition options.h:338
int verify_x509_type
Definition options.h:613
const char * cipher_list_tls13
Definition options.h:607
int status_file_update_freq
Definition options.h:408
const char * management_client_user
Definition options.h:453
const char * cipher_list
Definition options.h:606
bool ccd_exclusive
Definition options.h:506
const char * genkey_filename
Definition options.h:289
const struct x509_track * x509_track
Definition options.h:685
const char * chroot_dir
Definition options.h:379
bool auth_user_pass_username_only
Definition options.h:562
bool log
Definition options.h:395
bool shared_secret_file_inline
Definition options.h:572
int renegotiate_seconds
Definition options.h:646
int ping_rec_timeout
Definition options.h:351
const char * engine
Definition options.h:582
const char * management_addr
Definition options.h:446
const char * verify_x509_name
Definition options.h:614
int ping_send_timeout
Definition options.h:350
int route_delay
Definition options.h:431
const char * dev_node
Definition options.h:324
const char * client_crresponse_script
Definition options.h:504
struct route_ipv6_option_list * routes_ipv6
Definition options.h:435
int key_direction
Definition options.h:574
bool persist_remote_ip
Definition options.h:361
bool up_restart
Definition options.h:390
int keepalive_ping
Definition options.h:342
bool no_advance
Definition options.h:299
bool tls_crypt_v2_file_inline
Definition options.h:670
const char * groupname
Definition options.h:378
const char * cd_dir
Definition options.h:380
int nice
Definition options.h:398
int transition_window
Definition options.h:657
const char * ifconfig_remote_netmask
Definition options.h:328
const char * lladdr
Definition options.h:325
int verbosity
Definition options.h:399
enum tun_driver_type windows_driver
Definition options.h:697
int remap_sigusr1
Definition options.h:393
int64_t renegotiate_bytes
Definition options.h:644
const char * route_script
Definition options.h:424
const char * management_user_pass
Definition options.h:448
const char * shared_secret_file
Definition options.h:571
bool ifconfig_noexec
Definition options.h:332
const char * dev
Definition options.h:322
const char * management_client_group
Definition options.h:454
const char * client_config_dir
Definition options.h:505
enum genkey_type genkey_type
Definition options.h:288
bool advance_next_remote
Definition options.h:302
const char * ifconfig_pool_persist_filename
Definition options.h:492
int ifconfig_ipv6_netbits
Definition options.h:330
bool persist_local_ip
Definition options.h:360
struct openvpn_plugin_string_list * list[MAX_PLUGINS]
Definition plugin.h:106
char port[RH_PORT_LEN]
Definition options.h:236
char host[RH_HOST_LEN]
Definition options.h:234
struct gc_arena gc
Definition route.h:256
unsigned int flags
Definition route.h:113
struct gc_arena * gc
Definition route.h:115
struct gc_arena gc
Definition route.h:240
Wrapper struct to pass around SHA256 digests.
Definition crypto.h:133
const char * signal_text
Definition sig.h:44
volatile int signal_received
Definition sig.h:42
volatile int source
Definition sig.h:43
unsigned int flags
Definition status.h:52
struct frame frame
Definition ssl_pkt.h:81
struct buffer workbuf
Definition ssl_pkt.h:80
struct tls_wrap_ctx tls_wrap
Definition ssl_pkt.h:79
dco_context_t * dco
Definition ssl_common.h:727
char * peer_info
A multi-line string of general-purpose info received from peer over control channel.
Definition ssl_common.h:672
enum multi_status multi_state
Definition ssl_common.h:632
struct tls_options opt
Definition ssl_common.h:616
struct tls_session session[TM_SIZE]
Array of tls_session objects representing control channel sessions with the remote peer.
Definition ssl_common.h:713
bool use_peer_id
Definition ssl_common.h:701
uint32_t tx_peer_id
Definition ssl_common.h:700
uint32_t rx_peer_id
Definition ssl_common.h:699
int64_t renegotiate_bytes
Definition ssl_common.h:344
struct key_ctx auth_token_key
Definition ssl_common.h:407
unsigned int auth_token_renewal
Definition ssl_common.h:405
struct env_set * es
Definition ssl_common.h:413
unsigned int auth_token_lifetime
Definition ssl_common.h:404
struct tls_wrap_ctx tls_wrap
TLS handshake wrapping state.
Definition ssl_common.h:386
size_t ekm_label_size
Definition ssl_common.h:451
unsigned int crypto_flags
Definition ssl_common.h:367
unsigned remote_cert_ku[MAX_PARMS]
Definition ssl_common.h:358
interval_t packet_timeout
Definition ssl_common.h:343
const char * auth_user_pass_file
Definition ssl_common.h:396
const char * client_crresponse_script
Definition ssl_common.h:392
const struct static_challenge_info * sci
Definition ssl_common.h:443
openvpn_net_ctx_t * net_ctx
Definition ssl_common.h:414
const char * tmp_dir
Definition ssl_common.h:394
interval_t renegotiate_seconds
Definition ssl_common.h:349
struct frame frame
Definition ssl_common.h:388
int64_t renegotiate_packets
Definition ssl_common.h:345
bool auth_user_pass_file_inline
Definition ssl_common.h:397
int verify_hash_depth
Definition ssl_common.h:361
const struct plugin_list * plugins
Definition ssl_common.h:415
const char * client_config_dir_exclusive
Definition ssl_common.h:410
bool tls_crypt_v2
Definition ssl_common.h:381
const char * export_peer_cert_dir
Definition ssl_common.h:395
char * x509_username_field[MAX_PARMS]
Definition ssl_common.h:364
int tls_crypt_v2_max_age
Definition ssl_common.h:383
const char * verify_command
Definition ssl_common.h:352
struct verify_hash_list * verify_hash
Definition ssl_common.h:360
const char * ekm_label
Definition ssl_common.h:450
size_t ekm_size
Definition ssl_common.h:452
int transition_window
Definition ssl_common.h:341
const char * config_ciphername
Definition ssl_common.h:372
int verify_x509_type
Definition ssl_common.h:353
bool single_session
Definition ssl_common.h:327
bool data_epoch_supported
whether our underlying data channel supports new data channel features (epoch keys with AEAD tag at t...
Definition ssl_common.h:379
const char * verify_x509_name
Definition ssl_common.h:354
bool crl_file_inline
Definition ssl_common.h:356
const struct x509_track * x509_track
Definition ssl_common.h:440
bool verify_hash_no_ca
Definition ssl_common.h:362
struct man_def_auth_context * mda_context
Definition ssl_common.h:437
const char * tls_crypt_v2_verify_script
Definition ssl_common.h:382
bool auth_user_pass_verify_script_via_file
Definition ssl_common.h:393
const char * config_ncp_ciphers
Definition ssl_common.h:373
unsigned int ssl_flags
Definition ssl_common.h:434
bool auth_token_generate
Generate auth-tokens on successful user/pass auth,seet via options->auth_token_generate.
Definition ssl_common.h:400
struct key_type key_type
Definition ssl_common.h:313
int push_peer_info_detail
The detail of info we push in peer info.
Definition ssl_common.h:340
struct tls_root_ctx * ssl_ctx
Definition ssl_common.h:310
hash_algo_type verify_hash_algo
Definition ssl_common.h:363
bool auth_token_call_auth
always call normal authentication
Definition ssl_common.h:403
const char * crl_file
Definition ssl_common.h:355
int handshake_window
Definition ssl_common.h:342
bool dco_enabled
Whether keys have to be installed in DCO or not.
Definition ssl_common.h:454
const char * auth_user_pass_verify_script
Definition ssl_common.h:391
const char * remote_cert_eku
Definition ssl_common.h:359
bool auth_user_pass_username_only
Definition ssl_common.h:398
int replay_window
Definition ssl_common.h:369
Security parameter state of a single session within a VPN tunnel.
Definition ssl_common.h:489
struct crypto_options opt
Crypto state.
Definition ssl_common.h:283
enum tls_wrap_ctx::@28 mode
Control channel wrapping mode.
struct buffer work
Work buffer (only for –tls-crypt)
Definition ssl_common.h:284
struct key_ctx tls_crypt_v2_server_key
Decrypts client keys.
Definition ssl_common.h:285
const struct buffer * tls_crypt_v2_wkc
Wrapped client key, sent to server.
Definition ssl_common.h:286
struct key2 original_wrap_keydata
original key data to be xored in to the key for dynamic tls-crypt.
Definition ssl_common.h:298
HANDLE msg_channel
Definition tun.h:86
Definition tun.h:181
in_addr_t local
Definition tun.h:208
DWORD adapter_index
Definition tun.h:232
enum tun_driver_type backend_driver
The backend driver that used for this tun/tap device.
Definition tun.h:191
struct tuntap_options options
Definition tun.h:203
struct in6_addr local_ipv6
Definition tun.h:211
dco_context_t dco
Definition tun.h:247
char * actual_name
Definition tun.h:205
in_addr_t remote_netmask
Definition tun.h:209
char password[USER_PASS_LEN]
Definition misc.h:71
char username[USER_PASS_LEN]
Definition misc.h:70
#define SIGHUP
Definition syshead.h:55
#define srandom
Definition syshead.h:44
#define SOCKET_UNDEFINED
Definition syshead.h:443
#define SIGTERM
Definition syshead.h:59
#define SIGUSR1
Definition syshead.h:57
uint32_t in_addr_t
Definition syshead.h:52
struct env_set * es
struct gc_arena gc
Definition test_ssl.c:122
void open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt, openvpn_net_ctx_t *ctx)
Definition tun.c:5985
int dev_type_enum(const char *dev, const char *dev_type)
Definition tun.c:521
void fork_register_dns_action(struct tuntap *tt)
Definition tun.c:5377
struct tuntap * init_tun(const char *dev, const char *dev_type, int topology, const char *ifconfig_local_parm, const char *ifconfig_remote_netmask_parm, const char *ifconfig_ipv6_local_parm, int ifconfig_ipv6_netbits_parm, const char *ifconfig_ipv6_remote_parm, struct addrinfo *local_public, struct addrinfo *remote_public, const bool strict_warn, struct env_set *es, openvpn_net_ctx_t *ctx, struct tuntap *tt)
Definition tun.c:830
bool is_dev_type(const char *dev, const char *dev_type, const char *match_type)
Definition tun.c:503
void do_ifconfig(struct tuntap *tt, const char *ifname, int tun_mtu, const struct env_set *es, openvpn_net_ctx_t *ctx)
do_ifconfig - configure the tunnel interface
Definition tun.c:1566
const char * dev_type_string(const char *dev, const char *dev_type)
Definition tun.c:540
void close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx)
Definition tun.c:6136
void show_adapters(msglvl_t msglevel)
Definition tun.c:4643
void warn_on_use_of_common_subnets(openvpn_net_ctx_t *ctx)
Definition tun.c:670
void init_tun_post(struct tuntap *tt, const struct frame *frame, const struct tuntap_options *options)
Definition tun.c:954
const char * guess_tuntap_dev(const char *dev, const char *dev_type, const char *dev_node, struct gc_arena *gc)
Definition tun.c:560
void do_ifconfig_setenv(const struct tuntap *tt, struct env_set *es)
Definition tun.c:786
void undo_ifconfig(struct tuntap *tt, openvpn_net_ctx_t *ctx)
undo_ifconfig - undo configuration of the tunnel interface
Definition tun.c:1655
void tun_standby_init(struct tuntap *tt)
Definition tun.c:5295
const char * print_tun_backend_driver(enum tun_driver_type driver)
Return a string representation of the tun backed driver type.
Definition tun.c:59
#define IFCONFIG_AFTER_TUN_OPEN
Definition tun.h:349
#define IFCONFIG_BEFORE_TUN_OPEN
Definition tun.h:348
static bool tuntap_is_dco_win(struct tuntap *tt)
Definition tun.h:532
@ DRIVER_NULL
Definition tun.h:52
@ DRIVER_GENERIC_TUNTAP
Definition tun.h:47
@ DRIVER_AFUNIX
using an AF_UNIX socket to pass packets from/to an external program.
Definition tun.h:51
@ DRIVER_DCO
Definition tun.h:53
#define ROUTE_AFTER_TUN
Definition tun.h:380
static int ifconfig_order(struct tuntap *tt)
Definition tun.h:354
static void open_tun_null(struct tuntap *tt)
Definition tun.h:634
static int route_order(struct tuntap *tt)
Definition tun.h:384
#define ROUTE_BEFORE_TUN
Definition tun.h:379
static bool is_tun_type_set(const struct tuntap *tt)
Definition tun.h:628
void open_tun_afunix(struct options *o, int mtu, struct tuntap *tt, struct env_set *orig_env)
Opens an AF_UNIX based tun device.
Definition tun_afunix.c:76
void close_tun_afunix(struct tuntap *tt)
Closes the socket used for the AF_UNIX based device.
Definition tun_afunix.c:128
static bool is_tun_afunix(const char *devnode)
Checks whether a –dev-node parameter specifies a AF_UNIX device.
Definition tun_afunix.h:61
void win32_signal_open(struct win32_signal *ws, int force, const char *exit_event_name, bool exit_event_initial_state)
Definition win32.c:548
bool win_wfp_block(const NET_IFINDEX index, const HANDLE msg_channel, BOOL dns_only)
Definition win32.c:1249
void window_title_generate(const char *title)
Definition win32.c:820
void window_title_save(struct window_title *wt)
Definition win32.c:794
bool win_wfp_uninit(const NET_IFINDEX index, const HANDLE msg_channel)
Definition win32.c:1298
void init_win32(void)
Definition win32.c:109
#define WSO_MODE_CONSOLE
Definition win32.h:160
#define WSO_FORCE_SERVICE
Definition win32.h:174
#define WSO_FORCE_CONSOLE
Definition win32.h:175