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-2022 OpenVPN Inc <sales@openvpn.net>
9  *  Copyright (C) 2013-2022 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 #elif defined(_MSC_VER)
28 #include "config-msvc.h"
29 #endif
30 
31 #include "syshead.h"
32 
33 #if defined(ENABLE_LZ4)
34 
35 #if defined(NEED_COMPAT_LZ4)
36 #include "compat-lz4.h"
37 #else
38 #include <lz4.h>
39 #endif
40 
41 #include "comp.h"
42 #include "error.h"
43 
44 #include "memdbg.h"
45 
46 
47 static void
lz4_compress_init(struct compress_context * compctx)48 lz4_compress_init(struct compress_context *compctx)
49 {
50     msg(D_INIT_MEDIUM, "LZ4 compression initializing");
51     ASSERT(compctx->flags & COMP_F_SWAP);
52 }
53 
54 static void
lz4v2_compress_init(struct compress_context * compctx)55 lz4v2_compress_init(struct compress_context *compctx)
56 {
57     msg(D_INIT_MEDIUM, "LZ4v2 compression initializing");
58 }
59 
60 static void
lz4_compress_uninit(struct compress_context * compctx)61 lz4_compress_uninit(struct compress_context *compctx)
62 {
63 }
64 
65 static bool
do_lz4_compress(struct buffer * buf,struct buffer * work,struct compress_context * compctx,const struct frame * frame)66 do_lz4_compress(struct buffer *buf,
67                 struct buffer *work,
68                 struct compress_context *compctx,
69                 const struct frame *frame)
70 {
71     /*
72      * In order to attempt compression, length must be at least COMPRESS_THRESHOLD.
73      * and asymmetric compression must be disabled
74      */
75     if (buf->len >= COMPRESS_THRESHOLD && (compctx->flags & COMP_F_ALLOW_COMPRESS))
76     {
77         const size_t ps = PAYLOAD_SIZE(frame);
78         int zlen_max = ps + COMP_EXTRA_BUFFER(ps);
79         int zlen;
80 
81         ASSERT(buf_init(work, FRAME_HEADROOM(frame)));
82         ASSERT(buf_safe(work, zlen_max));
83 
84         if (buf->len > ps)
85         {
86             dmsg(D_COMP_ERRORS, "LZ4 compression buffer overflow");
87             buf->len = 0;
88             return false;
89         }
90 
91         zlen = LZ4_compress_default((const char *)BPTR(buf), (char *)BPTR(work), BLEN(buf), zlen_max);
92 
93         if (zlen <= 0)
94         {
95             dmsg(D_COMP_ERRORS, "LZ4 compression error");
96             buf->len = 0;
97             return false;
98         }
99 
100         ASSERT(buf_safe(work, zlen));
101         work->len = zlen;
102 
103 
104         dmsg(D_COMP, "LZ4 compress %d -> %d", buf->len, work->len);
105         compctx->pre_compress += buf->len;
106         compctx->post_compress += work->len;
107         return true;
108     }
109     return false;
110 }
111 
112 
113 static void
lz4_compress(struct buffer * buf,struct buffer work,struct compress_context * compctx,const struct frame * frame)114 lz4_compress(struct buffer *buf, struct buffer work,
115              struct compress_context *compctx,
116              const struct frame *frame)
117 {
118     bool compressed;
119     if (buf->len <= 0)
120     {
121         return;
122     }
123 
124     compressed = do_lz4_compress(buf, &work, compctx, frame);
125 
126     /* On error do_lz4_compress sets buf len to zero, just return */
127     if (buf->len == 0)
128     {
129         return;
130     }
131 
132     /* did compression save us anything? */
133     {
134         uint8_t comp_head_byte = NO_COMPRESS_BYTE_SWAP;
135         if (compressed && work.len < buf->len)
136         {
137             *buf = work;
138             comp_head_byte = LZ4_COMPRESS_BYTE;
139         }
140 
141         {
142             uint8_t *head = BPTR(buf);
143             uint8_t *tail  = BEND(buf);
144             ASSERT(buf_safe(buf, 1));
145             ++buf->len;
146 
147             /* move head byte of payload to tail */
148             *tail = *head;
149             *head = comp_head_byte;
150         }
151     }
152 }
153 
154 
155 static void
lz4v2_compress(struct buffer * buf,struct buffer work,struct compress_context * compctx,const struct frame * frame)156 lz4v2_compress(struct buffer *buf, struct buffer work,
157                struct compress_context *compctx,
158                const struct frame *frame)
159 {
160     bool compressed;
161     if (buf->len <= 0)
162     {
163         return;
164     }
165 
166     compressed = do_lz4_compress(buf, &work, compctx, frame);
167 
168     /* On Error just return */
169     if (buf->len == 0)
170     {
171         return;
172     }
173 
174     /* did compression save us anything?  Include 2 byte compression header
175      * in calculation */
176     if (compressed && work.len + 2 < buf->len)
177     {
178         ASSERT(buf_prepend(&work, 2));
179         uint8_t *head = BPTR(&work);
180         head[0] = COMP_ALGV2_INDICATOR_BYTE;
181         head[1] = COMP_ALGV2_LZ4_BYTE;
182         *buf = work;
183     }
184     else
185     {
186         compv2_escape_data_ifneeded(buf);
187     }
188 }
189 
190 static void
do_lz4_decompress(size_t zlen_max,struct buffer * work,struct buffer * buf,struct compress_context * compctx)191 do_lz4_decompress(size_t zlen_max,
192                   struct buffer *work,
193                   struct buffer *buf,
194                   struct compress_context *compctx)
195 {
196     int uncomp_len;
197     ASSERT(buf_safe(work, zlen_max));
198     uncomp_len = LZ4_decompress_safe((const char *)BPTR(buf), (char *)BPTR(work), (size_t)BLEN(buf), zlen_max);
199     if (uncomp_len <= 0)
200     {
201         dmsg(D_COMP_ERRORS, "LZ4 decompression error: %d", uncomp_len);
202         buf->len = 0;
203         return;
204     }
205 
206     ASSERT(buf_safe(work, uncomp_len));
207     work->len = uncomp_len;
208 
209     dmsg(D_COMP, "LZ4 decompress %d -> %d", buf->len, work->len);
210     compctx->pre_decompress += buf->len;
211     compctx->post_decompress += work->len;
212 
213     *buf = *work;
214 }
215 
216 static void
lz4_decompress(struct buffer * buf,struct buffer work,struct compress_context * compctx,const struct frame * frame)217 lz4_decompress(struct buffer *buf, struct buffer work,
218                struct compress_context *compctx,
219                const struct frame *frame)
220 {
221     size_t zlen_max = EXPANDED_SIZE(frame);
222     uint8_t c;          /* flag indicating whether or not our peer compressed */
223 
224     if (buf->len <= 0)
225     {
226         return;
227     }
228 
229     ASSERT(buf_init(&work, FRAME_HEADROOM(frame)));
230 
231     /* do unframing/swap (assumes buf->len > 0) */
232     {
233         uint8_t *head = BPTR(buf);
234         c = *head;
235         --buf->len;
236         *head = *BEND(buf);
237     }
238 
239     if (c == LZ4_COMPRESS_BYTE) /* packet was compressed */
240     {
241         do_lz4_decompress(zlen_max, &work, buf, compctx);
242     }
243     else if (c == NO_COMPRESS_BYTE_SWAP) /* packet was not compressed */
244     {
245     }
246     else
247     {
248         dmsg(D_COMP_ERRORS, "Bad LZ4 decompression header byte: %d", c);
249         buf->len = 0;
250     }
251 }
252 
253 static void
lz4v2_decompress(struct buffer * buf,struct buffer work,struct compress_context * compctx,const struct frame * frame)254 lz4v2_decompress(struct buffer *buf, struct buffer work,
255                  struct compress_context *compctx,
256                  const struct frame *frame)
257 {
258     size_t zlen_max = EXPANDED_SIZE(frame);
259     uint8_t c;          /* flag indicating whether or not our peer compressed */
260 
261     if (buf->len <= 0)
262     {
263         return;
264     }
265 
266     ASSERT(buf_init(&work, FRAME_HEADROOM(frame)));
267 
268     /* do unframing/swap (assumes buf->len > 0) */
269     uint8_t *head = BPTR(buf);
270     c = *head;
271 
272     /* Not compressed */
273     if (c != COMP_ALGV2_INDICATOR_BYTE)
274     {
275         return;
276     }
277 
278     /* Packet to short to make sense */
279     if (buf->len <= 1)
280     {
281         buf->len = 0;
282         return;
283     }
284 
285     c = head[1];
286     if (c == COMP_ALGV2_LZ4_BYTE) /* packet was compressed */
287     {
288         buf_advance(buf,2);
289         do_lz4_decompress(zlen_max, &work, buf, compctx);
290     }
291     else if (c == COMP_ALGV2_UNCOMPRESSED_BYTE)
292     {
293         buf_advance(buf,2);
294     }
295     else
296     {
297         dmsg(D_COMP_ERRORS, "Bad LZ4v2 decompression header byte: %d", c);
298         buf->len = 0;
299     }
300 }
301 
302 const struct compress_alg lz4_alg = {
303     "lz4",
304     lz4_compress_init,
305     lz4_compress_uninit,
306     lz4_compress,
307     lz4_decompress
308 };
309 
310 const struct compress_alg lz4v2_alg = {
311     "lz4v2",
312     lz4v2_compress_init,
313     lz4_compress_uninit,
314     lz4v2_compress,
315     lz4v2_decompress
316 };
317 
318 #else  /* if defined(ENABLE_LZ4) */
319 static void
dummy(void)320 dummy(void)
321 {
322 }
323 #endif /* ENABLE_LZ4 */
324