From: "ralf_lici (Code Review)" Date: Tue, 7 Jan 2025 10:30:44 +0000 In-Reply-To: References: Reply-To: ralf@...2726..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <26ad75bcb8d590caab042b61cb0e7eba8db9877e-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="6M/a/JBbHYY="; charset=UTF-8 Subject: [Openvpn-devel] [XL] Change in openvpn[master]: Add support for HAProxy's PROXY protocol List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel --6M/a/JBbHYY= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld, plaisthos. Hello flichtenheld, plaisthos, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/685?usp=3Demail to look at the new patch set (#4). Change subject: Add support for HAProxy's PROXY protocol ...................................................................... Add support for HAProxy's PROXY protocol The PROXY protocol is a simple protocol that can be used to communicate connection information such as a client's address across multiple layers of NAT or TCP proxies. This commit adds support for the parsing of the v1 and v2 PROXY protocol header but does not yet parse the TLV data in the v2 header. Check if a PROXY protocol header was sent by the client at the beginning of the connection. If so, parse it, store the extracted informations and replace the proxy address with the real client address. Checking for the header is done only if the packet has an invalid length so that there's minimal overhead for the common case. Also add a small document file showing how to configure HAProxy to test the feature and a unit test for the parsing. Change-Id: I9766c68feaba71279f26c3310eee4b5ca215e6ac Signed-off-by: Ralf Lici --- M CMakeLists.txt A doc/haproxy-protocol.txt M src/openvpn/Makefile.am A src/openvpn/haproxy_protocol.c A src/openvpn/haproxy_protocol.h M src/openvpn/init.c M src/openvpn/multi.c M src/openvpn/openvpn.h M src/openvpn/socket.c M src/openvpn/socket.h M tests/unit_tests/openvpn/Makefile.am A tests/unit_tests/openvpn/test_haproxy_protocol.c 12 files changed, 1,326 insertions(+), 14 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/85/685/4 diff --git a/CMakeLists.txt b/CMakeLists.txt index ad620fa..e927a01 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -419,6 +419,8 @@ src/openvpn/fragment.h src/openvpn/gremlin.c src/openvpn/gremlin.h + src/openvpn/haproxy_protocol.c + src/openvpn/haproxy_protocol.h src/openvpn/helper.c src/openvpn/helper.h src/openvpn/httpdigest.c diff --git a/doc/haproxy-protocol.txt b/doc/haproxy-protocol.txt new file mode 100644 index 0000000..604951a --- /dev/null +++ b/doc/haproxy-protocol.txt @@ -0,0 +1,166 @@ +HAProxy PROXY protocol support for OpenVPN +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +The PROXY protocol is a simple protocol that can be used to retreive the +original source address of a connection that has been proxied. Every conne= ction +is prepended with a header reporting the client IP address and port. This +document aims to breifly describe the protocol and how it can be tested in +OpenVPN. For more information about the protocol, see the official +specification at https://www.haproxy.org/download/2.4/doc/proxy-protocol.t= xt. + +Currently there are two versions of the protocol, a text based version (v1= ) and +a binary version (v2). Although the binary version is more efficient, the = text +based version is still the only version supported by some proxies therefor= e it +we support both versions. + +The protocol is agnostic to the underlying transport protocol, so it can b= e used +with both TCP, UDP and UNIX sockets but currently only TCP is supported by +OpenVPN. + +Version 1 +--------- + +The text based version is a human readable protocol that is easy to debug.= It +consists of a single line of text with the following fields: + +PROXY_SIGNATURE_V1 + single space + INET_PROTOCOL + single space + CLIENT_= IP + +single space + PROXY_IP + single space + CLIENT_PORT + single space + +PROXY_PORT + "\r\n" + +The only supported INET_PROTOCOL in version 1 are "TCP4" and "TCP6". + +Version 2 +--------- + +The binary version is a more efficient protocol that is more suitable for +production use. It consists of a fixed length header followed by the origi= nal +connection data. + +A PROXY Protocol binary header for an IPv4 incoming address has the format: + 0 1 2 3 + 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 ++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +| | ++ + +| Proxy Protocol v2 Signature | ++ + +| | ++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +|Version|Command| AF | Proto.| Address Length | ++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +| IPv4 Source Address | ++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +| IPv4 Destination Address | ++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +| Source Port | Destination Port | ++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +| | ++ + ++ TLVs + ++ + +| | ++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + +A PROXY Protocol binary header for a IPv6 incoming address has the format: + 0 1 2 3 + 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 ++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +| | ++ + +| Proxy Protocol v2 Signature | ++ + +| | ++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +|Version|Command| AF | Proto.| Address Length | ++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +| | ++ + +| | ++ IPv6 Source Address + +| | ++ + +| | ++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +| | ++ + +| | ++ IPv6 Destination Address + +| | ++ + +| | ++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +| Source Port | Destination Port | ++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +| | ++ + ++ TLVs + ++ + +| | ++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + +The fields are as follows: + +- Proxy Protocol v2 Signature: 12 bytes + The v2 signature of the protocol: "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\= x49\x54\x0A\x20". + +- Version: 4 bits + The version of the PROXY protocol (0x2). + +- Command: 4 bits + Can by one of the following: + - LOCAL: 0x0 + The connection was initiated by the proxy for health-checks or simil= ar. + This command is not supported and therefore the header is ignored. + - PROXY: 0x1 + The connection was initiated by the client. + +- AF: 4 bits + The address family of the connection can be: AF_UNSPEC (0x0), AF_INET = (0x1), + AF_INET6 (0x2) or AF_UNIX (0x3). Only AF_INET and AF_INET6 are support= ed. + +- Proto: 4 bits + The protocol of the connection can be: UNSPEC (0x0), STREAM (0x1) or + DGRAM (0x2). Only STREAM is supported. + +- Address Length: 16 bits + The length of the rest of the header in bytes. + +Finally there may be additional data arranged as a series of TLV +(Type-Length-Value) vectors that can be used to carry additional informati= on. + +OpenVPN testing +--------------- + +Currently only TCP is supported. To test the PROXY protocol support in Ope= nVPN, +you can use HAProxy as a proxy with a configuration like this: + +global + log stdout format raw local0 + log 127.0.0.1 local0 debug + daemon + +defaults + log global + mode tcp + option tcplog + timeout connect 5000ms + timeout client 50000ms + timeout server 50000ms + +frontend openvpn_front + bind 10.10.20.1:1195 + default_backend openvpn_back + +backend openvpn_back + server openvpn_server 10.10.10.1:1194 send-proxy + # server openvpn_server 10.10.10.1:1194 send-proxy-v2 proxy-v2-options= ssl,cert-cn,ssl-cipher,cert-sig,cert-key,authority,crc32c,unique-id + + +You can use the commented line to test the binary format and the TLV data +instead of the text based format. + +Note that you should set the keepalive option so that the connection is not +reset by HAProxy for inactivity (timeout option in the config). + +The OpenVPN TCP server should read and parse the header and use the inform= ation +to display the original source address in the logs. diff --git a/src/openvpn/Makefile.am b/src/openvpn/Makefile.am index 56cce9d..9dce362 100644 --- a/src/openvpn/Makefile.am +++ b/src/openvpn/Makefile.am @@ -65,6 +65,7 @@ forward.c forward.h \ fragment.c fragment.h \ gremlin.c gremlin.h \ + haproxy_protocol.c haproxy_protocol.h \ helper.c helper.h \ httpdigest.c httpdigest.h \ lladdr.c lladdr.h \ diff --git a/src/openvpn/haproxy_protocol.c b/src/openvpn/haproxy_protocol.c new file mode 100644 index 0000000..1786d2f --- /dev/null +++ b/src/openvpn/haproxy_protocol.c @@ -0,0 +1,487 @@ +/* + * OpenVPN -- An application to securely tunnel IP networks + * over a single TCP/UDP port, with support for SSL/TLS-based + * session authentication and key exchange, + * packet encryption, packet authentication, and + * packet compression. + * + * Copyright (C) 2024 OpenVPN Inc + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "syshead.h" + +#include "error.h" +#include "haproxy_protocol.h" + +#define HAPROXY_PROTOCOL_V1_MAX_WORDS 6 +#define HAPROXY_PROTOCOL_V1_MAX_WORD_LEN 40 + +typedef enum +{ + HAPROXY_PROTOCOL_PARSING_STATE_INVALID =3D -1, + HAPROXY_PROTOCOL_PARSING_STATE_OK =3D 0, + HAPROXY_PROTOCOL_PARSING_STATE_IGNORE =3D 1, +} haproxy_protocol_parsing_state_t; + +static const size_t HAPROXY_PROTOCOL_V2_ADDR_LEN_IPV4 =3D 12; +static const size_t HAPROXY_PROTOCOL_V2_ADDR_LEN_IPV6 =3D 36; +static const size_t HAPROXY_PROTOCOL_V2_ADDR_LEN_UNIX =3D 216; + +static haproxy_protocol_header_t *header =3D NULL; +static uint16_t header_len =3D 0; +static haproxy_protocol_parsing_state_t parsing_state =3D HAPROXY_PROTOCOL= _PARSING_STATE_OK; + +haproxy_protocol_version_t +haproxy_protocol_version(const uint8_t *buf, const int buf_len) +{ + if (buf_len >=3D HAPROXY_PROTOCOL_V2_MIN_HDR_LEN + && memcmp(buf, HAPROXY_PROTOCOL_V2_SIG, HAPROXY_PROTOCOL_V2_SIG_LE= N) =3D=3D 0) + { + return HAPROXY_PROTOCOL_VERSION_2; + } + else if (buf_len >=3D HAPROXY_PROTOCOL_V1_MIN_HDR_LEN + && memcmp(buf, HAPROXY_PROTOCOL_V1_SIG, HAPROXY_PROTOCOL_V1_S= IG_LEN) =3D=3D 0) + { + return HAPROXY_PROTOCOL_VERSION_1; + } + return HAPROXY_PROTOCOL_VERSION_INVALID; +} + +int +haproxy_protocol_header_len(const uint8_t *buf, const int buf_len, + const haproxy_protocol_version_t version) +{ + switch (version) + { + case HAPROXY_PROTOCOL_VERSION_1: + { + const char *end =3D memchr(buf, '\r', buf_len - 1); + /* partial or invalid header */ + if (!end || end[1] !=3D '\n') + { + return -1; + } + return (int)(end + 2 - (char *)buf); + } + + case HAPROXY_PROTOCOL_VERSION_2: + /* byte 15 and 16 contain the length */ + return HAPROXY_PROTOCOL_V2_MIN_HDR_LEN + ntohps(*(uint16_t *)(= buf + 14)); + + default: + return -1; + } +} + +/* + * Parse a port number from a string. + * + * @param port_str - The string to parse. + * + * @return - The port number or -1 if the string is invalid. + */ +uint16_t +haproxy_protocol_parse_port(const char *port_str) +{ + char *endptr; + errno =3D 0; + + long port =3D strtol(port_str, &endptr, 10); + if (errno !=3D 0 || endptr =3D=3D port_str || *endptr !=3D '\0' || por= t <=3D 0 || port > 65535) + { + return -1; + } + return (uint16_t)port; +} + +/* + * Set the source and destination addresses and ports in the proxy protocol + * info structure for later use. + * + * @param hpi - The haproxy protocol info structure to store the addresses. + * @param ver - The version of the PROXY protocol. + * @param fam - The address family (supports: AF_INET or AF_INET6). + * @param st - The socket type. + * @param src_addr - The source address. + * @param dst_addr - The destination address. + * @param src_port - The source port. + * @param dst_port - The destination port. + */ +void +haproxy_protocol_set_addr(struct haproxy_protocol_info *hpi, + const haproxy_protocol_version_t ver, const int = fam, const int st, + const void *src_addr, const void *dst_addr, + const uint16_t src_port, const uint16_t dst_port) +{ + + hpi->version =3D ver; + hpi->sock_type =3D st; + if (fam =3D=3D AF_INET) + { + hpi->src.addr.sa.sa_family =3D AF_INET; + hpi->dst.addr.sa.sa_family =3D AF_INET; + hpi->src.addr.in4.sin_addr.s_addr =3D *(uint32_t *)src_addr; + hpi->dst.addr.in4.sin_addr.s_addr =3D *(uint32_t *)dst_addr; + hpi->src.addr.in4.sin_port =3D src_port; + hpi->dst.addr.in4.sin_port =3D dst_port; + msg(M_DEBUG, "PROXY protocol %s: SRC %s:%u", + ver =3D=3D HAPROXY_PROTOCOL_VERSION_2 ? "v2" : "v1", + inet_ntoa(hpi->src.addr.in4.sin_addr), + ntohs(hpi->src.addr.in4.sin_port)); + msg(M_DEBUG, "PROXY protocol %s: DST %s:%u", + ver =3D=3D HAPROXY_PROTOCOL_VERSION_2 ? "v2" : "v1", + inet_ntoa(hpi->dst.addr.in4.sin_addr), + ntohs(hpi->dst.addr.in4.sin_port)); + } + else if (fam =3D=3D AF_INET6) + { + hpi->src.addr.sa.sa_family =3D AF_INET6; + hpi->dst.addr.sa.sa_family =3D AF_INET6; + memcpy(&hpi->src.addr.in6.sin6_addr, src_addr, sizeof(struct in6_a= ddr)); + memcpy(&hpi->dst.addr.in6.sin6_addr, dst_addr, sizeof(struct in6_a= ddr)); + hpi->src.addr.in6.sin6_port =3D src_port; + hpi->dst.addr.in6.sin6_port =3D dst_port; + + char ip6_str[INET6_ADDRSTRLEN]; + if (inet_ntop(AF_INET6, &hpi->src.addr.in6.sin6_addr, ip6_str, + INET6_ADDRSTRLEN)) + { + msg(M_DEBUG, "PROXY protocol %s: SRC %s:%u", + ver =3D=3D HAPROXY_PROTOCOL_VERSION_2 ? "v2" : "v1", + ip6_str, + ntohs(hpi->src.addr.in6.sin6_port)); + } + else + { + msg(M_NONFATAL, "PROXY protocol %s: could not parse source add= ress", + ver =3D=3D HAPROXY_PROTOCOL_VERSION_2 ? "v2" : "v1"); + } + if (inet_ntop(AF_INET6, &hpi->dst.addr.in6.sin6_addr, ip6_str, + INET6_ADDRSTRLEN)) + { + msg(M_DEBUG, "PROXY protocol %s: DST %s:%u", + ver =3D=3D HAPROXY_PROTOCOL_VERSION_2 ? "v2" : "v1", + ip6_str, + ntohs(hpi->dst.addr.in6.sin6_port)); + } + else + { + msg(M_NONFATAL, "PROXY protocol %s: could not parse destinatio= n address", + ver =3D=3D HAPROXY_PROTOCOL_VERSION_2 ? "v2" : "v1"); + } + } + else + { + msg(M_NONFATAL, "PROXY protocol %s: unsupported address family", + ver =3D=3D HAPROXY_PROTOCOL_VERSION_2 ? "v2" : "v1"); + } +} + +/* + * Split the v1 header line into words (space-separated) + * + * @param words - The array to store the words. + * @param line - The header line to split. + * @param len - The length of the header line. + * + * @return - The number of words found or -1 if an error occurred. + */ +int +haproxy_protocol_v1_split_words(char words[][HAPROXY_PROTOCOL_V1_MAX_WORD_= LEN], + const char *line, int len) +{ + int word_num =3D 0; + const char *start =3D line; + for (int i =3D 0; i < len; ++i) + { + if (line[i] =3D=3D ' ') + { + if (word_num < HAPROXY_PROTOCOL_V1_MAX_WORDS) + { + int word_len =3D (int)(line + i - start); + if (word_len < HAPROXY_PROTOCOL_V1_MAX_WORD_LEN) + { + memcpy(words[word_num], start, word_len); + words[word_num][word_len] =3D '\0'; + ++word_num; + } + else + { + msg(M_NONFATAL, "PROXY protocol v1: word too long"); + return -1; + } + } + start =3D line + i + 1; + } + } + return word_num; +} + +/* + * Parse the PROXY protocol v1 header line. + * + * @param hpi - The haproxy protocol info structure to store the parsed da= ta. + * @param line - The header line to parse. + * @param len - The length of the header line. + * + * @return - true if the header was successfully parsed. + */ +bool +haproxy_protocol_parse_v1(struct haproxy_protocol_info *hpi, + const char *line, int len) +{ + const haproxy_protocol_version_t version =3D HAPROXY_PROTOCOL_VERSION_= 1; + char words[HAPROXY_PROTOCOL_V1_MAX_WORDS][HAPROXY_PROTOCOL_V1_MAX_WORD= _LEN]; + + char *end =3D memchr(line, '\r', len - 1); + if (!end || end[1] !=3D '\n') /* partial or invalid header */ + { + return false; + } + *end =3D ' '; /* replace CRLF with space for easier splitting */ + int size =3D (int)(end - line + 1); + + int word_num =3D haproxy_protocol_v1_split_words(words, line, size); + if (word_num < 3) + { + msg(M_NONFATAL, "PROXY protocol v1: invalid header"); + return false; + } + + if (strcmp(words[1], "TCP4") =3D=3D 0) + { + msg(M_DEBUG, "PROXY protocol v1: TCP4 protocol"); + struct in_addr ip4_src_addr, ip4_dst_addr; + if (inet_pton(AF_INET, words[2], &ip4_src_addr) !=3D 1) + { + msg(M_NONFATAL, + "PROXY protocol v1: could not parse source address"); + return false; + } + if (inet_pton(AF_INET, words[3], &ip4_dst_addr) !=3D 1) + { + msg(M_NONFATAL, + "PROXY protocol v1: could not parse destination address"); + return false; + } + haproxy_protocol_set_addr(hpi, version, AF_INET, SOCK_STREAM, + &ip4_src_addr, &ip4_dst_addr, + ntohs(haproxy_protocol_parse_port(words[= 4])), + ntohs(haproxy_protocol_parse_port(words[= 5]))); + return true; + } + else if (strcmp(words[1], "TCP6") =3D=3D 0) + { + msg(M_DEBUG, "PROXY protocol v1: TCP6 protocol"); + struct in6_addr ip6_src_addr, ip6_dst_addr; + if (inet_pton(AF_INET6, words[2], &ip6_src_addr) !=3D 1) + { + msg(M_NONFATAL, + "PROXY protocol v1: could not parse source address"); + return false; + } + if (inet_pton(AF_INET6, words[3], &ip6_dst_addr) !=3D 1) + { + msg(M_NONFATAL, + "PROXY protocol v1: could not parse destination address"); + return false; + } + haproxy_protocol_set_addr(hpi, version, AF_INET6, SOCK_STREAM, + &ip6_src_addr, &ip6_dst_addr, + ntohs(haproxy_protocol_parse_port(words[= 4])), + ntohs(haproxy_protocol_parse_port(words[= 5]))); + return true; + } + else + { + msg(M_NONFATAL, "PROXY protocol v1: unsupported protocol"); + return false; + } +} + +/* + * Parse the PROXY protocol v2 header + * + * @param hpi - The haproxy protocol info structure to store the parsed da= ta. + * + * @return - The number of bytes parsed. + */ +int +haproxy_protocol_parse_v2(struct haproxy_protocol_info *hpi) +{ + size_t addr_len =3D 0; + int pos =3D HAPROXY_PROTOCOL_V2_SIG_LEN + sizeof(header->v2.ver_cmd); + const haproxy_protocol_version_t version =3D HAPROXY_PROTOCOL_VERSION_= 2; + + switch (header->v2.ver_cmd & HAPROXY_PROTOCOL_V2_CMD_MASK) + { + case HAPROXY_PROTOCOL_V2_LOCAL_CMD: + /* + * this command is sent by the proxy for health-checks and sim= ilar + * and it doesn't make much sense in the openvpn context, so we + * ignore the whole header + */ + msg(M_DEBUG, "PROXY protocol v2: LOCAL command"); + parsing_state =3D HAPROXY_PROTOCOL_PARSING_STATE_IGNORE; + break; + + case HAPROXY_PROTOCOL_V2_PROXY_CMD: + msg(M_DEBUG, "PROXY protocol v2: PROXY command"); + break; + + default: + msg(M_DEBUG, "PROXY protocol v2: unknown command"); + parsing_state =3D HAPROXY_PROTOCOL_PARSING_STATE_INVALID; + } + + if (parsing_state !=3D HAPROXY_PROTOCOL_PARSING_STATE_OK) + { + return pos; + } + pos +=3D sizeof(header->v2.fam); + + switch (header->v2.fam) + { + case HAPROXY_PROTOCOL_V2_AF_INET | HAPROXY_PROTOCOL_V2_TP_STREAM: + msg(M_DEBUG, "PROXY protocol v2: TCP over IPv4."); + haproxy_protocol_set_addr(hpi, version, AF_INET, SOCK_STREAM, + &header->v2.addr.ip4.src_addr, + &header->v2.addr.ip4.dst_addr, + header->v2.addr.ip4.src_port, + header->v2.addr.ip4.dst_port); + addr_len =3D HAPROXY_PROTOCOL_V2_ADDR_LEN_IPV4; + break; + + case HAPROXY_PROTOCOL_V2_AF_INET | HAPROXY_PROTOCOL_V2_TP_DGRAM: + msg(M_DEBUG, "PROXY protocol v2: UDP over IPv4"); + haproxy_protocol_set_addr(hpi, version, AF_INET, SOCK_DGRAM, + &header->v2.addr.ip4.src_addr, + &header->v2.addr.ip4.dst_addr, + header->v2.addr.ip4.src_port, + header->v2.addr.ip4.dst_port); + addr_len =3D HAPROXY_PROTOCOL_V2_ADDR_LEN_IPV4; + break; + + case HAPROXY_PROTOCOL_V2_AF_INET6 | HAPROXY_PROTOCOL_V2_TP_STREAM: + msg(M_DEBUG, "PROXY protocol v2: TCP over IPv6"); + haproxy_protocol_set_addr(hpi, version, AF_INET6, SOCK_STREAM, + header->v2.addr.ip6.src_addr, + header->v2.addr.ip6.dst_addr, + header->v2.addr.ip6.src_port, + header->v2.addr.ip6.dst_port); + addr_len =3D HAPROXY_PROTOCOL_V2_ADDR_LEN_IPV6; + break; + + case HAPROXY_PROTOCOL_V2_AF_INET6 | HAPROXY_PROTOCOL_V2_TP_DGRAM: + msg(M_DEBUG, "PROXY protocol v2: UDP over IPv6"); + haproxy_protocol_set_addr(hpi, version, AF_INET6, SOCK_DGRAM, + header->v2.addr.ip6.src_addr, + header->v2.addr.ip6.dst_addr, + header->v2.addr.ip6.src_port, + header->v2.addr.ip6.dst_port); + addr_len =3D HAPROXY_PROTOCOL_V2_ADDR_LEN_IPV6; + break; + + case HAPROXY_PROTOCOL_V2_AF_UNIX | HAPROXY_PROTOCOL_V2_TP_STREAM: + msg(M_DEBUG, "PROXY protocol v2: AF_UNIX stream"); + haproxy_protocol_set_addr(hpi, version, AF_UNIX, SOCK_STREAM, + header->v2.addr.unx.src_addr, + header->v2.addr.unx.dst_addr, 0, 0); + addr_len =3D HAPROXY_PROTOCOL_V2_ADDR_LEN_UNIX; + break; + + case HAPROXY_PROTOCOL_V2_AF_UNIX | HAPROXY_PROTOCOL_V2_TP_DGRAM: + msg(M_DEBUG, "PROXY protocol v2: AF_UNIX datagram"); + haproxy_protocol_set_addr(hpi, version, AF_UNIX, SOCK_DGRAM, + header->v2.addr.unx.src_addr, + header->v2.addr.unx.dst_addr, 0, 0); + addr_len =3D HAPROXY_PROTOCOL_V2_ADDR_LEN_UNIX; + break; + + default: + if ((header->v2.fam & HAPROXY_PROTOCOL_V2_AF_MASK) =3D=3D HAPR= OXY_PROTOCOL_V2_AF_UNSPEC + || (header->v2.fam & HAPROXY_PROTOCOL_V2_TP_MASK) =3D=3D H= APROXY_PROTOCOL_V2_TP_UNSPEC) + { + msg(M_DEBUG, "PROXY protocol v2: UNSPEC address family or = transport protocol"); + parsing_state =3D HAPROXY_PROTOCOL_PARSING_STATE_IGNORE; + break; + } + msg(M_DEBUG, "PROXY protocol v2: unknown address family (0x%02= x) " + "or transport protocol (0x%02x)", + header->v2.fam & HAPROXY_PROTOCOL_V2_AF_MASK, + header->v2.fam & HAPROXY_PROTOCOL_V2_TP_MASK); + parsing_state =3D HAPROXY_PROTOCOL_PARSING_STATE_IGNORE; + break; + } + return (int)(pos + sizeof(header->v2.len) + addr_len); +} + +bool +haproxy_protocol_parse(struct haproxy_protocol_info *hpi, const uint8_t *b= uf, const int buf_len) +{ + const haproxy_protocol_version_t version =3D haproxy_protocol_version(= buf, buf_len); + header_len =3D buf_len; + header =3D (haproxy_protocol_header_t *)buf; + + if (!hpi) + { + msg(M_NONFATAL, "PROXY protocol: invalid haproxy protocol info str= ucture"); + return false; + } + + switch (version) + { + case HAPROXY_PROTOCOL_VERSION_2: + { + if ((header->v2.ver_cmd & HAPROXY_PROTOCOL_V2_VER_MASK) =3D=3D= HAPROXY_PROTOCOL_V2_VER) + { + haproxy_protocol_parse_v2(hpi); + if (parsing_state !=3D HAPROXY_PROTOCOL_PARSING_STATE_OK) + { + msg(M_DEBUG, "PROXY protocol v2: %s header", + parsing_state =3D=3D HAPROXY_PROTOCOL_PARSING_STAT= E_IGNORE ? "ignoring" : "invalid"); + return false; + } + return true; + } + else + { + msg(M_NONFATAL, "PROXY protocol v2: expected version 2, go= t %d", + (header->v2.ver_cmd & HAPROXY_PROTOCOL_V2_VER_MASK) >>= 4); + return false; + } + } + + case HAPROXY_PROTOCOL_VERSION_1: + msg(M_DEBUG, "PROXY protocol header v1: %.*s", header_len - 2,= header->v1.line); + return haproxy_protocol_parse_v1(hpi, header->v1.line, header_= len); + + default: + return false; + } +} + +void +haproxy_protocol_reset(struct haproxy_protocol_info *hpi) +{ + header =3D NULL; + header_len =3D 0; + parsing_state =3D HAPROXY_PROTOCOL_PARSING_STATE_OK; +} diff --git a/src/openvpn/haproxy_protocol.h b/src/openvpn/haproxy_protocol.h new file mode 100644 index 0000000..70b5aa4 --- /dev/null +++ b/src/openvpn/haproxy_protocol.h @@ -0,0 +1,184 @@ +/* + * OpenVPN -- An application to securely tunnel IP networks + * over a single TCP/UDP port, with support for SSL/TLS-based + * session authentication and key exchange, + * packet encryption, packet authentication, and + * packet compression. + * + * Copyright (C) 2024 OpenVPN Inc + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef HAPROXY_PROTOCOL_H +#define HAPROXY_PROTOCOL_H + +#include "buffer.h" +#include "openvpn.h" +#include "socket.h" + +#include +#include + +/* https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt */ + +#define HAPROXY_PROTOCOL_V1_SIG "PROXY" +#define HAPROXY_PROTOCOL_V1_SIG_LEN 5 +#define HAPROXY_PROTOCOL_V1_MIN_HDR_LEN 8 +#define HAPROXY_PROTOCOL_V1_LINE_MAX_LEN 108 + +#define HAPROXY_PROTOCOL_V2_SIG "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\= x55\x49\x54\x0A" +#define HAPROXY_PROTOCOL_V2_SIG_LEN 12 +#define HAPROXY_PROTOCOL_V2_MIN_HDR_LEN 16 + +#define HAPROXY_PROTOCOL_V2_VER_MASK 0xF0 +#define HAPROXY_PROTOCOL_V2_VER (0x2 << 4) + +#define HAPROXY_PROTOCOL_V2_CMD_MASK 0x0F +#define HAPROXY_PROTOCOL_V2_LOCAL_CMD (0x0 << 0) +#define HAPROXY_PROTOCOL_V2_PROXY_CMD (0x1 << 0) + +#define HAPROXY_PROTOCOL_V2_AF_MASK 0xF0 +#define HAPROXY_PROTOCOL_V2_AF_UNSPEC (0x0 << 4) +#define HAPROXY_PROTOCOL_V2_AF_INET (0x1 << 4) +#define HAPROXY_PROTOCOL_V2_AF_INET6 (0x2 << 4) +#define HAPROXY_PROTOCOL_V2_AF_UNIX (0x3 << 4) + +#define HAPROXY_PROTOCOL_V2_TP_MASK 0x0F +#define HAPROXY_PROTOCOL_V2_TP_UNSPEC (0x0 << 0) +#define HAPROXY_PROTOCOL_V2_TP_STREAM (0x1 << 0) +#define HAPROXY_PROTOCOL_V2_TP_DGRAM (0x2 << 0) + +#define HAPROXY_PROTOCOL_TLV_TYPE_ALPN 0x01 +#define HAPROXY_PROTOCOL_TLV_TYPE_AUTHORITY 0x02 +#define HAPROXY_PROTOCOL_TLV_TYPE_CRC32C 0x03 +#define HAPROXY_PROTOCOL_TLV_TYPE_NOOP 0x04 +#define HAPROXY_PROTOCOL_TLV_TYPE_UNIQUE_ID 0x05 +#define HAPROXY_PROTOCOL_TLV_TYPE_SSL 0x20 +#define HAPROXY_PROTOCOL_TLV_SUBTYPE_SSL_VERSION 0x21 +#define HAPROXY_PROTOCOL_TLV_SUBTYPE_SSL_CN 0x22 +#define HAPROXY_PROTOCOL_TLV_SUBTYPE_SSL_CIPHER 0x23 +#define HAPROXY_PROTOCOL_TLV_SUBTYPE_SSL_SIG_ALG 0x24 +#define HAPROXY_PROTOCOL_TLV_SUBTYPE_SSL_KEY_ALG 0x25 +#define HAPROXY_PROTOCOL_TLV_TYPE_NETNS 0x30 + +#define HAPROXY_PROTOCOL_V2_CLIENT_SSL 0x01 +#define HAPROXY_PROTOCOL_V2_CLIENT_CERT_CONN 0x02 +#define HAPROXY_PROTOCOL_V2_CLIENT_CERT_SESS 0x04 + +#define HAPROXY_PROTOCOL_V2_TLV_UNIQUE_ID_MAX_LEN 128 + +typedef enum +{ + HAPROXY_PROTOCOL_VERSION_INVALID =3D -1, + + HAPROXY_PROTOCOL_VERSION_UNKNOWN =3D 0, + HAPROXY_PROTOCOL_VERSION_1, + HAPROXY_PROTOCOL_VERSION_2, +} haproxy_protocol_version_t; + +#pragma pack(push, 1) +typedef union +{ + struct + { + char line[HAPROXY_PROTOCOL_V1_LINE_MAX_LEN]; + } v1; + struct + { + uint8_t sig[HAPROXY_PROTOCOL_V2_SIG_LEN]; + uint8_t ver_cmd; + uint8_t fam; + uint16_t len; + union + { + struct + { + uint32_t src_addr; + uint32_t dst_addr; + uint16_t src_port; + uint16_t dst_port; + } ip4; + struct + { + uint8_t src_addr[16]; + uint8_t dst_addr[16]; + uint16_t src_port; + uint16_t dst_port; + } ip6; + struct + { + uint8_t src_addr[108]; + uint8_t dst_addr[108]; + } unx; + } addr; + } v2; +} __attribute__((packed)) haproxy_protocol_header_t; +#pragma pack(pop) + +struct haproxy_protocol_info +{ + haproxy_protocol_version_t version; + int sock_type; + struct openvpn_sockaddr src; + struct openvpn_sockaddr dst; +}; + +/* + * Check if the buffer contains an PROXY protocol header and return the ve= rsion. + * + * @param buf - The buffer to check. + * @param buf_len - The length of the buffer. + * + * @return - The PROXY protocol version or HAPROXY_PROTOCOL_VERSION_INVALI= D if + * the buffer does not contain a valid PROXY protocol header. + */ +haproxy_protocol_version_t +haproxy_protocol_version(const uint8_t *buf, int buf_len); + +/* + * Get the length of the PROXY protocol header. + * This function does not check if the header is fully contained in the bu= ffer + * therefore it is the caller's responsibility to perform this check. + * + * @param buf - The buffer to check. + * @param buf_len - The length of the buffer. + * @param version - The PROXY protocol version. + * + * @return - The length of the header or -1 if the header is partial or in= valid. + */ +int +haproxy_protocol_header_len(const uint8_t *buf, int len, haproxy_protocol_= version_t version); + +/* + * Parse the PROXY protocol header. + * + * @param hpi - The proxy protocol info structure to store the parsed data. + * @param buf - The header to parse. + * @param buf_len - The length of the header. + * + * @return - true if the header was successfully parsed. + */ +bool +haproxy_protocol_parse(struct haproxy_protocol_info *hpi, const uint8_t *b= uf, int buf_len); + +/* + * Free the allocated memory. + * + * @param hpi - The proxy protocol info structure. + */ +void +haproxy_protocol_reset(struct haproxy_protocol_info *hpi); + +#endif /* HAPROXY_PROTOCOL_H */ diff --git a/src/openvpn/init.c b/src/openvpn/init.c index a49e563..ecd3253 100644 --- a/src/openvpn/init.c +++ b/src/openvpn/init.c @@ -54,6 +54,7 @@ #include "mss.h" #include "mudp.h" #include "dco.h" +#include "haproxy_protocol.h" #include "memdbg.h" @@ -3926,6 +3927,14 @@ c->c1.link_socket_addr.bind_local =3D NULL; } + + /* Clear and free the memory allocated for the HAProxy protocol */ + if (c->c1.haproxy_protocol) + { + haproxy_protocol_reset(c->c1.haproxy_protocol); + free(c->c1.haproxy_protocol); + c->c1.haproxy_protocol =3D NULL; + } } /* @@ -4774,7 +4783,7 @@ /* free up environmental variable store */ do_env_set_destroy(c); - /* close HTTP or SOCKS proxy */ + /* close HTTP or SOCKS proxy or PROXY protocol object */ uninit_proxy(c); /* garbage collect */ diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index 03177bb..e6441cd 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -51,6 +51,7 @@ #include "ssl_util.h" #include "dco.h" #include "reflect_filter.h" +#include "haproxy_protocol.h" /*#define MULTI_DEBUG_EVENT_LOOP*/ @@ -779,6 +780,8 @@ goto err; } + ALLOC_OBJ_CLEAR(mi->context.c1.haproxy_protocol, struct haproxy_protoc= ol_info); + mi->context.c2.tls_multi->multi_state =3D CAS_NOT_CONNECTED; if (hash_n_elements(m->hash) >=3D m->max_clients) @@ -789,6 +792,9 @@ if (!real) /* TCP mode? */ { + /* allow checking for HAProxy protocol header at the beginning of = the connection */ + mi->context.c2.link_socket->haproxy_protocol_allowed =3D true; + if (!multi_tcp_instance_specific_init(m, mi)) { goto err; @@ -3207,6 +3213,57 @@ gc_free(&gc); } +void +multi_replace_proxy_addr(struct multi_context *m, struct multi_instance *m= i) +{ + struct mroute_addr real =3D {0}; + struct hash *hash =3D m->hash; + struct gc_arena gc =3D gc_new(); + + if (!mroute_extract_openvpn_sockaddr(&real, &mi->context.c1.haproxy_pr= otocol->src, true)) + { + goto done; + } + + const uint32_t hv =3D hash_value(hash, &real); + struct hash_bucket *bucket =3D hash_bucket(hash, hv); + + /* if the new address is already in the hash table, the new client tak= es precedence */ + struct hash_element *he =3D hash_lookup_fast(hash, bucket, &real, hv); + if (he) + { + struct multi_instance *oldmi =3D (struct multi_instance *) he->val= ue; + msg(D_MULTI_LOW, "Client's real address (behind proxy) matches " + "existing client address -- new client takes precedence"); + oldmi->did_real_hash =3D false; + multi_close_instance(m, oldmi, false); + he->key =3D &mi->real; + he->value =3D mi; + } + + clear_prefix(); + msg(D_MULTI_LOW, "Replacing proxy address: %s with client's real addre= ss: %s", + mroute_addr_print(&mi->real, &gc), + mroute_addr_print(&real, &gc)); + + /* remove proxy address from hash table before changing address */ + ASSERT(hash_remove(m->hash, &mi->real)); + ASSERT(hash_remove(m->iter, &mi->real)); + + mi->real =3D real; + generate_prefix(mi); + + ASSERT(hash_add(m->hash, &mi->real, mi, false)); + ASSERT(hash_add(m->iter, &mi->real, mi, false)); + +#ifdef ENABLE_MANAGEMENT + ASSERT(hash_add(m->cid_hash, &mi->context.c2.mda_context.cid, mi, true= )); +#endif + +done: + gc_free(&gc); +} + /* * Called when an instance should be closed due to the * reception of a soft signal. @@ -3348,6 +3405,7 @@ struct mroute_addr src, dest; unsigned int mroute_flags; struct multi_instance *mi; + struct link_socket_info *lsi; bool ret =3D true; bool floated =3D false; @@ -3387,15 +3445,29 @@ } } + lsi =3D get_link_socket_info(c); + + /* check for PROXY protocol */ + if (lsi->proto =3D=3D PROTO_TCP_SERVER && c->c2.link_socket->hapro= xy_protocol_allowed && BLEN(&c->c2.buf) > 0) + { + if (haproxy_protocol_parse(c->c1.haproxy_protocol, BPTR(&c->c2= .buf), BLEN(&c->c2.buf))) + { + /* replace proxy address with real address */ + multi_replace_proxy_addr(m, m->pending); + /* avoid processing the packet as an openvpn one */ + buf_reset_len(&c->c2.buf); + } + /* stop checking for HAProxy protocol headers */ + c->c2.link_socket->haproxy_protocol_allowed =3D false; + } + if (BLEN(&c->c2.buf) > 0) { - struct link_socket_info *lsi; const uint8_t *orig_buf; /* decrypt in instance context */ =20 perf_push(PERF_PROC_IN_LINK); - lsi =3D get_link_socket_info(c); orig_buf =3D c->c2.buf.data; if (process_incoming_link_part1(c, lsi, floated)) { diff --git a/src/openvpn/openvpn.h b/src/openvpn/openvpn.h index e98c93e..a192f7a 100644 --- a/src/openvpn/openvpn.h +++ b/src/openvpn/openvpn.h @@ -45,6 +45,7 @@ #include "pool.h" #include "plugin.h" #include "manage.h" +#include "haproxy_protocol.h" /* * Our global key schedules, packaged thusly @@ -191,6 +192,8 @@ struct socks_proxy_info *socks_proxy; bool socks_proxy_owned; + struct haproxy_protocol_info *haproxy_protocol; + /* persist --ifconfig-pool db to file */ struct ifconfig_pool_persist *ifconfig_pool_persist; bool ifconfig_pool_persist_owned; diff --git a/src/openvpn/socket.c b/src/openvpn/socket.c index 17c5e76..90914dd 100644 --- a/src/openvpn/socket.c +++ b/src/openvpn/socket.c @@ -39,6 +39,7 @@ #include "manage.h" #include "openvpn.h" #include "forward.h" +#include "haproxy_protocol.h" #include "memdbg.h" @@ -1617,7 +1618,7 @@ stream_buf_close(struct stream_buf *sb); =20 static bool -stream_buf_added(struct stream_buf *sb, int length_added); +stream_buf_added(struct stream_buf *sb, int length_added, bool haproxy_pro= tocol_allowed); /* For stream protocols, allocate a buffer to build up packet. * Called after frame has been finalized. */ @@ -2629,7 +2630,7 @@ { ASSERT(buf_copy(&sock->stream_buf.buf, &sock->stream_buf.residual)= ); ASSERT(buf_init(&sock->stream_buf.residual, 0)); - sock->stream_buf.residual_fully_formed =3D stream_buf_added(&sock-= >stream_buf, 0); + sock->stream_buf.residual_fully_formed =3D stream_buf_added(&sock-= >stream_buf, 0, false); dmsg(D_STREAM_DEBUG, "STREAM: RESIDUAL FULLY FORMED [%s], len=3D%d= ", sock->stream_buf.residual_fully_formed ? "YES" : "NO", sock->stream_buf.residual.len); @@ -2644,7 +2645,8 @@ static bool stream_buf_added(struct stream_buf *sb, - int length_added) + int length_added, + const bool haproxy_protocol_allowed) { dmsg(D_STREAM_DEBUG, "STREAM: ADD length_added=3D%d", length_added); if (length_added > 0) @@ -2661,7 +2663,9 @@ #if PORT_SHARE if (sb->port_share_state =3D=3D PS_ENABLED) { - if (!is_openvpn_protocol(&sb->buf)) + if (!is_openvpn_protocol(&sb->buf) && + (!haproxy_protocol_allowed || + haproxy_protocol_version(BPTR(&sb->buf), BLEN(&sb->buf)) = =3D=3D HAPROXY_PROTOCOL_VERSION_INVALID)) { msg(D_STREAM_ERRORS, "Non-OpenVPN client protocol detected= "); sb->port_share_state =3D PS_FOREIGN; @@ -2680,10 +2684,26 @@ if (sb->len < 1 || sb->len > sb->maxlen) { - msg(M_WARN, "WARNING: Bad encapsulated packet length from peer= (%d), which must be > 0 and <=3D %d -- please ensure that --tun-mtu or --l= ink-mtu is equal on both peers -- this condition could also indicate a poss= ible active attack on the TCP link -- [Attempting restart...]", sb->len, sb= ->maxlen); - stream_buf_reset(sb); - sb->error =3D true; - return false; + haproxy_protocol_version_t hp_version =3D HAPROXY_PROTOCOL_VER= SION_UNKNOWN; + int hp_len =3D -1; + /* if it's allowed (first packet), check for a HAProxy protoco= l header */ + if (haproxy_protocol_allowed) + { + /* undo the reading of net_size */ + ASSERT(buf_prepend(&sb->buf, sizeof(net_size))); + hp_version =3D haproxy_protocol_version(BPTR(&sb->buf), BL= EN(&sb->buf)); + hp_len =3D haproxy_protocol_header_len(BPTR(&sb->buf), BLE= N(&sb->buf), hp_version); + } + + /* we do not allow partial headers */ + if (hp_version <=3D 0 || hp_len < 0 || hp_len > sb->buf.len) + { + msg(M_WARN, "WARNING: Bad encapsulated packet length from = peer (%d), which must be > 0 and <=3D %d -- please ensure that --tun-mtu or= --link-mtu is equal on both peers -- this condition could also indicate a = possible active attack on the TCP link -- [Attempting restart...]", sb->len= , sb->maxlen); + stream_buf_reset(sb); + sb->error =3D true; + return false; + } + sb->len =3D hp_len; } } @@ -3267,7 +3287,7 @@ } if (sock->stream_buf.residual_fully_formed - || stream_buf_added(&sock->stream_buf, len)) /* packet complete? */ + || stream_buf_added(&sock->stream_buf, len, sock->haproxy_protocol= _allowed)) /* packet complete? */ { stream_buf_get_final(&sock->stream_buf, buf); stream_buf_reset(&sock->stream_buf); diff --git a/src/openvpn/socket.h b/src/openvpn/socket.h index 47083ad..1132b35 100644 --- a/src/openvpn/socket.h +++ b/src/openvpn/socket.h @@ -227,6 +227,9 @@ const char *proxy_dest_host; const char *proxy_dest_port; + /* check HAProxy PROXY protocol only if true */ + bool haproxy_protocol_allowed; + /* Pointer to the server-poll to trigger the timeout in function which= have * their own loop instead of using the main oop */ struct event_timeout *server_poll_timeout; diff --git a/tests/unit_tests/openvpn/Makefile.am b/tests/unit_tests/openvp= n/Makefile.am index a4e6235..1802132 100644 --- a/tests/unit_tests/openvpn/Makefile.am +++ b/tests/unit_tests/openvpn/Makefile.am @@ -10,8 +10,9 @@ test_binaries +=3D argv_testdriver buffer_testdriver endif -test_binaries +=3D crypto_testdriver packet_id_testdriver auth_token_testd= river ncp_testdriver misc_testdriver \ - pkt_testdriver ssl_testdriver user_pass_testdriver +test_binaries +=3D crypto_testdriver packet_id_testdriver auth_token_testd= river \ + haproxy_protocol_testdriver ncp_testdriver misc_testdriver \ + pkt_testdriver ssl_testdriver user_pass_testdriver if HAVE_LD_WRAP_SUPPORT if !WIN32 @@ -73,6 +74,16 @@ $(top_srcdir)/src/openvpn/win32-util.c \ $(top_srcdir)/src/openvpn/mss.c +haproxy_protocol_testdriver_CFLAGS =3D \ + -I$(top_srcdir)/include -I$(top_srcdir)/src/openvpn -I$(top_srcdir)/src/c= ompat \ + @TEST_CFLAGS@ +haproxy_protocol_testdriver_LDFLAGS =3D @TEST_LDFLAGS@ +haproxy_protocol_testdriver_SOURCES =3D test_haproxy_protocol.c mock_msg.c= mock_msg.h \ + mock_get_random.c \ + $(top_srcdir)/src/openvpn/platform.c \ + $(top_srcdir)/src/openvpn/buffer.c \ + $(top_srcdir)/src/openvpn/haproxy_protocol.c + ssl_testdriver_CFLAGS =3D \ -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/op= envpn \ @TEST_CFLAGS@ diff --git a/tests/unit_tests/openvpn/test_haproxy_protocol.c b/tests/unit_= tests/openvpn/test_haproxy_protocol.c new file mode 100644 index 0000000..0464315 --- /dev/null +++ b/tests/unit_tests/openvpn/test_haproxy_protocol.c @@ -0,0 +1,354 @@ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "syshead.h" + +#include +#include + +#include "platform.h" +#include "buffer.h" +#include "haproxy_protocol.h" +#include "test_common.h" + +extern uint32_t +haproxy_protocol_crc32c(const uint8_t *data, int len); + +/* header building blocks */ +#define HP1_SIG 0x50, 0x52, 0x4f, 0x58, 0x59 /* "PROXY" */ +#define HP1_TCP4_AF_UNKN 0x55, 0x4E, 0x4B, 0x4E, 0x4F, 0x57, 0x4E /* "UNK= NOWN" */ +#define HP1_TCP4_AF_INET 0x54, 0x43, 0x50, 0x34 /* "TCP4" */ +#define HP1_TCP6_AF_INET 0x54, 0x43, 0x50, 0x36 /* "TCP6" */ +#define HP1_TCP4_SRC_ADDR 0x31, 0x30, 0x2e, 0x31, 0x30, 0x2e, 0x32, 0x30, = 0x2e, 0x32 /* "10.10.20.2" */ +#define HP1_TCP4_DST_ADDR 0x31, 0x30, 0x2e, 0x31, 0x30, 0x2e, 0x32, 0x30, = 0x2e, 0x31 /* "10.10.20.1" */ +#define HP1_TCP4_SRC_PORT 0x34, 0x30, 0x31, 0x35, 0x30 /* 40150 */ +#define HP1_TCP4_DST_PORT 0x31, 0x31, 0x39, 0x35 /* 1195 */ +#define HP1_TCP6_SRC_ADDR 0x32, 0x30, 0x30, 0x31, 0x3a, 0x64, 0x62, 0x38, = 0x3a, 0x32, 0x30, 0x3a, 0x3a, 0x32 /* "2001:db8:20::2" */ +#define HP1_TCP6_DST_ADDR 0x32, 0x30, 0x30, 0x31, 0x3a, 0x64, 0x62, 0x38, = 0x3a, 0x32, 0x30, 0x3a, 0x3a, 0x31 /* "2001:db8:20::1" */ +#define HP1_TCP6_SRC_PORT 0x35, 0x33, 0x33, 0x34, 0x34 /* 53344 */ +#define HP1_TCP6_DST_PORT 0x31, 0x31, 0x39, 0x35 /* 1195 */ +#define HP1_END 0x0d, 0x0a /* "\r\n" */ + +#define HP2_SIG 0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x51, 0x= 55, 0x49, 0x54, 0x0a /* "\r\n\r\n\x00\r\nQUIT\n" */ +#define HP2_V4_SRC_ADDR 0x0a, 0x0a, 0x14, 0x02 /* 10.10.20.2 */ +#define HP2_V4_DST_ADDR 0x0a, 0x0a, 0x14, 0x01 /* 10.10.20.1 */ +#define HP2_V4_SRC_PORT 0xa5, 0x30 /* 42288 */ +#define HP2_V4_DST_PORT 0x04, 0xab /* 1195 */ +#define HP2_V6_SRC_ADDR 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x20, 0x00, 0x00, 0x= 00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 /* 2001:db8:20::2 */ +#define HP2_V6_DST_ADDR 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x20, 0x00, 0x00, 0x= 00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 /* 2001:db8:20::1 */ +#define HP2_V6_SRC_PORT 0xe8, 0xb6 /* 59574 */ +#define HP2_V6_DST_PORT 0x04, 0xab /* 1195 */ +#define HP2_TLV \ + 0x01, 0x00, 0x03, 'h', '2', '3', \ + 0x02, 0x00, 0x0b, 'e', 'x', 'a', 'm', 'p', 'l', 'e', '.', 'c', 'o', 'm= ', \ + 0x04, 0x00, 0x00, \ + 0x05, 0x00, 0x0f, 'u', 'n', 'i', 'q', 'u', 'e', '-', 'i', 'd', '-', 'v= ', 'a', 'l', 'u', 'e', \ + 0x20, 0x00, 0x33, 0x01, 0x00, 0x00, 0x00, 0x00, \ + 0x21, 0x00, 0x06, 'T', 'L', 'S', '1', '.', '3', \ + 0x22, 0x00, 0x06, 'c', 'l', 'i', 'e', 'n', 't', \ + 0x23, 0x00, 0x06, 'A', 'E', 'S', '2', '5', '6', \ + 0x24, 0x00, 0x06, 'S', 'H', 'A', '2', '5', '6', \ + 0x25, 0x00, 0x07, 'R', 'S', 'A', '2', '0', '4', '8', \ + 0x30, 0x00, 0x07, 'n', 'e', 't', 'n', 's', '-', '1' + +#define HP_TEST_OPENVPN \ + 0x00, 0x0e, 0x38, 0xa8, 0xf5, 0x24, 0xa0, 0xa7, \ + 0x89, 0x00, 0xca, 0x00, 0x00, 0x00, 0x00, 0x00 + +/* headers */ +static uint8_t HP1_TEST_UNKNOWN[] =3D { + HP1_SIG, 0x20, HP1_TCP4_AF_UNKN, HP1_END +}; + +static uint8_t HP1_TEST_TCP4[] =3D { + HP1_SIG, 0x20, + HP1_TCP4_AF_INET, 0x20, + HP1_TCP4_SRC_ADDR, 0x20, + HP1_TCP4_DST_ADDR, 0x20, + HP1_TCP4_SRC_PORT, 0x20, + HP1_TCP4_DST_PORT, + HP1_END +}; + +static uint8_t HP1_TEST_TCP4_PLUS_OPENVPN[] =3D { + HP1_SIG, 0x20, + HP1_TCP4_AF_INET, 0x20, + HP1_TCP4_SRC_ADDR, 0x20, + HP1_TCP4_DST_ADDR, 0x20, + HP1_TCP4_SRC_PORT, 0x20, + HP1_TCP4_DST_PORT, + HP1_END, + HP_TEST_OPENVPN +}; + +static uint8_t HP1_TEST_TCP6[] =3D { + HP1_SIG, 0x20, + HP1_TCP6_AF_INET, 0x20, + HP1_TCP6_SRC_ADDR, 0x20, + HP1_TCP6_DST_ADDR, 0x20, + HP1_TCP6_SRC_PORT, 0x20, + HP1_TCP6_DST_PORT, + HP1_END +}; + +static uint8_t HP1_TEST_TCP6_PLUS_OPENVPN[] =3D { + HP1_SIG, 0x20, + HP1_TCP6_AF_INET, 0x20, + HP1_TCP6_SRC_ADDR, 0x20, + HP1_TCP6_DST_ADDR, 0x20, + HP1_TCP6_SRC_PORT, 0x20, + HP1_TCP6_DST_PORT, + HP1_END, + HP_TEST_OPENVPN +}; + +static uint8_t HP2_TEST_LOCAL[] =3D { + HP2_SIG, + HAPROXY_PROTOCOL_V2_VER | HAPROXY_PROTOCOL_V2_LOCAL_CMD, + HAPROXY_PROTOCOL_V2_AF_UNSPEC | HAPROXY_PROTOCOL_V2_TP_UNSPEC, + 0x00, 0x00 /* length */ +}; + +static uint8_t HP2_TEST_WRONG_VER[] =3D { + HP2_SIG, + 0x10 | HAPROXY_PROTOCOL_V2_PROXY_CMD, + HAPROXY_PROTOCOL_V2_AF_INET | HAPROXY_PROTOCOL_V2_TP_STREAM, + 0x00, 0x0c, /* length */ + HP2_V4_SRC_ADDR, + HP2_V4_DST_ADDR, + HP2_V4_SRC_PORT, + HP2_V4_DST_PORT, +}; + +static uint8_t HP2_TEST_PROXY_TCP4[] =3D { + HP2_SIG, + HAPROXY_PROTOCOL_V2_VER | HAPROXY_PROTOCOL_V2_PROXY_CMD, + HAPROXY_PROTOCOL_V2_AF_INET | HAPROXY_PROTOCOL_V2_TP_STREAM, + 0x00, 0x7c, /* length */ + HP2_V4_SRC_ADDR, + HP2_V4_DST_ADDR, + HP2_V4_SRC_PORT, + HP2_V4_DST_PORT, + HP2_TLV, + 0x03, 0x00, 0x04, 0x6e, 0x8a, 0xbf, 0x7f /* crc32c */ +}; + +static uint8_t HP2_TEST_PROXY_TCP4_PLUS_OPENVPN[] =3D { + HP2_SIG, + HAPROXY_PROTOCOL_V2_VER | HAPROXY_PROTOCOL_V2_PROXY_CMD, + HAPROXY_PROTOCOL_V2_AF_INET | HAPROXY_PROTOCOL_V2_TP_STREAM, + 0x00, 0x7c, /* length */ + HP2_V4_SRC_ADDR, + HP2_V4_DST_ADDR, + HP2_V4_SRC_PORT, + HP2_V4_DST_PORT, + HP2_TLV, + 0x03, 0x00, 0x04, 0x6e, 0x8a, 0xbf, 0x7f, /* crc32c */ + HP_TEST_OPENVPN +}; + +static uint8_t HP2_TEST_PROXY_TCP6[] =3D { + HP2_SIG, + HAPROXY_PROTOCOL_V2_VER | HAPROXY_PROTOCOL_V2_PROXY_CMD, + HAPROXY_PROTOCOL_V2_AF_INET6 | HAPROXY_PROTOCOL_V2_TP_STREAM, + 0x00, 0x94, /* length */ + HP2_V6_SRC_ADDR, + HP2_V6_DST_ADDR, + HP2_V6_SRC_PORT, + HP2_V6_DST_PORT, + HP2_TLV, + 0x03, 0x00, 0x04, 0xed, 0xb0, 0x61, 0xb6 /* crc32c */ +}; + +static uint8_t HP2_TEST_PROXY_TCP6_PLUS_OPENVPN[] =3D { + HP2_SIG, + HAPROXY_PROTOCOL_V2_VER | HAPROXY_PROTOCOL_V2_PROXY_CMD, + HAPROXY_PROTOCOL_V2_AF_INET6 | HAPROXY_PROTOCOL_V2_TP_STREAM, + 0x00, 0x94, /* length */ + HP2_V6_SRC_ADDR, + HP2_V6_DST_ADDR, + HP2_V6_SRC_PORT, + HP2_V6_DST_PORT, + HP2_TLV, + 0x03, 0x00, 0x04, 0xed, 0xb0, 0x61, 0xb6, /* crc32c */ + HP_TEST_OPENVPN +}; + +static void +test_haproxy_protocol_version(void **state) +{ + assert_int_equal(haproxy_protocol_version(HP1_TEST_UNKNOWN, sizeof(HP1= _TEST_UNKNOWN)), HAPROXY_PROTOCOL_VERSION_1); + assert_int_equal(haproxy_protocol_version(HP1_TEST_TCP4, sizeof(HP1_TE= ST_TCP4)), HAPROXY_PROTOCOL_VERSION_1); + assert_int_equal(haproxy_protocol_version(HP1_TEST_TCP6, sizeof(HP1_TE= ST_TCP6)), HAPROXY_PROTOCOL_VERSION_1); + assert_int_equal(haproxy_protocol_version(HP2_TEST_WRONG_VER, sizeof(H= P2_TEST_WRONG_VER)), HAPROXY_PROTOCOL_VERSION_2); + assert_int_equal(haproxy_protocol_version(HP2_TEST_LOCAL, sizeof(HP2_T= EST_LOCAL)), HAPROXY_PROTOCOL_VERSION_2); + assert_int_equal(haproxy_protocol_version(HP2_TEST_PROXY_TCP4, sizeof(= HP2_TEST_PROXY_TCP4)), HAPROXY_PROTOCOL_VERSION_2); + assert_int_equal(haproxy_protocol_version(HP2_TEST_PROXY_TCP6, sizeof(= HP2_TEST_PROXY_TCP6)), HAPROXY_PROTOCOL_VERSION_2); + assert_int_equal(haproxy_protocol_version((uint8_t []){HP_TEST_OPENVPN= }, sizeof((uint8_t []){HP_TEST_OPENVPN})), HAPROXY_PROTOCOL_VERSION_INVALID= ); +} + +static void +test_haproxy_protocol_header_len(void **state) +{ + assert_int_equal(haproxy_protocol_header_len(HP1_TEST_UNKNOWN, sizeof(= HP1_TEST_UNKNOWN), HAPROXY_PROTOCOL_VERSION_1), sizeof(HP1_TEST_UNKNOWN)); + assert_int_equal(haproxy_protocol_header_len(HP1_TEST_TCP4_PLUS_OPENVP= N, sizeof(HP1_TEST_TCP4_PLUS_OPENVPN), HAPROXY_PROTOCOL_VERSION_1), sizeof(= HP1_TEST_TCP4)); + assert_int_equal(haproxy_protocol_header_len(HP1_TEST_TCP6_PLUS_OPENVP= N, sizeof(HP1_TEST_TCP6_PLUS_OPENVPN), HAPROXY_PROTOCOL_VERSION_1), sizeof(= HP1_TEST_TCP6)); + assert_int_equal(haproxy_protocol_header_len(HP2_TEST_WRONG_VER, sizeo= f(HP2_TEST_WRONG_VER), HAPROXY_PROTOCOL_VERSION_2), sizeof(HP2_TEST_WRONG_V= ER)); + assert_int_equal(haproxy_protocol_header_len(HP2_TEST_LOCAL, sizeof(HP= 2_TEST_LOCAL), HAPROXY_PROTOCOL_VERSION_2), sizeof(HP2_TEST_LOCAL)); + assert_int_equal(haproxy_protocol_header_len(HP2_TEST_PROXY_TCP4_PLUS_= OPENVPN, sizeof(HP2_TEST_PROXY_TCP4_PLUS_OPENVPN), HAPROXY_PROTOCOL_VERSION= _2), sizeof(HP2_TEST_PROXY_TCP4)); + assert_int_equal(haproxy_protocol_header_len(HP2_TEST_PROXY_TCP6_PLUS_= OPENVPN, sizeof(HP2_TEST_PROXY_TCP6_PLUS_OPENVPN), HAPROXY_PROTOCOL_VERSION= _2), sizeof(HP2_TEST_PROXY_TCP6)); + assert_int_equal(haproxy_protocol_header_len((uint8_t []){HP_TEST_OPEN= VPN}, sizeof((uint8_t []){HP_TEST_OPENVPN}), HAPROXY_PROTOCOL_VERSION_INVAL= ID), -1); +} + +static bool +compare_hpi_basic(void **state, struct haproxy_protocol_info *actual, stru= ct haproxy_protocol_info *expected) +{ + if (actual->src.addr.sa.sa_family !=3D expected->src.addr.sa.sa_family + || actual->dst.addr.sa.sa_family !=3D expected->dst.addr.sa.sa_fam= ily) + { + return false; + } + + switch (actual->src.addr.sa.sa_family) + { + case AF_INET: + return actual->version =3D=3D expected->version + && actual->sock_type =3D=3D expected->sock_type + && memcmp(&actual->src.addr.in4.sin_addr, &expected->sr= c.addr.in4.sin_addr, sizeof(struct in_addr)) =3D=3D 0 + && memcmp(&actual->dst.addr.in4.sin_addr, &expected->ds= t.addr.in4.sin_addr, sizeof(struct in_addr)) =3D=3D 0 + && actual->dst.addr.in4.sin_port =3D=3D expected->dst.a= ddr.in4.sin_port + && actual->src.addr.in4.sin_port =3D=3D expected->src.a= ddr.in4.sin_port; + + case AF_INET6: + return actual->version =3D=3D expected->version + && actual->sock_type =3D=3D expected->sock_type + && memcmp(&actual->src.addr.in6, &expected->src.addr.in= 6, sizeof(struct sockaddr_in6)) =3D=3D 0 + && memcmp(&actual->dst.addr.in6, &expected->dst.addr.in= 6, sizeof(struct sockaddr_in6)) =3D=3D 0; + + default: + return false; + } +} + +static bool +compare_hpi(void **state, struct haproxy_protocol_info *actual, struct hap= roxy_protocol_info *expected) +{ + return compare_hpi_basic(state, actual, expected); +} + +static int +setup_parse(void **state) +{ + struct haproxy_protocol_info *actual; + ALLOC_OBJ_CLEAR(actual, struct haproxy_protocol_info); + *state =3D actual; + return 0; +} + +static int +teardown_parse(void **state) +{ + haproxy_protocol_reset(*state); + free(*state); + return 0; +} + +static void +test_haproxy_protocol_parse(void **state) +{ + struct haproxy_protocol_info *expected; + struct haproxy_protocol_info *actual =3D *state; + + ALLOC_OBJ_CLEAR(expected, struct haproxy_protocol_info); + + /* v1 UNKNOWN protocol */ + assert_false(haproxy_protocol_parse(actual, HP1_TEST_UNKNOWN, sizeof(H= P1_TEST_UNKNOWN))); + + /* v1 TCP4 */ + memset((actual), 0, sizeof(struct haproxy_protocol_info)); + haproxy_protocol_reset(actual); + expected->version =3D HAPROXY_PROTOCOL_VERSION_1; + expected->sock_type =3D SOCK_STREAM; + expected->src.addr.sa.sa_family =3D AF_INET; + expected->src.addr.in4.sin_addr.s_addr =3D htonl(0x0a0a1402); + expected->src.addr.in4.sin_port =3D ntohs(40150); + expected->dst.addr.sa.sa_family =3D AF_INET; + expected->dst.addr.in4.sin_addr.s_addr =3D htonl(0x0a0a1401); + expected->dst.addr.in4.sin_port =3D ntohs(1195); + haproxy_protocol_parse(actual, HP1_TEST_TCP4, sizeof(HP1_TEST_TCP4)); + assert_true(compare_hpi(state, actual, expected)); + + /* v1 TCP6 */ + haproxy_protocol_reset(actual); + haproxy_protocol_reset(expected); + memset((actual), 0, sizeof(struct haproxy_protocol_info)); + memset((expected), 0, sizeof(struct haproxy_protocol_info)); + expected->version =3D HAPROXY_PROTOCOL_VERSION_1; + expected->sock_type =3D SOCK_STREAM; + expected->src.addr.in6.sin6_family =3D AF_INET6; + expected->src.addr.in6.sin6_port =3D ntohs(53344); + inet_pton(AF_INET6, "2001:db8:20::2", &expected->src.addr.in6.sin6_add= r); + expected->dst.addr.in6.sin6_family =3D AF_INET6; + expected->dst.addr.in6.sin6_port =3D ntohs(1195); + inet_pton(AF_INET6, "2001:db8:20::1", &expected->dst.addr.in6.sin6_add= r); + haproxy_protocol_parse(actual, HP1_TEST_TCP6, sizeof(HP1_TEST_TCP6)); + assert_true(compare_hpi(state, actual, expected)); + + /* v2 wrong version */ + haproxy_protocol_reset(actual); + memset((actual), 0, sizeof(struct haproxy_protocol_info)); + assert_false(haproxy_protocol_parse(actual, HP2_TEST_WRONG_VER, sizeof= (HP2_TEST_WRONG_VER))); + + /* v2 local */ + haproxy_protocol_reset(actual); + memset((actual), 0, sizeof(struct haproxy_protocol_info)); + assert_false(haproxy_protocol_parse(actual, HP2_TEST_LOCAL, sizeof(HP2= _TEST_LOCAL))); + + /* v2 TCP4 */ + haproxy_protocol_reset(actual); + haproxy_protocol_reset(expected); + memset((actual), 0, sizeof(struct haproxy_protocol_info)); + memset((expected), 0, sizeof(struct haproxy_protocol_info)); + expected->version =3D HAPROXY_PROTOCOL_VERSION_2; + expected->sock_type =3D SOCK_STREAM; + expected->src.addr.in4.sin_family =3D AF_INET; + expected->src.addr.in4.sin_port =3D ntohs(42288); + expected->src.addr.in4.sin_addr.s_addr =3D htonl(0x0a0a1402); + expected->dst.addr.in4.sin_family =3D AF_INET; + expected->dst.addr.in4.sin_port =3D ntohs(1195); + expected->dst.addr.in4.sin_addr.s_addr =3D htonl(0x0a0a1401); + haproxy_protocol_parse(actual, HP2_TEST_PROXY_TCP4, sizeof(HP2_TEST_PR= OXY_TCP4)); + assert_true(compare_hpi(state, actual, expected)); + + /* v2 TCP6 */ + haproxy_protocol_reset(actual); + haproxy_protocol_reset(expected); + memset((actual), 0, sizeof(struct haproxy_protocol_info)); + memset((expected), 0, sizeof(struct haproxy_protocol_info)); + expected->version =3D HAPROXY_PROTOCOL_VERSION_2; + expected->sock_type =3D SOCK_STREAM; + expected->src.addr.in6.sin6_family =3D AF_INET6; + expected->src.addr.in6.sin6_port =3D ntohs(59574); + inet_pton(AF_INET6, "2001:db8:20::2", &expected->src.addr.in6.sin6_add= r); + expected->dst.addr.in6.sin6_family =3D AF_INET6; + expected->dst.addr.in6.sin6_port =3D ntohs(1195); + inet_pton(AF_INET6, "2001:db8:20::1", &expected->dst.addr.in6.sin6_add= r); + haproxy_protocol_parse(actual, HP2_TEST_PROXY_TCP6, sizeof(HP2_TEST_PR= OXY_TCP6)); + assert_true(compare_hpi(state, actual, expected)); + + haproxy_protocol_reset(expected); + free(expected); +} + +int +main(void) +{ + openvpn_unit_test_setup(); + const struct CMUnitTest tests[] =3D { + cmocka_unit_test(test_haproxy_protocol_version), + cmocka_unit_test(test_haproxy_protocol_header_len), + cmocka_unit_test_setup_teardown(test_haproxy_protocol_parse, setup= _parse, teardown_parse) + }; + return cmocka_run_group_tests_name("haproxy_protocol", tests, NULL, NU= LL); +} -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/685?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I9766c68feaba71279f26c3310eee4b5ca215e6ac Gerrit-Change-Number: 685 Gerrit-PatchSet: 4 Gerrit-Owner: ralf_lici Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: flichtenheld Gerrit-MessageType: newpatchset --6M/a/JBbHYY= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld, plaisthos.

ralf_lici uploaded patch set #4 to this change.

<= a href=3D"http://gerrit.openvpn.net/c/openvpn/+/685?usp=3Demail">View Chang= e

Add support for HAProxy's PROXY protocol

The PROXY protoco= l is a simple protocol that can be used to
communicate connection inform= ation such as a client's address across
multiple layers of NAT or TC= P proxies. This commit adds support for
the parsing of the v1 and v2 PRO= XY protocol header but does not yet
parse the TLV data in the v2 header.=

Check if a PROXY protocol header was sent by the client at the
b= eginning of the connection. If so, parse it, store the extracted
informa= tions and replace the proxy address with the real client
address. Checki= ng for the header is done only if the packet has an
invalid length so th= at there's minimal overhead for the common case.

Also add a smal= l document file showing how to configure HAProxy to
test the feature and= a unit test for the parsing.

Change-Id: I9766c68feaba71279f26c3310e= ee4b5ca215e6ac
Signed-off-by: Ralf Lici <ralf@...2726...>
---M CMakeLists.txt
A doc/haproxy-protocol.txt
M src/openvpn/Makefile.= am
A src/openvpn/haproxy_protocol.c
A src/openvpn/haproxy_protocol.h<= br>M src/openvpn/init.c
M src/openvpn/multi.c
M src/openvpn/openvpn.h=
M src/openvpn/socket.c
M src/openvpn/socket.h
M tests/unit_tests/= openvpn/Makefile.am
A tests/unit_tests/openvpn/test_haproxy_protocol.c12 files changed, 1,326 insertions(+), 14 deletions(-)

git pull=
 ssh://gerrit.openvpn.net:29418/openvpn refs/changes/85/685/4
diff =
--git a/CMakeLists.txt b/CMakeLists.txt
index ad620fa..e927= a01 100644
--- a/CMakeLists.txt
+++ b/CMake= Lists.txt
@@ -419,6 +419,8 @@
src/open= vpn/fragment.h
src/openvpn/gremlin.c
= src/openvpn/gremlin.h
+ src/openvpn/haproxy_protocol.c
+ src/openvpn/haproxy_protocol.h
= src/openvpn/helper.c
src/openvpn/helper.h
= src/openvpn/httpdigest.c
diff --git a/doc/haprox= y-protocol.txt b/doc/haproxy-protocol.txt
new file mode 100= 644
index 0000000..604951a
--- /dev/null
+++ b/doc/haproxy-protocol.txt
@@ -0,0 +1,166= @@
+HAProxy PROXY pr= otocol support for OpenVPN
+=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
+

+The PROXY protocol is a simple protocol that can be u= sed to retreive the
+= original source address of a connection that has been proxied. Every connec= tion
+is prepended wi= th a header reporting the client IP address and port. This
+document aims to breifly describe th= e protocol and how it can be tested in
+OpenVPN. For more information about the protocol, see th= e official
+specifica= tion at https://www.haproxy.org/download/2.4/doc/proxy-protocol.txt.=
+
+Currently there are two versions of the proto= col, a text based version (v1) and
+a binary version (v2). Although the binary version is more e= fficient, the text
+b= ased version is still the only version supported by some proxies therefore = it
+we support both v= ersions.
+
= +The protocol is agnostic to th= e underlying transport protocol, so it can be used
+with both TCP, UDP and UNIX sockets but curr= ently only TCP is supported by
+OpenVPN.
+=
+Version 1+---------
+
+The text based version is a human readable protocol that i= s easy to debug. It
+= consists of a single line of text with the following fields:
+
+PROXY_SIGNATURE_V1 + single space + INET_PROTOCOL + s= ingle space + CLIENT_IP +
+single space + PROXY_IP + single space + CLIENT_PORT + single space +=
+PROXY_PORT + "= \r\n"
++The only supported INET_PROT= OCOL in version 1 are "TCP4" and "TCP6".
+
+Version 2
+---------
+
+The binary versio= n is a more efficient protocol that is more suitable for
+production use. It consists of a fixed= length header followed by the original
+connection data.
+
+A = PROXY Protocol binary header for an IPv4 incoming address has the format:
+ 0 1 = 2 3
+ 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7<= /span>
++-+-+-+-+-+-+-+-+-+-= +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+| = |
++ = +
+| Proxy Protoco= l v2 Signature |

++ = +
+| = |
++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-= +-+-+-+-+-+-+-+-+-+-+
+|Version|Command| AF | Proto.| Address Length |<= br>++-+-+-+-+-+-+-+-+-+-+-+-+-+= -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+| IPv4 Source Address = |
++-+-+-+-+-= +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+| IPv4 Destination A= ddress |
++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+| Source Port= | Destination Port |
++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+= -+-+-+-+-+-+-+
+| = |
++ = +

++ TLVs = +
++ = +
+| = |
+= +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-++
+A PROXY Protocol binary header for a IPv6 incomi= ng address has the format:
+ 0 1 2 3
+ 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 = 2 3 4 5 6 7 0 1 2 3 4 5 6 7
++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+| = |
++ = +
+| = Proxy Protocol v2 Signature |
++ = +

+| = |
++-+-+-+-+-+-+-+-= +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+|Version|Command| AF | Proto.| Add= ress Length |
= ++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-++| = |
++ = +
+| = |
++ IPv6 Source Addre= ss +
+| |
++ = +
+| = |
++-+-+-= +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+| = |

++ = +
+| = |
++ IPv6 Destination Address = +
+|= |
= ++ = +
+| = |
++-+-+-+-+-+-+= -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+| Source Port | = Destination Port |
++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

+| = |
++ = +
++ = TLVs +
++ = +
+| |=
++-+-+-+-+-+-+-+-+-+= -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+
+The fields are as follows:
+
+- Pr= oxy Protocol v2 Signature: 12 bytes
+ The v2 signature of the protocol: "\x0D\x0A\x0D\x0A\= x00\x0D\x0A\x51\x55\x49\x54\x0A\x20".
+
+- Version: 4 bits
+ The version of the PROXY protocol (0x2).
+
+- Command: 4 bits
+ Can by one of the following:
+ - LOCAL: 0x0
+ The connection was initiated by the proxy for health-chec= ks or similar.
+ = This command is not supported and therefore the header is ignored.<= br>+ - PROXY: 0x1
= + The connection was initi= ated by the client.
+=
+- AF: 4 bits=
+ The address family of = the connection can be: AF_UNSPEC (0x0), AF_INET (0x1),
+ AF_INET6 (0x2) or AF_UNIX (0x3). Onl= y AF_INET and AF_INET6 are supported.
+
+-= Proto: 4 bits
+ T= he protocol of the connection can be: UNSPEC (0x0), STREAM (0x1) or<= br>+ DGRAM (0x2). Only STREA= M is supported.
+
+- Address Length: 16 bi= ts
+ The length of= the rest of the header in bytes.
+
+Final= ly there may be additional data arranged as a series of TLV
+(Type-Length-Value) vectors that ca= n be used to carry additional information.

+
+OpenVPN testing
+= ---------------
+
+Currently only TCP is s= upported. To test the PROXY protocol support in OpenVPN,
+you can use HAProxy as a proxy with a = configuration like this:
+
+global<= br>+ log stdout format raw l= ocal0
+ log 127.0.= 0.1 local0 debug
+ = daemon
+
<= span style=3D"color: hsl(120, 100%, 40%);">+defaults

+ log global
+ mode tcp
+ option tcplog
+ timeout connect 5000ms
+ timeout client 50000ms
<= span style=3D"color: hsl(120, 100%, 40%);">+ timeout server 50000ms
+
+frontend openvpn_front
+ bind 10.10.20.1:1195
+ default_backend openvpn_back<= /span>
+
+backend openvpn_back
+ server openvpn_server 10.10.10.1:1= 194 send-proxy
+ #= server openvpn_server 10.10.10.1:1194 send-proxy-v2 proxy-v2-options ssl,c= ert-cn,ssl-cipher,cert-sig,cert-key,authority,crc32c,unique-id
+
+
+You can use the commented line to test the binary format and the TLV d= ata
+instead of the t= ext based format.
+
+Note that you should = set the keepalive option so that the connection is not
+reset by HAProxy for inactivity (timeout= option in the config).
+
+The OpenVPN TCP= server should read and parse the header and use the information
= +to display the original source= address in the logs.
diff --git a/src/openvpn/Makefile.am = b/src/openvpn/Makefile.am
index 56cce9d..9dce362 100644

--- a/src/openvpn/Makefile.am
+++ b/src/openvp= n/Makefile.am
@@ -65,6 +65,7 @@
forward.c= forward.h \
fragment.c fragment.h \
gr= emlin.c gremlin.h \
+= haproxy_protocol.c haproxy_protocol.h \
helper.c helper.= h \
httpdigest.c httpdigest.h \
lladdr.= c lladdr.h \
diff --git a/src/openvpn/haproxy_protocol.c b/= src/openvpn/haproxy_protocol.c
new file mode 100644<= br>index 0000000..1786d2f
--- /dev/null
+++ b/src/openvpn/haproxy_protocol.c
@@ -0,0 +1,487 @@
+/*
+ * OpenVPN -- An application to secur= ely tunnel IP networks
+ * ses= sion authentication and key exchange,
+ * packet encryption, packet authentication, = and
+ * p= acket compression.
+ = *
+ * Copyright (C) = 2024 OpenVPN Inc <sales@...515...>
+ *
+ * This program is free software; you can redistribute it and/or modif= y
+ * it under the t= erms of the GNU General Public License version 2
+ * as published by the Free Software Foundati= on.
+ *
+ * This program is distributed i= n the hope that it will be useful,

+ * but WITHOUT ANY WARRANTY; without even the implied warra= nty of
+ * MERCHANTA= BILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more d= etails.
+ *+ * You should have received = a copy of the GNU General Public License along
+ * with this program; if not, write to the Free= Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#inc= lude "syshead.h"
+
+#include &qu= ot;error.h"
+#in= clude "haproxy_protocol.h"
+
+#d= efine HAPROXY_PROTOCOL_V1_MAX_WORDS 6
+#define HAPROXY_PROTOCOL_V1_MAX_WORD_LEN 40
+
+typedef enum
+{
+ = HAPROXY_PROTOCOL_PARSING_STATE_INVALID =3D -1,
+ HAPROXY_PROTOCOL_PARSING_STATE_OK =3D 0,
+ HAPROXY_PROTOCOL_= PARSING_STATE_IGNORE =3D 1,
+} haproxy_protocol_parsing_state_t;
+
+static const size_t HAPROXY_PROTOCOL_V2_ADDR_LEN_IPV4 =3D 12;+static const size_t HAPROXY_= PROTOCOL_V2_ADDR_LEN_IPV6 =3D 36;
+static const size_t HAPROXY_PROTOCOL_V2_ADDR_LEN_UNIX =3D 216= ;
+
+static haproxy_protocol_header_t *hea= der =3D NULL;
+static= uint16_t header_len =3D 0;
+static haproxy_protocol_parsing_state_t parsing_state =3D HAPROXY_P= ROTOCOL_PARSING_STATE_OK;
+
+haproxy_proto= col_version_t
+haprox= y_protocol_version(const uint8_t *buf, const int buf_len)
+{
+ if (buf_len >=3D HAPROXY_PROTOCOL_V2_MIN_HDR_LEN=
+ && = memcmp(buf, HAPROXY_PROTOCOL_V2_SIG, HAPROXY_PROTOCOL_V2_SIG_LEN) =3D=3D 0)=
+ {
+ return HAPROXY_PROTOCOL_V= ERSION_2;

+ }

+ else if (buf_len >= ;=3D HAPROXY_PROTOCOL_V1_MIN_HDR_LEN
+ && memcmp(buf, HAPROXY_PROTOCOL_V1_SI= G, HAPROXY_PROTOCOL_V1_SIG_LEN) =3D=3D 0)
+ {
+ return HAPROXY_PROTOCOL_VERSION_1;
+ }
+ return HAPROXY_PROTOCOL_VERSION_INVALID;
+}
+
+int
+haproxy_pro= tocol_header_len(const uint8_t *buf, const int buf_len,
+ const hapro= xy_protocol_version_t version)
+{
+ swi= tch (version)
+ {<= /span>
+ case HAPROXY= _PROTOCOL_VERSION_1:
= + {
+ = const char *end =3D memchr(buf, '\r', buf_len - 1);
+ /* partial or invali= d header */

+ = if (!end || end[1] !=3D '\n')
+ {
+ return -1;
+ }
+ return (int)(end + 2 - (char *)buf);+ }
+
+ case HAPROXY_PROTOCOL_VERSION_2:
+ /* byte 15 and 16 contain= the length */
+ = return HAPROXY_PROTOCOL_V2_MIN_HDR_LEN + ntohps(*(uint16_t *)(buf + = 14));
+
+ default:
+ return -1;
+ }
+}
+
+/*=
+ * Parse a port number fro= m a string.
+ *
+ * @param port_str - The = string to parse.
+ *<= /span>
+ * @return - The por= t number or -1 if the string is invalid.
+ */
+uint16_t
+haprox= y_protocol_parse_port(const char *port_str)
+{
+ char *endptr;
+ errno =3D 0;
= +
+ long port =3D = strtol(port_str, &endptr, 10);
+ if (errno !=3D 0 || endptr =3D=3D port_str || *endptr != =3D '\0' || port <=3D 0 || port > 65535)
+ {
+ return -1;
+ }
+ return (uint16_t)port;
+}
+
+/*
+ * Set the source and destination address= es and ports in the proxy protocol
+ * info structure for later use.
+ *
+ * @param hpi - The haproxy protocol info structure to store the= addresses.
+ * @para= m ver - The version of the PROXY protocol.
+ * @param fam - The address family (supports: AF_INE= T or AF_INET6).
+ * @= param st - The socket type.
+ * @param src_addr - The source address.
+ * @param dst_addr - The destination address.<= /span>
+ * @param src_port -= The source port.
+ *= @param dst_port - The destination port.
+ */
+void
+haproxy_pr= otocol_set_addr(struct haproxy_protocol_info *hpi,
+ const haproxy_prot= ocol_version_t ver, const int fam, const int st,
+ const void *src_addr= , const void *dst_addr,
+ const uint16_t src_port, const uint16_t dst_p= ort)
+{
+

+ hpi->version =3D ver;
+ hpi->sock_type =3D st;
<= span style=3D"color: hsl(120, 100%, 40%);">+ if (fam =3D=3D AF_INET)

+ {
+ hpi->src.addr.sa.sa_family= =3D AF_INET;
+ = hpi->dst.addr.sa.sa_family =3D AF_INET;
+ hpi->src.addr.in4.sin_addr.s_addr =3D *= (uint32_t *)src_addr;
+ hpi->dst.addr.in4.sin_addr.s_addr =3D *(uint32_t *)dst_addr;
+ hpi->src.a= ddr.in4.sin_port =3D src_port;
+ hpi->dst.addr.in4.sin_port =3D dst_port;
+ msg(M_DEBUG, "PROXY= protocol %s: SRC %s:%u",

+ ver =3D=3D HAPROXY_PROTOCOL_VERSION_2 ? "v2&quo= t; : "v1",
= + inet_ntoa(hpi->src.addr.in4.sin_addr),
+ ntohs(hpi->src.addr.in4.s= in_port));
+ m= sg(M_DEBUG, "PROXY protocol %s: DST %s:%u",
+ ver =3D=3D HAPROXY_PROTOCOL_V= ERSION_2 ? "v2" : "v1",
+ inet_ntoa(hpi->dst.addr.in4.sin_addr= ),
+ ntohs= (hpi->dst.addr.in4.sin_port));
+ }
+= else if (fam =3D=3D AF_INET6)
+ {
+= hpi->src.addr.sa.sa_family =3D AF_INET6;
+ hpi->dst.addr.sa.sa_family =3D= AF_INET6;
+ m= emcpy(&hpi->src.addr.in6.sin6_addr, src_addr, sizeof(struct in6_addr= ));
+ memcpy(&= amp;hpi->dst.addr.in6.sin6_addr, dst_addr, sizeof(struct in6_addr));

+ hpi->src.add= r.in6.sin6_port =3D src_port;
+ hpi->dst.addr.in6.sin6_port =3D dst_port;
+
+ char ip6_str[INET6_ADDRSTRLEN];
<= span style=3D"color: hsl(120, 100%, 40%);">+ if (inet_ntop(AF_INET6,= &hpi->src.addr.in6.sin6_addr, ip6_str,
+ INET6_ADDRSTRLEN))<= br>+ {
+ msg(M_DEBUG, "PROXY = protocol %s: SRC %s:%u",
+ ver =3D=3D HAPROXY_PROTOCOL_VERSION_2 ? "v2&= quot; : "v1",
+ ip6_str,
+ ntohs(hpi->src.addr.in6.sin6_port));
= + }
+ else
+ {
+ msg(M_NONFATAL, "PROXY protocol %s: coul= d not parse source address",
+ ver =3D=3D HAPROXY_PROTOCOL_VERSION_2 ? "= ;v2" : "v1");
+ }
+ = if (inet_ntop(AF_INET6, &hpi->dst.addr.in6.sin6_addr, ip6_str= ,
+ = INET6_ADDRSTRLEN))
+ = msg(M_DEBUG, "PROXY protocol %s: DST %s:%u",
+ ver =3D=3D HAPROXY_= PROTOCOL_VERSION_2 ? "v2" : "v1",
+ ip6_str,
+ ntohs(hpi->dst.ad= dr.in6.sin6_port));
+= }
+ el= se
+ {<= br>+ msg(M_NONFATAL,= "PROXY protocol %s: could not parse destination address",=
+ ver =3D=3D= HAPROXY_PROTOCOL_VERSION_2 ? "v2" : "v1");
<= span style=3D"color: hsl(120, 100%, 40%);">+ }
+ }
+ else
+ {
+ = msg(M_NONFATAL, "PROXY protocol %s: unsupported address family"= ;,
+ ver = =3D=3D HAPROXY_PROTOCOL_VERSION_2 ? "v2" : "v1");

+ }
+}
+
+/= *
+ * Split the v1 he= ader line into words (space-separated)
+ *
+ * @param words - The array to store the words.
+ * @param line - The header line to split.

+ * @param len - The le= ngth of the header line.
+ *
+ * @return -= The number of words found or -1 if an error occurred.
+ */
+int
+ = const char *line, int len)
+{
+ int word_num =3D 0;
+ const char *start =3D line;
+ for (int i =3D 0; i < len; ++i)
<= span style=3D"color: hsl(120, 100%, 40%);">+ {
+ if (line[i] =3D=3D ' ')
+ {
+ if (word_num < HAPRO= XY_PROTOCOL_V1_MAX_WORDS)

+ {
+= int word_len =3D (int)(line + i - start);
+ if (word_len < HAP= ROXY_PROTOCOL_V1_MAX_WORD_LEN)
+ {
+ memcpy(words[word_num], start, word_len);
+ word= s[word_num][word_len] =3D '\0';
+ ++word_num;
+ }
+ else
+ {
+ msg(M_NONFATAL, "= ;PROXY protocol v1: word too long");
+ return -1;
+ }
+ }
+ start =3D line + i + 1;
<= span style=3D"color: hsl(120, 100%, 40%);">+ }

+ }
+ return word_num;
+}
+/*
+ * Parse the PROXY protocol v1 he= ader line.
+ *=
+ * @param hpi - The haprox= y protocol info structure to store the parsed data.
+ * @param line - The header line to parse= .
+ * @param len - Th= e length of the header line.
+ *
+ * @retu= rn - true if the header was successfully parsed.
+ */
+bool
+ha= proxy_protocol_parse_v1(struct haproxy_protocol_info *hpi,
+ const char= *line, int len)
+{
+ const haproxy_pro= tocol_version_t version =3D HAPROXY_PROTOCOL_VERSION_1;
+ char words[HAPROXY_PROTOCOL_V1_MAX_= WORDS][HAPROXY_PROTOCOL_V1_MAX_WORD_LEN];
+
+ if (!end || end[1] !=3D '\n&#= 39;) /* partial or invalid header */
+ {
+ }
+ *end = =3D ' '; /* replace CRLF with space for easier splitting */<= br>+ int size =3D (int)(end = - line + 1);
+=
+ int word_num =3D hapro= xy_protocol_v1_split_words(words, line, size);
+ if (word_num < 3)
+ {
+ msg(M_NONFATAL, "PROXY protocol v1: invali= d header");
+ = return false;
+ = }
+
+ if (strcmp(words[1], "TCP= 4") =3D=3D 0)

+ = {
+ msg(M_D= EBUG, "PROXY protocol v1: TCP4 protocol");
+ struct in_addr ip4_src_addr, ip4_= dst_addr;
+ if= (inet_pton(AF_INET, words[2], &ip4_src_addr) !=3D 1)
+ {
+ msg(M_NONFATAL,
+ "PROXY protocol v1= : could not parse source address");
+ return false;
+ }
+ if (inet_pton(AF_INET, words[3], &ip4_dst_addr= ) !=3D 1)
+ {<= /span>
+ msg(M_NO= NFATAL,
+ = "PROXY protocol v1: could not parse destination address");
+ return fal= se;
+ }=
+ haproxy_protocol_s= et_addr(hpi, version, AF_INET, SOCK_STREAM,
+ &ip4_src_addr= , &ip4_dst_addr,
= + ntohs(haproxy_protocol_parse_port(words[= 4])),
+ = ntohs(haproxy_protocol_parse_port(words[5])));+ return true;<= br>+ }
+ else if (strcmp(words[1], "TCP6&= quot;) =3D=3D 0)
+ = {
+ msg(M_DEB= UG, "PROXY protocol v1: TCP6 protocol");
+ struct in6_addr ip6_src_addr, ip6_ds= t_addr;
+ if (= inet_pton(AF_INET6, words[2], &ip6_src_addr) !=3D 1)
+ {
+ msg(M_NONFATAL,
+ "PROXY protocol v1:= could not parse source address");
+ return false;
+ }
+ if (inet_pton(AF_INET6, words[3], &ip6_dst_addr= ) !=3D 1)
+ {<= /span>
+ msg(M_NO= NFATAL,
+ = "PROXY protocol v1: could not parse destination address");
+ return fal= se;
+ }=
+ haproxy_protocol_s= et_addr(hpi, version, AF_INET6, SOCK_STREAM,
+ &ip6_src_add= r, &ip6_dst_addr,
+ ntohs(haproxy_protocol_parse_port(words= [4])),
+ = ntohs(haproxy_protocol_parse_port(words[5])));<= br>+ return true;=
+ }
+ else
+ {
+ msg(M_NONFATAL, "PROXY protocol v1: unsupported prot= ocol");
+ = return false;
+ }=
+}
+
+/*
+ * Parse the PROXY protocol v2 header
+ *
+ *
<= span style=3D"color: hsl(120, 100%, 40%);">+ * @return - The number of byte= s parsed.

+ */=
+int
+haproxy_protocol_parse_v2(struct haproxy_= protocol_info *hpi)
+= {
+ size_t addr_le= n =3D 0;
+ int pos= =3D HAPROXY_PROTOCOL_V2_SIG_LEN + sizeof(header->v2.ver_cmd);+ const haproxy_protocol_ve= rsion_t version =3D HAPROXY_PROTOCOL_VERSION_2;
+
+ switch (header->v2.ver_cmd & HAPROXY_PROTOCOL_V2_CMD_MA= SK)
+ {
= + case HAPROXY_PROTOCOL_= V2_LOCAL_CMD:
+ = /*
+ = * this command is sent by the proxy for health-checks and similar<= br>+ * and it doesn= 't make much sense in the openvpn context, so we
+ * ignore the whole header
+ */=
+ msg(M_DEBUG, &= quot;PROXY protocol v2: LOCAL command");
+ parsing_state =3D HAPROXY_PROTOCOL_PA= RSING_STATE_IGNORE;
+= break;
+<= /span>
+ case HAPROXY= _PROTOCOL_V2_PROXY_CMD:
+ msg(M_DEBUG, "PROXY protocol v2: PROXY command");=
+ break;<= /span>
+
+ default:
+ msg(M_DEBUG, "PROXY prot= ocol v2: unknown command");
+ parsing_state =3D HAPROXY_PROTOCOL_PARSING_STATE_I= NVALID;
+ }=
+
+ if (parsing_state !=3D HAPROXY_PROTOCOL_P= ARSING_STATE_OK)
+ = {
+ return po= s;
+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+ pos +=3D sizeof(header->= v2.fam);

+
= + switch (header->v2.fam)=
+ {
+ case HAPROXY_PROTOCOL_V2_= AF_INET | HAPROXY_PROTOCOL_V2_TP_STREAM:

+ msg(M_DEBUG, "PROXY protocol v2: TCP = over IPv4.");
+ = haproxy_protocol_set_addr(hpi, version, AF_INET, SOCK_STREAM,
+ = &header->v2.addr.ip4.src_addr,
+ &am= p;header->v2.addr.ip4.dst_addr,
+ header->v2.addr.ip4= .src_port,
+ = header->v2.addr.ip4.dst_port);
+ addr_len =3D HAPROXY= _PROTOCOL_V2_ADDR_LEN_IPV4;

+ break;
+
+ case= HAPROXY_PROTOCOL_V2_AF_INET | HAPROXY_PROTOCOL_V2_TP_DGRAM:
+ msg(M_DEBUG, "PRO= XY protocol v2: UDP over IPv4");

+ haproxy_protocol_set_addr(hpi, version, AF_IN= ET, SOCK_DGRAM,
+ = &header->v2.addr.ip4.src_addr,

+ = &header->v2.addr.ip4.dst_addr,
+ hea= der->v2.addr.ip4.src_port,
+ header->v2.addr.ip4.dst_= port);
+ a= ddr_len =3D HAPROXY_PROTOCOL_V2_ADDR_LEN_IPV4;
+ break;
+
+ case HAPROXY_PROTOCOL_V2_AF_INET6 | HAPROXY_PROTOCOL_V2_TP_S= TREAM:
+ m= sg(M_DEBUG, "PROXY protocol v2: TCP over IPv6");
+ haproxy_protocol_set_add= r(hpi, version, AF_INET6, SOCK_STREAM,
+ header->v2.addr= .ip6.src_addr,
+ = header->v2.addr.ip6.dst_addr,+ = header->v2.addr.ip6.src_port,
+ header->v2.a= ddr.ip6.dst_port);
+ = addr_len =3D HAPROXY_PROTOCOL_V2_ADDR_LEN_IPV6;
+ break;
+
+ case HAPROXY_PROTOCOL_V2_AF_INET6 | HAPROXY_PROT= OCOL_V2_TP_DGRAM:
+ = msg(M_DEBUG, "PROXY protocol v2: UDP over IPv6");
+ haproxy_proto= col_set_addr(hpi, version, AF_INET6, SOCK_DGRAM,
+ header-&= gt;v2.addr.ip6.src_addr,
+ header->v2.addr.ip6.dst_addr,=
+ = header->v2.addr.ip6.src_port,
+ hea= der->v2.addr.ip6.dst_port);
+ addr_len =3D HAPROXY_PROTOCOL_V2_ADDR_LEN_IPV6;
+ break;
+
+ case HAPROXY_PROTOCOL_V2_AF_UNIX | H= APROXY_PROTOCOL_V2_TP_STREAM:
+ msg(M_DEBUG, "PROXY protocol v2: AF_UNIX stream&= quot;);
+ = haproxy_protocol_set_addr(hpi, version, AF_UNIX, SOCK_STREAM,
+ = header->v2.addr.unx.src_addr,

+ header->v2.addr.= unx.dst_addr, 0, 0);
= + addr_len =3D HAPROXY_PROTOCOL_V2_ADDR_LEN_UNIX;
+ break;

+

+ case HAPROXY_PROTOCOL_V2_AF_UNIX | HAPROXY_PRO= TOCOL_V2_TP_DGRAM:
+ = msg(M_DEBUG, "PROXY protocol v2: AF_UNIX datagram");
+ haproxy_p= rotocol_set_addr(hpi, version, AF_UNIX, SOCK_DGRAM,
+ hea= der->v2.addr.unx.src_addr,
+ header->v2.addr.unx.dst_= addr, 0, 0);
+ = addr_len =3D HAPROXY_PROTOCOL_V2_ADDR_LEN_UNIX;
+ break;
+
+ default:
+ if ((header->v2.fam & HAPROXY_PROTOCOL_V2_AF= _MASK) =3D=3D HAPROXY_PROTOCOL_V2_AF_UNSPEC
+ || (header->v2.fam & HAPROXY= _PROTOCOL_V2_TP_MASK) =3D=3D HAPROXY_PROTOCOL_V2_TP_UNSPEC)
+ {

+ msg(M_DEBUG, "PROXY = protocol v2: UNSPEC address family or transport protocol");
= + parsing_state = =3D HAPROXY_PROTOCOL_PARSING_STATE_IGNORE;
+ break;
+ }
+ msg(M_DEBUG, "PROXY protocol v2: unkno= wn address family (0x%02x) "
+ "or transport protocol (0x%02x)",
+ heade= r->v2.fam & HAPROXY_PROTOCOL_V2_AF_MASK,
+ header->v2.fam & HAPROXY= _PROTOCOL_V2_TP_MASK);
+ break;
+ }
+ return (int)(pos + sizeof(header-&= gt;v2.len) + addr_len);
+}
+
+bool

+haproxy_protocol_parse(struct haproxy_protocol_i= nfo *hpi, const uint8_t *buf, const int buf_len)
+{
+ const haproxy_protocol_version_t version =3D haproxy_protoco= l_version(buf, buf_len);
+ header_len =3D buf_len;
+ header =3D (haproxy_protocol_header_t *)buf;
+

+ if (!hpi)
+ {
+ return false;
+ }
++ switch (version)+ {
+ case HAPROXY_PROTOCOL_VERSION_2:<= /span>
+ {
= + if ((header->v2= .ver_cmd & HAPROXY_PROTOCOL_V2_VER_MASK) =3D=3D HAPROXY_PROTOCOL_V2_VER= )
+ {
+ haproxy_= protocol_parse_v2(hpi);
+ if (parsing_state !=3D HAPROXY_PROTOCOL_PARSING_STATE_O= K)
+ {=
+ = msg(M_DEBUG, "PROXY protocol v2: %s header",
+ parsing_state = =3D=3D HAPROXY_PROTOCOL_PARSING_STATE_IGNORE ? "ignoring" : "= ;invalid");
+ = return false;
+ }
+ return true;
+ }
+ else
+ {
+ = (header->v2.ver_cmd & HAPROXY_PROTOCOL_V2_VER_MAS= K) >> 4);
+ = return false;
+ }
+= }
++ case HAPROXY_PROTOCOL= _VERSION_1:
+ = msg(M_DEBUG, "PROXY protocol header v1: %.*s", header_len - 2= , header->v1.line);
++ default:
<= span style=3D"color: hsl(120, 100%, 40%);">+ return false;
+ }
+}
+
+v= oid
+haproxy_protocol= _reset(struct haproxy_protocol_info *hpi)
+{
+ header =3D NULL;
+ header_len =3D 0;
+ parsing_state =3D HAPROXY_PROTOCOL_PARSING_STATE_OK;
<= span style=3D"color: hsl(120, 100%, 40%);">+}

diff --git a/= src/openvpn/haproxy_protocol.h b/src/openvpn/haproxy_protocol.h
<= span>new file mode 100644

index 0000000..70b5aa4
= --- /dev/null
+++ b/src/openvpn/haproxy_protocol.h
@@ -0,0 +1,184 @@
+/*
+ * O= penVPN -- An application to securely tunnel IP networks
+ * over a single TCP/UDP po= rt, with support for SSL/TLS-based
+ * session authentication and key exchange,
+ * packet en= cryption, packet authentication, and
+ * packet compression.
+ *
+ * Copyright (C) 2024 OpenVPN Inc <sales@...515...>=
+ *
+ * This program is free software; y= ou can redistribute it and/or modify
+ * it under the terms of the GNU General Public License v= ersion 2
+ * as publ= ished by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRA= NTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPO= SE. See the
+ * GNU= General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License = along
+ * with this = program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Flo= or, Boston, MA 02110-1301 USA.

+ */
+
+#ifndef HAPROXY_PROTOCOL= _H
+#define HAPROXY_P= ROTOCOL_H
++#include "buffer.h"=
+#include "open= vpn.h"
+#include= "socket.h"
+
+#include <stdb= ool.h>
+#include &= lt;stdint.h>
+

+/* https://www.haproxy.= org/download/1.8/doc/proxy-protocol.txt */
+
+#define HAPROXY_PROTOCOL_V1_SIG "PROXY"
+#define HAPROXY_PROTOCOL_V1_SIG_= LEN 5
+#define H= APROXY_PROTOCOL_V1_MIN_HDR_LEN 8
+#define HAPROXY_PROTOCOL_V1_LINE_MAX_LEN 108
+
+#define HAPROXY_PROTOCOL_V2_SIG "\x0D\x0A\= x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A"
+#define HAPROXY_PROTOCOL_V2_SIG_LEN 12=
+#define HAPROXY_PROTOCOL_V= 2_MIN_HDR_LEN 16
+

+#define HAPROXY_PROTOC= OL_V2_VER_MASK 0xF0
+= #define HAPROXY_PROTOCOL_V2_VER (0x2 << 4)
+
+#define HAPROXY_PROTOCOL_V2_CMD_MASK 0x0F
+#define HAPROXY_PROTOCOL_V2_LOCAL_CMD= (0x0 << 0)
+#d= efine HAPROXY_PROTOCOL_V2_PROXY_CMD (0x1 << 0)
+
+#define HAPROXY_PROTOCOL_V2_AF_MASK 0xF0
+#define HAPROXY_PROTOCOL_V2_AF_UNSPEC= (0x0 << 4)
+#d= efine HAPROXY_PROTOCOL_V2_AF_INET (0x1 << 4)
+#define HAPROXY_PROTOCOL_V2_AF_INET6 (0x= 2 << 4)
+#defin= e HAPROXY_PROTOCOL_V2_AF_UNIX (0x3 << 4)
+
+#define HAPROXY_PROTOCOL_V2_TP_MASK 0x0F
+#define HAPROXY_PROTOCOL_V2_TP_UNSPEC (0x= 0 << 0)
+#defin= e HAPROXY_PROTOCOL_V2_TP_STREAM (0x1 << 0)
+#define HAPROXY_PROTOCOL_V2_TP_DGRAM (0x2 <= ;< 0)
+
= +#define HAPROXY_PROTOCOL_TLV_T= YPE_ALPN 0x01
+#define HAPROXY_PROTOCOL_TLV_TYPE_AUTHORITY 0x02
+#define HAPROXY_PROTOCOL_TLV_TYPE_C= RC32C 0x03
+= #define HAPROXY_PROTOCOL_TLV_TYPE_NOOP 0x04
+#define HAPROXY_PROTOCOL_TLV_TYPE_UNIQUE= _ID 0x05
+#defi= ne HAPROXY_PROTOCOL_TLV_TYPE_SSL 0x20
+#define HAPROXY_PROTOCOL_TLV_SUBTYPE_SSL_VERS= ION 0x21
+#define HA= PROXY_PROTOCOL_TLV_SUBTYPE_SSL_CN 0x22
+#define HAPROXY_PROTOCOL_TLV_SUBTYPE_SSL_CIPHER = 0x23
+#define HAPROXY= _PROTOCOL_TLV_SUBTYPE_SSL_SIG_ALG 0x24
+#define HAPROXY_PROTOCOL_TLV_SUBTYPE_SSL_KEY_ALG 0x25<= /span>
+#define HAPROXY_PROT= OCOL_TLV_TYPE_NETNS 0x30
+
+#def= ine HAPROXY_PROTOCOL_V2_CLIENT_SSL 0x01
+#define HAPROXY_PROTOCOL_V2_CLIENT_CERT_CONN = 0x02
+#define HAPRO= XY_PROTOCOL_V2_CLIENT_CERT_SESS 0x04
+
= +#define HAPROXY_PROTOCOL_V2_TLV_UNIQUE_ID_MAX_LEN 128
+
+typedef enum
+{
+ HAPRO= XY_PROTOCOL_VERSION_INVALID =3D -1,
+
+ = HAPROXY_PROTOCOL_VERSION_UNKNOWN =3D 0,
+ HAPROXY_PROTOCOL_VERSION_1,
+ HAPROXY_PROTOCOL_VERSION_2,+} haproxy_protocol_version_t;=
+
+#pragma pack(push, 1)
+typedef union
+{
+ struct
+ {
+ = char line[HAPROXY_PROTOCOL_V1_LINE_MAX_LEN];
+ } v1;
+ struct
+ {
+ = uint8_t sig[HAPROXY_PROTOCOL_V2_SIG_LEN];
+ uint8_t ver_cmd;
+ uint8_t fam;
+ uint16_t len;
+ union
+ {
+ struct
+ {
+ uint32_t src_addr;
+ uint32_t dst_addr;
+ uint16_t src_port;
+ uint16= _t dst_port;
+ = } ip4;
+ = struct
+ = {
+ = uint8_t src_addr[16];
+ uint16_t src_port;
+ uint16_t dst_port;
+ } ip6;

+ struct=
+ {
+ uint8_t src_addr= [108];
+ = uint8_t dst_addr[108];
+ } unx;
+ } addr;
= + } v2;
+} __attri= bute__((packed)) haproxy_protocol_header_t;
+#pragma pack(pop)
+
+{
+ ha= proxy_protocol_version_t version;
+ int sock_type;
+ struct openvpn_sockaddr src;
+ struct openvpn_sockaddr dst;
+};
+
+ * Check if th= e buffer contains an PROXY protocol header and return the version.+ *
+ * @param buf - The buffer to check.+ * @param buf_len - The leng= th of the buffer.
+ *=
+ * @return - The PR= OXY protocol version or HAPROXY_PROTOCOL_VERSION_INVALID if
+ * the buffer does not con= tain a valid PROXY protocol header.
+ */
+= haproxy_protocol_version_t
+haproxy_protocol_version(const uint8_t *buf, int buf_len);+
+/*
+ * Get the length of the PROXY protocol header.
+ * This function does not check if th= e header is fully contained in the buffer
+ * therefore it is the caller's responsibility to= perform this check.
= + *
+ * @param buf - = The buffer to check.
= + * @param buf_len - The length of the buffer.
+ * @param version - The PROXY protocol version.<= /span>
+ *
+ * @return - The length of the header= or -1 if the header is partial or invalid.
+ */
+int
+haproxy_= protocol_header_len(const uint8_t *buf, int len, haproxy_protocol_version_t= version);
++/*
+ * Parse the PROXY protocol header.+ *
+ * @param hpi - The proxy protocol info struct= ure to store the parsed data.
+ * @param buf - The header to parse.
+ * @param buf_len - The length of the header.

+ *
+ * @return - true if the header was suc= cessfully parsed.
+ *= /
+bool
+haproxy_protocol_parse(struct hap= roxy_protocol_info *hpi, const uint8_t *buf, int buf_len);
+
+/*
+ *
+ * @= param hpi - The proxy protocol info structure.
+ */
+void
+hapr= oxy_protocol_reset(struct haproxy_protocol_info *hpi);
+
+#endif /* HAPROXY_PROTOCOL_H */
diff --git = a/src/openvpn/init.c b/src/openvpn/init.c
index a49e563..ec= d3253 100644
--- a/src/openvpn/init.c
+++ b= /src/openvpn/init.c
@@ -54,6 +54,7 @@
#inc= lude "mss.h"
#include "mudp.h"<= br> #include "dco.h"
+#include "haproxy_protocol.h"
<= /span>
#include "memdbg.h"

= @@ -3926,6 +3927,14 @@

c-&= gt;c1.link_socket_addr.bind_local =3D NULL;
}+
+ /* Clear and free the memory allocated for = the HAProxy protocol */
+ if (c->c1.haproxy_protocol)
+ {
+ haproxy_protocol_reset(c->c1.haproxy_protocol);
<= span style=3D"color: hsl(120, 100%, 40%);">+ free(c->c1.haproxy_p= rotocol);

+ c-= >c1.haproxy_protocol =3D NULL;
+ }
}

/= *
@@ -4774,7 +4783,7 @@
/* free up= environmental variable store */
do_env_set_destro= y(c);

- /* close HTTP or SOCKS proxy */
+ /* close HTTP or SOCKS proxy or PROXY protoco= l object */
uninit_proxy(c);

/* garbage collect */
diff --git a/sr= c/openvpn/multi.c b/src/openvpn/multi.c
index 03177bb..e644= 1cd 100644
--- a/src/openvpn/multi.c
+++ b/= src/openvpn/multi.c
@@ -51,6 +51,7 @@
#inc= lude "ssl_util.h"
#include "dco.h"
#include "reflect_filter.h"
+#include "haproxy_protocol.h"

/*#define MULTI_DEBUG_EVENT_LOOP*/=

@@ -779,6 +780,8 @@
go= to err;
}

+ ALLOC_OBJ_CLEAR(mi->context.c1.haproxy_= protocol, struct haproxy_protocol_info);
+
mi->context.c2.tls_multi->= multi_state =3D CAS_NOT_CONNECTED;

i= f (hash_n_elements(m->hash) >=3D m->max_clients)
@= @ -789,6 +792,9 @@

if (!real) /* TCP= mode? */
{
+ /* allow checking for HAProxy protocol header at the b= eginning of the connection */
+ mi->context.c2.link_socket->haproxy_protocol_allowe= d =3D true;
+<= br> if (!multi_tcp_instance_specific_init(m, mi))
<= span> {

goto err;
@@ -= 3207,6 +3213,57 @@
gc_free(&gc);
= }

+= void
+multi_replace_p= roxy_addr(struct multi_context *m, struct multi_instance *mi)
+{

+ struct mroute_addr real =3D {0};
+ struct hash *hash =3D m->ha= sh;
+ struct gc_ar= ena gc =3D gc_new();
= +
+ if (!mroute_ex= tract_openvpn_sockaddr(&real, &mi->context.c1.haproxy_protocol-&= gt;src, true))
+ {=
+ goto done;<= /span>
+ }
+

+ const uint32_t hv =3D hash_value(hash, &real)= ;
+ struct hash_bu= cket *bucket =3D hash_bucket(hash, hv);
+
= + /* if the new address is already in the hash table, the new client tak= es precedence */
+ = struct hash_element *he =3D hash_lookup_fast(hash, bucket, &real, hv);=
+ if (he)<= br>+ {
+ struct multi_instance *oldmi =3D = (struct multi_instance *) he->value;
+ msg(D_MULTI_LOW, "Client's real addres= s (behind proxy) matches "
+ "existing client address -- new client takes p= recedence");
+ = oldmi->did_real_hash =3D false;
+ multi_close_instance(m, oldmi, false);<= br>+ he->key =3D &= ;mi->real;
+ = he->value =3D mi;
+
= + clear_prefix();
= + msg(D_MULTI_LOW, "Rep= lacing proxy address: %s with client's real address: %s",+ mroute_addr_print(&a= mp;mi->real, &gc),
+ mroute_addr_print(&real, &gc));
+
+ /* remove proxy address from hash table before changing = address */
+ ASSER= T(hash_remove(m->hash, &mi->real));
+ ASSERT(hash_remove(m->iter, &mi->re= al));
+
+ mi->real =3D real;<= br>+ generate_prefix(mi);
+
+ ASSERT(hash_add(m->hash, &mi-&= gt;real, mi, false));
+ ASSERT(hash_add(m->iter, &mi->real, mi, false));
= +
+#ifdef ENABLE_MANAGEMENT
+ ASSERT(hash_add(m->cid_hash, &mi-= >context.c2.mda_context.cid, mi, true));
+#endif
+
+done:
+ gc_free(&gc);<= /span>
+}
+
/*
= * Called when an instance should be closed due to the
* = reception of a soft signal.
@@ -3348,6 +3405,7 @@ struct mroute_addr src, dest;
unsigned int= mroute_flags;
struct multi_instance *mi;
+ struct link_socket_info *lsi= ;

bool ret =3D true;
bool floated= =3D false;

@@ -3387,15 +3445,29 @@
}
}

+ lsi =3D get_link= _socket_info(c);
+
+ /* check for P= ROXY protocol */
+ = if (lsi->proto =3D=3D PROTO_TCP_SERVER && c->c2.link_soc= ket->haproxy_protocol_allowed && BLEN(&c->c2.buf) > 0)=
+ {+ if (haproxy_proto= col_parse(c->c1.haproxy_protocol, BPTR(&c->c2.buf), BLEN(&c-&= gt;c2.buf)))
+ = {
+ = /* replace proxy address with real address */
+ multi_replace_proxy_addr(m, m-= >pending);
+ = /* avoid processing the packet as an openvpn one */
+ buf_reset_len(&am= p;c->c2.buf);

+ = }
+ = /* stop checking for HAProxy protocol headers */
+ c->c2.link_socket->hapr= oxy_protocol_allowed =3D false;
+ }
+
if (BLEN(&c->c2.buf) > 0)
<= span> {
- = struct link_socket_info *lsi;
const uint= 8_t *orig_buf;

/* decrypt in= instance context */

perf_pu= sh(PERF_PROC_IN_LINK);
= - lsi =3D get_link_socket_info(c);
= orig_buf =3D c->c2.buf.data;
if (process_in= coming_link_part1(c, lsi, floated))
{diff --git a/src/openvpn/openvpn.h b/src/openvpn/openvpn.hindex e98c93e..a192f7a 100644
--- a/src/openvpn/ope= nvpn.h
+++ b/src/openvpn/openvpn.h
@@ -45,6= +45,7 @@
#include "pool.h"
#in= clude "plugin.h"
#include "manage.h"
+#include "haprox= y_protocol.h"

/*
= * Our global key schedules, packaged thusly
@@ -191,6 +192,= 8 @@
struct socks_proxy_info *socks_proxy;
<= span> bool socks_proxy_owned;


+ struct haproxy_protocol_info *haproxy= _protocol;
+ /* persist --ifconfig-pool db to file */
= struct ifconfig_pool_persist *ifconfig_pool_persist;
b= ool ifconfig_pool_persist_owned;
diff --git a/src/openvpn/s= ocket.c b/src/openvpn/socket.c
index 17c5e76..90914dd 10064= 4
--- a/src/openvpn/socket.c
+++ b/src/open= vpn/socket.c
@@ -39,6 +39,7 @@
#include &q= uot;manage.h"
#include "openvpn.h" #include "forward.h"
+#include "haproxy_protocol.h"

#include "memdbg.h"
<= br>@@ -1617,7 +1618,7 @@
stream_buf_close(struct str= eam_buf *sb);

static bool
-stream_buf_added(struct stream_buf *= sb, int length_added);
- sock->stream_buf.residual_fully_= formed =3D stream_buf_added(&sock->stream_buf, 0);
+ sock->stream_buf.residual_= fully_formed =3D stream_buf_added(&sock->stream_buf, 0, false);

dmsg(D_STREAM_DEBUG, "STREAM: RESIDUAL FULLY FORM= ED [%s], len=3D%d",
sock->stream_buf.= residual_fully_formed ? "YES" : "NO",
= sock->stream_buf.residual.len);
@@ -2644,7 = +2645,8 @@

static bool
= stream_buf_added(struct stream_buf *sb,
- int length_added)
+ int length_added,=
+ const boo= l haproxy_protocol_allowed)
{
dmsg(D_= STREAM_DEBUG, "STREAM: ADD length_added=3D%d", length_added);
if (length_added > 0)
@@ -2661,7 +2663= ,9 @@
#if PORT_SHARE
if (sb->p= ort_share_state =3D=3D PS_ENABLED)
{
- if (!is_openvpn_protoco= l(&sb->buf))

+= if (!is_openvpn_protocol(&sb->buf) &&
= + (!haproxy_prot= ocol_allowed ||
+ = haproxy_protocol_version(BPTR(&sb->buf), BLEN(&sb-&g= t;buf)) =3D=3D HAPROXY_PROTOCOL_VERSION_INVALID))
= {
msg(D_STREAM_ERRORS, "Non-OpenV= PN client protocol detected");
sb->= ;port_share_state =3D PS_FOREIGN;
@@ -2680,10 +2684,26 @@

if (sb->len < 1 || sb->l= en > sb->maxlen)
{
- msg(M_WARN, "WARNING: Bad enca= psulated packet length from peer (%d), which must be > 0 and <=3D %d = -- please ensure that --tun-mtu or --link-mtu is equal on both peers -- thi= s condition could also indicate a possible active attack on the TCP link --= [Attempting restart...]", sb->len, sb->maxlen);
- stream_buf_reset(sb);
- sb->error = =3D true;
- = return false;
+ = haproxy_protocol_version_t hp_version =3D HAPROXY_PROTOCOL_VERSION_UN= KNOWN;
+ i= nt hp_len =3D -1;
+ = /* if it's allowed (first packet), check for a HAProxy protoc= ol header */
+ = if (haproxy_protocol_allowed)
+ {
+ /* undo the reading of net_size */
+ ASSERT(buf_prepend(= &sb->buf, sizeof(net_size)));

+ hp_version =3D haproxy_protocol_version(BP= TR(&sb->buf), BLEN(&sb->buf));
+ hp_len =3D haproxy_protocol_header= _len(BPTR(&sb->buf), BLEN(&sb->buf), hp_version);
<= span style=3D"color: hsl(120, 100%, 40%);">+ }
+
+ /* we do not allow partial headers */=
+ if (hp_version= <=3D 0 || hp_len < 0 || hp_len > sb->buf.len)
+ {
+ msg(M_WARN, "WARNING= : Bad encapsulated packet length from peer (%d), which must be > 0 and &= lt;=3D %d -- please ensure that --tun-mtu or --link-mtu is equal on both pe= ers -- this condition could also indicate a possible active attack on the T= CP link -- [Attempting restart...]", sb->len, sb->maxlen);

+ stream_bu= f_reset(sb);
+ = sb->error =3D true;
+ return false;
+ }
+ sb->len =3D hp_len;
= }
}

@@ -3267,7 +328= 7,7 @@
}

if (so= ck->stream_buf.residual_fully_formed
- || stream_buf_added(&sock->stream_buf, len= )) /* packet complete? */
+ || stream_buf_added(&sock->stream_buf, len, sock->h= aproxy_protocol_allowed)) /* packet complete? */
{
stream_buf_get_final(&sock->stream_buf, buf);<= /span>
stream_buf_reset(&sock->stream_buf);=
diff --git a/src/openvpn/socket.h b/src/openvpn/socket.hindex 47083ad..1132b35 100644
--- a/src/openvpn/soc= ket.h
+++ b/src/openvpn/socket.h
@@ -227,6 = +227,9 @@
const char *proxy_dest_host;
const char *proxy_dest_port;


+ /* check HAProxy PROXY protocol only = if true */
+ bool = haproxy_protocol_allowed;
+
/* Pointer to the server-poll to trigger the ti= meout in function which have
* their own loop instead= of using the main oop */
struct event_timeout *server= _poll_timeout;
diff --git a/tests/unit_tests/openvpn/Makefi= le.am b/tests/unit_tests/openvpn/Makefile.am
index a4e6235.= .1802132 100644
--- a/tests/unit_tests/openvpn/Makefile.am<= /span>
+++ b/tests/unit_tests/openvpn/Makefile.am
= @@ -10,8 +10,9 @@
test_binaries +=3D argv_testdriver buffe= r_testdriver
endif

-test_binaries +=3D crypto_testdriver packet= _id_testdriver auth_token_testdriver ncp_testdriver misc_testdriver \
- pkt_testdriver ssl_testdri= ver user_pass_testdriver
+test_binaries +=3D crypto_testdriver packet_id_testdriver auth_token_t= estdriver \
+ hap= roxy_protocol_testdriver ncp_testdriver misc_testdriver \
+ pkt_testdriver ssl_testdriver us= er_pass_testdriver

if HAVE_LD_WRAP_SUPPO= RT
if !WIN32
@@ -73,6 +74,16 @@
= $(top_srcdir)/src/openvpn/win32-util.c \
$(top_sr= cdir)/src/openvpn/mss.c

+haproxy_protocol_testdriver_CFLAGS =3D \
<= span style=3D"color: hsl(120, 100%, 40%);">+ -I$(top_srcdir)/include -I$(to= p_srcdir)/src/openvpn -I$(top_srcdir)/src/compat \

+ @TEST_CFLAGS@
+haproxy_protocol_testdriver_LDFLAGS =3D @TEST_LDF= LAGS@
+haproxy_protoc= ol_testdriver_SOURCES =3D test_haproxy_protocol.c mock_msg.c mock_msg.h \
+ mock_get_random.c \<= /span>
+ $(top_srcdir)/src/o= penvpn/platform.c \
+= $(top_srcdir)/src/openvpn/buffer.c \
+ $(top_srcdir)/src/openvpn/haproxy_protocol.c
<= span style=3D"color: hsl(120, 100%, 40%);">+

ssl_testdrive= r_CFLAGS =3D \
-I$(top_srcdir)/include -I$(top_srcdir)/s= rc/compat -I$(top_srcdir)/src/openvpn \
@TEST_CFLAGS@
diff --git a/tests/unit_tests/openvpn/test_haproxy_protocol.c = b/tests/unit_tests/openvpn/test_haproxy_protocol.c
new file= mode 100644
index 0000000..0464315
--- /de= v/null
+++ b/tests/unit_tests/openvpn/test_haproxy_protocol= .c
@@ -0,0 +1,354 @@
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#inc= lude "syshead.h"
+
+#include <= ;setjmp.h>
+#inclu= de <cmocka.h>
+=
+#include "plat= form.h"
+#includ= e "buffer.h"
+#include "test_common.h"
+
+extern uint32_t
+haproxy_protocol_crc32c(const uint8_t *data, int len);
+
+/* header building blocks */
+#define HP1_SIG 0x50, 0x5= 2, 0x4f, 0x58, 0x59 /* "PROXY" */

+#define HP1_TCP4_AF_UNKN 0x55, 0x4E, 0x4B, 0x4E, 0= x4F, 0x57, 0x4E /* "UNKNOWN" */
+#define HP1_TCP4_AF_INET 0x54, 0x43, 0x50, 0x34 /* &= quot;TCP4" */
+#= define HP1_TCP6_AF_INET 0x54, 0x43, 0x50, 0x36 /* "TCP6" */
+#define HP1_TCP4_SRC_ADD= R 0x31, 0x30, 0x2e, 0x31, 0x30, 0x2e, 0x32, 0x30, 0x2e, 0x32 /* "10.10= .20.2" */
+#defi= ne HP1_TCP4_DST_ADDR 0x31, 0x30, 0x2e, 0x31, 0x30, 0x2e, 0x32, 0x30, 0x2e, = 0x31 /* "10.10.20.1" */
+#define HP1_TCP4_SRC_PORT 0x34, 0x30, 0x31, 0x35, 0x30 /* 401= 50 */
+#define HP1_TC= P4_DST_PORT 0x31, 0x31, 0x39, 0x35 /* 1195 */
+#define HP1_TCP6_SRC_ADDR 0x32, 0x30, 0x30, 0x31,= 0x3a, 0x64, 0x62, 0x38, 0x3a, 0x32, 0x30, 0x3a, 0x3a, 0x32 /* "2001:d= b8:20::2" */
+#d= efine HP1_TCP6_DST_ADDR 0x32, 0x30, 0x30, 0x31, 0x3a, 0x64, 0x62, 0x38, 0x3= a, 0x32, 0x30, 0x3a, 0x3a, 0x31 /* "2001:db8:20::1" */
= +#define HP1_TCP6_SRC_PORT 0x35= , 0x33, 0x33, 0x34, 0x34 /* 53344 */
+#define HP1_TCP6_DST_PORT 0x31, 0x31, 0x39, 0x35 /* 1195 *= /
+#define HP1_END = 0x0d, 0x0a /* "\r\n" */
+
+#define HP2_V4_SRC_ADDR 0x0a, 0= x0a, 0x14, 0x02 /* 10.10.20.2 */
+#define HP2_V4_DST_ADDR 0x0a, 0x0a, 0x14, 0x01 /* 10.10.20.1 *= /
+#define HP2_V4_SRC= _PORT 0xa5, 0x30 /* 42288 */
+#define HP2_V4_DST_PORT 0x04, 0xab /* 1195 */
+#define HP2_V6_SRC_ADDR 0x20, 0x01, 0x0d= , 0xb8, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0= x02 /* 2001:db8:20::2 */
+#define HP2_V6_DST_ADDR 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x20, 0x00, 0x00= , 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 /* 2001:db8:20::1 */
+#define HP2_V6_SRC_PORT 0= xe8, 0xb6 /* 59574 */
+#define HP2_V6_DST_PORT 0x04, 0xab /* 1195 */
+#define HP2_TLV \
+ 0x01, 0x00, 0x03, 'h', '2', &= #39;3', \
+ 0x= 02, 0x00, 0x0b, 'e', 'x', 'a', 'm', 'p&= #39;, 'l', 'e', '.', 'c', 'o', '= ;m', \
+ 0x04,= 0x00, 0x00, \
+ 0= x05, 0x00, 0x0f, 'u', 'n', 'i', 'q', 'u= ', 'e', '-', 'i', 'd', '-', = 9;v', 'a', 'l', 'u', 'e', \
<= span style=3D"color: hsl(120, 100%, 40%);">+ 0x20, 0x00, 0x33, 0x01, 0x0= 0, 0x00, 0x00, 0x00, \

+ 0x22, 0x00, 0x06, 'c', 'l', 'i', '= ;e', 'n', 't', \
+ 0x23, 0x00, 0x06, 'A', 'E', 'S'= ;, '2', '5', '6', \
+ 0x24, 0x00, 0x06, 'S', 'H', = 9;A', '2', '5', '6', \
+ 0x25, 0x00, 0x07, 'R', 'S= 9;, 'A', '2', '0', '4', '8', \
+ 0x30, 0x00, 0x07, &#= 39;n', 'e', 't', 'n', 's', '-',= '1'
+=
+#define HP_TEST_OPENVPN \<= /span>
+ 0x00, 0x0e, 0x38= , 0xa8, 0xf5, 0x24, 0xa0, 0xa7, \
+ 0x89, 0x00, 0xca, 0x00, 0x00, 0x00, 0x00, 0x00
= +
+/* headers */
+static uint8_t HP1_TEST_UNKNOWN[] =3D {
+ HP1_SIG, 0x20, HP1_TCP4_AF_UNKN= , HP1_END

+};<= br>+
+static uint8_t HP1_TEST_TCP4[] =3D {+ HP1_SIG, 0x20,
= + HP1_TCP4_AF_INET, 0x20,
+ HP1_TCP4_SRC_ADDR,= 0x20,
+ HP1_TCP4_= DST_ADDR, 0x20,
+ = HP1_TCP4_SRC_PORT, 0x20,
+ HP1_TCP4_DST_PORT,
+ HP1_END
+= };
+
+static uint8_t HP1_TEST_TCP4_PLUS_OP= ENVPN[] =3D {
+ HP= 1_SIG, 0x20,
+ HP1= _TCP4_AF_INET, 0x20,
= + HP1_TCP4_SRC_ADDR, 0x20,
+ HP1_TCP4_DST_ADDR, 0x20,
+ HP1_TCP4_SRC_PORT, 0x20,
+ HP1_TCP4_DST_PORT,
+ HP1_END,
+ HP_TEST_OPENVPN
+};
+
+static uint8= _t HP1_TEST_TCP6[] =3D {
+ HP1_SIG, 0x20,
+ HP1_TCP6_AF_INET, 0x20,
+ HP1_TCP6_SRC_ADDR, 0x20,
+ HP1_TCP6_DST_ADDR, 0x20,
+ HP1_TCP6_SRC_PORT, 0x20,
+ HP1_TCP6_DST_PORT,
+ HP1_END

+};
+
+st= atic uint8_t HP1_TEST_TCP6_PLUS_OPENVPN[] =3D {
+ HP1_SIG, 0x20,
+ HP1_TCP6_AF_INET, 0x20,
+ HP1_TCP6_SRC_ADDR, 0x20,
<= span style=3D"color: hsl(120, 100%, 40%);">+ HP1_TCP6_DST_ADDR, 0x20,
+ HP1_TCP6_SRC_PORT,= 0x20,
+ HP1_TCP6_= DST_PORT,
+ HP1_EN= D,
+ HP_TEST_OPENV= PN
+};
+

+static uint8_t HP2_TEST_LOCAL[] =3D {
+ HP2_SIG,

+ HAPROXY_PROTOCOL_V2_VER | HAPROXY_PRO= TOCOL_V2_LOCAL_CMD,
+= HAPROXY_PROTOCOL_V2_AF_UNSPEC | HAPROXY_PROTOCOL_V2_TP_UNSPEC,+ 0x00, 0x00 /* length */<= /span>
+};
+
+static uint8_t HP2_TEST_WRONG_VER[] =3D {
+ HP2_SIG,

+ 0x10 | HAPROXY_PROTOCOL_V2_PROXY_CMD,=
+ HAPROXY_PROTOCO= L_V2_AF_INET | HAPROXY_PROTOCOL_V2_TP_STREAM,
+ 0x00, 0x0c, /* length */
+ HP2_V4_SRC_ADDR,
+ HP2_V4_DST_ADDR,
+ HP2_V4_SRC_PORT,
+ HP2_V4_DST_PORT,
+};
+
+ HP2_SIG,
+ HAPROXY_PROTOCOL_V2_VER | HAPROXY_PROTOCOL_V2_PROXY_= CMD,
+ HAPROXY_PRO= TOCOL_V2_AF_INET | HAPROXY_PROTOCOL_V2_TP_STREAM,
+ 0x00, 0x7c, /* length */
+ HP2_V4_SRC_ADDR,
+ HP2_V4_DST_ADDR,

+ HP2_V4_SRC_PORT,

+ HP2_V4_DST_PORT,

+ HP2_TLV,

+ 0x03, 0x00, 0x04, 0x6e, 0x8a, 0xbf,= 0x7f /* crc32c */
+}= ;
+
+static uint8_t HP2_TEST_PROXY_TCP4_PL= US_OPENVPN[] =3D {
+ = HP2_SIG,
+ HAPR= OXY_PROTOCOL_V2_VER | HAPROXY_PROTOCOL_V2_PROXY_CMD,
+ HAPROXY_PROTOCOL_V2_AF_INET | HAPROXY= _PROTOCOL_V2_TP_STREAM,
+ 0x00, 0x7c, /* length */
+ HP2_V4_SRC_ADDR,
+ HP2_V4_DST_ADDR,
+ HP2_V4_SRC_PORT,
+ HP2_V4_DST_PORT,
+ HP2_TLV,
+ 0x03, 0x00, 0x04, 0x6e, 0x8a, 0xbf, 0x7f, /* crc32c */=
+ HP_TEST_OPENVPN=
+};
+
+static uint8_t HP2_TEST_PROXY_TCP6[] =3D {
+ HP2_SIG,
+ HAPROXY_PROTOCOL_V2_VER | HAPROXY_PROTOC= OL_V2_PROXY_CMD,
+ = HAPROXY_PROTOCOL_V2_AF_INET6 | HAPROXY_PROTOCOL_V2_TP_STREAM,
+ 0x00, 0x94, /* length */
+ HP2_V6_SRC_ADDR,

+ HP2_V6_DST_ADDR,
+ HP2_V6_SRC_PORT,<= /span>
+ HP2_V6_DST_PORT,=
+ HP2_TLV,=
+ 0x03, 0x00, 0x04, 0xed= , 0xb0, 0x61, 0xb6 /* crc32c */
+};
+
+static uint8_t HP2_TEST_= PROXY_TCP6_PLUS_OPENVPN[] =3D {
+ HP2_SIG,
+ HAPROXY_PROTOCOL_V2_VER | HAPROXY_PROTOCOL_V2_PROXY_CMD,+ HAPROXY_PROTOCOL_V2_AF_IN= ET6 | HAPROXY_PROTOCOL_V2_TP_STREAM,
+ 0x00, 0x94, /* length */
+ HP2_V6_SRC_ADDR,
+ HP2_V6_DST_ADDR,
+ HP2_V6_SRC_PORT,
+ HP2_V6_DST_PORT,
+ HP2_TLV,
+ 0x03, 0x00, 0x04, 0xed, 0xb0, 0x61, 0xb6, /* crc= 32c */
+ HP_TEST_O= PENVPN
+};
= +
+static void
+test_haproxy_protocol_version(void **state)
+{

+ assert_int_equal(haproxy_protocol_version(HP1_T= EST_UNKNOWN, sizeof(HP1_TEST_UNKNOWN)), HAPROXY_PROTOCOL_VERSION_1);=
+ assert_int_equal(hapro= xy_protocol_version(HP1_TEST_TCP4, sizeof(HP1_TEST_TCP4)), HAPROXY_PROTOCOL= _VERSION_1);
+ ass= ert_int_equal(haproxy_protocol_version(HP1_TEST_TCP6, sizeof(HP1_TEST_TCP6)= ), HAPROXY_PROTOCOL_VERSION_1);
+ assert_int_equal(haproxy_protocol_version(HP2_TEST_WRONG_VE= R, sizeof(HP2_TEST_WRONG_VER)), HAPROXY_PROTOCOL_VERSION_2);
+ assert_int_equal(haproxy_proto= col_version(HP2_TEST_LOCAL, sizeof(HP2_TEST_LOCAL)), HAPROXY_PROTOCOL_VERSI= ON_2);

+ assert_in= t_equal(haproxy_protocol_version(HP2_TEST_PROXY_TCP4, sizeof(HP2_TEST_PROXY= _TCP4)), HAPROXY_PROTOCOL_VERSION_2);
+ assert_int_equal(haproxy_protocol_version(HP2_TEST_PR= OXY_TCP6, sizeof(HP2_TEST_PROXY_TCP6)), HAPROXY_PROTOCOL_VERSION_2);=
+ assert_int_equal(hapro= xy_protocol_version((uint8_t []){HP_TEST_OPENVPN}, sizeof((uint8_t []){HP_T= EST_OPENVPN})), HAPROXY_PROTOCOL_VERSION_INVALID);
+}
+
+static= void
+test_haproxy_p= rotocol_header_len(void **state)
+{
+ a= ssert_int_equal(haproxy_protocol_header_len(HP1_TEST_UNKNOWN, sizeof(HP1_TE= ST_UNKNOWN), HAPROXY_PROTOCOL_VERSION_1), sizeof(HP1_TEST_UNKNOWN));=
+ assert_int_equal(hapro= xy_protocol_header_len(HP1_TEST_TCP4_PLUS_OPENVPN, sizeof(HP1_TEST_TCP4_PLU= S_OPENVPN), HAPROXY_PROTOCOL_VERSION_1), sizeof(HP1_TEST_TCP4));
= + assert_int_equal(haproxy_p= rotocol_header_len(HP1_TEST_TCP6_PLUS_OPENVPN, sizeof(HP1_TEST_TCP6_PLUS_OP= ENVPN), HAPROXY_PROTOCOL_VERSION_1), sizeof(HP1_TEST_TCP6));
+ assert_int_equal(haproxy_proto= col_header_len(HP2_TEST_WRONG_VER, sizeof(HP2_TEST_WRONG_VER), HAPROXY_PROT= OCOL_VERSION_2), sizeof(HP2_TEST_WRONG_VER));

+ assert_int_equal(haproxy_protocol_header_len(= HP2_TEST_LOCAL, sizeof(HP2_TEST_LOCAL), HAPROXY_PROTOCOL_VERSION_2), sizeof= (HP2_TEST_LOCAL));
+ = assert_int_equal(haproxy_protocol_header_len(HP2_TEST_PROXY_TCP4_PLUS_OP= ENVPN, sizeof(HP2_TEST_PROXY_TCP4_PLUS_OPENVPN), HAPROXY_PROTOCOL_VERSION_2= ), sizeof(HP2_TEST_PROXY_TCP4));
+ assert_int_equal(haproxy_protocol_header_len(HP2_TEST_PROX= Y_TCP6_PLUS_OPENVPN, sizeof(HP2_TEST_PROXY_TCP6_PLUS_OPENVPN), HAPROXY_PROT= OCOL_VERSION_2), sizeof(HP2_TEST_PROXY_TCP6));
+ assert_int_equal(haproxy_protocol_header_len= ((uint8_t []){HP_TEST_OPENVPN}, sizeof((uint8_t []){HP_TEST_OPENVPN}), HAPR= OXY_PROTOCOL_VERSION_INVALID), -1);
+}
+
+static bool+compare_hpi_basic(void **stat= e, struct haproxy_protocol_info *actual, struct haproxy_protocol_info *expe= cted)
+{
+ if (actual->src.addr.sa.s= a_family !=3D expected->src.addr.sa.sa_family

+ || actual->dst.addr.sa.sa_family != =3D expected->dst.addr.sa.sa_family)
+ {
+ return false;
+ }
+
+ switch (actual->sr= c.addr.sa.sa_family)
= + {
+ case = AF_INET:
+ = return actual->version =3D=3D expected->version
+ && actual->= ;sock_type =3D=3D expected->sock_type
+ && memcmp(&actual->s= rc.addr.in4.sin_addr, &expected->src.addr.in4.sin_addr, sizeof(struc= t in_addr)) =3D=3D 0
= + && memcmp(&actual->dst.addr.in4.sin_addr= , &expected->dst.addr.in4.sin_addr, sizeof(struct in_addr)) =3D=3D 0=
+ = && actual->dst.addr.in4.sin_port =3D=3D expected->dst.addr.in= 4.sin_port
+ = && actual->src.addr.in4.sin_port =3D=3D expected->s= rc.addr.in4.sin_port;
+
+ case AF_I= NET6:
+ re= turn actual->version =3D=3D expected->version
+ && actual->= sock_type =3D=3D expected->sock_type
+ && memcmp(&actual->sr= c.addr.in6, &expected->src.addr.in6, sizeof(struct sockaddr_in6)) = =3D=3D 0
+ = && memcmp(&actual->dst.addr.in6, &expected->d= st.addr.in6, sizeof(struct sockaddr_in6)) =3D=3D 0;
+
+ default:
+ return false;
+ }
+}
+
+static bool

+compare_hpi(void **state, struct haproxy_= protocol_info *actual, struct haproxy_protocol_info *expected)
+{
+ return compare_hpi_basic(state, actual, expect= ed);
+}
+
+static int
+setup_parse(void **state)
+{
+ struct haproxy_protocol_info *actual;
+ ALLOC_OBJ_CLEAR(actual, struct haproxy_protocol= _info);
+ *state = =3D actual;
+ retu= rn 0;
+}
+
+static int
+teardown_parse(void **state)
+{
+ haproxy_protocol_reset(*state);
+ free(*state);
+ return 0;
+}
+

+static void
<= span style=3D"color: hsl(120, 100%, 40%);">+test_haproxy_protocol_parse(voi= d **state)
+{<= br>+ struct haproxy_protocol= _info *expected;
+ = struct haproxy_protocol_info *actual =3D *state;
+
+ ALLOC_OBJ_CLEAR(expected, struct haproxy_protocol_info);
+
+ /* v1 UNKNOWN protocol */
<= span style=3D"color: hsl(120, 100%, 40%);">+ assert_false(haproxy_protoc= ol_parse(actual, HP1_TEST_UNKNOWN, sizeof(HP1_TEST_UNKNOWN)));

+
+ /* v1 TCP4 */
+ memset((actual), 0, sizeof(struct haproxy_protoco= l_info));
+ haprox= y_protocol_reset(actual);
+ expected->version =3D HAPROXY_PROTOCOL_VERSION_1;
+ expected->sock_type =3D S= OCK_STREAM;
+ expe= cted->src.addr.sa.sa_family =3D AF_INET;
+ expected->src.addr.in4.sin_addr.s_addr =3D h= tonl(0x0a0a1402);
+ = expected->src.addr.in4.sin_port =3D ntohs(40150);
+ expected->dst.addr.sa.sa_family = =3D AF_INET;
+ exp= ected->dst.addr.in4.sin_addr.s_addr =3D htonl(0x0a0a1401);
+ expected->dst.addr.in4.sin= _port =3D ntohs(1195);
+ assert_true(co= mpare_hpi(state, actual, expected));
+
+ = /* v1 TCP6 */
+ = haproxy_protocol_reset(actual);
+ haproxy_protocol_reset(expected);
+ memset((actual), 0, sizeof(struct haproxy= _protocol_info));
+ = memset((expected), 0, sizeof(struct haproxy_protocol_info));
+ expected->version =3D HAP= ROXY_PROTOCOL_VERSION_1;
+ expected->sock_type =3D SOCK_STREAM;
+ expected->src.addr.in6.sin6_family =3D = AF_INET6;
+ expect= ed->src.addr.in6.sin6_port =3D ntohs(53344);
+ inet_pton(AF_INET6, "2001:db8:20::2&qu= ot;, &expected->src.addr.in6.sin6_addr);
+ expected->dst.addr.in6.sin6_family =3D A= F_INET6;
+ expecte= d->dst.addr.in6.sin6_port =3D ntohs(1195);
+ inet_pton(AF_INET6, "2001:db8:20::1"= ;, &expected->dst.addr.in6.sin6_addr);
+ haproxy_protocol_parse(actual, HP1_TEST_TCP6,= sizeof(HP1_TEST_TCP6));
+ assert_true(compare_hpi(state, actual, expected));
+
+ /* v2 wrong version */
+ haproxy_protocol_reset(actual);
<= span style=3D"color: hsl(120, 100%, 40%);">+ memset((actual), 0, sizeof(= struct haproxy_protocol_info));
+ assert_false(haproxy_protocol_parse(actual, HP2_TEST_WRONG_= VER, sizeof(HP2_TEST_WRONG_VER)));
+
+ = /* v2 local */
+ h= aproxy_protocol_reset(actual);
+ memset((actual), 0, sizeof(struct haproxy_protocol_info));
+ assert_false(hapr= oxy_protocol_parse(actual, HP2_TEST_LOCAL, sizeof(HP2_TEST_LOCAL)));=
+
+ /* v2 TCP4 */
+ haproxy_protocol_reset(actual);
<= span style=3D"color: hsl(120, 100%, 40%);">+ haproxy_protocol_reset(expe= cted);

+ memset((a= ctual), 0, sizeof(struct haproxy_protocol_info));
+ memset((expected), 0, sizeof(struct hapro= xy_protocol_info));
+= expected->version =3D HAPROXY_PROTOCOL_VERSION_2;
+ expected->sock_type =3D SOCK_ST= REAM;
+ expected-&= gt;src.addr.in4.sin_family =3D AF_INET;
+ expected->src.addr.in4.sin_port =3D ntohs(42288)= ;
+ expected->s= rc.addr.in4.sin_addr.s_addr =3D htonl(0x0a0a1402);
+ expected->dst.addr.in4.sin_family =3D= AF_INET;
+ expect= ed->dst.addr.in4.sin_port =3D ntohs(1195);
+ expected->dst.addr.in4.sin_addr.s_addr =3D= htonl(0x0a0a1401);
+= haproxy_protocol_parse(actual, HP2_TEST_PROXY_TCP4, sizeof(HP2_TEST_PRO= XY_TCP4));
+ asser= t_true(compare_hpi(state, actual, expected));
+
+ /* v2 TCP6 */
+ haproxy_protocol_reset(actual);
+ haproxy_protocol_reset(expected);
+ memset((actual), 0, sizeof(struc= t haproxy_protocol_info));
+ memset((expected), 0, sizeof(struct haproxy_protocol_info));

+ expected->versio= n =3D HAPROXY_PROTOCOL_VERSION_2;
+ expected->sock_type =3D SOCK_STREAM;
+ expected->src.addr.in6.sin6_fa= mily =3D AF_INET6;
+ = expected->src.addr.in6.sin6_port =3D ntohs(59574);
+ inet_pton(AF_INET6, "2001:db8= :20::2", &expected->src.addr.in6.sin6_addr);
+ expected->dst.addr.in6.sin6_fam= ily =3D AF_INET6;
+ = expected->dst.addr.in6.sin6_port =3D ntohs(1195);
+ inet_pton(AF_INET6, "2001:db8:2= 0::1", &expected->dst.addr.in6.sin6_addr);
+ haproxy_protocol_parse(actual, HP2_T= EST_PROXY_TCP6, sizeof(HP2_TEST_PROXY_TCP6));
+ assert_true(compare_hpi(state, actual, expect= ed));
+
+ haproxy_protocol_reset(expect= ed);
+ free(expect= ed);
+}
+
+int
+main(void)
+{<= /span>
+ openvpn_unit_tes= t_setup();
+ const= struct CMUnitTest tests[] =3D {
+ cmocka_unit_test(test_haproxy_protocol_version),
+ cmocka_unit_test(= test_haproxy_protocol_header_len),
+ cmocka_unit_test_setup_teardown(test_haproxy_protoco= l_parse, setup_parse, teardown_parse)
+ };
+ return cmocka_run_group_tests_name("haproxy_protocol", t= ests, NULL, NULL);
+}=

To view, visit change 685. To unsubscribe, = or for help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I9766c68feaba71279f26c3310ee= e4b5ca215e6ac
Gerrit-Change-Number: 685
Gerrit-PatchSet: 4
Gerrit-Owner: ralf_lici <ralf@...2726...>= ;
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newpatchset
--6M/a/JBbHYY=--