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