OpenVPN
ping.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-2017 OpenVPN Technologies, 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, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #elif defined(_MSC_VER)
27 #include "config-msvc.h"
28 #endif
29 
30 #include "syshead.h"
31 
32 #include "ping.h"
33 
34 #include "memdbg.h"
35 
36 #include "ping-inline.h"
37 
38 /*
39  * This random string identifies an OpenVPN ping packet.
40  * It should be of sufficient length and randomness
41  * so as not to collide with other tunnel data.
42  *
43  * PING_STRING_SIZE must be sizeof (ping_string)
44  */
45 const uint8_t ping_string[] = {
46  0x2a, 0x18, 0x7b, 0xf3, 0x64, 0x1e, 0xb4, 0xcb,
47  0x07, 0xed, 0x2d, 0x0a, 0x98, 0x1f, 0xc7, 0x48
48 };
49 
50 /*
51  * Should we exit or restart due to ping (or other authenticated packet)
52  * not received in n seconds?
53  */
54 void
56 {
57  struct gc_arena gc = gc_new();
59  {
60  case PING_EXIT:
61  msg(M_INFO, "%sInactivity timeout (--ping-exit), exiting",
62  format_common_name(c, &gc));
64  c->sig->signal_text = "ping-exit";
65  break;
66 
67  case PING_RESTART:
68  msg(M_INFO, "%sInactivity timeout (--ping-restart), restarting",
69  format_common_name(c, &gc));
70  c->sig->signal_received = SIGUSR1; /* SOFT-SIGUSR1 -- Ping Restart */
71  c->sig->signal_text = "ping-restart";
72  break;
73 
74  default:
75  ASSERT(0);
76  }
77  gc_free(&gc);
78 }
79 
80 /*
81  * Should we ping the remote?
82  */
83 void
85 {
86  c->c2.buf = c->c2.buffers->aux_buf;
89  ASSERT(buf_write(&c->c2.buf, ping_string, sizeof(ping_string)));
90 
91  /*
92  * We will treat the ping like any other outgoing packet,
93  * encrypt, sign, etc.
94  */
95  encrypt_sign(c, true);
96  /* Set length to 0, so it won't be counted as activity */
97  c->c2.buf.len = 0;
98  dmsg(D_PING, "SENT PING");
99 }
void check_ping_send_dowork(struct context *c)
Definition: ping.c:84
struct options options
Options loaded from command line or configuration file.
Definition: openvpn.h:510
int ping_rec_timeout_action
Definition: options.h:261
#define M_INFO
Definition: errlevel.h:55
Contains all state information for one tunnel.
Definition: openvpn.h:508
static void gc_free(struct gc_arena *a)
Definition: buffer.h:990
struct buffer buf
Definition: openvpn.h:390
static bool buf_safe(const struct buffer *buf, int len)
Definition: buffer.h:515
#define ASSERT(x)
Definition: error.h:221
#define SIGUSR1
Definition: config-msvc.h:116
struct context_buffers * buffers
Definition: openvpn.h:382
struct signal_info * sig
Internal error signaling object.
Definition: openvpn.h:533
#define SIGTERM
Definition: config-msvc.h:118
#define PING_RESTART
Definition: options.h:260
void encrypt_sign(struct context *c, bool comp_frag)
Process a data channel packet that will be sent through a VPN tunnel.
Definition: forward.c:454
#define D_PING
Definition: errlevel.h:140
int len
Length in bytes of the actual content within the allocated memory.
Definition: buffer.h:66
static bool buf_write(struct buffer *dest, const void *src, int size)
Definition: buffer.h:663
static struct gc_arena gc_new(void)
Definition: buffer.h:982
#define FRAME_HEADROOM(f)
Definition: mtu.h:187
struct frame frame
Definition: openvpn.h:265
const uint8_t ping_string[]
Definition: ping.c:45
struct context_2 c2
Level 2 context.
Definition: openvpn.h:547
#define dmsg
Definition: error.h:174
#define PING_EXIT
Definition: options.h:259
struct buffer aux_buf
Definition: openvpn.h:96
unsigned __int8 uint8_t
Definition: config-msvc.h:123
volatile int signal_received
Definition: sig.h:45
#define msg
Definition: error.h:173
#define buf_init(buf, offset)
Definition: buffer.h:198
#define MAX_RW_SIZE_TUN(f)
Definition: mtu.h:180
void check_ping_restart_dowork(struct context *c)
Definition: ping.c:55
const char * format_common_name(struct context *c, struct gc_arena *gc)
Definition: init.c:1225
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
const char * signal_text
Definition: sig.h:47