From 0f18410ebaf1c503e5d0ca9624b24df930561d7d Mon Sep 17 00:00:00 2001 From: Darik Horn Date: Tue, 20 Jul 2021 10:29:39 +0600 Subject: [PATCH] Add LZ4 compression support --- configure.ac | 1 + doc/tinc.conf.5.in | 18 +++--- doc/tinc.texi | 19 +++++- m4/lz4.m4 | 105 ++++++++++++++++++++++++++++++ src/Makefile.am | 9 ++- src/net_packet.c | 155 +++++++++++++++++++++++++++++++++++---------- src/net_setup.c | 43 ++++++++++++- src/protocol_key.c | 43 ++++++++++++- src/tincd.c | 4 ++ 9 files changed, 350 insertions(+), 47 deletions(-) create mode 100644 m4/lz4.m4 diff --git a/configure.ac b/configure.ac index f278e3c7..944e63a0 100644 --- a/configure.ac +++ b/configure.ac @@ -236,6 +236,7 @@ tinc_CURSES tinc_READLINE tinc_ZLIB tinc_LZO +tinc_LZ4 AS_IF([test "x$enable_legacy_protocol" != "xno"], [AS_IF([test -n "$with_libgcrypt"], diff --git a/doc/tinc.conf.5.in b/doc/tinc.conf.5.in index 74942275..d7aa7d99 100644 --- a/doc/tinc.conf.5.in +++ b/doc/tinc.conf.5.in @@ -18,7 +18,7 @@ option to assign a network name to each tinc daemon. .Pp The effect of this option is that the daemon will set its configuration root to .Pa @sysconfdir@/tinc/ Ns Ar NETNAME Ns Pa / , -where +where .Ar NETNAME is your argument to the .Fl n @@ -35,7 +35,7 @@ In this case, the network name would just be empty, and .Nm tinc now looks for files in .Pa @sysconfdir@/tinc/ , -instead of +instead of .Pa @sysconfdir@/tinc/ Ns Ar NETNAME Ns Pa / ; the configuration file should be .Pa @sysconfdir@/tinc/tinc.conf , @@ -90,7 +90,7 @@ or assignments in the form of: .Pp The variable names are case insensitive, and any spaces, tabs, newlines and carriage returns are ignored. -Note: it is not required that you put in the +Note: it is not required that you put in the .Li = sign, but doing so improves readability. If you leave it out, remember to replace it with at least one space character. @@ -129,7 +129,7 @@ This is the same as however the address given with the .Va BindToAddress option will also be used for outgoing connections. This is useful if your -computer has more than one IPv4 or IPv6 address, and you want +computer has more than one IPv4 or IPv6 address, and you want .Nm tinc to only use a specific one for outgoing packets. .It Va BindToInterface Li = Ar interface Bq experimental @@ -231,7 +231,7 @@ Open a multicast UDP socket and bind it to the address and port (separated by sp Packets are read from and written to this multicast socket. This can be used to connect to UML, QEMU or KVM instances listening on the same multicast address. Do NOT connect multiple -.Nm tinc +.Nm tinc daemons to the same multicast address, this will very likely cause routing loops. Also note that this can cause decrypted VPN packets to be sent out on a real network if misconfigured. .It fd @@ -418,7 +418,7 @@ This is the name which identifies this tinc daemon. It must be unique for the virtual private network this daemon will connect to. .Va Name may only consist of alphanumeric and underscore characters (a-z, A-Z, 0-9 and _), and is case sensitive. -If +If .Va Name starts with a .Li $ , @@ -447,7 +447,7 @@ It will allow this tinc daemon to authenticate itself to other daemons. .It Va PrivateKeyFile Li = Ar filename Po Pa @sysconfdir@/tinc/ Ns Ar NETNAME Ns Pa /rsa_key.priv Pc The file in which the private RSA key of this tinc daemon resides. .It Va ProcessPriority Li = low | normal | high -When this option is used the priority of the +When this option is used the priority of the .Nm tincd process will be adjusted. Increasing the priority may help to reduce latency and packet loss on the VPN. @@ -578,7 +578,7 @@ Fragmentation Needed or Packet too Big messages are dropped by firewalls. .It Va Compression Li = Ar level Pq 0 This option sets the level of compression used for UDP packets. Possible values are 0 (off), 1 (fast zlib) and any integer up to 9 (best zlib), -10 (fast lzo) and 11 (best lzo). +10 (fast lzo), 11 (best lzo), and 12 (lz4). .It Va Digest Li = Ar digest Pq sha1 The digest algorithm used to authenticate UDP packets. Any digest supported by LibreSSL or OpenSSL is recognised. @@ -727,7 +727,7 @@ This script is started when an invitation has been used. .Pp The scripts are started without command line arguments, but can make use of certain environment variables. Under UNIX like operating systems the names of environment variables must be preceded by a -.Li $ +.Li $ in scripts. Under Windows, in .Pa .bat diff --git a/doc/tinc.texi b/doc/tinc.texi index 0ff95e9a..9921cda0 100644 --- a/doc/tinc.texi +++ b/doc/tinc.texi @@ -339,6 +339,7 @@ message, and stop. * LibreSSL/OpenSSL:: * zlib:: * LZO:: +* LZ4:: * libcurses:: * libreadline:: @end menu @@ -460,6 +461,22 @@ make sure you build development and runtime libraries (which is the default). +@c ================================================================== +@node LZ4 +@subsection LZ4 + +@cindex LZ4 +Another form of compression is offered using the LZ4 library. + +The LZ4 codec is bundled with Tinc and built-in by default as +compression level 12. Tinc can be linked to an external liblz4 +library by using the "--disable-lz4-builtin" configure switch. + +If LZ4 support is entirely disabled by passing "--disable-lz4" to the +configure script, then the resulting binary will not work correctly +on VPNs where LZ4 compression is used. + + @c ================================================================== @node libcurses @subsection libcurses @@ -1325,7 +1342,7 @@ Fragmentation Needed or Packet too Big messages are dropped by firewalls. @item Compression = <@var{level}> (0) This option sets the level of compression used for UDP packets. Possible values are 0 (off), 1 (fast zlib) and any integer up to 9 (best zlib), -10 (fast LZO) and 11 (best LZO). +10 (fast LZO), 11 (best LZO), and 12 (LZ4). @cindex Digest @item Digest = <@var{digest}> (sha1) diff --git a/m4/lz4.m4 b/m4/lz4.m4 new file mode 100644 index 00000000..f1128857 --- /dev/null +++ b/m4/lz4.m4 @@ -0,0 +1,105 @@ +dnl lz4.m4: Tinc autoconf integration for the LZ4 codec. +dnl Copyright 2015 Darik Horn . +dnl +dnl This program is free software; you can redistribute it and/or modify +dnl it under the terms of the GNU General Public License as published by +dnl the Free Software Foundation; either version 2 of the License, or +dnl (at your option) any later version. +dnl +dnl This program is distributed in the hope that it will be useful, +dnl but WITHOUT ANY WARRANTY; without even the implied warranty of +dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +dnl GNU General Public License for more details. +dnl +dnl You should have received a copy of the GNU General Public License along +dnl with this program; if not, write to the Free Software Foundation, Inc., +dnl 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + +AC_DEFUN([tinc_LZ4], [ + + AC_ARG_ENABLE([lz4], + AS_HELP_STRING([--disable-lz4], [disable all lz4 compression support]) + ) + + AC_ARG_ENABLE([lz4-builtin], + AS_HELP_STRING([--disable-lz4-builtin], [required to link an lz4 library]) + ) + + AC_ARG_WITH(lz4, + AS_HELP_STRING([--with-lz4=DIR], [lz4 shared library prefix (eg: /usr/local)]), + [lz4="$withval" CPPFLAGS="$CPPFLAGS -I$withval/include" LDFLAGS="$LDFLAGS -L$withval/lib"] + ) + + AC_ARG_WITH(lz4-include, + AS_HELP_STRING([--with-lz4-include=DIR], [lz4 shared header directory]), + [lz4_include="$withval" CPPFLAGS="$CPPFLAGS -I$withval"] + ) + + AC_ARG_WITH(lz4-lib, + AS_HELP_STRING([--with-lz4-lib=DIR], [lz4 shared object directory]), + [lz4_lib="$withval" LDFLAGS="$LDFLAGS -L$withval"] + ) + + dnl Calling this early prevents autoconf lint. + AM_CONDITIONAL([CONFIGURE_LZ4_BUILTIN], [test "$enable_lz4_builtin" != 'no']) + + AS_IF([test "$enable_lz4" != 'no' -a "$enable_lz4_builtin" != 'no' ], [ + AC_DEFINE(HAVE_LZ4, 1, [Enable lz4 support.]) + AC_DEFINE(HAVE_LZ4_BUILTIN, 1, [Enable lz4 builtin.]) + AC_DEFINE(HAVE_LZ4_STATE, 1, [Enable lz4 external state features.]) + AC_DEFINE( + [LZ4_compress_shim(a, b, c, d)], + [LZ4_compress_fast_extState(lz4_wrkmem, a, b, c, d, 0)], + [This is the best interface for the lz4 builtin.] + ) + ],[ + AS_IF([test "$enable_lz4" != 'no'], [ + AC_CHECK_HEADERS(lz4.h, [ + AC_DEFINE(LZ4_H, [], [Location of lz4.h]) + + AC_CHECK_LIB(lz4, LZ4_compress_fast_extState, [ + LIBS="$LIBS -llz4" + AC_DEFINE(HAVE_LZ4, 1, [Enable lz4 compression support.]) + AC_DEFINE(HAVE_LZ4_STATE, 1, [Enable lz4 external state features.]) + AC_DEFINE( + [LZ4_compress_shim(a, b, c, d)], + [LZ4_compress_fast_extState(lz4_wrkmem, a, b, c, d, 0)], + [The lz4-r129 library interface.] + ) + break + ]) + + AC_CHECK_LIB(lz4, LZ4_compress_default, [ + LIBS="$LIBS -llz4" + AC_DEFINE(HAVE_LZ4, 1, [Enable lz4 compression support.]) + AC_DEFINE( + [LZ4_compress_shim(a, b, c, d)], + [LZ4_compress_default(a, b, c, d)], + [The lz4-r128 library interface.] + ) + break + ]) + + AC_CHECK_LIB(lz4, LZ4_compress_limitedOutput, [ + LIBS="$LIBS -llz4" + AC_DEFINE(HAVE_LZ4, 1, [Enable lz4 compression support.]) + AC_DEFINE( + [LZ4_compress_shim(a, b, c, d)], + [LZ4_compress_limitedOutput(a, b, c, d)], + [The lz4-r59 library interface.] + ) + AC_MSG_WARN("Using deprecated lz4-r59 interface.") + break + ]) + + ],[ + AC_MSG_ERROR("lz4.h header file not found.") + break + ]) + + ]) + + ]) + +]) diff --git a/src/Makefile.am b/src/Makefile.am index 4b266a73..e0733beb 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -41,6 +41,12 @@ chacha_poly1305_SOURCES = \ chacha-poly1305/chacha-poly1305.c chacha-poly1305/chacha-poly1305.h \ chacha-poly1305/poly1305.c chacha-poly1305/poly1305.h +if CONFIGURE_LZ4_BUILTIN +lz4_SOURCES = lib/lz4/lz4.c lib/lz4/lz4.h +else +lz4_SOURCES = +endif + tincd_SOURCES = \ address_cache.c address_cache.h \ autoconnect.c autoconnect.h \ @@ -103,7 +109,8 @@ tincd_SOURCES = \ ed25519/ecdh.c \ ed25519/ecdsa.c \ $(ed25519_SOURCES) \ - $(chacha_poly1305_SOURCES) + $(chacha_poly1305_SOURCES) \ + $(lz4_SOURCES) tinc_SOURCES = \ dropin.c dropin.h \ diff --git a/src/net_packet.c b/src/net_packet.c index 8a5460cb..b8997e51 100644 --- a/src/net_packet.c +++ b/src/net_packet.c @@ -31,6 +31,14 @@ #include LZO1X_H #endif +#ifdef LZ4_H +#include LZ4_H +#endif + +#ifdef HAVE_LZ4_BUILTIN +#include "lib/lz4/lz4.h" +#endif + #include "address_cache.h" #include "cipher.h" #include "conf.h" @@ -64,6 +72,14 @@ int keylifetime = 0; static char lzo_wrkmem[LZO1X_999_MEM_COMPRESS > LZO1X_1_MEM_COMPRESS ? LZO1X_999_MEM_COMPRESS : LZO1X_1_MEM_COMPRESS]; #endif +#ifdef HAVE_LZ4_BUILTIN +static LZ4_stream_t lz4_stream; +#else +#ifdef HAVE_LZ4_STATE +static void *lz4_state = NULL; +#endif /* HAVE_LZ4_STATE */ +#endif /* HAVE_LZ4_BUILTIN */ + static void send_udppacket(node_t *, vpn_packet_t *); unsigned replaywin = 32; @@ -207,56 +223,121 @@ static void udp_probe_h(node_t *n, vpn_packet_t *packet, length_t len) { } static length_t compress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level) { - if(level == 0) { - memcpy(dest, source, len); - return len; - } else if(level == 10) { -#ifdef HAVE_LZO - lzo_uint lzolen = MAXSIZE; - lzo1x_1_compress(source, len, dest, &lzolen, lzo_wrkmem); - return lzolen; + switch(level) { +#ifdef HAVE_LZ4 + + case 12: +#ifdef HAVE_LZ4_BUILTIN + return LZ4_compress_fast_extState(&lz4_stream, (char *)source, (char *) dest, len, MAXSIZE, 0); + #else - return 0; -#endif - } else if(level < 10) { -#ifdef HAVE_ZLIB - unsigned long destlen = MAXSIZE; +#ifdef HAVE_LZ4_STATE - if(compress2(dest, &destlen, source, len, level) == Z_OK) { - return destlen; - } else -#endif + /* @FIXME: Put this in a better place, and free() it too. */ + if(lz4_state == NULL) { + lz4_state = malloc(LZ4_sizeofState()); + } + + if(lz4_state == NULL) { + logger(DEBUG_ALWAYS, LOG_ERR, "Failed to allocate lz4_state, error: %i", errno); return 0; - } else { + } + + return LZ4_compress_fast_extState(lz4_state, source, dest, len, MAXSIZE, 0); + +#else + return LZ4_compress_shim(source, dest, len, MAXSIZE); + +#endif /* HAVE_LZ4_STATE */ +#endif /* HAVE_LZ4_BUILTIN */ +#endif /* HAVE_LZ4 */ #ifdef HAVE_LZO + + case 11: { lzo_uint lzolen = MAXSIZE; - lzo1x_999_compress(source, len, dest, &lzolen, lzo_wrkmem); - return lzolen; -#else - return 0; + + if(lzo1x_999_compress(source, len, dest, &lzolen, lzo_wrkmem) == LZO_E_OK) { + return lzolen; + } else { + return 0; + } + } + + case 10: { + lzo_uint lzolen = MAXSIZE; + + if(lzo1x_1_compress(source, len, dest, &lzolen, lzo_wrkmem) == LZO_E_OK) { + return lzolen; + } else { + return 0; + } + } + #endif +#ifdef HAVE_ZLIB + + case 9: + case 8: + case 7: + case 6: + case 5: + case 4: + case 3: + case 2: + case 1: { + unsigned long dest_len = MAXSIZE; + + if(compress2(dest, (unsigned long *) &dest_len, source, len, level) == Z_OK) { + return dest_len; + } else { + return 0; + } } - return 0; -} +#endif -static length_t uncompress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level) { - if(level == 0) { + case 0: memcpy(dest, source, len); return len; - } else if(level > 9) { -#ifdef HAVE_LZO - lzo_uint lzolen = MAXSIZE; - if(lzo1x_decompress_safe(source, len, dest, &lzolen, NULL) == LZO_E_OK) { - return lzolen; - } else + default: + return 0; + } +} + +static length_t uncompress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level) { + switch(level) { +#ifdef HAVE_LZ4 + + case 12: + return LZ4_decompress_safe((char *)source, (char *) dest, len, MAXSIZE); + #endif +#ifdef HAVE_LZO + + case 11: + case 10: { + lzo_uint dst_len = MAXSIZE; + + if(lzo1x_decompress_safe(source, len, dest, (lzo_uint *) &dst_len, NULL) == LZO_E_OK) { + return dst_len; + } else { return 0; + } } +#endif #ifdef HAVE_ZLIB - else { + + case 9: + case 8: + case 7: + case 6: + case 5: + case 4: + case 3: + case 2: + case 1: { unsigned long destlen = MAXSIZE; static z_stream stream; @@ -281,7 +362,13 @@ static length_t uncompress_packet(uint8_t *dest, const uint8_t *source, length_t #endif - return 0; + case 0: + memcpy(dest, source, len); + return len; + + default: + return 0; + } } /* VPN packet I/O */ diff --git a/src/net_setup.c b/src/net_setup.c index 82f9bbcd..1ecb3c65 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -1019,8 +1019,49 @@ static bool setup_myself(void) { /* Compression */ if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) { - if(myself->incompression < 0 || myself->incompression > 11) { + switch(myself->incompression) { + case 12: +#ifdef HAVE_LZ4 + break; +#else + logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!"); + logger(DEBUG_ALWAYS, LOG_ERR, "LZ4 compression is unavailable on this node."); + return false; +#endif + + case 11: + case 10: +#ifdef HAVE_LZO + break; +#else + logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!"); + logger(DEBUG_ALWAYS, LOG_ERR, "LZO compression is unavailable on this node."); + return false; +#endif + + case 9: + case 8: + case 7: + case 6: + case 5: + case 4: + case 3: + case 2: + case 1: +#ifdef HAVE_ZLIB + break; +#else + logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!"); + logger(DEBUG_ALWAYS, LOG_ERR, "ZLIB compression is unavailable on this node."); + return false; +#endif + + case 0: + break; + + default: logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!"); + logger(DEBUG_ALWAYS, LOG_ERR, "Compression level %i is unrecognized by this node.", myself->incompression); return false; } } else { diff --git a/src/protocol_key.c b/src/protocol_key.c index d9c58d95..b93bec18 100644 --- a/src/protocol_key.c +++ b/src/protocol_key.c @@ -462,8 +462,49 @@ bool ans_key_h(connection_t *c, const char *request) { from->status.validkey = false; } - if(compression < 0 || compression > 11) { + switch(compression) { + case 12: +#ifdef HAVE_LZ4 + break; +#else + logger(DEBUG_ALWAYS, LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname); + logger(DEBUG_ALWAYS, LOG_ERR, "LZ4 compression is unavailable on this node."); + return true; +#endif + + case 11: + case 10: +#ifdef HAVE_LZO + break; +#else + logger(DEBUG_ALWAYS, LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname); + logger(DEBUG_ALWAYS, LOG_ERR, "LZO compression is unavailable on this node."); + return true; +#endif + + case 9: + case 8: + case 7: + case 6: + case 5: + case 4: + case 3: + case 2: + case 1: +#ifdef HAVE_ZLIB + break; +#else + logger(DEBUG_ALWAYS, LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname); + logger(DEBUG_ALWAYS, LOG_ERR, "ZLIB compression is unavailable on this node."); + return true; +#endif + + case 0: + break; + + default: logger(DEBUG_ALWAYS, LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname); + logger(DEBUG_ALWAYS, LOG_ERR, "Compression level %i is unrecognized by this node.", compression); return true; } diff --git a/src/tincd.c b/src/tincd.c index cf96b298..3a3703ca 100644 --- a/src/tincd.c +++ b/src/tincd.c @@ -37,6 +37,10 @@ #include LZO1X_H #endif +#ifdef LZ4_H +#include LZ4_H +#endif + #ifndef HAVE_MINGW #include #include -- 2.20.1