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-2023 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, 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 #endif
27 
28 #include "syshead.h"
29 
30 #include "ping.h"
31 
32 #include "memdbg.h"
33 
34 
35 /*
36  * This random string identifies an OpenVPN ping packet.
37  * It should be of sufficient length and randomness
38  * so as not to collide with other tunnel data.
39  *
40  * PING_STRING_SIZE must be sizeof (ping_string)
41  */
42 const uint8_t ping_string[] = {
43  0x2a, 0x18, 0x7b, 0xf3, 0x64, 0x1e, 0xb4, 0xcb,
44  0x07, 0xed, 0x2d, 0x0a, 0x98, 0x1f, 0xc7, 0x48
45 };
46 
47 void
49 {
50  struct gc_arena gc = gc_new();
52  {
53  case PING_EXIT:
54  msg(M_INFO, "%sInactivity timeout (--ping-exit), exiting",
55  format_common_name(c, &gc));
56  register_signal(c->sig, SIGTERM, "ping-exit");
57  break;
58 
59  case PING_RESTART:
60  msg(M_INFO, "%sInactivity timeout (--ping-restart), restarting",
61  format_common_name(c, &gc));
62  register_signal(c->sig, SIGUSR1, "ping-restart");
63  break;
64 
65  default:
66  ASSERT(0);
67  }
68  gc_free(&gc);
69 }
70 
71 /*
72  * Should we ping the remote?
73  */
74 void
76 {
77  c->c2.buf = c->c2.buffers->aux_buf;
80  ASSERT(buf_write(&c->c2.buf, ping_string, sizeof(ping_string)));
81 
82  /*
83  * We will treat the ping like any other outgoing packet,
84  * encrypt, sign, etc.
85  */
86  encrypt_sign(c, true);
87  /* Set length to 0, so it won't be counted as activity */
88  c->c2.buf.len = 0;
89  dmsg(D_PING, "SENT PING");
90 }
buf_safe
static bool buf_safe(const struct buffer *buf, size_t len)
Definition: buffer.h:538
M_INFO
#define M_INFO
Definition: errlevel.h:55
gc_new
static struct gc_arena gc_new(void)
Definition: buffer.h:1031
buffer::len
int len
Length in bytes of the actual content within the allocated memory.
Definition: buffer.h:66
context_2::buf
struct buffer buf
Definition: openvpn.h:378
buf_init
#define buf_init(buf, offset)
Definition: buffer.h:209
context
Contains all state information for one tunnel.
Definition: openvpn.h:476
dmsg
#define dmsg(flags,...)
Definition: error.h:154
ping_string
const uint8_t ping_string[]
Definition: ping.c:42
context_2::buffers
struct context_buffers * buffers
Definition: openvpn.h:370
check_ping_send_dowork
void check_ping_send_dowork(struct context *c)
Definition: ping.c:75
PING_RESTART
#define PING_RESTART
Definition: options.h:341
context::c2
struct context_2 c2
Level 2 context.
Definition: openvpn.h:517
ASSERT
#define ASSERT(x)
Definition: error.h:201
ping.h
encrypt_sign
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:604
frame::payload_size
int payload_size
the maximum size that a payload that our buffers can hold from either tun device or network link.
Definition: mtu.h:102
D_PING
#define D_PING
Definition: errlevel.h:144
context::options
struct options options
Options loaded from command line or configuration file.
Definition: openvpn.h:478
format_common_name
const char * format_common_name(struct context *c, struct gc_arena *gc)
Definition: init.c:1293
trigger_ping_timeout_signal
void trigger_ping_timeout_signal(struct context *c)
Trigger the correct signal on a –ping timeout depending if –ping-exit is set (SIGTERM) or not (SIGUSR...
Definition: ping.c:48
buf_write
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Definition: buffer.h:686
context_buffers::aux_buf
struct buffer aux_buf
Definition: openvpn.h:97
frame::buf
struct frame::@6 buf
context_2::frame
struct frame frame
Definition: openvpn.h:251
syshead.h
gc_arena
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
context::sig
struct signal_info * sig
Internal error signaling object.
Definition: openvpn.h:503
PING_EXIT
#define PING_EXIT
Definition: options.h:340
gc_free
static void gc_free(struct gc_arena *a)
Definition: buffer.h:1039
config.h
register_signal
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:231
memdbg.h
options::ping_rec_timeout_action
int ping_rec_timeout_action
Definition: options.h:342
msg
#define msg(flags,...)
Definition: error.h:150
frame::headroom
int headroom
the headroom in the buffer, this is choosen to allow all potential header to be added before the pack...
Definition: mtu.h:108