Skip to contentSkip to conversation

proxy protocol v2 for port-share

PATCH Superseded corubba 3 participants 14 messages Dec 14, 2024 – Dec 26, 2024

lore
Share & tools
lore

corubba

author
#0 lore raw

cover proxy protocol v2 for port-share

Hello,

since v2.1 (released 2009) OpenVPN has the "port-share" feature, where it listens on a tcp port (like 443) and forwards/proxies all incoming non-OpenVPN connections (like HTTPS) to a different server/port (hereafter called the "downstream server"). Because this terminates the tcp connection and the downstream server does not see the actual client ip, in v2.3 (released 2013) a "journal directory" feature was added where OpenVPN writes temporary files for every forwarded connection which the downstream server can use to determine the real client ip. While this works okay, it has a few drawbacks:

1) Since this is a custom solution, you also need a custom integration in the downstream server software to consume the journal files.
2) It is relatively straight forward to use on the same host, getting it to work across hosts is more difficult; but not impossible.
3) Because it basically is an side channel, there is the potential for race conditions. For example is the journal file written *after* the connection to the downstream server is opened, so it may not exist yet when the downstream server tries to access/read it.

The goal of this patch set is to add an additional mechanism for transmitting the real client ip to the downstream server using the PROXY protocol [0]. It was created by the fine people from HAProxy, releasing the specification of protocol version 1 in 2010, followed by version 2 in 2012. OpenVPNs port-share feature behaves like a "dumb proxy", for which that protocol was designed. Compared to the "journal directory" feature, it does not suffer from the above-mentioned drawbacks:

1) Standardized protocol which is natively supported by a wide range of software, allowing plug-and-play deployment.
2) Easy to use on the same or across different hosts.
3) Uses in-band transmission, no side-channel required.

The first patch adds normalization of IPv4-mapped IPv6 addresses to plain IPv4 addresses, which can be seen as a general improvement of the "journal" feature. The second patch adds the PROXY protocol (version 2) implementation.

The third patch extends the PROXY protocol implementation beyond what is currently required. It is not meant to be merged as-is right now, but only attached for completeness should these features ever be needed.

This patch set was not created out of necessity, but rather as an exercise while playing around with the port-share feature. Feel free to consider accepting this patch set without any pressure. I do believe it has merit thought, and it may be worth considering to go as far as to completely deprecate/replace the "journal directory" with it.

[0] https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt

Best regards
--
Corubba

author
#1 lore raw

PATCH 1/2 port-share: Normalize IPv4-mapped IPv6 addresses

Before passing IPv4-mapped IPv6 addresses to the proxy journal, translate them to plain IPv4 addresses. Whether the connection was accepted by OpenVPN on a "dual stack" socket is of no importance to the proxy receiver.

---
 src/openvpn/ps.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
src/openvpn/ps.c +18 -0
index 06bf91a8..36ea63b8 100644
@@ -330,6 +330,22 @@ proxy_list_housekeeping(struct proxy_connection **list)
     }
 }
 
+/*
+ * In-place transformation of an openvpn_sockaddr with an IPv4-mapped IPv6
+ * address to one with a plain IPv4 address. No-op otherwise.
+ */
+static void
+transform_mapped_v4_sockaddr(struct openvpn_sockaddr *sock)
+{
+    if (sock->addr.sa.sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sock->addr.in6.sin6_addr))
+    {
+        sock->addr.in4.sin_family = AF_INET;
+        /* sin_port and sin6_port are the same already */
+        memcpy(&sock->addr.in4.sin_addr, &sock->addr.in6.sin6_addr.s6_addr[12], 4);
+        memset(&sock->addr.in4 + 1, 0, sizeof(sock->addr) - sizeof(sock->addr.in4));
+    }
+}
+
 /*
  * Record IP/port of client in filesystem, so that server receiving
  * the proxy can determine true client origin.
@@ -349,6 +365,8 @@ journal_add(const char *journal_dir, struct proxy_connection *pc, struct proxy_c
     if (!getpeername(pc->sd, (struct sockaddr *) &from.addr.sa, &slen)
         && !getsockname(cp->sd, (struct sockaddr *) &to.addr.sa, &dlen))
     {
+        transform_mapped_v4_sockaddr(&from);
+        transform_mapped_v4_sockaddr(&to);
         const char *f = print_openvpn_sockaddr(&from, &gc);
         const char *t = print_openvpn_sockaddr(&to, &gc);
         fnlen =  strlen(journal_dir) + strlen(t) + 2;

--
2.47.1

author
#12 lore raw

PATCH 2/2 port-share: Add proxy protocol v2 support

In reply to corubba

In addition to the custom journal solution, also support the widely used binary PROXY protocol version 2 to convey the original client connection parameters to the proxy receiver. This makes the port-share journal feature more accessable and easier to use, because one doesn't need a custom integration.

While this is a spec-compliant sender implementation of the PROXY protocol, it does not implement it in full. Version 1 was left out entirely, in favour of the superior and easier-to-implement version 2. The implementation was also kept minimal with regards to what OpenVPN supports/requires: Local commands, unix sockets, UDP and TLVs are not implemented.

---
 doc/man-sections/server-options.rst |   4 +
 src/openvpn/ps.c                    | 110 +++++++++++++++++++++++++++-
 2 files changed, 113 insertions(+), 1 deletion(-)
doc/man-sections/server-options.rst +4 -0
index 3fe9862c..5fdd4a22 100644
@@ -435,6 +435,10 @@ fast hardware. SSL/TLS authentication must be used in this mode.
   the origin of the connection. Each generated file will be automatically
   deleted when the proxied connection is torn down.
 
+  ``dir`` can be set to the special value ``proxy_protocol_v2`` to make
+  OpenVPN use the binary PROXY protocol version 2 towards the proxy receiver.
+  No temporary files will be written in this mode.
+
   Not implemented on Windows.
 
 --push option
src/openvpn/ps.c +109 -1

122 lines not on this page · raw message

--
2.47.1

author
#13 lore raw

[Openvpn-devel] [PATCH 3/2] port-share: Add unix-socket and udp support for proxy protocol

In reply to corubba

Subject: [PATCH 3/3] port-share: Add unix-socket and udp support for proxy
protocol v2

Just in case it is ever needed.

---
 src/openvpn/ps.c     | 42 +++++++++++++++++++++++++++++++++++-------
 src/openvpn/socket.h |  1 +
 2 files changed, 36 insertions(+), 7 deletions(-)
src/openvpn/ps.c +35 -7
index b5d04c5b..b34df315 100644
@@ -400,18 +400,19 @@ journal_add(const char *journal_dir, struct proxy_connection *pc, struct proxy_c
 static void
 send_proxy_protocol_v2_header(const struct proxy_connection *const pc, const struct proxy_connection *const cp)
 {
-    static const uint8_t PP2_AF_UNSPEC = 0x0, PP2_AF_INET = 0x1, PP2_AF_INET6 = 0x2;
-    static const uint8_t PP2_PROTO_STREAM = 0x1;
+    static const uint8_t PP2_AF_UNSPEC = 0x0, PP2_AF_INET = 0x1, PP2_AF_INET6 = 0x2, PP2_AF_UNIX = 0x3;
+    static const uint8_t PP2_PROTO_UNSPEC = 0x0, PP2_PROTO_STREAM = 0x1, PP2_PROTO_DGRAM = 0x2;
 
     struct openvpn_sockaddr src, dst;
-    socklen_t src_len, dst_len;
-    unsigned char header[52] = {
+    socklen_t src_len, dst_len, socket_type_len;
+    unsigned char header[232] = {
         "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A" /* signature */
         "\x21" /* version=2 + command=proxy */
         /* initialize the rest to zero for now */
     };
-    uint8_t addr_fam, header_len = 16;
+    uint8_t addr_fam, proto, header_len = 16;
     uint16_t addr_len;
+    int socket_type;
 
     src_len = sizeof(src.addr);
     dst_len = sizeof(dst.addr);
@@ -467,7 +468,14 @@ send_proxy_protocol_v2_header(const struct proxy_connection *const pc, const str
             memcpy(&header[50], &dst.addr.in6.sin6_port, sizeof(dst.addr.in6.sin6_port));
             break;
 
-        /* AF_UNIX is currently not suppported by OpenVPN */
+        case AF_UNIX:
+            addr_fam = PP2_AF_UNIX;
+            addr_len = 216;
+            ASSERT(108 >= sizeof(src.addr.un.sun_path));
+            ASSERT(108 >= sizeof(dst.addr.un.sun_path));
+            memcpy(&header[16], &src.addr.un.sun_path, 108);
+            memcpy(&header[124], &dst.addr.un.sun_path, 108);
+            break;
 
         default:
             addr_fam = PP2_AF_UNSPEC;
@@ -475,7 +483,27 @@ send_proxy_protocol_v2_header(const struct proxy_connection *const pc, const str
             break;
     }
 
-    const uint8_t proto = PP2_PROTO_STREAM; /* DGRAM is currently not supported by port-share */
+    socket_type_len = sizeof(socket_type);
+    if (0 != getsockopt(pc->sd, SOL_SOCKET, SO_TYPE, &socket_type, &socket_type_len))
+    {
+        msg(M_WARN, "PORT SHARE PROXY: getting socket type failed");
+        socket_type = -1; /* fallback to unspec */
+    }
+    switch (socket_type)
+    {
+        case SOCK_STREAM:
+            proto = PP2_PROTO_STREAM;
+            break;
+
+        case SOCK_DGRAM:
+            proto = PP2_PROTO_DGRAM;
+            break;
+
+        default:
+            proto = PP2_PROTO_UNSPEC;
+            break;
+    }
+
     header[13] = (addr_fam << 4) | proto;
 
     /* TLV is currently not implemented */
src/openvpn/socket.h +1 -0
index 465d92ba..3578b3c3 100644
@@ -69,6 +69,7 @@ struct openvpn_sockaddr
         struct sockaddr sa;
         struct sockaddr_in in4;
         struct sockaddr_in6 in6;
+        struct sockaddr_un un;
     } addr;
 };
 

--
2.47.1

#2 lore raw

Re: [Openvpn-devel] [PATCH 1/2] port-share: Normalize IPv4-mapped IPv6 addresses

In reply to corubba

Hello,

Quoted 4 lines · corubba via Openvpn-devel

Before passing IPv4-mapped IPv6 addresses to the proxy journal, translate them to plain IPv4 addresses. Whether the connection was accepted by OpenVPN on a "dual stack" socket is of no importance to the proxy receiver.

Thank you very much for all these improvements!
We truly appreciate your work (especially because they are IPv6 related ;-D)

Quoted 2 lines

However, for legal reasons we need the Signed-off-by line to contain your real name and surname. This line represents a real signature of acceptance of the license OpenVPN is shipped with.

Would you mind sending a v2 of this patchset with the modified signed-off-by line?

Thanks a lot.

Best Regards,

Signature · 1 line
Antonio Quartulli
author
#3 lore raw

PATCH v2 1/2 port-share: Normalize IPv4-mapped IPv6 addresses

Before passing IPv4-mapped IPv6 addresses to the proxy journal, translate them to plain IPv4 addresses. Whether the connection was accepted by OpenVPN on a "dual stack" socket is of no importance to the proxy receiver.

---
 src/openvpn/ps.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
src/openvpn/ps.c +18 -0
index 06bf91a8..36ea63b8 100644
@@ -330,6 +330,22 @@ proxy_list_housekeeping(struct proxy_connection **list)
     }
 }
 
+/*
+ * In-place transformation of an openvpn_sockaddr with an IPv4-mapped IPv6
+ * address to one with a plain IPv4 address. No-op otherwise.
+ */
+static void
+transform_mapped_v4_sockaddr(struct openvpn_sockaddr *sock)
+{
+    if (sock->addr.sa.sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sock->addr.in6.sin6_addr))
+    {
+        sock->addr.in4.sin_family = AF_INET;
+        /* sin_port and sin6_port are the same already */
+        memcpy(&sock->addr.in4.sin_addr, &sock->addr.in6.sin6_addr.s6_addr[12], 4);
+        memset(&sock->addr.in4 + 1, 0, sizeof(sock->addr) - sizeof(sock->addr.in4));
+    }
+}
+
 /*
  * Record IP/port of client in filesystem, so that server receiving
  * the proxy can determine true client origin.
@@ -349,6 +365,8 @@ journal_add(const char *journal_dir, struct proxy_connection *pc, struct proxy_c
     if (!getpeername(pc->sd, (struct sockaddr *) &from.addr.sa, &slen)
         && !getsockname(cp->sd, (struct sockaddr *) &to.addr.sa, &dlen))
     {
+        transform_mapped_v4_sockaddr(&from);
+        transform_mapped_v4_sockaddr(&to);
         const char *f = print_openvpn_sockaddr(&from, &gc);
         const char *t = print_openvpn_sockaddr(&to, &gc);
         fnlen =  strlen(journal_dir) + strlen(t) + 2;

--
2.47.1

author
#7 lore raw

PATCH v2 2/2 port-share: Add proxy protocol v2 support

In reply to Antonio Quartulli

In addition to the custom journal solution, also support the widely used binary PROXY protocol version 2 to convey the original client connection parameters to the proxy receiver. This makes the port-share journal feature more accessable and easier to use, because one doesn't need a custom integration.

While this is a spec-compliant sender implementation of the PROXY protocol, it does not implement it in full. Version 1 was left out entirely, in favour of the superior and easier-to-implement version 2. The implementation was also kept minimal with regards to what OpenVPN supports/requires: Local commands, unix sockets, UDP and TLVs are not implemented.

---
 doc/man-sections/server-options.rst |   4 +
 src/openvpn/ps.c                    | 110 +++++++++++++++++++++++++++-
 2 files changed, 113 insertions(+), 1 deletion(-)
doc/man-sections/server-options.rst +4 -0
index 3fe9862c..5fdd4a22 100644
@@ -435,6 +435,10 @@ fast hardware. SSL/TLS authentication must be used in this mode.
   the origin of the connection. Each generated file will be automatically
   deleted when the proxied connection is torn down.
 
+  ``dir`` can be set to the special value ``proxy_protocol_v2`` to make
+  OpenVPN use the binary PROXY protocol version 2 towards the proxy receiver.
+  No temporary files will be written in this mode.
+
   Not implemented on Windows.
 
 --push option
src/openvpn/ps.c +109 -1

122 lines not on this page · raw message

--
2.47.1

author
#11 lore raw

[Openvpn-devel] [PATCH v2 3/2] port-share: Add unix-socket and udp support for proxy protocol

In reply to Antonio Quartulli

Just in case it is ever needed.

---
 src/openvpn/ps.c     | 42 +++++++++++++++++++++++++++++++++++-------
 src/openvpn/socket.h |  1 +
 2 files changed, 36 insertions(+), 7 deletions(-)
src/openvpn/ps.c +35 -7
index b5d04c5b..b34df315 100644
@@ -400,18 +400,19 @@ journal_add(const char *journal_dir, struct proxy_connection *pc, struct proxy_c
 static void
 send_proxy_protocol_v2_header(const struct proxy_connection *const pc, const struct proxy_connection *const cp)
 {
-    static const uint8_t PP2_AF_UNSPEC = 0x0, PP2_AF_INET = 0x1, PP2_AF_INET6 = 0x2;
-    static const uint8_t PP2_PROTO_STREAM = 0x1;
+    static const uint8_t PP2_AF_UNSPEC = 0x0, PP2_AF_INET = 0x1, PP2_AF_INET6 = 0x2, PP2_AF_UNIX = 0x3;
+    static const uint8_t PP2_PROTO_UNSPEC = 0x0, PP2_PROTO_STREAM = 0x1, PP2_PROTO_DGRAM = 0x2;
 
     struct openvpn_sockaddr src, dst;
-    socklen_t src_len, dst_len;
-    unsigned char header[52] = {
+    socklen_t src_len, dst_len, socket_type_len;
+    unsigned char header[232] = {
         "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A" /* signature */
         "\x21" /* version=2 + command=proxy */
         /* initialize the rest to zero for now */
     };
-    uint8_t addr_fam, header_len = 16;
+    uint8_t addr_fam, proto, header_len = 16;
     uint16_t addr_len;
+    int socket_type;
 
     src_len = sizeof(src.addr);
     dst_len = sizeof(dst.addr);
@@ -467,7 +468,14 @@ send_proxy_protocol_v2_header(const struct proxy_connection *const pc, const str
             memcpy(&header[50], &dst.addr.in6.sin6_port, sizeof(dst.addr.in6.sin6_port));
             break;
 
-        /* AF_UNIX is currently not suppported by OpenVPN */
+        case AF_UNIX:
+            addr_fam = PP2_AF_UNIX;
+            addr_len = 216;
+            ASSERT(108 >= sizeof(src.addr.un.sun_path));
+            ASSERT(108 >= sizeof(dst.addr.un.sun_path));
+            memcpy(&header[16], &src.addr.un.sun_path, 108);
+            memcpy(&header[124], &dst.addr.un.sun_path, 108);
+            break;
 
         default:
             addr_fam = PP2_AF_UNSPEC;
@@ -475,7 +483,27 @@ send_proxy_protocol_v2_header(const struct proxy_connection *const pc, const str
             break;
     }
 
-    const uint8_t proto = PP2_PROTO_STREAM; /* DGRAM is currently not supported by port-share */
+    socket_type_len = sizeof(socket_type);
+    if (0 != getsockopt(pc->sd, SOL_SOCKET, SO_TYPE, &socket_type, &socket_type_len))
+    {
+        msg(M_WARN, "PORT SHARE PROXY: getting socket type failed");
+        socket_type = -1; /* fallback to unspec */
+    }
+    switch (socket_type)
+    {
+        case SOCK_STREAM:
+            proto = PP2_PROTO_STREAM;
+            break;
+
+        case SOCK_DGRAM:
+            proto = PP2_PROTO_DGRAM;
+            break;
+
+        default:
+            proto = PP2_PROTO_UNSPEC;
+            break;
+    }
+
     header[13] = (addr_fam << 4) | proto;
 
     /* TLV is currently not implemented */
src/openvpn/socket.h +1 -0
index 465d92ba..3578b3c3 100644
@@ -69,6 +69,7 @@ struct openvpn_sockaddr
         struct sockaddr sa;
         struct sockaddr_in in4;
         struct sockaddr_in6 in6;
+        struct sockaddr_un un;
     } addr;
 };
 

--
2.47.1

#4 lore raw

Re: [Openvpn-devel] [PATCH v2 1/2] port-share: Normalize IPv4-mapped IPv6 addresses

In reply to corubba

Hi,

Quoted 6 lines · corubba via Openvpn-devel

Before passing IPv4-mapped IPv6 addresses to the proxy journal, translate them to plain IPv4 addresses. Whether the connection was accepted by OpenVPN on a "dual stack" socket is of no importance to the proxy receiver.

I'm fine with the idea, but I do not like the implementation with "yet another localized sockaddr manipulation function" - we already have similar code in socket.c/setenv_sockaddr(), so maybe we need to make this a more general thing.

print_openvpn_sockaddr() is just a wrapper around print_sockaddr_ex(), which takes flags. So we could introduce something like a PS_NO_V4MAPPED (naming is hard :-) ) which would then do "this" inside print_sockaddr_ex().

It's not fully straightforward, though (it operates on a pointer passed to it, so we'd need to have a local sockaddr_in for "unmapped v4" then).

Mmmmh, need to think more about this.

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#5 lore raw

Re: [Openvpn-devel] [PATCH v2 1/2] port-share: Normalize IPv4-mapped IPv6 addresses

Hi,

Quoted 4 lines · Gert Doering

I'm fine with the idea, but I do not like the implementation with "yet another localized sockaddr manipulation function" - we already have similar code in socket.c/setenv_sockaddr(), so maybe we need to make this a more general thing.

I now see that it's not *that* easy, as transform_mapped_v4_sockaddr() is also used by part 2/3 of the series... so maybe we'll just take 1/3 as it is, and see if we can overhaul the whole handling of v4-mapped addresses in a more coordinated way later.

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#8 lore raw

Re: [Openvpn-devel] [PATCH v2 2/2] port-share: Add proxy protocol v2 support

In reply to corubba

Hi,

Quoted 6 lines · corubba via Openvpn-devel

In addition to the custom journal solution, also support the widely used binary PROXY protocol version 2 to convey the original client connection parameters to the proxy receiver. This makes the port-share journal feature more accessable and easier to use, because one doesn't need a custom integration.

Quoted 6 lines

While this is a spec-compliant sender implementation of the PROXY protocol, it does not implement it in full. Version 1 was left out entirely, in favour of the superior and easier-to-implement version 2. The implementation was also kept minimal with regards to what OpenVPN supports/requires: Local commands, unix sockets, UDP and TLVs are not implemented.

Are there any reference URLs for the PROXY protocol? If yes, I think it would be good to include a reference into one of the comments - the next person to read that code would then know which document this is based on.

Besides this, I'm not sure what to do with these

src/openvpn/ps.c journal_add(const char *journal_dir, struct proxy_connection *pc, struct proxy_c · line 450

Quoted 4 lines
src/openvpn/ps.c +4 -0
+            ASSERT(4 >= sizeof(src.addr.in4.sin_addr));
+            ASSERT(4 >= sizeof(dst.addr.in4.sin_addr));
+            memcpy(&header[16], &src.addr.in4.sin_addr, sizeof(src.addr.in4.sin_addr));
+            memcpy(&header[20], &dst.addr.in4.sin_addr, sizeof(dst.addr.in4.sin_addr));

... this is all obviously correct, but it feels heavy handed, and many lines of code... (especially 2x sizeof on the same structure element)

I think we can fairly safely assume that an in4.sin_addr will always be 4 bytes, an in4.sin_port will always be 2 bytes, and an IPv6 address will always be 16 bytes... so I wonder if it wouln't be easier to just copy a well-known number of bytes (openvpn_sockaddr is also "always bigger than that", so no read overrun) here.

I know that "magic numbers" in code are considered a bad pattern, but these things are really constant length, and the buffer layout *requires* "exactly 4" and "exactly 2" bytes...

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
author
#6 lore raw

Re: [Openvpn-devel] [PATCH v2 1/2] port-share: Normalize IPv4-mapped IPv6 addresses

In reply to Gert Doering

Hi,

Quoted 10 lines · Gert Doering
Quoted 4 lines · Gert Doering

I'm fine with the idea, but I do not like the implementation with "yet another localized sockaddr manipulation function" - we already have similar code in socket.c/setenv_sockaddr(), so maybe we need to make this a more general thing.

I now see that it's not *that* easy, as transform_mapped_v4_sockaddr() is also used by part 2/3 of the series... so maybe we'll just take 1/3 as it is, and see if we can overhaul the whole handling of v4-mapped addresses in a more coordinated way later.

I actually had initially implemented it in socket.c, but reverted it to a local and simpler implementation because I was sure to otherwise break stuff with it. I dug it up again and refined it a bit, feel free to have a look at the attached patch and form an opinion. But please consider it still WIP, it is not intended as a v3 of the original patch.

Best regards
--
Corubba

From f749d6ab799cc40e33c15fdd95841ab19c9c39fa Mon Sep 17 00:00:00 2001
From: Corubba Smith <[email protected]>
Date: Thu, 26 Dec 2024 21:23:30 +0100
Subject: [PATCH] WIP: generic v4-mapped-normalize implementation in socket.c

Implement three functions to do v4-mapped normalization of
openvpn_sockaddr: in-place, into an existing struct, and with a new gc-allocated struct.

Also do the normalization in print_sockaddr by default because I feel that is what users expect, and add a flag do disable it (in case it is ever needed).

Since print_sockaddr does normalization by default and the journal directory is solely based on that, no explicit normalization is needed anymore.

Haven't tested the dco part, but the rest works. mroute doesn't use a sockaddr but only the addr structs, requires more code to also support.

---
 src/openvpn/dco_linux.c | 13 +-------
 src/openvpn/socket.c    | 72 +++++++++++++++++++++++++++++++++--------
 src/openvpn/socket.h    |  7 ++++
 3 files changed, 67 insertions(+), 25 deletions(-)
src/openvpn/dco_linux.c +1 -12
index b0383828..ad4b110f 100644
@@ -203,18 +203,7 @@ ovpn_nl_msg_send(dco_context_t *dco, struct nl_msg *nl_msg, ovpn_nl_cb cb,
 struct sockaddr *
 mapped_v4_to_v6(struct sockaddr *sock, struct gc_arena *gc)
 {
-    struct sockaddr_in6 *sock6 = (struct sockaddr_in6 *)sock;
-    if (sock->sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sock6->sin6_addr))
-    {
-
-        struct sockaddr_in *sock4;
-        ALLOC_OBJ_CLEAR_GC(sock4, struct sockaddr_in, gc);
-        memcpy(&sock4->sin_addr, sock6->sin6_addr.s6_addr + 12, 4);
-        sock4->sin_port = sock6->sin6_port;
-        sock4->sin_family = AF_INET;
-        return (struct sockaddr *)sock4;
-    }
-    return sock;
+    return (struct sockaddr *) normalized_sockaddr((const struct openvpn_sockaddr *) sock, gc);
 }
 
 int
src/openvpn/socket.c +59 -13

96 lines not on this page · raw message

src/openvpn/socket.h +7 -0
index 465d92ba..07fb7b73 100644
@@ -359,6 +359,7 @@ void sd_close(socket_descriptor_t *sd);
 #define PS_SHOW_PKTINFO         (1<<2)
 #define PS_DONT_SHOW_ADDR       (1<<3)
 #define PS_DONT_SHOW_FAMILY     (1<<4)
+#define PS_DONT_NORMALIZE       (1<<5)
 
 const char *print_sockaddr_ex(const struct sockaddr *addr,
                               const char *separator,
@@ -398,6 +399,12 @@ const char *print_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_arena
 
 const char *print_in6_addr(struct in6_addr addr6, unsigned int flags, struct gc_arena *gc);
 
+void normalize_sockaddr(struct openvpn_sockaddr *sock);
+
+void copy_normalized_sockaddr(const struct openvpn_sockaddr *src, struct openvpn_sockaddr *dst);
+
+struct openvpn_sockaddr * normalized_sockaddr(const struct openvpn_sockaddr *sock, struct gc_arena *gc);
+
 const char *print_in_port_t(in_port_t port, struct gc_arena *gc);
 
 struct in6_addr add_in6_addr( struct in6_addr base, uint32_t add );
Signature · 1 line
2.47.1
author
#9 lore raw

Re: [Openvpn-devel] [PATCH v2 2/2] port-share: Add proxy protocol v2 support

In reply to Gert Doering

Hi,

Quoted 14 lines · Gert Doering
Quoted 6 lines · corubba via Openvpn-devel

In addition to the custom journal solution, also support the widely used binary PROXY protocol version 2 to convey the original client connection parameters to the proxy receiver. This makes the port-share journal feature more accessable and easier to use, because one doesn't need a custom integration.

Quoted 6 lines

While this is a spec-compliant sender implementation of the PROXY protocol, it does not implement it in full. Version 1 was left out entirely, in favour of the superior and easier-to-implement version 2. The implementation was also kept minimal with regards to what OpenVPN supports/requires: Local commands, unix sockets, UDP and TLVs are not implemented.

Quoted 3 lines

Are there any reference URLs for the PROXY protocol? If yes, I think it would be good to include a reference into one of the comments - the next person to read that code would then know which document this is based on.

I had included the link to the spec I used only in the cover letter, but I agree putting it into the commit message is more sensible.

Quoted 16 lines

Besides this, I'm not sure what to do with these

src/openvpn/ps.c journal_add(const char *journal_dir, struct proxy_connection *pc, struct proxy_c · line 450

Quoted 4 lines
src/openvpn/ps.c +4 -0
+            ASSERT(4 >= sizeof(src.addr.in4.sin_addr));
+            ASSERT(4 >= sizeof(dst.addr.in4.sin_addr));
+            memcpy(&header[16], &src.addr.in4.sin_addr, sizeof(src.addr.in4.sin_addr));
+            memcpy(&header[20], &dst.addr.in4.sin_addr, sizeof(dst.addr.in4.sin_addr));

... this is all obviously correct, but it feels heavy handed, and many lines of code... (especially 2x sizeof on the same structure element)

I think we can fairly safely assume that an in4.sin_addr will always be 4 bytes, an in4.sin_port will always be 2 bytes, and an IPv6 address will always be 16 bytes... so I wonder if it wouln't be easier to just copy a well-known number of bytes (openvpn_sockaddr is also "always bigger than that", so no read overrun) here.

Quoted 3 lines

I know that "magic numbers" in code are considered a bad pattern, but these things are really constant length, and the buffer layout *requires* "exactly 4" and "exactly 2" bytes...

I wasn't sure about that either, and in the end opted for the "better safe than sorry" variant. If your better-informed opinion too is that these are "practically constant", then I am happy to oblige.

Best regards
--
Corubba

author
#10 lore raw

PATCH v3 2/2 port-share: Add proxy protocol v2 support

In addition to the custom journal solution, also support the widely used binary PROXY protocol version 2 to convey the original client connection parameters to the proxy receiver. This makes the port-share journal feature more accessable and easier to use, because one doesn't need a custom integration.

While this is a spec-compliant [0] sender implementation of the PROXY protocol, it does not implement it in full. Version 1 was left out entirely, in favour of the superior and easier-to-implement version 2. The implementation was also kept minimal with regards to what OpenVPN supports/requires: Local commands, unix sockets, UDP and TLVs are not implemented.

[0] https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt

---
 doc/man-sections/server-options.rst |   4 ++
 src/openvpn/ps.c                    | 102 +++++++++++++++++++++++++++-
 2 files changed, 105 insertions(+), 1 deletion(-)
doc/man-sections/server-options.rst +4 -0
index 3fe9862c..5fdd4a22 100644
@@ -435,6 +435,10 @@ fast hardware. SSL/TLS authentication must be used in this mode.
   the origin of the connection. Each generated file will be automatically
   deleted when the proxied connection is torn down.
 
+  ``dir`` can be set to the special value ``proxy_protocol_v2`` to make
+  OpenVPN use the binary PROXY protocol version 2 towards the proxy receiver.
+  No temporary files will be written in this mode.
+
   Not implemented on Windows.
 
 --push option
src/openvpn/ps.c +101 -1

114 lines not on this page · raw message

--
2.47.1