OpenVPN
fragment.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-2025 OpenVPN Inc <sales@openvpn.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifndef FRAGMENT_H
24#define FRAGMENT_H
25
32#ifdef ENABLE_FRAGMENT
33
40#include "common.h"
41#include "buffer.h"
42#include "interval.h"
43#include "mtu.h"
44#include "shaper.h"
45#include "error.h"
46
47
48#define N_FRAG_BUF 25
53#define FRAG_TTL_SEC 10
56#define FRAG_WAKEUP_INTERVAL 5
60/**************************************************************************/
65{
66 bool defined;
71#define FRAG_MAP_MASK 0xFFFFFFFF
73#define MAX_FRAGS 32
74 unsigned int map;
84 time_t timestamp;
86 struct buffer buf;
88};
89
90
96{
105 int index;
106
118};
119
120
140{
144#define N_SEQ_ID 256
156#define MAX_FRAG_PKT_SIZE 65536
182};
183
184
185/**************************************************************************/
/*************************************/
190
191typedef uint32_t fragment_header_type;
194#define hton_fragment_header_type(x) htonl(x)
197#define ntoh_fragment_header_type(x) ntohl(x)
200#define FRAG_TYPE_MASK 0x00000003
201#define FRAG_TYPE_SHIFT 0
203#define FRAG_WHOLE 0
204#define FRAG_YES_NOTLAST 1
207#define FRAG_YES_LAST 2
209#define FRAG_TEST 3
213#define FRAG_SEQ_ID_MASK 0x000000ff
214#define FRAG_SEQ_ID_SHIFT 2
216#define FRAG_ID_MASK 0x0000001f
217#define FRAG_ID_SHIFT 10
220/*
221 * FRAG_SIZE 14 bits
222 *
223 * IF FRAG_YES_LAST (FRAG_SIZE):
224 * The max size of a %fragment. If a %fragment is not the last %fragment in the packet,
225 * then the %fragment size is guaranteed to be equal to the max %fragment size. Therefore,
226 * max_frag_size is only sent over the wire if FRAG_LAST is set. Otherwise it is assumed
227 * to be the actual %fragment size received.
228 */
229#define FRAG_SIZE_MASK 0x00003fff
230#define FRAG_SIZE_SHIFT 15
231#define FRAG_SIZE_ROUND_SHIFT 2
232#define FRAG_SIZE_ROUND_MASK ((1 << FRAG_SIZE_ROUND_SHIFT) - 1)
236 * FRAG_EXTRA 16 bits
237 *
238 * IF FRAG_WHOLE or FRAG_YES_NOTLAST, these 16 bits are available (not currently used)
239 */
240#define FRAG_EXTRA_MASK 0x0000ffff
241#define FRAG_EXTRA_SHIFT 15
/********************************************/
244
245
246/**************************************************************************/
/************/
248
262
263
273void fragment_frame_init(struct fragment_master *f, const struct frame *frame);
274
275
281void fragment_free(struct fragment_master *f);
282
/*******************/
284
285
286/**************************************************************************/
331void fragment_incoming(struct fragment_master *f, struct buffer *buf, const struct frame *frame);
332
336/**************************************************************************/
382void fragment_outgoing(struct fragment_master *f, struct buffer *buf, const struct frame *frame);
383
411bool fragment_ready_to_send(struct fragment_master *f, struct buffer *buf,
412 const struct frame *frame);
413
425static inline bool
427{
428 return f->outgoing.len > 0;
429}
430
434void fragment_wakeup(struct fragment_master *f, struct frame *frame);
435
436
437/**************************************************************************/
/******************/
439
453static inline void
454fragment_housekeeping(struct fragment_master *f, struct frame *frame, struct timeval *tv)
455{
457 {
459 }
460}
461
/*************************/
463
464
/****************************************/
466
467
468#endif /* ifdef ENABLE_FRAGMENT */
469#endif /* ifndef FRAGMENT_H */
uint32_t fragment_header_type
Fragmentation information is stored in a 32-bit packet header.
Definition fragment.h:191
static void fragment_housekeeping(struct fragment_master *f, struct frame *frame, struct timeval *tv)
Perform housekeeping of a fragment_master structure.
Definition fragment.h:456
void fragment_frame_init(struct fragment_master *f, const struct frame *frame)
Allocate internal packet buffers for a fragment_master structure.
Definition fragment.c:125
static bool fragment_outgoing_defined(struct fragment_master *f)
Check whether a fragment_master structure contains fragments ready to be sent.
Definition fragment.h:428
void fragment_outgoing(struct fragment_master *f, struct buffer *buf, const struct frame *frame)
Process an outgoing packet, which may or may not need to be fragmented.
Definition fragment.c:313
void fragment_incoming(struct fragment_master *f, struct buffer *buf, const struct frame *frame)
Process an incoming packet, which may or may not be fragmented.
Definition fragment.c:139
#define N_FRAG_BUF
Number of packet buffers for reassembling incoming fragmented packets.
Definition fragment.h:48
void fragment_wakeup(struct fragment_master *f, struct frame *frame)
Definition fragment.c:414
struct fragment_master * fragment_init(struct frame *frame)
Allocate and initialize a fragment_master structure.
Definition fragment.c:92
void fragment_free(struct fragment_master *f)
Free a fragment_master structure and its internal packet buffers.
Definition fragment.c:116
bool fragment_ready_to_send(struct fragment_master *f, struct buffer *buf, const struct frame *frame)
Check whether outgoing fragments are ready to be send, and if so make one available.
Definition fragment.c:363
bool event_timeout_trigger(struct event_timeout *et, struct timeval *tv, const int et_const_retry)
This is the principal function for testing and triggering recurring timers.
Definition interval.c:42
#define ETT_DEFAULT
Definition interval.h:222
Wrapper structure for dynamically allocated memory.
Definition buffer.h:60
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:65
List of fragment structures for reassembling multiple incoming packets concurrently.
Definition fragment.h:96
int seq_id
Highest fragmentation sequence ID of the packets currently being reassembled.
Definition fragment.h:100
int index
Index of the packet being reassembled with the highest fragmentation sequence ID into the fragment_li...
Definition fragment.h:105
struct fragment fragments[N_FRAG_BUF]
Array of reassembly structures, each can contain one whole packet.
Definition fragment.h:117
Fragmentation and reassembly state for one VPN tunnel instance.
Definition fragment.h:140
struct buffer outgoing
Buffer containing the remaining parts of the fragmented packet being sent.
Definition fragment.h:172
int outgoing_frag_id
The fragment ID of the next part to be sent.
Definition fragment.h:169
struct event_timeout wakeup
Timeout structure used by the main event loop to know when to do fragmentation housekeeping.
Definition fragment.h:141
struct fragment_list incoming
List of structures for reassembling incoming packets.
Definition fragment.h:179
int outgoing_seq_id
Fragment sequence ID of the current fragmented packet waiting to be sent.
Definition fragment.h:148
int outgoing_frag_size
Size in bytes of each part to be sent, except for the last part which may be smaller.
Definition fragment.h:157
struct buffer outgoing_return
Buffer used by fragment_ready_to_send() to return a part to send.
Definition fragment.h:174
Structure for reassembling one incoming fragmented packet.
Definition fragment.h:65
int max_frag_size
Maximum size of each fragment.
Definition fragment.h:69
unsigned int map
Reassembly map for recording which fragments have been received.
Definition fragment.h:74
bool defined
Whether reassembly is currently taking place in this structure.
Definition fragment.h:66
time_t timestamp
Timestamp for time-to-live purposes.
Definition fragment.h:84
struct buffer buf
Buffer in which received datagrams are reassembled.
Definition fragment.h:86
Packet geometry parameters.
Definition mtu.h:103