OpenVPN
openvpn.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-2025 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#include "init.h"
30#include "forward.h"
31#include "multi.h"
32#include "win32.h"
33#include "platform.h"
34#include "string.h"
35
36#include "memdbg.h"
37
38#define P2P_CHECK_SIG() EVENT_LOOP_CHECK_SIGNAL(c, process_signal_p2p, c);
39
40static bool
42{
43 remap_signal(c);
44 return process_signal(c);
45}
46
47
48/**************************************************************************/
56static void
58{
60
61 /* set point-to-point mode */
62 c->mode = CM_P2P;
63 /* initialize tunnel instance, avoid SIGHUP when config is stdin since
64 * re-reading the config from stdin will not work */
65 bool stdin_config = c->options.config && (strcmp(c->options.config, "stdin") == 0);
66 init_instance_handle_signals(c, c->es, stdin_config ? 0 : CC_HARD_USR1_TO_HUP);
67 if (IS_SIG(c))
68 {
69 return;
70 }
71
72 /* main event loop */
73 while (true)
74 {
75 /* process timers, TLS, etc. */
76 pre_select(c);
78
79 /* set up and do the I/O wait */
82
83 /* timeout? */
85 {
86 continue;
87 }
88
89 /* process the I/O which triggered select */
90 process_io(c, c->c2.link_sockets[0]);
92 }
93
95
97
98 /* tear down tunnel instance (unless --persist-tun) */
100}
101
102#undef PROCESS_SIGNAL_P2P
103
104void
106{
107 net_ctx_init(c, &c->net_ctx);
108
109 /* init verbosity and mute levels */
111
112 /* Initialise OpenSSL provider, this needs to be initialised this
113 * early since option post-processing and also openssl info
114 * printing depends on it */
115 for (int j = 1; j < MAX_PARMS && c->options.providers.names[j]; j++)
116 {
118 }
119}
120
121static void
123{
124 for (int j = 1; j < MAX_PARMS && c->options.providers.providers[j]; j++)
125 {
127 }
129}
130
131
132/**************************************************************************/
152static int
153openvpn_main(int argc, char *argv[])
154{
155 struct context c;
156
157#if PEDANTIC
158 fprintf(stderr, "Sorry, I was built with --enable-pedantic and I am incapable of doing any real work!\n");
159 return 1;
160#endif
161
162#ifdef _WIN32
163 SetConsoleOutputCP(CP_UTF8);
164#endif
165
166 CLEAR(c);
167
168 /* signify first time for components which can
169 * only be initialized once per program instantiation. */
170 c.first_time = true;
171
172 /* initialize program-wide statics */
173 if (init_static())
174 {
175 /*
176 * This loop is initially executed on startup and then
177 * once per SIGHUP.
178 */
179 do
180 {
181 /* enter pre-initialization mode with regard to signal handling */
183
184 /* zero context struct but leave first_time member alone */
186
187 /* static signal info object */
188 c.sig = &siginfo_static;
189
190 /* initialize garbage collector scoped to context object */
191 gc_init(&c.gc);
192
193 /* initialize environmental variable store */
194 c.es = env_set_create(NULL);
195#ifdef _WIN32
197#endif
198
199#ifdef ENABLE_MANAGEMENT
200 /* initialize management subsystem */
202#endif
203
204 /* initialize options to default state */
205 init_options(&c.options, true);
206
207 /* parse command line options, and read configuration file */
208 parse_argv(&c.options, argc, argv, M_USAGE, OPT_P_DEFAULT, NULL, c.es);
209
210#ifdef ENABLE_PLUGIN
211 /* plugins may contribute options configuration */
213 init_plugins(&c);
214 open_plugins(&c, true, OPENVPN_PLUGIN_INIT_PRE_CONFIG_PARSE);
215#endif
216
217 /* Early initialisation that need to happen before option
218 * post processing and other early startup but after parsing */
219 init_early(&c);
220
221 /* set dev options */
223
224 /* openssl print info? */
226 {
227 break;
228 }
229
230 /* --genkey mode? */
231 if (do_genkey(&c.options))
232 {
233 break;
234 }
235
236 /* tun/tap persist command? */
238 {
239 break;
240 }
241
242 /* sanity check on options */
244
245 /* show all option settings */
247
248 /* print version number */
249 msg(M_INFO, "%s", title_string);
250#ifdef _WIN32
252#endif
254
256
257 /* misc stuff */
258 pre_setup(&c.options);
259
260 /* test crypto? */
261 if (c.options.test_crypto)
262 {
263 do_test_crypto(&c);
264 break;
265 }
266
267 /* Query passwords before becoming a daemon if we don't use the
268 * management interface to get them. */
270 {
272 }
273
274 /* become a daemon if --daemon */
275 if (c.first_time)
276 {
279 }
280
281#ifdef ENABLE_MANAGEMENT
282 /* open management subsystem */
283 if (!open_management(&c))
284 {
285 break;
286 }
287 /* query for passwords through management interface, if needed */
289 {
291 }
292#endif
293
294 /* set certain options as environmental variables */
296
297 /* finish context init */
298 context_init_1(&c);
299
300 do
301 {
302 /* run tunnel depending on mode */
303 switch (c.options.mode)
304 {
307 break;
308
309 case MODE_SERVER:
310 tunnel_server(&c);
311 break;
312
313 default:
314 ASSERT(0);
315 }
316
317 /* indicates first iteration -- has program-wide scope */
318 c.first_time = false;
319
320 /* any signals received? */
321 if (IS_SIG(&c))
322 {
323 print_signal(c.sig, NULL, M_INFO);
324 }
325
326 /* pass restart status to management subsystem */
328 } while (signal_reset(c.sig, SIGUSR1) == SIGUSR1);
329
332 gc_reset(&c.gc);
333 uninit_early(&c);
334 } while (signal_reset(c.sig, SIGHUP) == SIGHUP);
335 }
336
337 context_gc_free(&c);
338
339#ifdef ENABLE_MANAGEMENT
340 /* close management interface */
342#endif
343
344 /* uninitialize program-wide statics */
346
347 openvpn_exit(OPENVPN_EXIT_STATUS_GOOD); /* exit point */
348 return 0; /* NOTREACHED */
349}
350
351#ifdef _WIN32
352int
353wmain(int argc, wchar_t *wargv[])
354{
355 char **argv;
356 int ret;
357 int i;
358
359 if ((argv = calloc(argc + 1, sizeof(char *))) == NULL)
360 {
361 return 1;
362 }
363
364 for (i = 0; i < argc; i++)
365 {
366 int n = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, NULL, 0, NULL, NULL);
367 argv[i] = malloc(n);
368 WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, argv[i], n, NULL, NULL);
369 }
370
371 ret = openvpn_main(argc, argv);
372
373 for (i = 0; i < argc; i++)
374 {
375 free(argv[i]);
376 }
377 free(argv);
378
379 return ret;
380}
381#else /* ifdef _WIN32 */
382int
383main(int argc, char *argv[])
384{
385 return openvpn_main(argc, argv);
386}
387#endif /* ifdef _WIN32 */
static void gc_init(struct gc_arena *a)
Definition buffer.h:1004
static void gc_reset(struct gc_arena *a)
Definition buffer.h:1038
void crypto_unload_provider(const char *provname, provider_t *provider)
Unloads the given (OpenSSL) provider.
provider_t * crypto_load_provider(const char *provider)
Load the given (OpenSSL) providers.
void env_set_destroy(struct env_set *es)
Definition env_set.c:166
struct env_set * env_set_create(struct gc_arena *gc)
Definition env_set.c:156
#define M_INFO
Definition errlevel.h:54
#define ES_TIMEOUT
Definition event.h:67
void process_io(struct context *c, struct link_socket *sock)
Definition forward.c:2298
void pre_select(struct context *c)
Definition forward.c:1977
void io_wait(struct context *c, const unsigned int flags)
Definition forward.c:2175
Interface functions to the internal and external multiplexers.
static unsigned int p2p_iow_flags(const struct context *c)
Definition forward.h:368
static int openvpn_main(int argc, char *argv[])
OpenVPN's main init-run-cleanup loop.
Definition openvpn.c:153
void tunnel_server(struct context *top)
Main event loop for OpenVPN in server mode.
Definition multi.c:4207
static void tunnel_point_to_point(struct context *c)
Main event loop for OpenVPN in client mode, where only one VPN tunnel is active.
Definition openvpn.c:57
void uninit_management_callback(void)
Definition init.c:4414
bool open_management(struct context *c)
Definition init.c:4361
bool do_genkey(const struct options *options)
Definition init.c:1004
void open_plugins(struct context *c, const bool import_options, int init_point)
Definition init.c:4198
void init_verb_mute(struct context *c, unsigned int flags)
Definition init.c:932
void close_instance(struct context *c)
Definition init.c:4748
void persist_client_stats(struct context *c)
Definition init.c:4425
void init_plugins(struct context *c)
Definition init.c:4188
void context_init_1(struct context *c)
Definition init.c:739
void do_test_crypto(struct context *c)
Definition init.c:5035
void pre_setup(const struct options *options)
Definition init.c:1273
void init_management(void)
Definition init.c:4352
void uninit_static(void)
Definition init.c:914
void init_instance_handle_signals(struct context *c, const struct env_set *env, const unsigned int flags)
Definition init.c:4726
void write_pid_file(const char *filename, const char *chroot_dir)
Definition init.c:4987
void context_gc_free(struct context *c)
Definition init.c:790
void init_options_dev(struct options *options)
Definition init.c:955
void init_query_passwords(const struct context *c)
Query for private key and auth-user-pass username/passwords.
Definition init.c:644
void context_clear_2(struct context *c)
Definition init.c:87
bool print_openssl_info(const struct options *options)
Definition init.c:966
bool do_persist_tuntap(struct options *options, openvpn_net_ctx_t *ctx)
Definition init.c:1085
bool init_static(void)
Definition init.c:828
bool possibly_become_daemon(const struct options *options)
Definition init.c:1144
void context_clear_all_except_first_time(struct context *c)
Definition init.c:93
void close_management(void)
Definition init.c:4401
#define IVM_LEVEL_1
Definition init.h:44
#define CC_HARD_USR1_TO_HUP
Definition init.h:111
#define MF_QUERY_PASSWORDS
Definition manage.h:28
Header file for server-mode related structures and functions.
static int net_ctx_init(struct context *c, openvpn_net_ctx_t *ctx)
Definition networking.h:47
static void net_ctx_free(openvpn_net_ctx_t *ctx)
Definition networking.h:62
int main(void)
#define CLEAR(x)
Definition basic.h:32
void openvpn_exit(const int status)
Definition error.c:700
#define M_USAGE
Definition error.h:107
#define OPENVPN_EXIT_STATUS_GOOD
Definition error.h:52
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:219
static void uninit_early(struct context *c)
Definition openvpn.c:122
int wmain(int argc, wchar_t *wargv[])
Definition openvpn.c:353
static bool process_signal_p2p(struct context *c)
Definition openvpn.c:41
#define P2P_CHECK_SIG()
Definition openvpn.c:38
void init_early(struct context *c)
Definition openvpn.c:105
#define CM_P2P
Definition openvpn.h:479
void uninit_options(struct options *o)
Definition options.c:930
void show_windows_version(const unsigned int flags)
Definition options.c:4875
void show_dco_version(const unsigned int flags)
Definition options.c:4884
void show_settings(const struct options *o)
Definition options.c:1688
void options_postprocess(struct options *options, struct env_set *es)
Definition options.c:4278
void show_library_versions(const unsigned int flags)
Definition options.c:4894
void setenv_settings(struct env_set *es, const struct options *o)
Definition options.c:1009
void init_options(struct options *o, const bool init_gc)
Definition options.c:802
const char title_string[]
Definition options.c:72
#define MODE_POINT_TO_POINT
Definition options.h:263
void parse_argv(struct options *options, const int argc, char *argv[], const msglvl_t msglevel, const unsigned int permission_mask, unsigned int *option_types_found, struct env_set *es)
#define MODE_SERVER
Definition options.h:264
#define OPT_P_DEFAULT
Definition options.h:764
#define MAX_PARMS
Definition options.h:51
bool process_signal(struct context *c)
Definition sig.c:636
void signal_restart_status(const struct signal_info *si)
Definition sig.c:345
int signal_reset(struct signal_info *si, int signum)
Clear the signal if its current value equals signum.
Definition sig.c:262
void pre_init_signal_catch(void)
Definition sig.c:392
void remap_signal(struct context *c)
Definition sig.c:588
struct signal_info siginfo_static
Definition sig.c:44
void print_signal(const struct signal_info *si, const char *title, msglvl_t msglevel)
Definition sig.c:290
#define IS_SIG(c)
Definition sig.h:47
Definition argv.h:35
unsigned int event_set_status
Definition openvpn.h:235
struct link_socket ** link_sockets
Definition openvpn.h:237
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
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 context_2 c2
Level 2 context.
Definition openvpn.h:514
struct env_set * es
Set of environment variables.
Definition openvpn.h:496
struct options options
Options loaded from command line or configuration file.
Definition openvpn.h:472
struct gc_arena gc
Garbage collection arena for allocations done in the scope of this context structure.
Definition openvpn.h:492
struct provider_list providers
Definition options.h:581
bool test_crypto
Definition options.h:586
const char * writepid
Definition options.h:383
unsigned int management_flags
Definition options.h:458
int mode
Definition options.h:265
const char * config
Definition options.h:260
const char * chroot_dir
Definition options.h:378
provider_t * providers[MAX_PARMS]
Definition options.h:219
const char * names[MAX_PARMS]
Definition options.h:217
#define SIGHUP
Definition syshead.h:55
#define SIGUSR1
Definition syshead.h:57
void set_win_sys_path_via_env(struct env_set *es)
Definition win32.c:1124