OpenVPN
comp.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/*
24 * Generic compression support. Currently we support
25 * LZO 2 and LZ4.
26 */
27#ifndef OPENVPN_COMP_H
28#define OPENVPN_COMP_H
29
30/* We always parse all compression options, so we include these defines/struct
31 * outside of the USE_COMP define */
32
33/* Compression flags */
34/* Removed
35 #define COMP_F_ADAPTIVE (1u<<0) / * COMP_ALG_LZO only * /
36 #define COMP_F_ALLOW_COMPRESS (1u<<1) / * not only incoming is compressed but also outgoing *
37 /
38 */
40#define COMP_F_SWAP (1u << 2)
42#define COMP_F_ADVERTISE_STUBS_ONLY (1u << 3)
45#define COMP_F_ALLOW_STUB_ONLY (1u << 4)
47#define COMP_F_MIGRATE (1u << 5)
49#define COMP_F_ALLOW_ASYM (1u << 6)
51#define COMP_F_ALLOW_NOCOMP_ONLY (1u << 7)
52
53/* algorithms */
54#define COMP_ALG_UNDEF 0
56#define COMP_ALG_STUB 1
57#define COMP_ALG_LZO 2
58#define COMP_ALG_SNAPPY 3
59#define COMP_ALG_LZ4 4
62/* algorithm v2 */
63#define COMP_ALGV2_UNCOMPRESSED 10
64#define COMP_ALGV2_LZ4 11
65/*
66 #define COMP_ALGV2_LZO 12
67 #define COMP_ALGV2_SNAPPY 13
68 */
69
70/*
71 * Information that basically identifies a compression
72 * algorithm and related flags.
73 */
75{
76 int alg;
77 unsigned int flags;
78};
79
80static inline bool
82{
83 return info->alg != COMP_ALGV2_UNCOMPRESSED && info->alg != COMP_ALG_STUB
84 && info->alg != COMP_ALG_UNDEF;
85}
86
87#include "error.h"
88
95
96#ifdef USE_COMP
97#include "buffer.h"
98#include "mtu.h"
99#include "common.h"
100#include "status.h"
101
102/*
103 * Length of prepended prefix on compressed packets
104 */
105#define COMP_PREFIX_LEN 1
106
107/*
108 * Prefix bytes
109 */
110
111/* V1 on wire codes */
112/* Initial command byte to tell our peer if we compressed */
113#define LZO_COMPRESS_BYTE 0x66
114#define LZ4_COMPRESS_BYTE 0x69
115#define NO_COMPRESS_BYTE 0xFA
117#define NO_COMPRESS_BYTE_SWAP 0xFB
118
119/* V2 on wire code */
120#define COMP_ALGV2_INDICATOR_BYTE 0x50
121#define COMP_ALGV2_UNCOMPRESSED_BYTE 0
122#define COMP_ALGV2_LZ4_BYTE 1
123#define COMP_ALGV2_LZO_BYTE 2
124#define COMP_ALGV2_SNAPPY_BYTE 3
125
126/*
127 * Compress worst case size expansion (for any algorithm)
128 *
129 * LZO: len + len/8 + 128 + 3
130 * Snappy: len + len/6 + 32
131 * LZ4: len + len/255 + 16 (LZ4_COMPRESSBOUND(len))
132 */
133#define COMP_EXTRA_BUFFER(len) ((len) / 6 + 128 + 3 + COMP_PREFIX_LEN)
134
135/*
136 * Don't try to compress any packet smaller than this.
137 */
138#define COMPRESS_THRESHOLD 100
139
140/* Forward declaration of compression context */
141struct compress_context;
142
143/*
144 * Virtual methods and other static info for each compression algorithm
145 */
146struct compress_alg
147{
148 const char *name;
149 void (*compress_init)(struct compress_context *compctx);
150 void (*compress_uninit)(struct compress_context *compctx);
151 void (*compress)(struct buffer *buf, struct buffer work, struct compress_context *compctx,
152 const struct frame *frame);
153
154 void (*decompress)(struct buffer *buf, struct buffer work, struct compress_context *compctx,
155 const struct frame *frame);
156};
157
158/*
159 * Headers for each compression implementation
160 */
161#ifdef ENABLE_LZO
162#include "lzo.h"
163#endif
164
165#ifdef ENABLE_LZ4
166#include "comp-lz4.h"
167#endif
168
169/*
170 * Workspace union of all supported compression algorithms
171 */
172union compress_workspace_union
173{
174#ifdef ENABLE_LZO
175 struct lzo_compress_workspace lzo;
176#endif
177#ifdef ENABLE_LZ4
178 struct lz4_workspace lz4;
179#endif
180};
181
182/*
183 * Context for active compression session
184 */
185struct compress_context
186{
187 unsigned int flags;
188 struct compress_alg alg;
189 union compress_workspace_union wu;
190
191 /* statistics */
192 counter_type pre_decompress;
193 counter_type post_decompress;
194 counter_type pre_compress;
195 counter_type post_compress;
196};
197
198extern const struct compress_alg comp_stub_alg;
199extern const struct compress_alg compv2_stub_alg;
200
201struct compress_context *comp_init(const struct compress_options *opt);
202
203void comp_uninit(struct compress_context *compctx);
204
205void comp_print_stats(const struct compress_context *compctx, struct status_output *so);
206
207void comp_generate_peer_info_string(const struct compress_options *opt, struct buffer *out);
208
209void compv2_escape_data_ifneeded(struct buffer *buf);
210
211static inline bool
212comp_enabled(const struct compress_options *info)
213{
214 return info->alg != COMP_ALG_UNDEF;
215}
216#endif /* USE_COMP */
217#endif /* ifndef OPENVPN_COMP_H */
uint64_t counter_type
Definition common.h:29
bool check_compression_settings_valid(struct compress_options *info, msglvl_t msglevel)
Checks if the compression settings are valid.
Definition comp.c:162
#define COMP_ALGV2_UNCOMPRESSED
Definition comp.h:63
#define COMP_ALG_STUB
support compression command byte and framing without actual compression
Definition comp.h:56
static bool comp_non_stub_enabled(const struct compress_options *info)
Definition comp.h:81
#define COMP_ALG_UNDEF
Definition comp.h:54
Data Channel Compression module header file.
unsigned int msglvl_t
Definition error.h:77
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
unsigned int flags
Definition comp.h:77
Packet geometry parameters.
Definition mtu.h:103