OpenVPN
comp-lz4.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 * Copyright (C) 2013-2026 Gert Doering <gert@greenie.muc.de>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, see <https://www.gnu.org/licenses/>.
22 */
23
24#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
28#include "syshead.h"
29
30#if defined(ENABLE_LZ4)
31#include <lz4.h>
32
33#include "comp.h"
34#include "comp-lz4.h"
35#include "error.h"
36
37#include "memdbg.h"
38
39
40static void
41lz4_compress_init(struct compress_context *compctx)
42{
43 msg(D_INIT_MEDIUM, "LZ4 compression initializing");
44 ASSERT(compctx->flags & COMP_F_SWAP);
45}
46
47static void
48lz4v2_compress_init(struct compress_context *compctx)
49{
50 msg(D_INIT_MEDIUM, "LZ4v2 compression initializing");
51}
52
53static void
54lz4_compress_uninit(struct compress_context *compctx)
55{
56}
57
58/* Doesn't do any actual compression anymore */
59static void
60lz4_compress(struct buffer *buf, struct buffer work, struct compress_context *compctx,
61 const struct frame *frame)
62{
63 if (buf->len <= 0)
64 {
65 return;
66 }
67
68 uint8_t comp_head_byte = NO_COMPRESS_BYTE_SWAP;
69 uint8_t *head = BPTR(buf);
70 uint8_t *tail = BEND(buf);
71 ASSERT(buf_safe(buf, 1));
72 ++buf->len;
73
74 /* move head byte of payload to tail */
75 *tail = *head;
76 *head = comp_head_byte;
77}
78
79/* Doesn't do any actual compression anymore */
80static void
81lz4v2_compress(struct buffer *buf, struct buffer work, struct compress_context *compctx,
82 const struct frame *frame)
83{
84 if (buf->len <= 0)
85 {
86 return;
87 }
88
89 compv2_escape_data_ifneeded(buf);
90}
91
92static void
93do_lz4_decompress(int zlen_max, struct buffer *work, struct buffer *buf,
94 struct compress_context *compctx)
95{
96 ASSERT(buf_safe(work, zlen_max));
97 int uncomp_len = LZ4_decompress_safe((const char *)BPTR(buf), (char *)BPTR(work), BLEN(buf),
98 zlen_max);
99 if (uncomp_len <= 0)
100 {
101 dmsg(D_COMP_ERRORS, "LZ4 decompression error: %d", uncomp_len);
102 buf->len = 0;
103 return;
104 }
105
106 ASSERT(buf_safe(work, uncomp_len));
107 work->len = uncomp_len;
108
109 dmsg(D_COMP, "LZ4 decompress %d -> %d", buf->len, work->len);
110 compctx->pre_decompress += buf->len;
111 compctx->post_decompress += work->len;
112
113 *buf = *work;
114}
115
116static void
117lz4_decompress(struct buffer *buf, struct buffer work, struct compress_context *compctx,
118 const struct frame *frame)
119{
120 int zlen_max = frame->buf.payload_size;
121 uint8_t c; /* flag indicating whether or not our peer compressed */
122
123 if (buf->len <= 0)
124 {
125 return;
126 }
127
128 ASSERT(buf_init(&work, frame->buf.headroom));
129
130 /* do unframing/swap (assumes buf->len > 0) */
131 {
132 uint8_t *head = BPTR(buf);
133 c = *head;
134 --buf->len;
135 *head = *BEND(buf);
136 }
137
138 if (c == LZ4_COMPRESS_BYTE) /* packet was compressed */
139 {
140 do_lz4_decompress(zlen_max, &work, buf, compctx);
141 }
142 else if (c == NO_COMPRESS_BYTE_SWAP) /* packet was not compressed */
143 {
144 /* nothing to do */
145 }
146 else
147 {
148 dmsg(D_COMP_ERRORS, "Bad LZ4 decompression header byte: %d", c);
149 buf->len = 0;
150 }
151}
152
153static void
154lz4v2_decompress(struct buffer *buf, struct buffer work, struct compress_context *compctx,
155 const struct frame *frame)
156{
157 int zlen_max = frame->buf.payload_size;
158 uint8_t c; /* flag indicating whether or not our peer compressed */
159
160 if (buf->len <= 0)
161 {
162 return;
163 }
164
165 ASSERT(buf_init(&work, frame->buf.headroom));
166
167 /* do unframing/swap (assumes buf->len > 0) */
168 const uint8_t *head = BPTR(buf);
169 c = *head;
170
171 /* Not compressed */
172 if (c != COMP_ALGV2_INDICATOR_BYTE)
173 {
174 return;
175 }
176
177 /* Packet to short to make sense */
178 if (buf->len <= 1)
179 {
180 buf->len = 0;
181 return;
182 }
183
184 c = head[1];
185 if (c == COMP_ALGV2_LZ4_BYTE) /* packet was compressed */
186 {
187 buf_advance(buf, 2);
188 do_lz4_decompress(zlen_max, &work, buf, compctx);
189 }
190 else if (c == COMP_ALGV2_UNCOMPRESSED_BYTE)
191 {
192 buf_advance(buf, 2);
193 }
194 else
195 {
196 dmsg(D_COMP_ERRORS, "Bad LZ4v2 decompression header byte: %d", c);
197 buf->len = 0;
198 }
199}
200
201const struct compress_alg lz4_alg = { "lz4", lz4_compress_init, lz4_compress_uninit, lz4_compress,
202 lz4_decompress };
203
204const struct compress_alg lz4v2_alg = { "lz4v2", lz4v2_compress_init, lz4_compress_uninit,
205 lz4v2_compress, lz4v2_decompress };
206#endif /* ENABLE_LZ4 */
#define BEND(buf)
Return a pointer one past the end of the buffer content.
Definition buffer.h:143
#define BPTR(buf)
Return a pointer to the start of the buffer content.
Definition buffer.h:139
static bool buf_safe(const struct buffer *buf, size_t len)
Check whether len bytes can be appended to a buffer.
Definition buffer.h:994
static bool buf_advance(struct buffer *buf, ssize_t size)
Advance the content start of a buffer, consuming bytes from the front.
Definition buffer.h:1165
#define BLEN(buf)
Return the length of the buffer content in bytes.
Definition buffer.h:151
#define buf_init(buf, offset)
Definition buffer.h:364
#define COMP_F_SWAP
initial command byte is swapped with last byte in buffer to preserve payload alignment
Definition comp.h:40
#define D_COMP_ERRORS
Definition errlevel.h:60
#define D_COMP
Definition errlevel.h:165
#define D_INIT_MEDIUM
Definition errlevel.h:103
#define dmsg(flags,...)
Definition error.h:164
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:221
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:76
Packet geometry parameters.
Definition mtu.h:113
struct frame::@074234134026241341315172337061247271261307273127 buf
int payload_size
the maximum size that a payload that our buffers can hold from either tun device or network link.
Definition mtu.h:118
int headroom
the headroom in the buffer, this is choosen to allow all potential header to be added before the pack...
Definition mtu.h:124