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
8 * Copyright (C) 2002-2023 OpenVPN Inc <sales@openvpn.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
39 struct compress_context
*
40 comp_init(const struct compress_options
*opt
)
42 struct compress_context
*compctx
= NULL
;
46 ALLOC_OBJ_CLEAR(compctx
, struct compress_context
);
47 compctx
->flags
= opt
->flags
;
48 compctx
->alg
= comp_stub_alg
;
51 case COMP_ALGV2_UNCOMPRESSED
:
52 ALLOC_OBJ_CLEAR(compctx
, struct compress_context
);
53 compctx
->flags
= opt
->flags
;
54 compctx
->alg
= compv2_stub_alg
;
59 ALLOC_OBJ_CLEAR(compctx
, struct compress_context
);
60 compctx
->flags
= opt
->flags
;
61 compctx
->alg
= lzo_alg
;
67 ALLOC_OBJ_CLEAR(compctx
, struct compress_context
);
68 compctx
->flags
= opt
->flags
;
69 compctx
->alg
= lz4_alg
;
73 ALLOC_OBJ_CLEAR(compctx
, struct compress_context
);
74 compctx
->flags
= opt
->flags
;
75 compctx
->alg
= lz4v2_alg
;
81 (*compctx
->alg
.compress_init
)(compctx
);
87 /* In the v2 compression schemes, an uncompressed packet has
88 * has no opcode in front, unless the first byte is 0x50. In this
89 * case the packet needs to be escaped */
91 compv2_escape_data_ifneeded(struct buffer
*buf
)
93 uint8_t *head
= BPTR(buf
);
94 if (head
[0] != COMP_ALGV2_INDICATOR_BYTE
)
100 ASSERT(buf_prepend(buf
, 2));
103 head
[0] = COMP_ALGV2_INDICATOR_BYTE
;
104 head
[1] = COMP_ALGV2_UNCOMPRESSED
;
109 comp_uninit(struct compress_context
*compctx
)
113 (*compctx
->alg
.compress_uninit
)(compctx
);
119 comp_print_stats(const struct compress_context
*compctx
, struct status_output
*so
)
123 status_printf(so
, "pre-compress bytes," counter_format
, compctx
->pre_compress
);
124 status_printf(so
, "post-compress bytes," counter_format
, compctx
->post_compress
);
125 status_printf(so
, "pre-decompress bytes," counter_format
, compctx
->pre_decompress
);
126 status_printf(so
, "post-decompress bytes," counter_format
, compctx
->post_decompress
);
131 * Tell our peer which compression algorithms we support.
134 comp_generate_peer_info_string(const struct compress_options
*opt
, struct buffer
*out
)
136 if (!opt
|| opt
->flags
& COMP_F_ALLOW_NOCOMP_ONLY
)
141 bool lzo_avail
= false;
142 if (!(opt
->flags
& COMP_F_ADVERTISE_STUBS_ONLY
))
144 #if defined(ENABLE_LZ4)
145 buf_printf(out
, "IV_LZ4=1\n");
146 buf_printf(out
, "IV_LZ4v2=1\n");
148 #if defined(ENABLE_LZO)
149 buf_printf(out
, "IV_LZO=1\n");
155 buf_printf(out
, "IV_LZO_STUB=1\n");
157 buf_printf(out
, "IV_COMP_STUB=1\n");
158 buf_printf(out
, "IV_COMP_STUBv2=1\n");
160 #endif /* USE_COMP */
163 check_compression_settings_valid(struct compress_options
*info
, int msglevel
)
166 * We also allow comp-stub-v2 here as it technically allows escaping of
167 * weird mac address and IPv5 protocol but practically always is used
168 * as an way to disable all framing.
170 if (info
->alg
!= COMP_ALGV2_UNCOMPRESSED
&& info
->alg
!= COMP_ALG_UNDEF
171 && (info
->flags
& COMP_F_ALLOW_NOCOMP_ONLY
))
174 msg(msglevel
, "Compression or compression stub framing is not allowed "
175 "since data-channel offloading is enabled.");
177 msg(msglevel
, "Compression or compression stub framing is not allowed "
178 "since OpenVPN was built without compression support.");
183 if ((info
->flags
& COMP_F_ALLOW_STUB_ONLY
) && comp_non_stub_enabled(info
))
185 msg(msglevel
, "Compression is not allowed since allow-compression is "
186 "set to 'stub-only'");
190 if (info
->alg
== COMP_ALGV2_LZ4
|| info
->alg
== COMP_ALG_LZ4
)
192 msg(msglevel
, "OpenVPN is compiled without LZ4 support. Requested "
193 "compression cannot be enabled.");
198 if (info
->alg
== COMP_ALG_LZO
)
200 msg(msglevel
, "OpenVPN is compiled without LZO support. Requested "
201 "compression cannot be enabled.");