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-2024 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#ifndef FRAGMENT_H
25#define FRAGMENT_H
26
33#ifdef ENABLE_FRAGMENT
34
41#include "common.h"
42#include "buffer.h"
43#include "interval.h"
44#include "mtu.h"
45#include "shaper.h"
46#include "error.h"
47
48
49#define N_FRAG_BUF 25
54#define FRAG_TTL_SEC 10
57#define FRAG_WAKEUP_INTERVAL 5
61/**************************************************************************/
65struct fragment {
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
116
117
140#define N_SEQ_ID 256
152#define MAX_FRAG_PKT_SIZE 65536
180};
181
182
183/**************************************************************************/
/*************************************/
187
188typedef uint32_t fragment_header_type;
192#define hton_fragment_header_type(x) htonl(x)
196#define ntoh_fragment_header_type(x) ntohl(x)
200#define FRAG_TYPE_MASK 0x00000003
202#define FRAG_TYPE_SHIFT 0
204#define FRAG_WHOLE 0
206#define FRAG_YES_NOTLAST 1
209#define FRAG_YES_LAST 2
212#define FRAG_TEST 3
217#define FRAG_SEQ_ID_MASK 0x000000ff
219#define FRAG_SEQ_ID_SHIFT 2
221#define FRAG_ID_MASK 0x0000001f
223#define FRAG_ID_SHIFT 10
227 * FRAG_SIZE 14 bits
228 *
229 * IF FRAG_YES_LAST (FRAG_SIZE):
230 * The max size of a %fragment. If a %fragment is not the last %fragment in the packet,
231 * then the %fragment size is guaranteed to be equal to the max %fragment size. Therefore,
232 * max_frag_size is only sent over the wire if FRAG_LAST is set. Otherwise it is assumed
233 * to be the actual %fragment size received.
234 */
235#define FRAG_SIZE_MASK 0x00003fff
237#define FRAG_SIZE_SHIFT 15
239#define FRAG_SIZE_ROUND_SHIFT 2
240#define FRAG_SIZE_ROUND_MASK ((1 << FRAG_SIZE_ROUND_SHIFT) - 1)
244 * FRAG_EXTRA 16 bits
245 *
246 * IF FRAG_WHOLE or FRAG_YES_NOTLAST, these 16 bits are available (not currently used)
247 */
248#define FRAG_EXTRA_MASK 0x0000ffff
250#define FRAG_EXTRA_SHIFT 15
/********************************************/
254
255
256/**************************************************************************/
/************/
258
272
273
283void fragment_frame_init(struct fragment_master *f, const struct frame *frame);
284
285
291void fragment_free(struct fragment_master *f);
292
/*******************/
294
295
296/**************************************************************************/
340void fragment_incoming(struct fragment_master *f, struct buffer *buf,
341 const struct frame *frame);
342
346/**************************************************************************/
391void fragment_outgoing(struct fragment_master *f, struct buffer *buf,
392 const struct frame *frame);
393
421bool fragment_ready_to_send(struct fragment_master *f, struct buffer *buf,
422 const struct frame *frame);
423
435static inline bool
437{
438 return f->outgoing.len > 0;
439}
440
444void fragment_wakeup(struct fragment_master *f, struct frame *frame);
445
446
447/**************************************************************************/
/******************/
449
462static inline void
463fragment_housekeeping(struct fragment_master *f, struct frame *frame, struct timeval *tv)
464{
465 if (event_timeout_trigger(&f->wakeup, tv, ETT_DEFAULT))
466 {
468 }
469}
470
/*************************/
472
473
/****************************************/
475
476
477#endif /* ifdef ENABLE_FRAGMENT */
478#endif /* ifndef FRAGMENT_H */
uint32_t fragment_header_type
Fragmentation information is stored in a 32-bit packet header.
Definition fragment.h:188
static void fragment_housekeeping(struct fragment_master *f, struct frame *frame, struct timeval *tv)
Perform housekeeping of a fragment_master structure.
Definition fragment.h:465
void fragment_frame_init(struct fragment_master *f, const struct frame *frame)
Allocate internal packet buffers for a fragment_master structure.
Definition fragment.c:122
static bool fragment_outgoing_defined(struct fragment_master *f)
Check whether a fragment_master structure contains fragments ready to be sent.
Definition fragment.h:438
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:321
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:136
#define N_FRAG_BUF
Number of packet buffers for reassembling incoming fragmented packets.
Definition fragment.h:49
void fragment_wakeup(struct fragment_master *f, struct frame *frame)
Definition fragment.c:429
struct fragment_master * fragment_init(struct frame *frame)
Allocate and initialize a fragment_master structure.
Definition fragment.c:89
void fragment_free(struct fragment_master *f)
Free a fragment_master structure and its internal packet buffers.
Definition fragment.c:113
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:376
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:43
#define ETT_DEFAULT
Definition interval.h:224
Wrapper structure for dynamically allocated memory.
Definition buffer.h:61
List of fragment structures for reassembling multiple incoming packets concurrently.
Definition fragment.h:95
int seq_id
Highest fragmentation sequence ID of the packets currently being reassembled.
Definition fragment.h:96
int index
Index of the packet being reassembled with the highest fragmentation sequence ID into the fragment_li...
Definition fragment.h:99
struct fragment fragments[N_FRAG_BUF]
Array of reassembly structures, each can contain one whole packet.
Definition fragment.h:114
Fragmentation and reassembly state for one VPN tunnel instance.
Definition fragment.h:136
struct buffer outgoing
Buffer containing the remaining parts of the fragmented packet being sent.
Definition fragment.h:170
int outgoing_frag_id
The fragment ID of the next part to be sent.
Definition fragment.h:167
struct event_timeout wakeup
Timeout structure used by the main event loop to know when to do fragmentation housekeeping.
Definition fragment.h:137
struct fragment_list incoming
List of structures for reassembling incoming packets.
Definition fragment.h:177
int outgoing_seq_id
Fragment sequence ID of the current fragmented packet waiting to be sent.
Definition fragment.h:144
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:155
struct buffer outgoing_return
Buffer used by fragment_ready_to_send() to return a part to send.
Definition fragment.h:172
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:98