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-2025 OpenVPN Inc <sales@openvpn.net>
9 * Copyright (C) 2013-2025 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 "error.h"
35
36#include "memdbg.h"
37
38
39static void
40lz4_compress_init(struct compress_context *compctx)
41{
42 msg(D_INIT_MEDIUM, "LZ4 compression initializing");
43 ASSERT(compctx->flags & COMP_F_SWAP);
44}
45
46static void
47lz4v2_compress_init(struct compress_context *compctx)
48{
49 msg(D_INIT_MEDIUM, "LZ4v2 compression initializing");
50}
51
52static void
53lz4_compress_uninit(struct compress_context *compctx)
54{
55}
56
57/* Doesn't do any actual compression anymore */
58static void
59lz4_compress(struct buffer *buf, struct buffer work, struct compress_context *compctx,
60 const struct frame *frame)
61{
62 if (buf->len <= 0)
63 {
64 return;
65 }
66
67 uint8_t comp_head_byte = NO_COMPRESS_BYTE_SWAP;
68 uint8_t *head = BPTR(buf);
69 uint8_t *tail = BEND(buf);
70 ASSERT(buf_safe(buf, 1));
71 ++buf->len;
72
73 /* move head byte of payload to tail */
74 *tail = *head;
75 *head = comp_head_byte;
76}
77
78/* Doesn't do any actual compression anymore */
79static void
80lz4v2_compress(struct buffer *buf, struct buffer work, struct compress_context *compctx,
81 const struct frame *frame)
82{
83 if (buf->len <= 0)
84 {
85 return;
86 }
87
88 compv2_escape_data_ifneeded(buf);
89}
90
91static void
92do_lz4_decompress(size_t zlen_max, struct buffer *work, struct buffer *buf,
93 struct compress_context *compctx)
94{
95 int uncomp_len;
96 ASSERT(buf_safe(work, zlen_max));
97 uncomp_len = LZ4_decompress_safe((const char *)BPTR(buf), (char *)BPTR(work), (size_t)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 size_t 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 size_t 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 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)
Definition buffer.h:124
#define BPTR(buf)
Definition buffer.h:123
static bool buf_safe(const struct buffer *buf, size_t len)
Definition buffer.h:518
static bool buf_advance(struct buffer *buf, int size)
Definition buffer.h:616
#define BLEN(buf)
Definition buffer.h:126
#define buf_init(buf, offset)
Definition buffer.h:209
#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:170
#define msg(flags,...)
Definition error.h:150
#define ASSERT(x)
Definition error.h:217
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
Packet geometry parameters.
Definition mtu.h:103
int payload_size
the maximum size that a payload that our buffers can hold from either tun device or network link.
Definition mtu.h:108
int headroom
the headroom in the buffer, this is choosen to allow all potential header to be added before the pack...
Definition mtu.h:114
struct frame::@8 buf