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-2024 OpenVPN Inc <sales@openvpn.net>
9 * Copyright (C) 2013-2024 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, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24
25#ifdef HAVE_CONFIG_H
26#include "config.h"
27#endif
28
29#include "syshead.h"
30
31#if defined(ENABLE_LZ4)
32#include <lz4.h>
33
34#include "comp.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,
61 struct compress_context *compctx,
62 const struct frame *frame)
63{
64 if (buf->len <= 0)
65 {
66 return;
67 }
68
69 uint8_t comp_head_byte = NO_COMPRESS_BYTE_SWAP;
70 uint8_t *head = BPTR(buf);
71 uint8_t *tail = BEND(buf);
72 ASSERT(buf_safe(buf, 1));
73 ++buf->len;
74
75 /* move head byte of payload to tail */
76 *tail = *head;
77 *head = comp_head_byte;
78}
79
80/* Doesn't do any actual compression anymore */
81static void
82lz4v2_compress(struct buffer *buf, struct buffer work,
83 struct compress_context *compctx,
84 const struct frame *frame)
85{
86 if (buf->len <= 0)
87 {
88 return;
89 }
90
91 compv2_escape_data_ifneeded(buf);
92}
93
94static void
95do_lz4_decompress(size_t zlen_max,
96 struct buffer *work,
97 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), zlen_max);
103 if (uncomp_len <= 0)
104 {
105 dmsg(D_COMP_ERRORS, "LZ4 decompression error: %d", uncomp_len);
106 buf->len = 0;
107 return;
108 }
109
110 ASSERT(buf_safe(work, uncomp_len));
111 work->len = uncomp_len;
112
113 dmsg(D_COMP, "LZ4 decompress %d -> %d", buf->len, work->len);
114 compctx->pre_decompress += buf->len;
115 compctx->post_decompress += work->len;
116
117 *buf = *work;
118}
119
120static void
121lz4_decompress(struct buffer *buf, struct buffer work,
122 struct compress_context *compctx,
123 const struct frame *frame)
124{
125 size_t zlen_max = frame->buf.payload_size;
126 uint8_t c; /* flag indicating whether or not our peer compressed */
127
128 if (buf->len <= 0)
129 {
130 return;
131 }
132
133 ASSERT(buf_init(&work, frame->buf.headroom));
134
135 /* do unframing/swap (assumes buf->len > 0) */
136 {
137 uint8_t *head = BPTR(buf);
138 c = *head;
139 --buf->len;
140 *head = *BEND(buf);
141 }
142
143 if (c == LZ4_COMPRESS_BYTE) /* packet was compressed */
144 {
145 do_lz4_decompress(zlen_max, &work, buf, compctx);
146 }
147 else if (c == NO_COMPRESS_BYTE_SWAP) /* packet was not compressed */
148 {
149 /* nothing to do */
150 }
151 else
152 {
153 dmsg(D_COMP_ERRORS, "Bad LZ4 decompression header byte: %d", c);
154 buf->len = 0;
155 }
156}
157
158static void
159lz4v2_decompress(struct buffer *buf, struct buffer work,
160 struct compress_context *compctx,
161 const struct frame *frame)
162{
163 size_t zlen_max = frame->buf.payload_size;
164 uint8_t c; /* flag indicating whether or not our peer compressed */
165
166 if (buf->len <= 0)
167 {
168 return;
169 }
170
171 ASSERT(buf_init(&work, frame->buf.headroom));
172
173 /* do unframing/swap (assumes buf->len > 0) */
174 uint8_t *head = BPTR(buf);
175 c = *head;
176
177 /* Not compressed */
178 if (c != COMP_ALGV2_INDICATOR_BYTE)
179 {
180 return;
181 }
182
183 /* Packet to short to make sense */
184 if (buf->len <= 1)
185 {
186 buf->len = 0;
187 return;
188 }
189
190 c = head[1];
191 if (c == COMP_ALGV2_LZ4_BYTE) /* packet was compressed */
192 {
193 buf_advance(buf, 2);
194 do_lz4_decompress(zlen_max, &work, buf, compctx);
195 }
196 else if (c == COMP_ALGV2_UNCOMPRESSED_BYTE)
197 {
198 buf_advance(buf, 2);
199 }
200 else
201 {
202 dmsg(D_COMP_ERRORS, "Bad LZ4v2 decompression header byte: %d", c);
203 buf->len = 0;
204 }
205}
206
207const struct compress_alg lz4_alg = {
208 "lz4",
209 lz4_compress_init,
210 lz4_compress_uninit,
211 lz4_compress,
212 lz4_decompress
213};
214
215const struct compress_alg lz4v2_alg = {
216 "lz4v2",
217 lz4v2_compress_init,
218 lz4_compress_uninit,
219 lz4v2_compress,
220 lz4v2_decompress
221};
222#endif /* ENABLE_LZ4 */
#define BEND(buf)
Definition buffer.h:125
#define BPTR(buf)
Definition buffer.h:124
static bool buf_safe(const struct buffer *buf, size_t len)
Definition buffer.h:520
static bool buf_advance(struct buffer *buf, int size)
Definition buffer.h:618
#define BLEN(buf)
Definition buffer.h:127
#define buf_init(buf, offset)
Definition buffer.h:209
#define COMP_F_SWAP
Definition comp.h:39
#define D_COMP_ERRORS
Definition errlevel.h:61
#define D_COMP
Definition errlevel.h:166
#define D_INIT_MEDIUM
Definition errlevel.h:104
#define dmsg(flags,...)
Definition error.h:148
#define msg(flags,...)
Definition error.h:144
#define ASSERT(x)
Definition error.h:195
Wrapper structure for dynamically allocated memory.
Definition buffer.h:61
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:66
Packet geometry parameters.
Definition mtu.h:98
int payload_size
the maximum size that a payload that our buffers can hold from either tun device or network link.
Definition mtu.h:102
int headroom
the headroom in the buffer, this is choosen to allow all potential header to be added before the pack...
Definition mtu.h:108
struct frame::@8 buf