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
8
* Copyright (C) 2002-2004 James Yonan <jim@yonan.net>
10
* This program is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation; either version 2 of the License, or
13
* (at your option) any later version.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this program (see the file COPYING included with this
22
* distribution); if not, write to the Free Software Foundation, Inc.,
23
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27
#include "config-win32.h"
60
* This random string identifies an OpenVPN ping packet.
61
* It should be of sufficient length and randomness
62
* so as not to collide with other tunnel data.
64
static const uint8_t ping_string[] = {
65
0x2a, 0x18, 0x7b, 0xf3, 0x64, 0x1e, 0xb4, 0xcb,
66
0x07, 0xed, 0x2d, 0x0a, 0x98, 0x1f, 0xc7, 0x48
70
* This random string identifies an OpenVPN
71
* options consistency check packet.
72
* It should be of sufficient length and randomness
73
* so as not to collide with other tunnel data.
75
* The OCC protocol is as follows:
77
* occ_magic -- (16 octets)
79
* type [OCC_REQUEST | OCC_REPLY] (1 octet)
80
* null terminated options string if OCC_REPLY (variable)
82
* When encryption is used, the OCC packet
83
* is encapsulated within the encrypted
87
static const uint8_t occ_magic[] = {
88
0x28, 0x7f, 0x34, 0x6b, 0xd4, 0xef, 0x7a, 0x81,
89
0x2d, 0x56, 0xb8, 0xd3, 0xaf, 0xc5, 0x45, 0x9c
93
* OCC protocol opcodes used for options consistency checks.
96
#define OCC_REQUEST 0 /* request options string from peer */
97
#define OCC_REPLY 1 /* deliver options string to peer */
100
* Send an OCC_REQUEST once every OCC_INTERVAL
101
* seconds until a reply is received.
103
* If we haven't received a reply after
104
* OCC_N_TRIES, give up.
106
#define OCC_INTERVAL_SECONDS 10
107
#define OCC_N_TRIES 12
110
* Other OCC protocol opcodes used to estimate the MTU empirically.
112
#define OCC_MTU_LOAD_REQUEST 2 /* Ask peer to send a big packet to us */
113
#define OCC_MTU_LOAD 3 /* Send a big packet to peer */
114
#define OCC_MTU_REQUEST 4 /* Ask peer to tell us the largest
115
packet it has received from us so far */
116
#define OCC_MTU_REPLY 5 /* Send largest packet size to peer */
119
* Process one command from mtu_load_test_sequence
120
* once every n seconds, if --mtu-test is specified.
122
#define OCC_MTU_LOAD_INTERVAL_SECONDS 3
125
* Used to conduct a load test command sequence
126
* of UDP connection for empirical MTU measurement.
130
int op; /* OCC opcode to send to peer */
131
int delta; /* determine packet size to send by using
132
this delta against currently
136
static const struct mtu_load_test mtu_load_test_sequence[] = {
138
{ OCC_MTU_LOAD_REQUEST, -1000 },
139
{ OCC_MTU_LOAD, -1000 },
140
{ OCC_MTU_LOAD_REQUEST, -1000 },
141
{ OCC_MTU_LOAD, -1000 },
142
{ OCC_MTU_LOAD_REQUEST, -1000 },
143
{ OCC_MTU_LOAD, -1000 },
145
{ OCC_MTU_LOAD_REQUEST, -500 },
146
{ OCC_MTU_LOAD, -500 },
147
{ OCC_MTU_LOAD_REQUEST, -500 },
148
{ OCC_MTU_LOAD, -500 },
149
{ OCC_MTU_LOAD_REQUEST, -500 },
150
{ OCC_MTU_LOAD, -500 },
152
{ OCC_MTU_LOAD_REQUEST, -750 },
153
{ OCC_MTU_LOAD, -750 },
154
{ OCC_MTU_LOAD_REQUEST, -750 },
155
{ OCC_MTU_LOAD, -750 },
156
{ OCC_MTU_LOAD_REQUEST, -750 },
157
{ OCC_MTU_LOAD, -750 },
159
{ OCC_MTU_LOAD_REQUEST, -400 },
160
{ OCC_MTU_LOAD, -400 },
161
{ OCC_MTU_LOAD_REQUEST, -400 },
162
{ OCC_MTU_LOAD, -400 },
163
{ OCC_MTU_LOAD_REQUEST, -400 },
164
{ OCC_MTU_LOAD, -400 },
166
{ OCC_MTU_LOAD_REQUEST, -300 },
167
{ OCC_MTU_LOAD, -300 },
168
{ OCC_MTU_LOAD_REQUEST, -300 },
169
{ OCC_MTU_LOAD, -300 },
170
{ OCC_MTU_LOAD_REQUEST, -300 },
171
{ OCC_MTU_LOAD, -300 },
173
{ OCC_MTU_LOAD_REQUEST, -200 },
174
{ OCC_MTU_LOAD, -200 },
175
{ OCC_MTU_LOAD_REQUEST, -200 },
176
{ OCC_MTU_LOAD, -200 },
177
{ OCC_MTU_LOAD_REQUEST, -200 },
178
{ OCC_MTU_LOAD, -200 },
180
{ OCC_MTU_LOAD_REQUEST, -150 },
181
{ OCC_MTU_LOAD, -150 },
182
{ OCC_MTU_LOAD_REQUEST, -150 },
183
{ OCC_MTU_LOAD, -150 },
184
{ OCC_MTU_LOAD_REQUEST, -150 },
185
{ OCC_MTU_LOAD, -150 },
187
{ OCC_MTU_LOAD_REQUEST, -100 },
188
{ OCC_MTU_LOAD, -100 },
189
{ OCC_MTU_LOAD_REQUEST, -100 },
190
{ OCC_MTU_LOAD, -100 },
191
{ OCC_MTU_LOAD_REQUEST, -100 },
192
{ OCC_MTU_LOAD, -100 },
194
{ OCC_MTU_LOAD_REQUEST, -50 },
195
{ OCC_MTU_LOAD, -50 },
196
{ OCC_MTU_LOAD_REQUEST, -50 },
197
{ OCC_MTU_LOAD, -50 },
198
{ OCC_MTU_LOAD_REQUEST, -50 },
199
{ OCC_MTU_LOAD, -50 },
201
{ OCC_MTU_LOAD_REQUEST, 0 },
203
{ OCC_MTU_LOAD_REQUEST, 0 },
205
{ OCC_MTU_LOAD_REQUEST, 0 },
208
{ OCC_MTU_REQUEST, 0 },
209
{ OCC_MTU_REQUEST, 0 },
210
{ OCC_MTU_REQUEST, 0 },
211
{ OCC_MTU_REQUEST, 0 },
212
{ OCC_MTU_REQUEST, 0 },
213
{ OCC_MTU_REQUEST, 0 },
214
{ OCC_MTU_REQUEST, 0 },
215
{ OCC_MTU_REQUEST, 0 },
216
{ OCC_MTU_REQUEST, 0 },
217
{ OCC_MTU_REQUEST, 0 },
223
* Should we become a daemon?
224
* level == 0 after parameters have been parsed but before any initialization
225
* level == 1 after initialization but before any SSL/TLS negotiation or
226
* tunnel data is forwarded
227
* first_time is true until first exit of openvpn() function
229
* Return true if we did it.
232
possibly_become_daemon (int level, const struct options* options, const bool first_time)
235
if (first_time && options->daemon)
237
ASSERT (!options->inetd);
238
if (level == DAEMONIZATION_LEVEL)
240
if (daemon (options->cd_dir != NULL, options->log) < 0)
241
msg (M_ERR, "daemon() failed");
249
* Initialize the route list, resolving any DNS names in route
250
* options and saving routes in the environment.
253
do_init_route_list (const struct options *options,
254
struct route_list *route_list,
255
struct link_socket *link_socket,
258
const char *gw = NULL;
259
int dev = dev_type_enum (options->dev, options->dev_type);
261
if (dev == DEV_TYPE_TUN)
262
gw = options->ifconfig_remote_netmask;
263
if (options->route_default_gateway)
264
gw = options->route_default_gateway;
266
if (!init_route_list (route_list,
269
link_socket_current_remote (link_socket)))
272
openvpn_exit (OPENVPN_EXIT_STATUS_ERROR); /* exit point */
276
/* copy routes to environment */
277
setenv_routes (route_list);
282
* Possibly add routes and/or call route-up script
286
do_route (const struct options* options,
287
struct route_list *route_list)
289
if (!options->route_noexec)
290
add_routes (route_list, false);
291
if (options->route_script)
293
setenv_str ("script_type", "route-up");
294
system_check (options->route_script, "Route script failed", false);
299
* Open tun/tap device, ifconfig, call up script, etc.
303
do_open_tun (const struct options *options,
305
struct link_socket *link_socket,
306
struct tuntap *tuntap,
307
struct route_list *route_list)
311
if (!tuntap_defined (tuntap))
313
/* parse and resolve the route option list */
314
do_init_route_list (options, route_list, link_socket, true);
317
if (!options->ifconfig_noexec
318
&& ifconfig_order() == IFCONFIG_BEFORE_TUN_OPEN)
320
/* guess actual tun/tap unit number that will be returned
322
const char *guess = guess_tuntap_dev (options->dev,
327
TUN_MTU_SIZE (frame));
330
/* open the tun device */
331
open_tun (options->dev, options->dev_type, options->dev_node,
332
options->tun_ipv6, tuntap);
335
if (!options->ifconfig_noexec
336
&& ifconfig_order() == IFCONFIG_AFTER_TUN_OPEN)
339
TUN_MTU_SIZE (frame));
341
/* run the up script */
342
run_script (options->up_script,
344
TUN_MTU_SIZE (frame),
345
EXPANDED_SIZE (frame),
346
print_in_addr_t (tuntap->local, true),
347
print_in_addr_t (tuntap->remote_netmask, true),
352
/* possibly add routes */
353
if (!options->route_delay_defined)
354
do_route (options, route_list);
357
* Did tun/tap driver give us an MTU?
359
if (tuntap->post_open_mtu)
360
frame_set_mtu_dynamic (
362
tuntap->post_open_mtu,
363
SET_MTU_TUN | SET_MTU_UPPER_BOUND);
366
* On Windows, it is usually wrong if --tun-mtu != 1500.
369
if (TUN_MTU_SIZE (frame) != 1500)
370
msg (M_WARN, "WARNING: in general you should use '--tun-mtu 1500 --mssfix 1400' on both sides of the connection if at least one side is running Windows, unless you have explicitly modified the TAP-Win32 driver properties");
376
msg (M_INFO, "Preserving previous TUN/TAP instance: %s", tuntap->actual);
378
/* run the up script if user specified --up-restart */
379
if (options->up_restart)
380
run_script (options->up_script,
382
TUN_MTU_SIZE (frame),
383
EXPANDED_SIZE (frame),
384
print_in_addr_t (tuntap->local, true),
385
print_in_addr_t (tuntap->remote_netmask, true),
394
* Depending on protocol, sleep before restart to prevent
398
socket_restart_pause (int proto, bool http_proxy, bool socks_proxy)
404
sec = socks_proxy ? 3 : 0;
406
case PROTO_TCPv4_SERVER:
409
case PROTO_TCPv4_CLIENT:
410
sec = (http_proxy || socks_proxy) ? 10 : 3;
415
msg (D_RESTART, "Restart pause, %d second(s)", sec);
422
static volatile int signal_received = 0;
425
signal_description (int signum, const char *sigtext)
449
print_signal (int signum)
454
msg (M_INFO, "SIGINT received, exiting");
457
msg (M_INFO, "SIGTERM received, exiting");
460
msg (M_INFO, "SIGHUP received, restarting");
463
msg (M_INFO, "SIGUSR1 received, restarting");
466
msg (M_INFO, "Unknown signal %d received", signal_received);
473
/* normal signal handler, when we are in event loop */
475
signal_handler (int signum)
477
signal_received = signum;
478
signal (signum, signal_handler);
481
/* temporary signal handler, before we are fully initialized */
483
signal_handler_exit (int signum)
485
msg (M_FATAL | M_NOLOCK,
486
"Signal %d (%s) received during initialization, exiting",
488
signal_description (signum, NULL));
491
#endif /* HAVE_SIGNAL_H */
494
* For debugging, dump a packet in
495
* nominally human-readable form.
497
#if defined(USE_CRYPTO) && defined(USE_SSL)
498
#define TLS_MODE (tls_multi != NULL)
499
#define PROTO_DUMP_FLAGS (check_debug_level (D_LINK_RW_VERBOSE) ? (PD_SHOW_DATA|PD_VERBOSE) : 0)
500
#define PROTO_DUMP(buf) protocol_dump(buf, \
502
(tls_multi ? PD_TLS : 0) | \
503
(options->tls_auth_file ? ks->key_type.hmac_length : 0) \
506
#define TLS_MODE (false)
507
#define PROTO_DUMP(buf) format_hex (BPTR (buf), BLEN (buf), 80)
511
#define MD5SUM(buf, len) md5sum(buf, len, 0)
513
#define MD5SUM(buf, len) "[unavailable]"
516
#if defined(USE_PTHREAD) && defined(USE_CRYPTO)
517
static void *test_crypto_thread (void *arg);
521
* Our global key schedules, packaged thusly
522
* to facilitate --persist-key.
528
/* which cipher, HMAC digest, and key sizes are we using? */
529
struct key_type key_type;
531
/* pre-shared static key, read from a file */
532
struct key_ctx_bi static_key;
535
/* our global SSL context */
538
/* optional authentication HMAC key for TLS control channel */
539
struct key_ctx_bi tls_auth_key;
542
#else /* USE_CRYPTO */
544
#endif /* USE_CRYPTO */
548
key_schedule_free(struct key_schedule* ks)
551
free_key_ctx_bi (&ks->static_key);
554
SSL_CTX_free (ks->ssl_ctx);
555
free_key_ctx_bi (&ks->tls_auth_key);
557
#endif /* USE_CRYPTO */
562
* struct packet_id_persist should be empty if we are not
563
* building with crypto.
566
struct packet_id_persist { int dummy; };
567
static inline void packet_id_persist_init (struct packet_id_persist *p) {}
571
* Finalize MTU parameters based on command line or config file options.
574
frame_finalize_options (struct frame *frame, const struct options *options)
577
frame_finalize (frame,
578
options->link_mtu_defined,
580
options->tun_mtu_defined,
585
* Do the work. Initialize and enter main event loop.
586
* Called after command line has been parsed.
588
* first_time is true during our first call -- we may
589
* be called multiple times due to SIGHUP or SIGUSR1.
592
openvpn (const struct options *options,
593
struct link_socket_addr *link_socket_addr,
594
struct tuntap *tuntap,
595
struct key_schedule *ks,
596
struct packet_id_persist *pid_persist,
597
struct route_list *route_list,
598
struct http_proxy_info *http_proxy,
599
struct socks_proxy_info *socks_proxy,
603
* Initialize garbage collection level.
604
* When we pop the level at the end
605
* of the routine, everything we
606
* allocated with gc_malloc at our level
607
* or recursively lower levels will
608
* automatically be freed.
610
const int gc_level = gc_new_level ();
612
/* our global wait event */
613
struct event_wait event_wait;
615
#if PASSTOS_CAPABILITY
616
/* used to get/set TOS. */
618
bool ptos_defined = false;
621
/* declare various buffers */
622
struct buffer to_tun = clear_buf ();
623
struct buffer to_link = clear_buf ();
624
struct buffer buf = clear_buf ();
625
struct buffer aux_buf = clear_buf ();
626
struct buffer nullbuf = clear_buf ();
628
/* tells us to free to_link buffer after it has been written to TCP/UDP port */
629
bool free_to_link = false;
631
struct link_socket link_socket; /* socket used for TCP/UDP connection to remote */
632
struct sockaddr_in to_link_addr; /* IP address of remote */
634
/* MTU frame parameters */
637
#ifdef FRAGMENT_ENABLE
638
/* Object to handle advanced MTU negotiation and datagram fragmentation */
639
struct fragment_master *fragment = NULL;
640
struct frame frame_fragment;
641
struct frame frame_fragment_omit;
644
/* Always set to current time. */
647
#ifdef HAVE_GETTIMEOFDAY
649
* Traffic shaper object.
651
struct shaper shaper;
657
counter_type tun_read_bytes = 0;
658
counter_type tun_write_bytes = 0;
659
counter_type link_read_bytes = 0;
660
counter_type link_read_bytes_auth = 0;
661
counter_type link_write_bytes = 0;
664
* Timer objects for ping and inactivity
667
struct event_timeout wait_for_connect = event_timeout_clear_ret ();
668
struct event_timeout inactivity_interval = event_timeout_clear_ret ();
669
struct event_timeout ping_send_interval = event_timeout_clear_ret ();
670
struct event_timeout ping_rec_interval = event_timeout_clear_ret ();
672
/* the option strings must match across peers */
673
char *options_string_local = NULL;
674
char *options_string_remote = NULL;
676
int occ_op = -1; /* OCC request code received from remote */
678
struct event_timeout occ_interval = event_timeout_clear_ret ();
681
* Keep track of maximum packet size received so far
682
* (of authenticated packets).
684
int original_recv_size = 0; /* temporary */
685
int max_recv_size_local = 0; /* max packet size received */
686
int max_recv_size_remote = 0; /* max packet size received by remote */
687
int max_send_size_local = 0; /* max packet size sent */
688
int max_send_size_remote = 0; /* max packet size sent by remote */
690
/* remote wants us to send back a load test packet of this size */
691
int occ_mtu_load_size = 0;
693
struct event_timeout occ_mtu_load_test_interval = event_timeout_clear_ret ();
694
int occ_mtu_load_n_tries = 0;
699
* TLS-mode crypto objects.
703
/* master OpenVPN SSL/TLS object */
704
struct tls_multi *tls_multi = NULL;
708
/* object containing TLS thread state */
709
struct thread_parms thread_parms;
711
/* object sent to us by TLS thread */
712
struct tt_ret tt_ret;
714
/* did we open TLS thread? */
715
bool thread_opened = false;
719
/* used to optimize calls to tls_multi_process
720
in single-threaded mode */
721
struct interval tmp_int;
726
/* workspace buffers used by crypto routines */
727
struct buffer encrypt_buf = clear_buf ();
728
struct buffer decrypt_buf = clear_buf ();
730
/* passed to encrypt or decrypt, contains all
731
crypto-related command line options related
732
to data channel encryption/decryption */
733
struct crypto_options crypto_options;
735
/* used to keep track of data channel packet sequence numbers */
736
struct packet_id packet_id;
740
* LZO compression library objects.
743
struct buffer lzo_compress_buf = clear_buf ();
744
struct buffer lzo_decompress_buf = clear_buf ();
745
struct lzo_compress_workspace lzo_compwork;
749
* Buffers used to read from TUN device
752
struct buffer read_link_buf = clear_buf ();
753
struct buffer read_tun_buf = clear_buf ();
758
bool ipv4_tun = (!options->tun_ipv6 && is_dev_type (options->dev, options->dev_type, "tun"));
760
/* workspace for get_pid_file/write_pid */
761
struct pid_state pid_state;
763
/* workspace for --user/--group */
764
struct user_state user_state;
765
struct group_state group_state;
767
/* temporary variable */
768
bool did_we_daemonize = false;
770
/* description of signal */
771
const char *signal_text = NULL;
774
/* should we print R|W|r|w to console on packet transfers? */
775
const bool log_rw = (check_debug_level (D_LOG_RW) && !check_debug_level (D_LOG_RW + 1));
779
struct event_timeout route_wakeup = event_timeout_clear_ret ();
781
/* did we open tun/tap dev during this cycle? */
782
bool did_open_tun = false;
788
* Special handling if signal arrives before
789
* we are properly initialized.
791
signal (SIGINT, signal_handler_exit);
792
signal (SIGTERM, signal_handler_exit);
793
signal (SIGHUP, signal_handler_exit);
794
signal (SIGUSR1, signal_handler_exit);
795
signal (SIGUSR2, signal_handler_exit);
796
signal (SIGPIPE, SIG_IGN);
797
#endif /* HAVE_SIGNAL_H */
800
socket_restart_pause (options->proto, options->http_proxy_server != NULL,
801
options->socks_proxy_server != NULL);
803
wait_init (&event_wait);
804
link_socket_reset (&link_socket);
808
#ifdef FRAGMENT_ENABLE
809
CLEAR (frame_fragment_omit);
812
/* should we disable paging? */
813
if (first_time && options->mlock)
817
* Initialize advanced MTU negotiation and datagram fragmentation
819
#ifdef FRAGMENT_ENABLE
820
if (options->fragment)
821
fragment = fragment_init (&frame);
825
/* init PRNG used for IV generation */
828
/* load a persisted packet-id for cross-session replay-protection */
829
if (options->packet_id_file)
830
packet_id_persist_load (pid_persist, options->packet_id_file);
832
/* Initialize crypto options */
834
CLEAR (crypto_options);
836
crypto_options.use_iv = options->use_iv;
838
if (options->shared_secret_file)
841
* Static Key Mode (using a pre-shared key)
844
/* Initialize packet ID tracking */
847
packet_id_init (&packet_id, options->replay_window, options->replay_time);
848
crypto_options.packet_id = &packet_id;
849
crypto_options.pid_persist = pid_persist;
850
crypto_options.packet_id_long_form = true;
851
packet_id_persist_load_obj (pid_persist, crypto_options.packet_id);
854
if (!key_ctx_bi_defined (&ks->static_key))
857
struct key_direction_state kds;
859
/* Get cipher & hash algorithms */
860
init_key_type (&ks->key_type, options->ciphername,
861
options->ciphername_defined, options->authname,
862
options->authname_defined, options->keysize,
863
options->test_crypto, true);
865
/* Read cipher and hmac keys from shared secret file */
866
read_key_file (&key2, options->shared_secret_file, true);
868
/* Check for and fix highly unlikely key problems */
869
verify_fix_key2 (&key2, &ks->key_type, options->shared_secret_file);
871
/* Initialize OpenSSL key objects */
872
key_direction_state_init (&kds, options->key_direction);
873
must_have_n_keys (options->shared_secret_file, "secret", &key2, kds.need_keys);
874
init_key_ctx (&ks->static_key.encrypt, &key2.keys[kds.out_key], &ks->key_type, DO_ENCRYPT, "Static Encrypt");
875
init_key_ctx (&ks->static_key.decrypt, &key2.keys[kds.in_key], &ks->key_type, DO_DECRYPT, "Static Decrypt");
877
/* Erase the temporary copy of key */
882
msg (M_INFO, "Re-using pre-shared static key");
885
/* Get key schedule */
886
crypto_options.key_ctx_bi = &ks->static_key;
888
/* Compute MTU parameters */
889
crypto_adjust_frame_parameters(&frame,
891
options->ciphername_defined,
896
/* Sanity check on IV, sequence number, and cipher mode options */
897
check_replay_iv_consistency(&ks->key_type, options->replay, options->use_iv);
900
* Test-crypto is a debugging tool
901
* that basically does a loopback test
902
* on the crypto subsystem.
904
if (options->test_crypto)
910
work_thread_create(test_crypto_thread, (void*) options);
913
frame_finalize_options (&frame, options);
915
test_crypto (&crypto_options, &frame);
916
key_schedule_free (ks);
926
else if (options->tls_server || options->tls_client)
929
* TLS-based dynamic key exchange mode
931
struct tls_options to;
932
bool packet_id_long_form;
934
ASSERT (!options->test_crypto);
936
/* Make sure we are either a TLS client or server but not both */
937
ASSERT (options->tls_server == !options->tls_client);
939
/* Let user specify a script to verify the incoming certificate */
940
tls_set_verify_command (options->tls_verify);
942
/* Verify the X509 name of the incoming host */
943
tls_set_verify_x509name (options->tls_remote);
945
/* Let user specify a certificate revocation list to
946
check the incoming certificate */
947
tls_set_crl_verify (options->crl_file);
952
* Initialize the OpenSSL library's global
955
ks->ssl_ctx = init_ssl (options->tls_server,
959
options->priv_key_file,
960
options->cipher_list);
962
/* Get cipher & hash algorithms */
963
init_key_type (&ks->key_type, options->ciphername,
964
options->ciphername_defined, options->authname,
965
options->authname_defined, options->keysize,
968
/* TLS handshake authentication (--tls-auth) */
969
if (options->tls_auth_file)
970
get_tls_handshake_key (&ks->key_type,
972
options->tls_auth_file,
973
options->key_direction);
977
msg (M_INFO, "Re-using SSL/TLS context");
980
/* Sanity check on IV, sequence number, and cipher mode options */
981
check_replay_iv_consistency(&ks->key_type, options->replay, options->use_iv);
983
/* In short form, unique datagram identifier is 32 bits, in long form 64 bits */
984
packet_id_long_form = cfb_ofb_mode (&ks->key_type);
986
/* Compute MTU parameters */
987
crypto_adjust_frame_parameters(&frame,
989
options->ciphername_defined,
992
packet_id_long_form);
993
tls_adjust_frame_parameters(&frame);
995
/* Set all command-line TLS-related options */
997
to.ssl_ctx = ks->ssl_ctx;
998
to.key_type = ks->key_type;
999
to.server = options->tls_server;
1000
to.key_method = options->key_method;
1001
to.replay = options->replay;
1002
to.packet_id_long_form = packet_id_long_form;
1003
to.replay_window = options->replay_window;
1004
to.replay_time = options->replay_time;
1005
to.transition_window = options->transition_window;
1006
to.handshake_window = options->handshake_window;
1007
to.packet_timeout = options->tls_timeout;
1008
to.renegotiate_bytes = options->renegotiate_bytes;
1009
to.renegotiate_packets = options->renegotiate_packets;
1010
to.renegotiate_seconds = options->renegotiate_seconds;
1011
to.single_session = options->single_session;
1012
to.disable_occ = !options->occ;
1014
/* TLS handshake authentication (--tls-auth) */
1015
if (options->tls_auth_file)
1017
to.tls_auth_key = ks->tls_auth_key;
1018
to.tls_auth.pid_persist = pid_persist;
1019
to.tls_auth.packet_id_long_form = true;
1020
crypto_adjust_frame_parameters(&to.frame,
1028
/* If we are running over TCP, allow for
1030
socket_adjust_frame_parameters (&to.frame, options->proto);
1033
* Initialize OpenVPN's master TLS-mode object.
1035
tls_multi = tls_multi_init (&to);
1041
* No encryption or authentication.
1043
ASSERT (!options->test_crypto);
1044
free_key_ctx_bi (&ks->static_key);
1045
crypto_options.key_ctx_bi = &ks->static_key;
1047
"******* WARNING *******: all encryption and authentication features disabled -- all data will be tunnelled as cleartext");
1050
#else /* USE_CRYPTO */
1053
"******* WARNING *******: " PACKAGE_NAME " built without OpenSSL -- encryption and authentication features disabled -- all data will be tunnelled as cleartext");
1055
#endif /* USE_CRYPTO */
1059
* Initialize LZO compression library.
1061
if (options->comp_lzo)
1063
lzo_compress_init (&lzo_compwork, options->comp_lzo_adaptive);
1064
lzo_adjust_frame_parameters (&frame);
1065
#ifdef FRAGMENT_ENABLE
1066
lzo_adjust_frame_parameters (&frame_fragment_omit); /* omit LZO frame delta from final frame_fragment */
1072
* Adjust frame size for UDP Socks support.
1074
if (options->socks_proxy_server)
1075
socks_adjust_frame_parameters (&frame, options->proto);
1078
* Adjust frame size based on the --tun-mtu-extra parameter.
1080
if (options->tun_mtu_extra_defined)
1081
tun_adjust_frame_parameters (&frame, options->tun_mtu_extra);
1084
* Adjust frame size based on link socket parameters.
1085
* (Since TCP is a stream protocol, we need to insert
1086
* a packet length uint16_t in the buffer.)
1088
socket_adjust_frame_parameters (&frame, options->proto);
1091
* Fill in the blanks in the frame parameters structure,
1092
* make sure values are rational, etc.
1094
frame_finalize_options (&frame, options);
1097
* Set frame parameter for fragment code. This is necessary because
1098
* the fragmentation code deals with payloads which have already been
1099
* passed through the compression code.
1101
#ifdef FRAGMENT_ENABLE
1102
frame_fragment = frame;
1103
frame_subtract_extra (&frame_fragment, &frame_fragment_omit);
1106
#if defined(USE_CRYPTO) && defined(USE_SSL)
1109
tls_multi_init_finalize (tls_multi, &frame);
1110
ASSERT (EXPANDED_SIZE (&tls_multi->opt.frame) <= EXPANDED_SIZE (&frame));
1111
frame_print (&tls_multi->opt.frame, D_MTU_INFO, "Control Channel MTU parms");
1116
* Now that we know all frame parameters, initialize
1120
read_link_buf = alloc_buf (BUF_SIZE (&frame));
1121
read_tun_buf = alloc_buf (BUF_SIZE (&frame));
1122
aux_buf = alloc_buf (BUF_SIZE (&frame));
1125
encrypt_buf = alloc_buf (BUF_SIZE (&frame));
1126
decrypt_buf = alloc_buf (BUF_SIZE (&frame));
1130
if (options->comp_lzo)
1132
lzo_compress_buf = alloc_buf (BUF_SIZE (&frame));
1133
lzo_decompress_buf = alloc_buf (BUF_SIZE (&frame));
1137
#ifdef FRAGMENT_ENABLE
1138
/* fragmenting code has buffers to initialize
1139
once frame parameters are known */
1142
ASSERT (options->fragment);
1143
frame_set_mtu_dynamic (
1148
fragment_frame_init (fragment, &frame_fragment);
1153
* Set the dynamic MTU parameter, used by the --mssfix
1154
* option. If --mssfix is supplied without a parameter,
1155
* then default to --fragment size. Otherwise default
1156
* to udp_mtu or (on Windows) TAP-Win32 mtu size which
1157
* is set in the adapter advanced properties dialog.
1159
if (options->mssfix_defined)
1161
if (options->mssfix)
1163
frame_set_mtu_dynamic (
1169
#ifdef FRAGMENT_ENABLE
1172
frame_set_mtu_dynamic (
1174
EXPANDED_SIZE_DYNAMIC (&frame_fragment),
1181
#ifdef FRAGMENT_ENABLE
1183
if (options->fragment && options->mtu_test)
1184
msg (M_WARN, "WARNING: using --fragment and --mtu-test together may produce an inaccurate MTU test result");
1186
if ((options->mssfix || options->fragment) && TUN_MTU_SIZE (&frame_fragment) != ETHERNET_MTU)
1187
msg (M_WARN, "WARNING: normally if you use --mssfix and/or --fragment, you should also set --tun-mtu %d (currently it is %d)",
1189
TUN_MTU_SIZE (&frame_fragment));
1193
/* bind the TCP/UDP socket */
1195
link_socket_init_phase1 (&link_socket,
1196
options->local, options->remote,
1197
options->local_port, options->remote_port,
1199
http_proxy->defined ? http_proxy : NULL,
1200
socks_proxy->defined ? socks_proxy : NULL,
1201
options->bind_local,
1202
options->remote_float,
1206
options->resolve_retry_seconds,
1207
options->connect_retry_seconds,
1208
options->mtu_discover_type);
1210
/* initialize tun/tap device object */
1215
options->ifconfig_local,
1216
options->ifconfig_remote_netmask,
1217
addr_host (&link_socket.lsa->local),
1218
addr_host (&link_socket.lsa->remote),
1220
&options->tuntap_options);
1222
/* open tun/tap device, ifconfig, run up script, etc. */
1224
if (!options->up_delay)
1225
did_open_tun = do_open_tun (options, &frame, &link_socket, tuntap, route_list);
1230
frame_print (&frame, D_MTU_INFO, "Data Channel MTU parms");
1231
#ifdef FRAGMENT_ENABLE
1233
frame_print (&frame_fragment, D_MTU_INFO, "Fragmentation MTU parms");
1237
* Get local and remote options compatibility strings.
1239
options_string_local = options_string (options, &frame, tuntap, false);
1240
options_string_remote = options_string (options, &frame, tuntap, true);
1242
msg (D_SHOW_OCC, "Local Options String: '%s'", options_string_local);
1243
msg (D_SHOW_OCC, "Expected Remote Options String: '%s'", options_string_remote);
1246
msg (D_SHOW_OCC_HASH, "Local Options hash (VER=%s): '%s'",
1247
options_string_version (options_string_local),
1248
md5sum (options_string_local, strlen(options_string_local), 9));
1249
msg (D_SHOW_OCC_HASH, "Expected Remote Options hash (VER=%s): '%s'",
1250
options_string_version (options_string_remote),
1251
md5sum (options_string_remote, strlen (options_string_remote), 9));
1254
#if defined(USE_CRYPTO) && defined(USE_SSL)
1256
tls_multi_init_set_options(tls_multi,
1257
options_string_local,
1258
options_string_remote);
1261
#ifdef HAVE_GETTIMEOFDAY
1262
/* initialize traffic shaper (i.e. transmit bandwidth limiter) */
1263
if (options->shaper)
1265
shaper_init (&shaper, options->shaper);
1266
shaper_msg (&shaper);
1272
/* get user and/or group that we want to setuid/setgid to */
1273
get_group (options->groupname, &group_state);
1274
get_user (options->username, &user_state);
1276
/* get --writepid file descriptor */
1277
get_pid_file (options->writepid, &pid_state);
1279
/* chroot if requested */
1280
if (options->chroot_dir)
1283
/* not needed because gethostbyname is now called in
1284
link_socket_init_phase1 even if --resolv-retry is also specified. */
1286
/* do a dummy DNS lookup before entering the chroot jail
1287
to load the resolver libraries */
1288
if (options->remote)
1289
(void) gethostbyname (options->remote);
1291
do_chroot (options->chroot_dir);
1295
/* become a daemon if --daemon */
1296
did_we_daemonize = possibly_become_daemon (1, options, first_time);
1298
#ifdef HAVE_SIGNAL_H
1300
signal (SIGINT, signal_handler);
1301
signal (SIGTERM, signal_handler);
1302
signal (SIGHUP, signal_handler);
1303
signal (SIGUSR1, signal_handler);
1304
signal (SIGUSR2, signal_handler);
1305
#endif /* HAVE_SIGNAL_H */
1309
/* should we disable paging? */
1310
if (options->mlock && did_we_daemonize)
1311
do_mlockall (true); /* call again in case we daemonized */
1313
/* should we change scheduling priority? */
1314
set_nice (options->nice);
1316
/* set user and/or group that we want to setuid/setgid to */
1317
set_group (&group_state);
1318
set_user (&user_state);
1320
/* save process ID in a file */
1321
write_pid (&pid_state);
1323
/* initialize threading if pthread configure option enabled */
1327
/* finalize the TCP/UDP socket */
1328
link_socket_init_phase2 (&link_socket, &frame, &signal_received);
1329
if (signal_received)
1331
signal_text = "socket";
1332
print_signal (signal_received);
1336
/* start the TLS thread */
1337
#if defined(USE_CRYPTO) && defined(USE_SSL) && defined(USE_PTHREAD)
1340
tls_thread_create (&thread_parms, tls_multi, &link_socket,
1341
options->nice_work, options->mlock);
1342
thread_opened = true;
1349
* Pipe TCP/UDP -> tun and tun -> TCP/UDP using nonblocked i/o.
1351
* If tls_multi is defined, multiplex a TLS
1352
* control channel over the TCP/UDP connection which
1353
* will be used for secure key exchange with our peer.
1357
/* select wants maximum fd + 1 (why doesn't it just figure it out for itself?) */
1358
SOCKET_SETMAXFD(link_socket);
1359
TUNTAP_SETMAXFD(tuntap);
1361
current = time (NULL);
1363
/* initialize connection establishment timer */
1364
event_timeout_init (&wait_for_connect, current, 5);
1366
/* initialize inactivity timeout */
1367
if (options->inactivity_timeout)
1368
event_timeout_init (&inactivity_interval, current, options->inactivity_timeout);
1370
/* initialize pings */
1372
if (options->ping_send_timeout)
1373
event_timeout_init (&ping_send_interval, 0, options->ping_send_timeout);
1375
if (options->ping_rec_timeout)
1376
event_timeout_init (&ping_rec_interval, current, options->ping_rec_timeout);
1378
/* initialize occ timers */
1382
&& options_string_local
1383
&& options_string_remote)
1384
event_timeout_init (&occ_interval, current, OCC_INTERVAL_SECONDS);
1386
if (options->mtu_test)
1387
event_timeout_init (&occ_mtu_load_test_interval, current, OCC_MTU_LOAD_INTERVAL_SECONDS);
1389
#if defined(USE_CRYPTO) && defined(USE_SSL)
1391
TLS_THREAD_SOCKET_SETMAXFD (thread_parms);
1393
/* initialize tmp_int optimization that limits the number of times we call
1394
tls_multi_process in the main event loop */
1395
interval_init (&tmp_int, TLS_MULTI_HORIZON, TLS_MULTI_REFRESH);
1399
/* this flag is true for buffers coming from the TLS background thread */
1400
free_to_link = false;
1405
struct timeval *tv = NULL;
1406
struct timeval timeval;
1410
/* initialize select() timeout */
1411
timeval.tv_sec = BIG_TIMEOUT;
1412
timeval.tv_usec = 0;
1415
#if defined(WIN32) && defined(TAP_WIN32_DEBUG)
1417
if (check_debug_level (D_TAP_WIN32_DEBUG))
1418
tun_show_debug (tuntap);
1422
/* flush current packet-id to file once per 60
1423
seconds if --replay-persist was specified */
1424
packet_id_persist_flush (pid_persist, current, 60);
1427
#if defined(USE_CRYPTO) && defined(USE_SSL) && !defined(USE_PTHREAD)
1429
* In TLS mode, let TLS level respond to any control-channel
1430
* packets which were received, or prepare any packets for
1433
* tmp_int is purely an optimization that allows us to call
1434
* tls_multi_process less frequently when there's not much
1435
* traffic on the control-channel.
1440
interval_t wakeup = BIG_TIMEOUT;
1442
if (interval_test (&tmp_int, current))
1444
if (tls_multi_process (tls_multi, &to_link, &to_link_addr,
1445
&link_socket, &wakeup, current))
1446
interval_action (&tmp_int, current);
1448
interval_future_trigger (&tmp_int, wakeup, current);
1449
free_to_link = false;
1452
interval_schedule_wakeup (&tmp_int, current, &wakeup);
1456
timeval.tv_sec = wakeup;
1457
timeval.tv_usec = 0;
1462
current = time (NULL);
1464
#if defined(USE_CRYPTO) && defined(USE_SSL)
1465
if (tls_multi && link_socket_connection_oriented (&link_socket) && tls_multi->n_errors)
1467
/* TLS errors are fatal in TCP mode */
1468
signal_received = SIGUSR1;
1469
msg (D_STREAM_ERRORS, "Fatal decryption error, restarting");
1470
signal_text = "tls-error";
1476
* Things that need to happen immediately after connection initiation should go here.
1478
if (event_timeout_defined (&wait_for_connect))
1480
if (event_timeout_trigger (&wait_for_connect, current, &timeval))
1482
if (CONNECTION_ESTABLISHED (&link_socket))
1484
/* if --up-delay specified, open tun, do ifconfig, and run up script now */
1485
if (options->up_delay)
1487
did_open_tun = do_open_tun (options, &frame, &link_socket, tuntap, route_list);
1488
TUNTAP_SETMAXFD(tuntap);
1489
current = time (NULL);
1494
/* if --route-delay was specified, start timer */
1495
if (options->route_delay_defined)
1496
event_timeout_init (&route_wakeup, current, options->route_delay);
1499
event_timeout_clear (&wait_for_connect);
1505
* Should we add routes?
1507
if (event_timeout_trigger (&route_wakeup, current, &timeval))
1509
do_route (options, route_list);
1510
current = time (NULL);
1511
event_timeout_clear (&route_wakeup);
1515
* Should we exit due to inactivity timeout?
1517
if (options->inactivity_timeout)
1519
if (event_timeout_trigger (&inactivity_interval, current, &timeval))
1521
msg (M_INFO, "Inactivity timeout (--inactive), exiting");
1522
signal_received = 0;
1523
signal_text = "inactive";
1529
* Should we exit or restart due to ping (or other authenticated packet)
1530
* not received in n seconds?
1532
if (options->ping_rec_timeout &&
1533
(!options->ping_timer_remote || addr_defined (&link_socket_addr->actual)))
1535
if (event_timeout_trigger (&ping_rec_interval, current, &timeval))
1537
switch (options->ping_rec_timeout_action)
1540
msg (M_INFO, "Inactivity timeout (--ping-exit), exiting");
1541
signal_received = 0;
1542
signal_text = "ping-exit";
1545
msg (M_INFO, "Inactivity timeout (--ping-restart), restarting");
1546
signal_received = SIGUSR1;
1547
signal_text = "ping-restart";
1557
* Should we send an OCC_REQUEST message?
1559
if (event_timeout_defined (&occ_interval)
1563
if (event_timeout_trigger (&occ_interval, current, &timeval))
1565
if (++occ_n_tries >= OCC_N_TRIES)
1567
if (options->remote)
1569
* No OCC_REPLY from peer after repeated attempts.
1572
msg (D_SHOW_OCC, "NOTE: failed to obtain options consistency info from peer -- this could occur if the remote peer is running a version of " PACKAGE_NAME " before 1.5-beta8 or if there is a network connectivity problem, and will not necessarily prevent " PACKAGE_NAME " from running (%u bytes received from peer, %u bytes authenticated data channel traffic) -- you can disable the options consistency check with --disable-occ.", (unsigned int) link_read_bytes, (unsigned int) link_read_bytes_auth);
1573
event_timeout_clear (&occ_interval);
1577
occ_op = OCC_REQUEST;
1580
* If we don't hear back from peer, send another
1581
* OCC_REQUEST in OCC_INTERVAL_SECONDS.
1583
event_timeout_reset (&occ_interval, current);
1589
* Should we send an MTU load test?
1591
if (event_timeout_defined (&occ_mtu_load_test_interval)
1595
if (event_timeout_trigger (&occ_mtu_load_test_interval, current, &timeval))
1597
if (CONNECTION_ESTABLISHED (&link_socket))
1599
const struct mtu_load_test *entry;
1601
if (!occ_mtu_load_n_tries)
1602
msg (M_INFO, "NOTE: Beginning empirical MTU test -- results should be available in 3 to 4 minutes.");
1604
entry = &mtu_load_test_sequence[occ_mtu_load_n_tries++];
1608
occ_mtu_load_size = EXPANDED_SIZE (&frame) + entry->delta;
1612
msg (M_INFO, "NOTE: failed to empirically measure MTU (requires 1.5-beta8 or higher at other end of connection).");
1613
event_timeout_clear (&occ_mtu_load_test_interval);
1614
occ_mtu_load_n_tries = 0;
1621
* Should we send an OCC message?
1623
if (occ_op >= 0 && !to_link.len
1624
#ifdef FRAGMENT_ENABLE
1625
&& (!fragment || !fragment_outgoing_defined (fragment))
1632
ASSERT (buf_init (&buf, FRAME_HEADROOM (&frame)));
1633
ASSERT (buf_safe (&buf, MAX_RW_SIZE_TUN (&frame)));
1634
ASSERT (buf_write (&buf, occ_magic, sizeof (occ_magic)));
1639
if (!buf_write_u8 (&buf, OCC_REQUEST))
1641
msg (D_PACKET_CONTENT, "SENT OCC_REQUEST");
1646
if (!options_string_local)
1648
if (!buf_write_u8 (&buf, OCC_REPLY))
1650
if (!buf_write (&buf, options_string_local,
1651
strlen (options_string_local) + 1))
1653
msg (D_PACKET_CONTENT, "SENT OCC_REPLY");
1657
case OCC_MTU_REQUEST:
1658
if (!buf_write_u8 (&buf, OCC_MTU_REQUEST))
1660
msg (D_PACKET_CONTENT, "SENT OCC_MTU_REQUEST");
1665
if (!buf_write_u8 (&buf, OCC_MTU_REPLY))
1667
if (!buf_write_u16 (&buf, max_recv_size_local))
1669
if (!buf_write_u16 (&buf, max_send_size_local))
1671
msg (D_PACKET_CONTENT, "SENT OCC_MTU_REPLY");
1675
case OCC_MTU_LOAD_REQUEST:
1676
if (!buf_write_u8 (&buf, OCC_MTU_LOAD_REQUEST))
1678
if (!buf_write_u16 (&buf, occ_mtu_load_size))
1680
msg (D_PACKET_CONTENT, "SENT OCC_MTU_LOAD_REQUEST");
1688
if (!buf_write_u8 (&buf, OCC_MTU_LOAD))
1690
need_to_add = min_int (
1692
- sizeof (occ_magic)
1694
- EXTRA_FRAME (&frame),
1695
EXPANDED_SIZE (&frame));
1696
while (need_to_add > 0)
1699
* Fill the load test packet with pseudo-random bytes.
1701
if (!buf_write_u8 (&buf, get_random() & 0xFF))
1705
msg (D_PACKET_CONTENT, "SENT OCC_MTU_LOAD %d", occ_mtu_load_size);
1714
* We will treat the packet like any other outgoing packet,
1715
* compress, encrypt, sign, etc.
1717
# include "encrypt_sign.h"
1723
#ifdef FRAGMENT_ENABLE
1725
* Should we deliver a datagram fragment to remote?
1730
if (link_socket.mtu_changed && ipv4_tun)
1732
frame_adjust_path_mtu (&frame_fragment, link_socket.mtu, options->proto);
1733
link_socket.mtu_changed = false;
1736
&& fragment_outgoing_defined (fragment)
1737
&& fragment_ready_to_send (fragment, &buf, &frame_fragment))
1739
# define NO_COMP_FRAG
1740
# include "encrypt_sign.h"
1742
fragment_housekeeping (fragment, &frame_fragment, current, &timeval);
1745
#endif /* FRAGMENT_ENABLE */
1748
* Should we ping the remote?
1750
if (options->ping_send_timeout)
1754
if (event_timeout_trigger (&ping_send_interval, current, &timeval))
1757
ASSERT (buf_init (&buf, FRAME_HEADROOM (&frame)));
1758
ASSERT (buf_safe (&buf, MAX_RW_SIZE_TUN (&frame)));
1759
ASSERT (buf_write (&buf, ping_string, sizeof (ping_string)));
1762
* We will treat the ping like any other outgoing packet,
1763
* encrypt, sign, etc.
1765
# include "encrypt_sign.h"
1766
msg (D_PACKET_CONTENT, "SENT PING");
1771
/* do a quick garbage collect */
1772
gc_collect (gc_level);
1775
* Set up for select call.
1777
* Decide what kind of events we want to wait for.
1779
wait_reset (&event_wait);
1782
* On win32 we use the keyboard or an event object as a source
1783
* of asynchronous signals.
1785
WAIT_SIGNAL (&event_wait);
1788
* If outgoing data (for TCP/UDP port) pending, wait for ready-to-send
1789
* status from TCP/UDP port. Otherwise, wait for incoming data on
1792
if (to_link.len > 0)
1795
* If sending this packet would put us over our traffic shaping
1796
* quota, don't send -- instead compute the delay we must wait
1797
* until it will be OK to send the packet.
1800
#ifdef HAVE_GETTIMEOFDAY
1803
/* set traffic shaping delay in microseconds */
1804
if (options->shaper)
1805
delay = max_int (delay, shaper_delay (&shaper));
1809
SOCKET_SET_WRITE (link_socket);
1813
shaper_soonest_event (&timeval, delay);
1816
#else /* HAVE_GETTIMEOFDAY */
1817
SOCKET_SET_WRITE (link_socket);
1818
#endif /* HAVE_GETTIMEOFDAY */
1820
#ifdef FRAGMENT_ENABLE
1821
else if (!fragment || !fragment_outgoing_defined (fragment))
1826
TUNTAP_SET_READ (tuntap);
1827
#if defined(USE_CRYPTO) && defined(USE_SSL) && defined(USE_PTHREAD)
1828
TLS_THREAD_SOCKET_SET (thread_parms, reads);
1833
* If outgoing data (for TUN/TAP device) pending, wait for ready-to-send status
1834
* from device. Otherwise, wait for incoming data on TCP/UDP port.
1838
TUNTAP_SET_WRITE (tuntap);
1842
SOCKET_SET_READ (link_socket);
1846
* Possible scenarios:
1847
* (1) tcp/udp port has data available to read
1848
* (2) tcp/udp port is ready to accept more data to write
1849
* (3) tun dev has data available to read
1850
* (4) tun dev is ready to accept more data to write
1851
* (5) tls background thread has data available to forward to
1853
* (6) we received a signal (handler sets signal_received)
1854
* (7) timeout (tv) expired (from TLS, shaper, inactivity
1855
* timeout, or ping timeout)
1859
* Wait for something to happen.
1861
stat = 1; /* this will be our return "status" if select doesn't get called */
1862
if (!signal_received && !SOCKET_READ_RESIDUAL (link_socket)) {
1863
msg (D_SELECT, "SELECT %s|%s|%s|%s %d/%d",
1864
TUNTAP_READ_STAT (tuntap),
1865
TUNTAP_WRITE_STAT (tuntap),
1866
SOCKET_READ_STAT (link_socket),
1867
SOCKET_WRITE_STAT (link_socket),
1868
tv ? (int)tv->tv_sec : -1,
1869
tv ? (int)tv->tv_usec : -1
1873
check_status (stat, "select", NULL, NULL);
1876
/* current should always be a reasonably up-to-date timestamp */
1877
current = time (NULL);
1879
/* set signal_received if a signal was received */
1880
SELECT_SIGNAL_RECEIVED ();
1883
* Did we get a signal before or while we were waiting
1886
if (signal_received)
1888
if (signal_received == SIGUSR2)
1890
msg (M_INFO, "Current " PACKAGE_NAME " Statistics:");
1891
msg (M_INFO, " TUN/TAP read bytes: " counter_format, tun_read_bytes);
1892
msg (M_INFO, " TUN/TAP write bytes: " counter_format, tun_write_bytes);
1893
msg (M_INFO, " TCP/UDP read bytes: " counter_format, link_read_bytes);
1894
msg (M_INFO, " TCP/UDP write bytes: " counter_format, link_write_bytes);
1895
msg (M_INFO, " Auth read bytes: " counter_format, link_read_bytes_auth);
1897
if (options->comp_lzo)
1898
lzo_print_stats (&lzo_compwork);
1901
msg (M_INFO, " TAP-WIN32 driver status: %s", tap_win32_getinfo (tuntap));
1903
signal_received = 0;
1907
print_signal (signal_received);
1909
/* for all other signals (INT, TERM, HUP, USR1) we break */
1913
if (!stat) /* timeout? */
1918
/* Incoming data on TCP/UDP port */
1919
if (SOCKET_READ_RESIDUAL (link_socket) || SOCKET_ISSET (link_socket, reads))
1922
* Set up for recvfrom call to read datagram
1923
* sent to our TCP/UDP port.
1925
struct sockaddr_in from;
1928
ASSERT (!to_tun.len);
1929
buf = read_link_buf;
1930
ASSERT (buf_init (&buf, FRAME_HEADROOM (&frame)));
1932
status = link_socket_read (&link_socket, &buf, MAX_RW_SIZE_LINK (&frame), &from);
1934
if (socket_connection_reset (&link_socket, status))
1936
/* received a disconnect from a connection-oriented protocol */
1939
signal_received = SIGTERM;
1940
msg (D_STREAM_ERRORS, "Connection reset, inetd/xinetd exit [%d]", status);
1944
signal_received = SIGUSR1;
1945
msg (D_STREAM_ERRORS, "Connection reset, restarting [%d]", status);
1947
signal_text = "connection-reset";
1953
link_read_bytes += buf.len;
1954
original_recv_size = buf.len;
1957
original_recv_size = 0;
1959
/* check recvfrom status */
1960
check_status (status, "read", &link_socket, NULL);
1962
/* take action to corrupt packet if we are in gremlin test mode */
1963
if (options->gremlin) {
1966
corrupt_gremlin(&buf);
1969
/* log incoming packet */
1972
fprintf (stderr, "R");
1974
msg (D_LINK_RW, "%s READ [%d] from %s: %s",
1975
proto2ascii (link_socket.proto, true),
1977
print_sockaddr (&from),
1981
* Good, non-zero length packet received.
1982
* Commence multi-stage processing of packet,
1983
* such as authenticate, decrypt, decompress.
1984
* If any stage fails, it sets buf.len to 0 or -1,
1985
* telling downstream stages to ignore the packet.
1989
link_socket_incoming_addr (&buf, &link_socket, &from);
1996
* If tls_pre_decrypt returns true, it means the incoming
1997
* packet was a good TLS control channel packet. If so, TLS code
1998
* will deal with the packet and set buf.len to 0 so downstream
2001
* If the packet is a data channel packet, tls_pre_decrypt
2002
* will load crypto_options with the correct encryption key
2005
if (tls_pre_decrypt (tls_multi, &from, &buf, &crypto_options, current))
2008
/* tell TLS thread a packet is waiting */
2009
if (tls_thread_process (&thread_parms) == -1)
2011
msg (M_WARN, "TLS thread is not responding, exiting (1)");
2012
signal_received = 0;
2013
signal_text = "error";
2014
mutex_unlock (L_TLS);
2018
interval_action (&tmp_int, current);
2019
#endif /* USE_PTHREAD */
2020
/* reset packet received timer if TLS packet */
2021
if (options->ping_rec_timeout)
2022
event_timeout_reset (&ping_rec_interval, current);
2025
#endif /* USE_SSL */
2026
/* authenticate and decrypt the incoming packet */
2027
if (!openvpn_decrypt (&buf, decrypt_buf, &crypto_options, &frame, current))
2029
if (link_socket_connection_oriented (&link_socket))
2031
/* decryption errors are fatal in TCP mode */
2032
signal_received = SIGUSR1;
2033
msg (D_STREAM_ERRORS, "Fatal decryption error, restarting");
2034
signal_text = "decryption-error";
2035
mutex_unlock (L_TLS);
2040
mutex_unlock (L_TLS);
2041
#endif /* USE_SSL */
2042
#endif /* USE_CRYPTO */
2043
#ifdef FRAGMENT_ENABLE
2045
fragment_incoming (fragment, &buf, &frame_fragment, current);
2048
/* decompress the incoming packet */
2049
if (options->comp_lzo)
2050
lzo_decompress (&buf, lzo_decompress_buf, &lzo_compwork, &frame);
2053
* Set our "official" outgoing address, since
2054
* if buf.len is non-zero, we know the packet
2055
* authenticated. In TLS mode we do nothing
2056
* because TLS mode takes care of source address
2059
* Also, update the persisted version of our packet-id.
2062
link_socket_set_outgoing_addr (&buf, &link_socket, &from);
2064
/* reset packet received timer */
2065
if (options->ping_rec_timeout && buf.len > 0)
2066
event_timeout_reset (&ping_rec_interval, current);
2068
/* increment authenticated receive byte count */
2071
link_read_bytes_auth += buf.len;
2072
max_recv_size_local = max_int (original_recv_size, max_recv_size_local);
2075
/* Did we just receive an openvpn ping packet? */
2076
if (buf_string_match (&buf, ping_string, sizeof (ping_string)))
2078
msg (D_PACKET_CONTENT, "RECEIVED PING PACKET");
2079
buf.len = 0; /* drop packet */
2082
/* Did we just receive an OCC packet? */
2083
if (buf_string_match_head (&buf, occ_magic, sizeof (occ_magic)))
2085
ASSERT (buf_advance (&buf, sizeof (occ_magic)));
2086
switch (buf_read_u8 (&buf))
2089
msg (D_PACKET_CONTENT, "RECEIVED OCC_REQUEST");
2093
case OCC_MTU_REQUEST:
2094
msg (D_PACKET_CONTENT, "RECEIVED OCC_MTU_REQUEST");
2095
occ_op = OCC_MTU_REPLY;
2098
case OCC_MTU_LOAD_REQUEST:
2099
msg (D_PACKET_CONTENT, "RECEIVED OCC_MTU_LOAD_REQUEST");
2100
occ_mtu_load_size = buf_read_u16 (&buf);
2101
if (occ_mtu_load_size >= 0)
2102
occ_op = OCC_MTU_LOAD;
2106
msg (D_PACKET_CONTENT, "RECEIVED OCC_REPLY");
2107
if (options->occ && !TLS_MODE && options_string_remote)
2109
if (!options_cmp_equal (BPTR (&buf),
2110
options_string_remote,
2113
options_warning (BPTR (&buf),
2114
options_string_remote,
2118
event_timeout_clear (&occ_interval);
2122
msg (D_PACKET_CONTENT, "RECEIVED OCC_MTU_REPLY");
2123
max_recv_size_remote = buf_read_u16 (&buf);
2124
max_send_size_remote = buf_read_u16 (&buf);
2125
if (options->mtu_test
2126
&& max_recv_size_remote > 0
2127
&& max_send_size_remote > 0)
2129
msg (M_INFO, "NOTE: Empirical MTU test completed [Tried,Actual] local->remote=[%d,%d] remote->local=[%d,%d]",
2130
max_send_size_local,
2131
max_recv_size_remote,
2132
max_send_size_remote,
2133
max_recv_size_local);
2134
if (!options->mssfix_defined
2135
#ifdef FRAGMENT_ENABLE
2136
&& !options->fragment
2138
&& options->proto == PROTO_UDPv4
2139
&& max_send_size_local > TUN_MTU_MIN
2140
&& (max_recv_size_remote < max_send_size_local
2141
|| max_recv_size_local < max_send_size_remote))
2142
msg (M_INFO, "NOTE: This connection is unable to accomodate a UDP packet size of %d. Consider using --fragment or --mssfix options as a workaround.",
2143
max_send_size_local);
2145
event_timeout_clear (&occ_mtu_load_test_interval);
2148
buf.len = 0; /* don't pass packet on */
2159
#if defined(USE_CRYPTO) && defined(USE_SSL) && defined(USE_PTHREAD)
2160
/* Incoming data from TLS background thread */
2161
else if (TLS_THREAD_SOCKET_ISSET (thread_parms, reads))
2164
ASSERT (!to_link.len);
2166
s = tls_thread_rec_buf (&thread_parms, &tt_ret, true);
2170
* TLS background thread has a control channel
2171
* packet to send to remote.
2173
to_link = tt_ret.to_link;
2174
to_link_addr = tt_ret.to_link_addr;
2176
/* tell TCP/UDP packet writer to free buffer after write */
2177
free_to_link = true;
2183
msg (M_WARN, "TLS thread is not responding, exiting (2)");
2184
signal_received = 0;
2185
signal_text = "error";
2191
/* Incoming data on TUN device */
2192
else if (TUNTAP_ISSET (tuntap, reads))
2195
* Setup for read() call on TUN/TAP device.
2197
ASSERT (!to_link.len);
2200
#ifdef TUN_PASS_BUFFER
2201
read_tun_buffered (tuntap, &buf, MAX_RW_SIZE_TUN (&frame));
2203
ASSERT (buf_init (&buf, FRAME_HEADROOM (&frame)));
2204
ASSERT (buf_safe (&buf, MAX_RW_SIZE_TUN (&frame)));
2205
buf.len = read_tun (tuntap, BPTR (&buf), MAX_RW_SIZE_TUN (&frame));
2209
tun_read_bytes += buf.len;
2211
/* Was TUN/TAP interface stopped? */
2212
if (tuntap_stop (buf.len))
2214
signal_received = SIGTERM;
2215
signal_text = "tun-stop";
2216
msg (M_INFO, "TUN/TAP interface has been stopped, exiting");
2220
/* Check the status return from read() */
2221
check_status (buf.len, "read from TUN/TAP", NULL, tuntap);
2225
fprintf (stderr, "r");
2228
/* Show packet content */
2229
msg (D_TUN_RW, "TUN READ [%d]: %s md5=%s",
2231
format_hex (BPTR (&buf), BLEN (&buf), 80),
2232
MD5SUM (BPTR (&buf), BLEN (&buf)));
2237
* The --passtos and --mssfix options require
2238
* us to examine the IPv4 header.
2240
if (options->mssfix_defined
2241
#if PASSTOS_CAPABILITY
2246
struct buffer ipbuf = buf;
2247
if (is_ipv4 (tuntap->type, &ipbuf))
2249
#if PASSTOS_CAPABILITY
2250
/* extract TOS from IP header */
2251
if (options->passtos)
2253
struct openvpn_iphdr *iph =
2254
(struct openvpn_iphdr *) BPTR (&ipbuf);
2256
ptos_defined = true;
2260
/* possibly alter the TCP MSS */
2261
if (options->mssfix_defined)
2262
mss_fixup (&ipbuf, MTU_TO_MSS (TUN_MTU_SIZE_DYNAMIC (&frame)));
2265
# include "encrypt_sign.h"
2270
free_to_link = false;
2274
/* TUN device ready to accept write */
2275
else if (TUNTAP_ISSET (tuntap, writes))
2278
* Set up for write() call to TUN/TAP
2281
ASSERT (to_tun.len > 0);
2284
* The --mssfix option requires
2285
* us to examine the IPv4 header.
2287
if (options->mssfix_defined)
2289
struct buffer ipbuf = to_tun;
2291
if (is_ipv4 (tuntap->type, &ipbuf))
2293
/* possibly alter the TCP MSS */
2294
if (options->mssfix_defined)
2295
mss_fixup (&ipbuf, MTU_TO_MSS (TUN_MTU_SIZE_DYNAMIC (&frame)));
2299
if (to_tun.len <= MAX_RW_SIZE_TUN(&frame))
2302
* Write to TUN/TAP device.
2308
fprintf (stderr, "w");
2310
msg (D_TUN_RW, "TUN WRITE [%d]: %s md5=%s",
2312
format_hex (BPTR (&to_tun), BLEN (&to_tun), 80),
2313
MD5SUM (BPTR (&to_tun), BLEN (&to_tun)));
2315
#ifdef TUN_PASS_BUFFER
2316
size = write_tun_buffered (tuntap, &to_tun);
2318
size = write_tun (tuntap, BPTR (&to_tun), BLEN (&to_tun));
2322
tun_write_bytes += size;
2323
check_status (size, "write to TUN/TAP", NULL, tuntap);
2325
/* check written packet size */
2328
/* Did we write a different size packet than we intended? */
2329
if (size != BLEN (&to_tun))
2331
"TUN/TAP packet was fragmented on write to %s (tried=%d,actual=%d)",
2340
* This should never happen, probably indicates some kind
2343
msg (D_LINK_ERRORS, "tun packet too large on write (tried=%d,max=%d)",
2345
MAX_RW_SIZE_TUN (&frame));
2349
* Putting the --inactive timeout reset here, ensures that we will timeout
2350
* if the remote goes away, even if we are trying to send data to the
2351
* remote and failing.
2353
if (options->inactivity_timeout)
2354
event_timeout_reset (&inactivity_interval, current);
2359
/* TCP/UDP port ready to accept write */
2360
else if (SOCKET_ISSET (link_socket, writes))
2362
if (to_link.len > 0 && to_link.len <= EXPANDED_SIZE (&frame))
2365
* Setup for call to send/sendto which will send
2366
* packet to remote over the TCP/UDP port.
2369
ASSERT (addr_defined (&to_link_addr));
2371
/* In gremlin-test mode, we may choose to drop this packet */
2372
if (!options->gremlin || ask_gremlin())
2375
* Let the traffic shaper know how many bytes
2378
#ifdef HAVE_GETTIMEOFDAY
2379
if (options->shaper)
2380
shaper_wrote_bytes (&shaper, BLEN (&to_link)
2381
+ datagram_overhead (options->proto));
2384
* Let the pinger know that we sent a packet.
2386
if (options->ping_send_timeout)
2387
event_timeout_reset (&ping_send_interval, current);
2389
#if PASSTOS_CAPABILITY
2392
setsockopt(link_socket.sd, IPPROTO_IP, IP_TOS, &ptos, sizeof(ptos));
2395
/* Log packet send */
2398
fprintf (stderr, "W");
2400
msg (D_LINK_RW, "%s WRITE [%d] to %s: %s",
2401
proto2ascii (link_socket.proto, true),
2403
print_sockaddr (&to_link_addr),
2404
PROTO_DUMP (&to_link));
2407
size = link_socket_write (&link_socket, &to_link, &to_link_addr);
2411
max_send_size_local = max_int (size, max_send_size_local);
2412
link_write_bytes += size;
2418
/* Check return status */
2419
check_status (size, "write", &link_socket, NULL);
2423
/* Did we write a different size packet than we intended? */
2424
if (size != BLEN (&to_link))
2426
"TCP/UDP packet was truncated/expanded on write to %s (tried=%d,actual=%d)",
2427
print_sockaddr (&to_link_addr),
2434
msg (D_LINK_ERRORS, "TCP/UDP packet too large on write to %s (tried=%d,max=%d)",
2435
print_sockaddr (&to_link_addr),
2437
EXPANDED_SIZE (&frame));
2441
* The free_to_link flag means that we should free the packet buffer
2442
* after send. This flag is usually set when the TLS background
2443
* thread generated the packet buffer.
2447
free_to_link = false;
2448
free_buf (&to_link);
2462
* If xinetd/inetd mode, don't allow restart.
2464
if (options->inetd && (signal_received == SIGHUP || signal_received == SIGUSR1))
2466
signal_received = SIGTERM;
2467
msg (M_INFO, PACKAGE_NAME " started by inetd/xinetd cannot restart... Exiting.");
2471
free_buf (&to_link);
2473
#if defined(USE_CRYPTO) && defined(USE_SSL) && defined(USE_PTHREAD)
2475
tls_thread_close (&thread_parms);
2478
free_buf (&read_link_buf);
2479
free_buf (&read_tun_buf);
2480
free_buf (&aux_buf);
2483
if (options->comp_lzo)
2485
lzo_compress_uninit (&lzo_compwork);
2486
free_buf (&lzo_compress_buf);
2487
free_buf (&lzo_decompress_buf);
2493
packet_id_free (&packet_id);
2495
free_buf (&encrypt_buf);
2496
free_buf (&decrypt_buf);
2500
tls_multi_free (tls_multi, true);
2502
/* free options compatibility strings */
2503
if (options_string_local)
2504
free (options_string_local);
2505
if (options_string_remote)
2506
free (options_string_remote);
2509
#endif /* USE_CRYPTO */
2512
* Free key schedules
2514
if ( !(signal_received == SIGUSR1 && options->persist_key) )
2515
key_schedule_free (ks);
2518
* Close TCP/UDP connection
2520
link_socket_close (&link_socket);
2521
if ( !(signal_received == SIGUSR1 && options->persist_remote_ip) )
2523
CLEAR (link_socket_addr->remote);
2524
CLEAR (link_socket_addr->actual);
2526
if ( !(signal_received == SIGUSR1 && options->persist_local_ip) )
2527
CLEAR (link_socket_addr->local);
2530
* Close TUN/TAP device
2532
if (tuntap_defined (tuntap))
2534
if ( !(signal_received == SIGUSR1 && options->persist_tun) )
2536
char* tuntap_actual = (char *) gc_malloc (sizeof (tuntap->actual));
2537
strcpy (tuntap_actual, tuntap->actual);
2539
/* delete any routes we added */
2540
delete_routes (route_list);
2542
msg (D_CLOSE, "Closing TUN/TAP device");
2545
/* Run the down script -- note that it will run at reduced
2546
privilege if, for example, "--user nobody" was used. */
2547
run_script (options->down_script,
2549
TUN_MTU_SIZE (&frame),
2550
EXPANDED_SIZE (&frame),
2551
print_in_addr_t (tuntap->local, true),
2552
print_in_addr_t (tuntap->remote_netmask, true),
2554
signal_description (signal_received, signal_text),
2559
/* run the down script on this restart if --up-restart was specified */
2560
if (options->up_restart)
2561
run_script (options->down_script,
2563
TUN_MTU_SIZE (&frame),
2564
EXPANDED_SIZE (&frame),
2565
print_in_addr_t (tuntap->local, true),
2566
print_in_addr_t (tuntap->remote_netmask, true),
2568
signal_description (signal_received, signal_text),
2573
/* remove non-parameter environmental vars except for signal */
2575
#if defined(USE_CRYPTO) && defined(USE_SSL)
2576
get_max_tls_verify_id ()
2584
* Close packet-id persistance file
2586
packet_id_persist_save (pid_persist);
2587
if ( !(signal_received == SIGUSR1) )
2588
packet_id_persist_close (pid_persist);
2592
* Close fragmentation handler.
2594
#ifdef FRAGMENT_ENABLE
2596
fragment_free (fragment);
2600
/* pop our garbage collection level */
2601
gc_free_level (gc_level);
2603
/* return the signal that brought us here */
2605
const int s = signal_received;
2606
signal_received = 0;
2612
main (int argc, char *argv[])
2614
const int gc_level = gc_new_level ();
2615
bool first_time = true;
2618
init_random_seed(); /* init random() function, only used as
2619
source for weak random numbers */
2620
error_reset (); /* initialize error.c */
2621
reset_check_status (); /* initialize status check code in socket.c */
2624
packet_id_interactive_test(); /* test the sequence number code */
2632
#ifdef OPENVPN_DEBUG_COMMAND_LINE
2635
for (i = 0; i < argc; ++i)
2636
msg (M_INFO, "argv[%d] = '%s'", i, argv[i]);
2640
del_env_nonparm (0);
2643
* This loop is initially executed on startup and then
2647
struct options options;
2648
struct options defaults;
2649
int dev = DEV_TYPE_UNDEF;
2651
init_options (&options);
2652
init_options (&defaults);
2655
* Parse command line options,
2656
* and read configuration file.
2658
parse_argv (&options, argc, argv);
2660
/* set verbosity and mute levels */
2661
set_check_status (D_LINK_ERRORS, D_READ_WRITE);
2662
set_debug_level (options.verbosity);
2663
set_mute_cutoff (options.mute);
2666
* Possibly set --dev based on --dev-node.
2667
* For example, if --dev-node /tmp/foo/tun, and --dev undefined,
2671
options.dev = dev_component_in_dev_node (options.dev_node);
2674
* OpenSSL info print mode?
2677
if (options.show_ciphers || options.show_digests
2679
|| options.show_tls_ciphers
2685
if (options.show_ciphers)
2686
show_available_ciphers ();
2687
if (options.show_digests)
2688
show_available_digests ();
2690
if (options.show_tls_ciphers)
2691
show_available_tls_ciphers ();
2698
* Static pre-shared key generation mode?
2704
notnull (options.shared_secret_file,
2705
"shared secret output file (--secret)");
2707
if (options.mlock) /* should we disable paging? */
2710
nbits_written = write_key_file (2, options.shared_secret_file);
2712
msg (D_GENKEY|M_NOPREFIX, "Randomly generated %d bit key written to %s",
2714
options.shared_secret_file);
2717
#endif /* USE_CRYPTO */
2720
* Persistent TUN/TAP device management mode?
2722
#ifdef TUNSETPERSIST
2723
if (options.persist_config)
2725
/* sanity check on options for --mktun or --rmtun */
2726
notnull (options.dev, "TUN/TAP device (--dev)");
2727
if (options.remote || options.ifconfig_local || options.ifconfig_remote_netmask
2729
|| options.shared_secret_file
2731
|| options.tls_server || options.tls_client
2735
msg (M_FATAL, "Options error: options --mktun or --rmtun should only be used together with --dev");
2736
tuncfg (options.dev, options.dev_type, options.dev_node,
2737
options.tun_ipv6, options.persist_mode);
2743
* Main OpenVPN block -- tunnel generation mode
2747
if (options.test_crypto)
2749
notnull (options.shared_secret_file, "key file (--secret)");
2753
notnull (options.dev, "TUN/TAP device (--dev)");
2756
* Get tun/tap/null device type
2758
dev = dev_type_enum (options.dev, options.dev_type);
2761
* Sanity check on daemon/inetd modes
2764
if (options.daemon && options.inetd)
2765
msg (M_USAGE, "Options error: only one of --daemon or --inetd may be specified");
2767
if (options.inetd && (options.local || options.remote))
2768
msg (M_USAGE, "Options error: --local or --remote cannot be used with --inetd");
2770
if (options.inetd && options.proto == PROTO_TCPv4_CLIENT)
2771
msg (M_USAGE, "Options error: --proto tcp-client cannot be used with --inetd");
2773
if (options.inetd == INETD_NOWAIT && options.proto != PROTO_TCPv4_SERVER)
2774
msg (M_USAGE, "Options error: --inetd nowait can only be used with --proto tcp-server");
2776
if (options.inetd == INETD_NOWAIT
2777
#if defined(USE_CRYPTO) && defined(USE_SSL)
2778
&& !(options.tls_server || options.tls_client)
2781
msg (M_USAGE, "Options error: --inetd nowait can only be used in TLS mode");
2783
if (options.inetd == INETD_NOWAIT && dev != DEV_TYPE_TAP)
2784
msg (M_USAGE, "Options error: --inetd nowait only makes sense in --dev tap mode");
2787
* In forking TCP server mode, you don't need to ifconfig
2788
* the tap device (the assumption is that it will be bridged).
2790
if (options.inetd == INETD_NOWAIT)
2791
options.ifconfig_noexec = true;
2794
* Sanity check on TCP mode options
2797
if (options.connect_retry_defined && options.proto != PROTO_TCPv4_CLIENT)
2798
msg (M_USAGE, "Options error: --connect-retry doesn't make sense unless also used with --proto tcp-client");
2801
* Sanity check on MTU parameters
2803
if (options.tun_mtu_defined && options.link_mtu_defined)
2804
msg (M_USAGE, "Options error: only one of --tun-mtu or --link-mtu may be defined (note that --ifconfig implies --link-mtu %d)", LINK_MTU_DEFAULT);
2806
if (options.proto != PROTO_UDPv4 && options.mtu_test)
2807
msg (M_USAGE, "Options error: --mtu-test only makes sense with --proto udp");
2813
if (!options.tun_mtu_defined && !options.link_mtu_defined)
2815
if ((dev == DEV_TYPE_TAP) || WIN32_0_1)
2817
options.tun_mtu_defined = true;
2818
options.tun_mtu = TAP_MTU_DEFAULT;
2822
if (options.ifconfig_local || options.ifconfig_remote_netmask)
2823
options.link_mtu_defined = true;
2825
options.tun_mtu_defined = true;
2828
if ((dev == DEV_TYPE_TAP) && !options.tun_mtu_extra_defined)
2830
options.tun_mtu_extra_defined = true;
2831
options.tun_mtu_extra = TAP_MTU_EXTRA_DEFAULT;
2836
* Sanity check on --local, --remote, and ifconfig
2838
if (string_defined_equal (options.local, options.remote)
2839
&& options.local_port == options.remote_port)
2840
msg (M_USAGE, "Options error: --remote and --local addresses are the same");
2842
if (string_defined_equal (options.local, options.ifconfig_local)
2843
|| string_defined_equal (options.local, options.ifconfig_remote_netmask)
2844
|| string_defined_equal (options.remote, options.ifconfig_local)
2845
|| string_defined_equal (options.remote, options.ifconfig_remote_netmask))
2846
msg (M_USAGE, "Options error: --local and --remote addresses must be distinct from --ifconfig addresses");
2848
if (string_defined_equal (options.ifconfig_local, options.ifconfig_remote_netmask))
2849
msg (M_USAGE, "Options error: local and remote/netmask --ifconfig addresses must be different");
2852
if (dev == DEV_TYPE_TUN && !(options.ifconfig_local && options.ifconfig_remote_netmask))
2853
msg (M_USAGE, "Options error: On Windows, --ifconfig is required when --dev tun is used");
2855
if ((options.tuntap_options.ip_win32_defined)
2856
&& !(options.ifconfig_local && options.ifconfig_remote_netmask))
2857
msg (M_USAGE, "Options error: On Windows, --ip-win32 doesn't make sense unless --ifconfig is also used");
2859
if (options.tuntap_options.dhcp_options &&
2860
options.tuntap_options.ip_win32_type != IPW32_SET_DHCP_MASQ)
2861
msg (M_USAGE, "Options error: --dhcp-options requires --ip-win32 dynamic");
2863
if (options.tuntap_options.ip_win32_type == IPW32_SET_DHCP_MASQ
2864
&& !options.route_delay_defined)
2866
options.route_delay_defined = true;
2867
options.route_delay = 10;
2870
if (options.ifconfig_noexec)
2872
options.tuntap_options.ip_win32_type = IPW32_SET_MANUAL;
2873
options.ifconfig_noexec = false;
2878
* Check that protocol options make sense.
2881
#ifdef FRAGMENT_ENABLE
2882
if (options.proto != PROTO_UDPv4 && options.fragment)
2883
msg (M_USAGE, "Options error: --fragment can only be used with --proto udp");
2885
if (!options.remote && options.proto == PROTO_TCPv4_CLIENT)
2886
msg (M_USAGE, "Options error: --remote MUST be used in TCP Client mode");
2888
if (options.http_proxy_server && options.proto != PROTO_TCPv4_CLIENT)
2889
msg (M_USAGE, "Options error: --http-proxy MUST be used in TCP Client mode (i.e. --proto tcp-client)");
2891
if (options.http_proxy_server && options.socks_proxy_server)
2892
msg (M_USAGE, "Options error: --http-proxy can not be used together with --socks-proxy");
2894
if (options.socks_proxy_server && options.proto == PROTO_TCPv4_SERVER)
2895
msg (M_USAGE, "Options error: --socks-proxy can not be used in TCP Server mode");
2903
* Check consistency of replay options
2905
if ((options.proto != PROTO_UDPv4)
2906
&& (options.replay_window != defaults.replay_window
2907
|| options.replay_time != defaults.replay_time))
2908
msg (M_USAGE, "Options error: --replay-window only makes sense with --proto udp");
2911
&& (options.replay_window != defaults.replay_window
2912
|| options.replay_time != defaults.replay_time))
2913
msg (M_USAGE, "Options error: --replay-window doesn't make sense when replay protection is disabled with --no-replay");
2915
/* Don't use replay window for TCP mode (i.e. require that packets
2916
be strictly in sequence). */
2917
if (link_socket_proto_connection_oriented (options.proto))
2918
options.replay_window = options.replay_time = 0;
2921
if (options.tls_server + options.tls_client +
2922
(options.shared_secret_file != NULL) > 1)
2923
msg (M_USAGE, "specify only one of --tls-server, --tls-client, or --secret");
2925
if (options.tls_server)
2927
notnull (options.dh_file, "DH file (--dh)");
2929
if (options.tls_server || options.tls_client)
2931
notnull (options.ca_file, "CA file (--ca)");
2932
notnull (options.cert_file, "certificate file (--cert)");
2933
notnull (options.priv_key_file, "private key file (--key)");
2934
if (first_time && options.askpass)
2935
pem_password_callback (NULL, 0, 0, NULL);
2940
* Make sure user doesn't specify any TLS options
2941
* when in non-TLS mode.
2944
#define MUST_BE_UNDEF(parm) if (options.parm != defaults.parm) msg (M_USAGE, err, #parm);
2946
const char err[] = "Parameter %s can only be specified in TLS-mode, i.e. where --tls-server or --tls-client is also specified.";
2948
MUST_BE_UNDEF (ca_file);
2949
MUST_BE_UNDEF (dh_file);
2950
MUST_BE_UNDEF (cert_file);
2951
MUST_BE_UNDEF (priv_key_file);
2952
MUST_BE_UNDEF (cipher_list);
2953
MUST_BE_UNDEF (tls_verify);
2954
MUST_BE_UNDEF (tls_remote);
2955
MUST_BE_UNDEF (tls_timeout);
2956
MUST_BE_UNDEF (renegotiate_bytes);
2957
MUST_BE_UNDEF (renegotiate_packets);
2958
MUST_BE_UNDEF (renegotiate_seconds);
2959
MUST_BE_UNDEF (handshake_window);
2960
MUST_BE_UNDEF (transition_window);
2961
MUST_BE_UNDEF (tls_auth_file);
2962
MUST_BE_UNDEF (single_session);
2963
MUST_BE_UNDEF (crl_file);
2964
MUST_BE_UNDEF (key_method);
2966
#undef MUST_BE_UNDEF
2967
#endif /* USE_CRYPTO */
2968
#endif /* USE_SSL */
2970
/* Become a daemon if requested */
2971
possibly_become_daemon (0, &options, first_time);
2973
/* show all option settings */
2974
show_settings (&options);
2976
/* set certain options as environmental variables */
2977
setenv_settings (&options);
2980
/* put a title on the top window bar */
2981
generate_window_title (options.config ? options.config : "");
2986
/* these objects are potentially persistent across SIGUSR1 resets */
2987
struct link_socket_addr usa;
2988
struct key_schedule ks;
2989
struct tuntap tuntap;
2990
struct packet_id_persist pid_persist;
2991
struct route_list route_list;
2992
struct http_proxy_info http_proxy;
2993
struct socks_proxy_info socks_proxy;
2995
/* print version number */
2996
msg (M_INFO, "%s", title_string);
3000
clear_tuntap (&tuntap);
3001
packet_id_persist_init (&pid_persist);
3002
clear_route_list (&route_list);
3004
CLEAR (socks_proxy);
3005
if (options.http_proxy_server)
3007
init_http_proxy (&http_proxy,
3008
options.http_proxy_server,
3009
options.http_proxy_port,
3010
options.http_proxy_retry,
3011
options.http_proxy_auth_method,
3012
options.http_proxy_auth_file);
3015
if (options.socks_proxy_server)
3017
init_socks_proxy (&socks_proxy,
3018
options.socks_proxy_server,
3019
options.socks_proxy_port,
3020
options.socks_proxy_retry);
3024
sig = openvpn (&options, &usa, &tuntap, &ks, &pid_persist, &route_list, &http_proxy, &socks_proxy, first_time);
3026
} while (sig == SIGUSR1);
3029
gc_collect (gc_level);
3031
} while (sig == SIGHUP);
3041
#if defined(MEASURE_TLS_HANDSHAKE_STATS) && defined(USE_CRYPTO) && defined(USE_SSL)
3042
show_tls_performance_stats();
3045
/* pop our garbage collection level */
3046
gc_free_level (gc_level);
3048
openvpn_exit (OPENVPN_EXIT_STATUS_GOOD); /* exit point */
3049
return 0; /* NOTREACHED */
3053
* Basic threading test.
3055
#if defined(USE_PTHREAD) && defined(USE_CRYPTO)
3057
test_crypto_thread (void *arg)
3059
struct link_socket_addr usa;
3060
struct tuntap tuntap;
3061
struct key_schedule ks;
3062
struct packet_id_persist pid_persist;
3063
struct route_list route_list;
3064
struct http_proxy_info http_proxy;
3065
struct socks_proxy_info socks_proxy;
3066
const struct options *opt = (struct options*) arg;
3068
/* print version number */
3069
msg (M_INFO, "%s", title_string);
3071
set_nice (opt->nice_work);
3074
clear_tuntap (&tuntap);
3075
packet_id_persist_init (&pid_persist);
3076
clear_route_list (&route_list);
3078
CLEAR (socks_proxy);
3079
openvpn (opt, &usa, &tuntap, &ks, &pid_persist, &route_list, &http_proxy, &socks_proxy, false);