OpenVPN
reliable.h
Go to the documentation of this file.
1 /*
2  * OpenVPN -- An application to securely tunnel IP networks
3  * over a single 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 
31 #ifndef RELIABLE_H
32 #define RELIABLE_H
33 
34 #include "basic.h"
35 #include "buffer.h"
36 #include "packet_id.h"
37 #include "session_id.h"
38 #include "mtu.h"
39 
44 #define RELIABLE_ACK_SIZE 8
49 #define RELIABLE_CAPACITY 12
53 #define N_ACK_RETRANSMIT 3
61 struct reliable_ack
62 {
63  int len;
65 };
66 
67 /* The size of the ACK header */
68 #define ACK_SIZE(n) (sizeof(uint8_t) + ((n) ? SID_SIZE : 0) + sizeof(packet_id_type) * (n))
69 
75 {
76  bool active;
78  time_t next_try;
80  size_t n_acks; /* Number of acks received for packets with higher PID.
81  * Used for fast retransmission when there were at least
82  * N_ACK_RETRANSMIT. */
83  int opcode;
84  struct buffer buf;
85 };
86 
91 struct reliable
92 {
93  int size;
96  int offset;
97  bool hold; /* don't xmit until reliable_schedule_now is called */
99 };
100 
101 
102 /**************************************************************************/
124 bool reliable_ack_read(struct reliable_ack *ack,
125  struct buffer *buf, const struct session_id *sid);
126 
127 
145 bool
146 reliable_ack_parse(struct buffer *buf, struct reliable_ack *ack,
147  struct session_id *session_id_remote);
148 
156 void reliable_send_purge(struct reliable *rel, const struct reliable_ack *ack);
157 
161 /**************************************************************************/
175 static inline bool
177 {
178  return !ack->len;
179 }
180 
188 static inline int
190 {
191  return ack->len;
192 }
193 
194 
216 bool reliable_ack_write(struct reliable_ack *ack,
217  struct reliable_ack *ack_mru,
218  struct buffer *buf,
219  const struct session_id *sid, int max, bool prepend);
220 
224 /**************************************************************************/
240 void reliable_init(struct reliable *rel, int buf_size, int offset, int array_size, bool hold);
241 
249 void reliable_free(struct reliable *rel);
250 
254 /**************************************************************************/
268 bool reliable_can_get(const struct reliable *rel);
269 
281 bool reliable_not_replay(const struct reliable *rel, packet_id_type id);
282 
305 bool reliable_wont_break_sequentiality(const struct reliable *rel, packet_id_type id);
306 
318 
330 struct buffer *reliable_get_buf(struct reliable *rel);
331 
341 void reliable_mark_active_incoming(struct reliable *rel, struct buffer *buf,
342  packet_id_type pid, int opcode);
343 
358 
362 /**************************************************************************/
376 
377 
378 
386 void
387 copy_acks_to_mru(struct reliable_ack *ack, struct reliable_ack *ack_mru, int n);
388 
389 
396 void reliable_mark_deleted(struct reliable *rel, struct buffer *buf);
397 
401 /**************************************************************************/
418 
419 
430 int
432 
444 void reliable_mark_active_outgoing(struct reliable *rel, struct buffer *buf, int opcode);
445 
449 /**************************************************************************/
465 bool reliable_can_send(const struct reliable *rel);
466 
484 struct buffer *reliable_send(struct reliable *rel, int *opcode);
485 
489 /**************************************************************************/
503 bool reliable_empty(const struct reliable *rel);
504 
516 interval_t reliable_send_timeout(const struct reliable *rel);
517 
525 void reliable_schedule_now(struct reliable *rel);
526 
527 void reliable_debug_print(const struct reliable *rel, char *desc);
528 
529 /* set sending timeout (after this time we send again until ACK) */
530 static inline void
532 {
533  rel->initial_timeout = timeout;
534 }
535 
536 /* print a reliable ACK record coming off the wire */
537 const char *reliable_ack_print(struct buffer *buf, bool verbose, struct gc_arena *gc);
538 
539 void reliable_ack_debug_print(const struct reliable_ack *ack, char *desc);
540 
547 #endif /* RELIABLE_H */
reliable_schedule_now
void reliable_schedule_now(struct reliable *rel)
Reschedule all entries of a reliable structure to be ready for (re)sending immediately.
Definition: reliable.c:702
reliable_ack_acknowledge_packet_id
bool reliable_ack_acknowledge_packet_id(struct reliable_ack *ack, packet_id_type pid)
Record a packet ID for later acknowledgment.
Definition: reliable.c:132
reliable::hold
bool hold
Definition: reliable.h:97
RELIABLE_ACK_SIZE
#define RELIABLE_ACK_SIZE
The maximum number of packet IDs waiting to be acknowledged which can be stored in one reliable_ack s...
Definition: reliable.h:44
interval_t
int interval_t
Definition: common.h:36
reliable_empty
bool reliable_empty(const struct reliable *rel)
Check whether a reliable structure is empty.
Definition: reliable.c:393
reliable_entry::buf
struct buffer buf
Definition: reliable.h:84
reliable_get_buf
struct buffer * reliable_get_buf(struct reliable *rel)
Get the buffer of a free reliable entry in which to store a packet.
Definition: reliable.c:536
reliable::array
struct reliable_entry array[RELIABLE_CAPACITY]
Definition: reliable.h:98
reliable_ack_parse
bool reliable_ack_parse(struct buffer *buf, struct reliable_ack *ack, struct session_id *session_id_remote)
Parse an acknowledgment record from a received packet.
Definition: reliable.c:173
session_id.h
reliable_debug_print
void reliable_debug_print(const struct reliable *rel, char *desc)
reliable_entry::n_acks
size_t n_acks
Definition: reliable.h:80
reliable_set_timeout
static void reliable_set_timeout(struct reliable *rel, interval_t timeout)
Definition: reliable.h:531
reliable_entry::opcode
int opcode
Definition: reliable.h:83
packet_id
Definition: packet_id.h:200
session_id
Definition: session_id.h:38
mtu.h
reliable_ack::len
int len
Definition: reliable.h:63
reliable_get_num_output_sequenced_available
int reliable_get_num_output_sequenced_available(struct reliable *rel)
Counts the number of free buffers in output that can be potentially used for sending.
Definition: reliable.c:552
reliable_mark_active_outgoing
void reliable_mark_active_outgoing(struct reliable *rel, struct buffer *buf, int opcode)
Mark the reliable entry associated with the given buffer as active outgoing.
Definition: reliable.c:791
packet_id.h
reliable_get_buf_output_sequenced
struct buffer * reliable_get_buf_output_sequenced(struct reliable *rel)
Get the buffer of free reliable entry and check whether the outgoing acknowledgment sequence is still...
Definition: reliable.c:583
reliable_not_replay
bool reliable_not_replay(const struct reliable *rel, packet_id_type id)
Check that a received packet's ID is not a replay.
Definition: reliable.c:489
reliable_entry::next_try
time_t next_try
Definition: reliable.h:78
reliable_send_timeout
interval_t reliable_send_timeout(const struct reliable *rel)
Determined how many seconds until the earliest resend should be attempted.
Definition: reliable.c:721
reliable_mark_deleted
void reliable_mark_deleted(struct reliable *rel, struct buffer *buf)
Remove an entry from a reliable structure.
Definition: reliable.c:818
reliable_ack_print
const char * reliable_ack_print(struct buffer *buf, bool verbose, struct gc_arena *gc)
Definition: reliable.c:314
packet_id_type
uint32_t packet_id_type
Definition: packet_id.h:44
reliable_ack_debug_print
void reliable_ack_debug_print(const struct reliable_ack *ack, char *desc)
buffer
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
RELIABLE_CAPACITY
#define RELIABLE_CAPACITY
The maximum number of packets that the reliability layer for one VPN tunnel in one direction can stor...
Definition: reliable.h:49
reliable::packet_id
packet_id_type packet_id
Definition: reliable.h:95
reliable_ack_empty
static bool reliable_ack_empty(struct reliable_ack *ack)
Check whether an acknowledgment structure contains any packet IDs to be acknowledged.
Definition: reliable.h:176
buffer.h
reliable_send
struct buffer * reliable_send(struct reliable *rel, int *opcode)
Get the next packet to send to the remote peer.
Definition: reliable.c:663
gc_arena
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
reliable::offset
int offset
Offset of the bufs in the reliable_entry array.
Definition: reliable.h:96
reliable
The reliability layer storage structure for one VPN tunnel's control channel in one direction.
Definition: reliable.h:91
reliable_entry::packet_id
packet_id_type packet_id
Definition: reliable.h:79
reliable_ack_read_packet_id
bool reliable_ack_read_packet_id(struct buffer *buf, packet_id_type *pid)
Read the packet ID of a received packet.
Definition: reliable.c:114
reliable_entry::active
bool active
Definition: reliable.h:76
reliable_wont_break_sequentiality
bool reliable_wont_break_sequentiality(const struct reliable *rel, packet_id_type id)
Check that a received packet's ID can safely be stored in the reliable structure's processing window.
Definition: reliable.c:516
basic.h
reliable_can_get
bool reliable_can_get(const struct reliable *rel)
Check whether a reliable structure has any free buffers available for use.
Definition: reliable.c:470
reliable_ack_read
bool reliable_ack_read(struct reliable_ack *ack, struct buffer *buf, const struct session_id *sid)
Read an acknowledgment record from a received packet.
Definition: reliable.c:149
reliable_mark_active_incoming
void reliable_mark_active_incoming(struct reliable *rel, struct buffer *buf, packet_id_type pid, int opcode)
Mark the reliable entry associated with the given buffer as active incoming.
Definition: reliable.c:758
reliable_ack_outstanding
static int reliable_ack_outstanding(struct reliable_ack *ack)
Returns the number of packets that need to be acked.
Definition: reliable.h:189
reliable::size
int size
Definition: reliable.h:93
reliable_get_entry_sequenced
struct reliable_entry * reliable_get_entry_sequenced(struct reliable *rel)
Get the buffer of the next sequential and active entry.
Definition: reliable.c:619
reliable_can_send
bool reliable_can_send(const struct reliable *rel)
Check whether a reliable structure has any active entries ready to be (re)sent.
Definition: reliable.c:635
reliable_init
void reliable_init(struct reliable *rel, int buf_size, int offset, int array_size, bool hold)
Initialize a reliable structure.
Definition: reliable.c:358
reliable::initial_timeout
interval_t initial_timeout
Definition: reliable.h:94
reliable_ack_write
bool reliable_ack_write(struct reliable_ack *ack, struct reliable_ack *ack_mru, struct buffer *buf, const struct session_id *sid, int max, bool prepend)
Write a packet ID acknowledgment record to a buffer.
Definition: reliable.c:255
reliable_send_purge
void reliable_send_purge(struct reliable *rel, const struct reliable_ack *ack)
Remove acknowledged packets from a reliable structure.
Definition: reliable.c:409
copy_acks_to_mru
void copy_acks_to_mru(struct reliable_ack *ack, struct reliable_ack *ack_mru, int n)
Copies the first n acks from ack to ack_mru.
Definition: reliable.c:211
reliable_ack
The acknowledgment structure in which packet IDs are stored for later acknowledgment.
Definition: reliable.h:61
reliable_free
void reliable_free(struct reliable *rel)
Free allocated memory associated with a reliable structure and the pointer itself.
Definition: reliable.c:376
reliable_entry::timeout
interval_t timeout
Definition: reliable.h:77
reliable_entry
The structure in which the reliability layer stores a single incoming or outgoing packet.
Definition: reliable.h:74