Skip to contentSkip to conversation

When using UDP over SOCKS5, send the actual remote hostname (FQDN) to the proxy server in the first packet.

PATCH In review Jesse Glick 5 participants 9 messages Aug 8, 2013 – Aug 26, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 2/2 When using UDP over SOCKS5, send the actual remote hostname (FQDN) to the proxy server in the first packet.

Also as: https://github.com/OpenVPN/openvpn/pull/6

I was exploring the possibility of using a SOCKS5 server to multiplex OpenVPN connections based on hostname (in the remote option), akin to how HTTP reverse proxies commonly delegate to a physical server according to the HTTP 1.1 Host header—so that many hostnames can map to the same IP of the reverse proxy.

I found that the SOCKS5 protocol does support DOMAINNAME rather than IP V4 when using the UDP relay server. However, OpenVPN was never using this option; it always sent a preresolved IP (v4) address, giving the proxy server no way of inferring what hostname was originally requested.

This patch amends the socks_process_outgoing_udp method to send DOMAINNAME for the first datagram of the connection, when an FQDN was specified. Thereafter it sends IP V4since this is more compact—and experimentation with the Dante server revealed that it calledcgethostbyname on many, if not all, such datagrams it received, which could be quite expensive. Since in the case of OpenVPN the “desired destination address” is constant for the life of the tunnel, it should suffice to send the hostname just once.

socks_adjust_frame_parameters also needed to be changed to allow for the worst-case buffer size, a 255-character hostname (Linux generally only allows 64) plus fixed-length fields (7 bytes). It would be nicer to set this according to the actual hostname length, but I could not see how to obtain that hostname during this early stage of initialization.

Filing this for the purpose of getting feedback. Probably there needs to be a user option to configure this behavior:

· disable (restoring original behavior; perhaps the default) · enable for first packet only (as in the current patch) · enable for all packets, which could be useful in case the reverse proxy is using a single UDP relay server for many clients (since SOCKS5 offers no apparent connection identifier other than the BND.PORT)

Note that the original code probably does not work with IPv6, but I did not attempt to address that in this patch.

 src/openvpn/forward.c |  2 +-
 src/openvpn/openvpn.h |  5 +++++
 src/openvpn/socks.c   | 38 +++++++++++++++++++++++++++-----------
 src/openvpn/socks.h   |  3 +--
 4 files changed, 34 insertions(+), 14 deletions(-)
src/openvpn/forward.c +0 -0
index 447b86d..ba084aa 100644
@@ -631,7 +631,7 @@ socks_preprocess_outgoing_link (struct context *c,
 {
   if (c->c2.link_socket->socks_proxy && c->c2.link_socket->info.proto

== PROTO_UDPv4)

     {
-      *size_delta += socks_process_outgoing_udp (&c->c2.to_link,
c->c2.to_link_addr);
+      *size_delta += socks_process_outgoing_udp (c);
       *to_addr = &c->c2.link_socket->socks_relay;
     }

}

src/openvpn/openvpn.h +2 -0
index 606a4f5..e040fe7 100644
@@ -291,6 +291,11 @@ struct context_2
   counter_type n_trunc_post_decrypt;
 #endif
 
+#ifdef ENABLE_SOCKS
+  /* Set to true after first UDP packet sent over SOCKS, so we only

send ATYP=DOMAINNAME once */
+ bool socks_sent_hostname;
+#endif
+
/*
* Timer objects for ping and inactivity
* timeout features.

src/openvpn/socks.c +10 -7
index 235982e..d98887f 100644
@@ -49,14 +49,17 @@
 #include "proxy.h"
 
 #include "memdbg.h"
+#include "openvpn.h"
 
 #define UP_TYPE_SOCKS "SOCKS Proxy"
 
+/* See \c socks_process_outgoing_udp for motivation. */
 void
 socks_adjust_frame_parameters (struct frame *frame, int proto)
 {
   if (proto == PROTO_UDPv4)
-    frame_add_to_extra_link (frame, 10);
+    // TODO no access to remote_host at this time, so have to assume the worst
+    frame_add_to_extra_link (frame, 255 + 7);
 }
 
 struct socks_proxy_info *
@@ -510,33 +513,46 @@ socks_process_incoming_udp (struct buffer *buf,
 }
 
 /*
- * Add a 10 byte socks header prior to UDP write.
- * *to is the destination address.
+ * Add a socks header prior to UDP write.
  *
  * Run before UDP write.
  * Returns the size of the header.
  */
 int
-socks_process_outgoing_udp (struct buffer *buf,
-    const struct link_socket_actual *to)
+socks_process_outgoing_udp (struct context *c)
 {
+  struct link_socket_actual *to = c->c2.to_link_addr;
+  const char *host = c->c2.link_socket->remote_host;
   /*
-   * Get a 10 byte subset buffer prepended to buf --
+   * Get a subset buffer prepended to buf --
    * we expect these bytes will be here because
    * we allocated frame space in socks_adjust_frame_parameters.
    */
-  struct buffer head = buf_sub (buf, 10, true);
+  int len = c->c2.socks_sent_hostname ||

ip_addr_dotted_quad_safe(host) ? /* use ATYP = IP V4 */ 0 :
strlen(host);
+ c->c2.socks_sent_hostname = true; // only ever use ATYP = DOMAINNAME once per connection
+ int size = len ? len + 7 : 10;
+ struct buffer head = buf_sub (&c->c2.to_link, size, true);

/* crash if not enough headroom in buf */
ASSERT (buf_defined (&head));

buf_write_u16 (&head, 0); /* RSV = 0 */ buf_write_u8 (&head, 0); /* FRAG = 0 */
- buf_write_u8 (&head, '\x01'); /* ATYP = 1 (IP V4) */
- buf_write (&head, &to->dest.addr.in4.sin_addr, sizeof (to->dest.addr.in4.sin_addr));
- buf_write (&head, &to->dest.addr.in4.sin_port, sizeof (to->dest.addr.in4.sin_port));
+ if (len)
+ {

+      msg(M_INFO, "Sending hostname %s to SOCKS5 proxy", host);
+      buf_write_u8(&head, '\x03'); /* ATYP = 3 (DOMAINNAME) */
+      buf_write_u8(&head, len);
+      buf_write(&head, host, len);

+ }
+ else
+ {
+ buf_write_u8(&head, '\x01'); /* ATYP = 1 (IP V4) */
+ buf_write(&head, &to->dest.addr.in4.sin_addr, /* 4 */sizeof
(to->dest.addr.in4.sin_addr));
+ }
+ buf_write (&head, &to->dest.addr.in4.sin_port, /* 2 */sizeof (to->dest.addr.in4.sin_port));

- return 10;
+ return size;
}

#else

src/openvpn/socks.h +0 -0
index b55ff6f..a6160d2 100644
@@ -70,8 +70,7 @@ void establish_socks_proxy_udpassoc (struct

socks_proxy_info *p,
void socks_process_incoming_udp (struct buffer *buf,
struct link_socket_actual *from);

-int socks_process_outgoing_udp (struct buffer *buf,
- const struct link_socket_actual *to);
+int socks_process_outgoing_udp (struct context *c);

#endif
#endif

Signature · 1 line
1.8.1.2
#2 lore raw

Yes. The patch is on the official "to be reviewed" queue here:

<https://community.openvpn.net/openvpn/wiki/Patches>

We try to be as responsive as possible, but we are few in number so patch review tends to drag on for too long. We try to review unreviewed patches and tickets during IRC meetings[1] to get them merged eventually.

Samuli

[1] Meeting are on Thursdays at 18:00 on #openvpn-devel at irc.freenode.net. We don't have them every single week, but lately we've been fairly actively arranging them.

Quoted 14 lines · Jesse Glick
Quoted 1 line

Also as: https://github.com/OpenVPN/openvpn/pull/6

Ping. Anyone care about this?

------------------------------------------------------------------------------
Introducing Performance Central, a new site from SourceForge and AppDynamics. Performance Central is your source for news, insights, analysis and resources for efficient Application Performance Management. Visit us today!
http://pubads.g.doubleclick.net/gampad/clk?id=48897511&iu=/4140/ostg.clktrk
_______________________________________________
Openvpn-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#4 lore raw
Quoted 1 line · Arne Schwabe

I will be happy to look at the patch

Thanks.

(As mentioned in the initial summary, the newly introduced behavior probably needs to be a configurable option, but I did not want to spend time implementing that unless there was general agreement on the direction of the patch.)

#5 lore raw
In reply to Jesse Glick

We discussed this patch briefly in today's IRC developer meeting, and wanted some clarification on the intent/goal here. Comments inline (code removed since we're trying to wrap our heads around the feature in the general sense first.) Once we have a better idea of the intent, it will be easier to look at the code itself more in-depth.

As Arne noted in this thread, detailed code review & a merge strategy may well wait until the dual-stack support is in as to not complicate his work on that.

Quoted 7 lines · Jesse Glick

Also as: https://github.com/OpenVPN/openvpn/pull/6

I was exploring the possibility of using a SOCKS5 server to multiplex OpenVPN connections based on hostname (in the remote option), akin to how HTTP reverse proxies commonly delegate to a physical server according to the HTTP 1.1 Host header—so that many hostnames can map to the same IP of the reverse proxy.

There was some issue figuring out what this all means. Do you have an example use-case where this is helpful?

I don't understand the bit about multiplexing, since the idea of a SOCKS transport is a one-to-one relationship. Since OpenVPN uses a single UDP (or TCP) socket to establish remote connections, I'm not sure what there is to multiplex.

Quoted 6 lines

I found that the SOCKS5 protocol does support DOMAINNAME rather than IP V4 when using the UDP relay server. However, OpenVPN was never using this option; it always sent a preresolved IP (v4) address, giving the proxy server no way of inferring what hostname was originally requested.

Quoted 8 lines

This patch amends the socks_process_outgoing_udp method to send DOMAINNAME for the first datagram of the connection, when an FQDN was specified. Thereafter it sends IP V4since this is more compact—and experimentation with the Dante server revealed that it calledcgethostbyname on many, if not all, such datagrams it received, which could be quite expensive. Since in the case of OpenVPN the “desired destination address” is constant for the life of the tunnel, it should suffice to send the hostname just once.

This confused us too; what's the advantage of doing the DNS lookup remotely? Presumably this is in case the remote end comes up with a different IP than the local one, but what's to then stop the local side from using the then-miss-matched IPv4 address the _local_ end resolved and sending all future datagrams to a different IP than the first?

Quoted 17 lines

socks_adjust_frame_parameters also needed to be changed to allow for the worst-case buffer size, a 255-character hostname (Linux generally only allows 64) plus fixed-length fields (7 bytes). It would be nicer to set this according to the actual hostname length, but I could not see how to obtain that hostname during this early stage of initialization.

Filing this for the purpose of getting feedback. Probably there needs to be a user option to configure this behavior:

· disable (restoring original behavior; perhaps the default) · enable for first packet only (as in the current patch) · enable for all packets, which could be useful in case the reverse proxy is using a single UDP relay server for many clients (since SOCKS5 offers no apparent connection identifier other than the BND.PORT)

Quoted 2 lines

Note that the original code probably does not work with IPv6, but I did not attempt to address that in this patch.

Since >=2.3.0 officially supports IPv6, new features should probably do so as well. The code as I see will simply "not offer" this feature when info.proto == PROTO_UDPv6, but then we need to document the distinction.

This is probably less important to address now; let's first see if the rest of us can figure out what this feature's use-case is.

Thanks,

--
Josh Cepek

#6 lore raw
Quoted 6 lines · Josh Cepek
Quoted 3 lines

akin to how HTTP reverse proxies commonly delegate to a physical server according to the HTTP 1.1 Host header—so that many hostnames can map to the same IP of the reverse proxy.

There was some issue figuring out what this all means. Do you have an example use-case where this is helpful?

To expand on my explanation as quoted above:

For HTTP protocol, if you want to provide a single server with a fixed IP address which services multiple sites—either directly, or by acting as a reverse proxy to other servers with variable IPs and ports—it is
easy: you just have your server inspect the ‘Host’ header which represents what DNS name the client intended to connect to. HTTP/1.1 clients are obliged to send this, so you can safely have them contact
http://joe.accounts.yourcorp.com/ (where the DNS entry for *.accounts.yourcorp.com is that of proxy.yourcorp.com) and when you see ‘Host: joe.accounts.yourcorp.com’ you know where to forward the request.

Now what if you want to do the same for a different procotol, such as OpenVPN? I.e. run hundreds of OpenVPN servers but give clients a single public contact point? This protocol does not send the original host name that I know of, and to make a reverse proxy directly working with the OpenVPN protocol (e.g. inspecting the CN on a client certificate) you would have to copy a bunch of OpenVPN C code and then start forwarding a half-completed handshake—a daunting task.

My idea was to take advantage of the fact that OpenVPN has built-in SOCKS5 support, and use a SOCKS5 “proxy” as a reverse proxy. So the client connects to joe.vpns.yourcorp.com:1194 (UDP), but with a SOCKS5 proxy of vpns.yourcorp.com. Now you just need to write a SOCKS5 server, which is a much easier task since its protocol is very simple (especially when no authentication is sent, as here). For each connection made, the server sets up a UDP relay server and blindly copies all the packets on to the real OpenVPN server, which might have a floating IP address or even be on a private network.

All that is needed for this to work is for the SOCKS5 server to receive information about the original hostname (joe.vpns.yourcorp.com) rather than the singleton IP address it resolved to, i.e. the equivalent of the Host header. But alas, OpenVPN today does not send it: it resolves the host locally, then sends only an IP address to SOCKS5 when creating packets for the UDP relay. My patch merely tells the client to follow the option in the SOCKS5 protocol to send the hostname rather than IP.

(Beware that I have not yet tried to write such a SOCKS5 UDP reverse proxy. It is conceivable there would be some subtle problem that cannot be worked around even if the original hostname is available. I have only confirmed so far that with the current OpenVPN client it is *not* possible to write such a proxy, and believe with my patch it *should* be possible.)

Quoted 1 line

what's the advantage of doing the DNS lookup remotely?

There is no DNS lookup remotely. The ‘joe.vpns.yourcorp.com’ is merely a hint to forward packets to a particular delegate (that associated with Joe’s account).

Again this is analogous to, say, Apache working as an HTTP reverse proxy. If it gets ‘Host: joe.accounts.yourcorp.com’, it does not try to resolve that hostname, since the result is known in advance. It just uses this information to match against a rule in its configuration that tells it how to process that virtual site.

Quoted 1 line

Since >=2.3.0 officially supports IPv6, new features should probably do so as well.

Makes sense, though I doubt I am set up to test IPv6 operation.

#7 lore raw

Hi,

Quoted 9 lines · Jesse Glick
Quoted 6 lines · Josh Cepek
Quoted 3 lines

akin to how HTTP reverse proxies commonly delegate to a physical server according to the HTTP 1.1 Host header???so that many hostnames can map to the same IP of the reverse proxy.

There was some issue figuring out what this all means. Do you have an example use-case where this is helpful?

To expand on my explanation as quoted above:

Thanks for the explanation.

I'm not convinced it makes sense to merge this right now.

Why? HTTP/1.1 with Host: headers is extremely widespread, because there are zillions of "lightweight" web servers that can easily share one physical machine + IP address, because they hardly cause any load etc.

OTOH, I do not really see people sharing multiple OpenVPN services behind one single "reverse proxy" - and if you really need that (multiple OpenVPN services on a single IP address), you could run multiple OpenVPN services on different UDP ports already today (either on the same box, or using some sort of UDP forwarder).

With web browsers, using different ports is a pain. OTOH, with OpenVPN you usually specify the port anyway, it's just part of the .ovpn you ship to your clients.

As you say yourself that there is no such service currently, as the server side proxy has not been written yet, balancing "cool feature" vs. "amount of special cases in the code we have to maintain" makes me tend to "let's postpone this until some real-world experience shows it's really useful".

(OTOH, a patch that is based on Arne's dual-stack work and ensures that SOCKS5 works nicely over IPv4 and IPv6, talking to IPv4 or IPv6 servers via the proxy, that would be more immediately relevant... and with IPv6, you can run your OpenVPN server farm with "one IP address per OpenVPN service" again, so this whole discussion becomes moot anyway :-) )

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...
#8 lore raw
Quoted 2 lines · Gert Doering

I do not really see people sharing multiple OpenVPN services behind one single "reverse proxy"

That was my intent but obviously there will not be such a service if the client cannot support it.

Quoted 3 lines

with OpenVPN
you usually specify the port anyway, it's just part of the .ovpn you ship to your clients.

Of course; the point is that a service provider would rather not allocate a dedicated port or IP for every account when there are potentially thousands of them. You want a permanent *.ovpn that identifies that account by name.

Oh well, I guess I give up on this possibility and will look for other workarounds. Thanks for review.