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