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(int zlen_max, struct buffer *work, struct buffer *buf,
93 struct compress_context *compctx)
94{
95 ASSERT(buf_safe(work, zlen_max));
96 int uncomp_len = LZ4_decompress_safe((const char *)BPTR(buf), (char *)BPTR(work), BLEN(buf),
97 zlen_max);
98 if (uncomp_len <= 0)
99 {
100 dmsg(D_COMP_ERRORS, "LZ4 decompression error: %d", uncomp_len);
101 buf->len = 0;
102 return;
103 }
104
105 ASSERT(buf_safe(work, uncomp_len));
106 work->len = uncomp_len;
107
108 dmsg(D_COMP, "LZ4 decompress %d -> %d", buf->len, work->len);
109 compctx->pre_decompress += buf->len;
110 compctx->post_decompress += work->len;
111
112 *buf = *work;
113}
114
115static void
116lz4_decompress(struct buffer *buf, struct buffer work, struct compress_context *compctx,
117 const struct frame *frame)
118{
119 int zlen_max = frame->buf.payload_size;
120 uint8_t c; /* flag indicating whether or not our peer compressed */
121
122 if (buf->len <= 0)
123 {
124 return;
125 }
126
127 ASSERT(buf_init(&work, frame->buf.headroom));
128
129 /* do unframing/swap (assumes buf->len > 0) */
130 {
131 uint8_t *head = BPTR(buf);
132 c = *head;
133 --buf->len;
134 *head = *BEND(buf);
135 }
136
137 if (c == LZ4_COMPRESS_BYTE) /* packet was compressed */
138 {
139 do_lz4_decompress(zlen_max, &work, buf, compctx);
140 }
141 else if (c == NO_COMPRESS_BYTE_SWAP) /* packet was not compressed */
142 {
143 /* nothing to do */
144 }
145 else
146 {
147 dmsg(D_COMP_ERRORS, "Bad LZ4 decompression header byte: %d", c);
148 buf->len = 0;
149 }
150}
151
152static void
153lz4v2_decompress(struct buffer *buf, struct buffer work, struct compress_context *compctx,
154 const struct frame *frame)
155{
156 int zlen_max = frame->buf.payload_size;
157 uint8_t c; /* flag indicating whether or not our peer compressed */
158
159 if (buf->len <= 0)
160 {
161 return;
162 }
163
164 ASSERT(buf_init(&work, frame->buf.headroom));
165
166 /* do unframing/swap (assumes buf->len > 0) */
167 uint8_t *head = BPTR(buf);
168 c = *head;
169
170 /* Not compressed */
171 if (c != COMP_ALGV2_INDICATOR_BYTE)
172 {
173 return;
174 }
175
176 /* Packet to short to make sense */
177 if (buf->len <= 1)
178 {
179 buf->len = 0;
180 return;
181 }
182
183 c = head[1];
184 if (c == COMP_ALGV2_LZ4_BYTE) /* packet was compressed */
185 {
186 buf_advance(buf, 2);
187 do_lz4_decompress(zlen_max, &work, buf, compctx);
188 }
189 else if (c == COMP_ALGV2_UNCOMPRESSED_BYTE)
190 {
191 buf_advance(buf, 2);
192 }
193 else
194 {
195 dmsg(D_COMP_ERRORS, "Bad LZ4v2 decompression header byte: %d", c);
196 buf->len = 0;
197 }
198}
199
200const struct compress_alg lz4_alg = { "lz4", lz4_compress_init, lz4_compress_uninit, lz4_compress,
201 lz4_decompress };
202
203const struct compress_alg lz4v2_alg = { "lz4v2", lz4v2_compress_init, lz4_compress_uninit,
204 lz4v2_compress, lz4v2_decompress };
205#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:172
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:219
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