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