OpenVPN
comp.c
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-2026 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#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include "syshead.h"
28
29#include "comp.h"
30#include "error.h"
31
32#ifdef USE_COMP
33
34#include "otime.h"
35
36#include "memdbg.h"
37
38struct compress_context *
39comp_init(const struct compress_options *opt)
40{
41 const struct compress_alg *alg;
42 switch (opt->alg)
43 {
44 case COMP_ALG_STUB:
45 alg = &comp_stub_alg;
46 break;
47
49 alg = &compv2_stub_alg;
50 break;
51
52#ifdef ENABLE_LZO
53 case COMP_ALG_LZO:
54 alg = &lzo_alg;
55 break;
56
57#endif
58#ifdef ENABLE_LZ4
59 case COMP_ALG_LZ4:
60 alg = &lz4_alg;
61 break;
62
63 case COMP_ALGV2_LZ4:
64 alg = &lz4v2_alg;
65 break;
66#endif
67 default:
68 return NULL;
69 }
70
71 struct compress_context *compctx;
72 ALLOC_OBJ_CLEAR(compctx, struct compress_context);
73 compctx->flags = opt->flags;
74 compctx->alg = *alg;
75 (*compctx->alg.compress_init)(compctx);
76
77 return compctx;
78}
79
80/* In the v2 compression schemes, an uncompressed packet has
81 * has no opcode in front, unless the first byte is 0x50. In this
82 * case the packet needs to be escaped */
83void
84compv2_escape_data_ifneeded(struct buffer *buf)
85{
86 uint8_t *head = BPTR(buf);
87 if (head[0] != COMP_ALGV2_INDICATOR_BYTE)
88 {
89 return;
90 }
91
92 /* Header is 0x50 */
93 ASSERT(buf_prepend(buf, 2));
94
95 head = BPTR(buf);
96 head[0] = COMP_ALGV2_INDICATOR_BYTE;
97 head[1] = COMP_ALGV2_UNCOMPRESSED_BYTE;
98}
99
100
101void
102comp_uninit(struct compress_context *compctx)
103{
104 if (compctx)
105 {
106 (*compctx->alg.compress_uninit)(compctx);
107 free(compctx);
108 }
109}
110
111void
112comp_print_stats(const struct compress_context *compctx, struct status_output *so)
113{
114 if (compctx)
115 {
116 status_printf(so, "pre-compress bytes," counter_format, compctx->pre_compress);
117 status_printf(so, "post-compress bytes," counter_format, compctx->post_compress);
118 status_printf(so, "pre-decompress bytes," counter_format, compctx->pre_decompress);
119 status_printf(so, "post-decompress bytes," counter_format, compctx->post_decompress);
120 }
121}
122
123/*
124 * Tell our peer which compression algorithms we support.
125 */
126void
127comp_generate_peer_info_string(const struct compress_options *opt, struct buffer *out)
128{
129 if (!opt || opt->flags & COMP_F_ALLOW_NOCOMP_ONLY)
130 {
131 return;
132 }
133
134 bool lzo_avail = false;
136 {
137#if defined(ENABLE_LZ4)
138 buf_printf(out, "IV_LZ4=1\n");
139 buf_printf(out, "IV_LZ4v2=1\n");
140#endif
141#if defined(ENABLE_LZO)
142 buf_printf(out, "IV_LZO=1\n");
143 lzo_avail = true;
144#endif
145 }
146 if (!lzo_avail)
147 {
148 buf_printf(out, "IV_LZO_STUB=1\n");
149 }
150 buf_printf(out, "IV_COMP_STUB=1\n");
151 buf_printf(out, "IV_COMP_STUBv2=1\n");
152}
153#endif /* USE_COMP */
154
155bool
157{
158 /*
159 * We also allow comp-stub-v2 here as it technically allows escaping of
160 * weird mac address and IPv5 protocol but practically always is used
161 * as an way to disable all framing.
162 */
163 if (info->alg != COMP_ALGV2_UNCOMPRESSED && info->alg != COMP_ALG_UNDEF
164 && (info->flags & COMP_F_ALLOW_NOCOMP_ONLY))
165 {
166#ifdef USE_COMP
167 msg(msglevel, "Compression or compression stub framing is not allowed "
168 "since data-channel offloading is enabled.");
169#else
170 msg(msglevel, "Compression or compression stub framing is not allowed "
171 "since OpenVPN was built without compression support.");
172#endif
173 return false;
174 }
175
177 {
178 msg(msglevel, "Compression is not allowed since allow-compression is "
179 "set to 'stub-only'");
180 return false;
181 }
182#ifndef ENABLE_LZ4
183 if (info->alg == COMP_ALGV2_LZ4 || info->alg == COMP_ALG_LZ4)
184 {
185 msg(msglevel, "OpenVPN is compiled without LZ4 support. Requested "
186 "compression cannot be enabled.");
187 return false;
188 }
189#endif
190#ifndef ENABLE_LZO
191 if (info->alg == COMP_ALG_LZO)
192 {
193 msg(msglevel, "OpenVPN is compiled without LZO support. Requested "
194 "compression cannot be enabled.");
195 return false;
196 }
197#endif
198 return true;
199}
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
Definition buffer.c:231
#define BPTR(buf)
Return a pointer to the start of the buffer content.
Definition buffer.h:139
static uint8_t * buf_prepend(struct buffer *buf, ssize_t size)
Make space at the front of a buffer for prepending data.
Definition buffer.h:1142
#define ALLOC_OBJ_CLEAR(dptr, type)
Allocate and zero-initialise memory for a single object of the given type.
Definition buffer.h:2036
#define counter_format
Definition common.h:32
bool check_compression_settings_valid(const struct compress_options *info, msglvl_t msglevel)
Checks if the compression settings are valid.
Definition comp.c:156
#define COMP_F_ALLOW_STUB_ONLY
Only accept stub compression, even with COMP_F_ADVERTISE_STUBS_ONLY we still accept other compression...
Definition comp.h:45
#define COMP_ALG_LZ4
LZ4 algorithm.
Definition comp.h:59
#define COMP_ALGV2_LZ4
Definition comp.h:64
#define COMP_F_ALLOW_NOCOMP_ONLY
Do not allow compression framing (breaks DCO)
Definition comp.h:51
#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
#define COMP_ALG_LZO
LZO algorithm.
Definition comp.h:57
#define COMP_F_ADVERTISE_STUBS_ONLY
tell server that we only support compression stubs
Definition comp.h:42
static bool comp_non_stub_enabled(const struct compress_options *info)
Definition comp.h:81
#define COMP_ALG_UNDEF
Definition comp.h:54
#define msg(flags,...)
Definition error.h:152
unsigned int msglvl_t
Definition error.h:77
#define ASSERT(x)
Definition error.h:221
void status_printf(struct status_output *so, const char *format,...)
Definition status.c:221
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
unsigned int flags
Definition comp.h:77