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