Attention is currently required from: Arne Schwabe.

Hello Arne Schwabe,

I'd like you to do a code review.
Please visit

    https://gerrit.openvpn.net/c/openvpn/+/2053?usp=email

to review the following change.


Change subject: socket: introduce INDIRECT transport protocol abstraction
......................................................................

socket: introduce INDIRECT transport protocol abstraction

This new transport protocol is used to tell the core code that traffic
should not be directly processed, but should rather be rerouted to a
transport plugin. It is basically an abstraction as it does not say tell
the code how to process the data, but simply forces its redirection to
the external code.

Change-Id: Ibc3194b64f8a69d2877097157311ff5d6cd2911e
Signed-off-by: Robin Tarsiger <[email protected]>
[[email protected]: refactored commits, restyled code]
[[email protected]: rebased, adapted to multisocket]
---
M src/openvpn/forward.c
M src/openvpn/options.c
M src/openvpn/socket.c
M src/openvpn/socket.h
M src/openvpn/socket_util.c
M src/openvpn/socket_util.h
M src/openvpn/transport.h
7 files changed, 220 insertions(+), 7 deletions(-)



  git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/53/2053/1

diff --git a/src/openvpn/forward.c b/src/openvpn/forward.c
index 681210d..0343646 100644
--- a/src/openvpn/forward.c
+++ b/src/openvpn/forward.c
@@ -2217,6 +2217,11 @@
             {
                 int i;
                 c->c2.event_set_status = 0;
+                c->c2.event_set_status |=
+                    (socket_indirect_pump(c->c2.link_sockets[0], esr, &status)
+                     & (EVENT_READ | EVENT_WRITE))
+                    << socket_shift;
+
                 for (i = 0; i < status; ++i)
                 {
                     const struct event_set_return *e = &esr[i];
diff --git a/src/openvpn/options.c b/src/openvpn/options.c
index 8e0c602..912d8e0 100644
--- a/src/openvpn/options.c
+++ b/src/openvpn/options.c
@@ -1828,7 +1828,8 @@
         {
             msg(M_WARN, "--pull-filter ignored for --mode server");
         }
-        if (!(proto_is_udp(ce->proto) || ce->proto == PROTO_TCP_SERVER))
+        if (!(proto_is_udp(ce->proto) || proto_is_indirect(ce->proto)
+              || ce->proto == PROTO_TCP_SERVER))
         {
             msg(M_USAGE, USAGE_VALID_SERVER_PROTOS);
         }
diff --git a/src/openvpn/socket.c b/src/openvpn/socket.c
index 307b5c6..9629cb3 100644
--- a/src/openvpn/socket.c
+++ b/src/openvpn/socket.c
@@ -39,6 +39,7 @@
 #include "openvpn.h"
 #include "forward.h"
 #include "resolve.h"
+#include "transport.h"

 #include "memdbg.h"

@@ -326,6 +327,50 @@
     return sd;
 }

+#ifdef ENABLE_PLUGIN
+
+static void
+create_socket_indirect(struct link_socket *sock, sa_family_t ai_family)
+{
+    struct addrinfo *bind_addresses = NULL;
+    if (sock->bind_local)
+    {
+        bind_addresses = sock->info.lsa->bind_local;
+    }
+
+    sock->indirect = transport_bind(sock->info.plugins,
+                                    sock->info.transport_plugin_argv,
+                                    ai_family,
+                                    bind_addresses);
+}
+
+unsigned
+socket_indirect_pump(struct link_socket *s, struct event_set_return *esr, int 
*esrlen)
+{
+    if (s->indirect)
+    {
+        return transport_update_event(s->indirect, esr, esrlen);
+    }
+    else
+    {
+        return 0;
+    }
+}
+
+#else  /* ifdef ENABLE_PLUGIN */
+
+static void
+create_socket_indirect(struct link_socket *sock, sa_family_t ai_family)
+{
+}
+
+unsigned
+socket_indirect_pump(struct link_socket *s, struct event_set_return *esr, int 
*esrlen)
+{
+    return 0;
+}
+#endif /* ENABLE_PLUGIN */
+
 static void
 create_socket(struct link_socket *sock, struct addrinfo *addr)
 {
@@ -333,6 +378,12 @@
      * of the created socket */
     sock->info.af = (sa_family_t)addr->ai_family;

+    if (proto_is_indirect(sock->info.proto))
+    {
+        create_socket_indirect(sock, sock->info.af);
+        return;
+    }
+
     if (addr->ai_protocol == IPPROTO_UDP || addr->ai_socktype == SOCK_DGRAM)
     {
         /* With a SOCKS proxy the local bind goes on the control socket instead
@@ -1367,7 +1418,11 @@
     }

     /* If socket has not already been created create it now */
-    if (sock->sd == SOCKET_UNDEFINED)
+    if (sock->sd == SOCKET_UNDEFINED
+#ifdef ENABLE_PLUGIN
+        && !sock->indirect
+#endif
+    )
     {
         /* If we have no --remote and have still not figured out the
          * protocol family to use we will use the first of the bind */
@@ -1387,7 +1442,11 @@
     }

     /* Socket still undefined, give a warning and abort connection */
-    if (sock->sd == SOCKET_UNDEFINED)
+    if (sock->sd == SOCKET_UNDEFINED
+#ifdef ENABLE_PLUGIN
+        && !sock->indirect
+#endif
+    )
     {
         msg(M_WARN, "Could not determine IPv4/IPv6 protocol");
         register_signal(sig_info, SIGUSR1, "Could not determine IPv4/IPv6 
protocol");
@@ -1422,7 +1481,10 @@
         goto done;
     }

-    phase2_set_socket_flags(sock);
+    if (sock->sd != SOCKET_UNDEFINED)
+    {
+        phase2_set_socket_flags(sock);
+    }
     linksock_print_addr(sock);

 done:
@@ -1451,6 +1513,14 @@
         const int gremlin = 0;
 #endif

+#ifdef ENABLE_PLUGIN
+        if (sock->indirect)
+        {
+            sock->indirect->vtab->close(sock->indirect);
+            sock->indirect = NULL;
+        }
+#endif
+
         if (socket_defined(sock->sd))
         {
 #ifdef _WIN32
@@ -1966,6 +2036,29 @@
     }
 }

+#ifdef ENABLE_PLUGIN
+
+int
+link_socket_read_indirect(struct link_socket *sock,
+                          struct buffer *buf,
+                          struct link_socket_actual *from)
+{
+    ASSERT(sock->indirect);
+    socklen_t fromlen = sizeof(from->dest.addr);
+    socklen_t expectedlen = (socklen_t)af_addr_size(sock->info.af);
+    addr_zero_host(&from->dest);
+    int len = transport_read(sock->indirect, buf,
+                             &from->dest.addr.sa, &fromlen);
+    if (len >= 0 && expectedlen && fromlen != expectedlen)
+    {
+        bad_address_length(fromlen, expectedlen);
+    }
+
+    return buf->len = len;
+}
+
+#endif /* ENABLE_PLUGIN */
+
 #ifndef _WIN32

 #if ENABLE_IP_PKTINFO
@@ -2114,6 +2207,21 @@
 #endif
 }

+#ifdef ENABLE_PLUGIN
+
+int
+link_socket_write_indirect(struct link_socket *sock,
+                           struct buffer *buf,
+                           struct link_socket_actual *to)
+{
+    ASSERT(sock->indirect);
+    struct sockaddr *addr = (struct sockaddr *)&to->dest.addr.sa;
+    socklen_t addrlen = (socklen_t)af_addr_size(to->dest.addr.sa.sa_family);
+    return transport_write(sock->indirect, buf, addr, addrlen);
+}
+
+#endif /* ENABLE_PLUGIN */
+
 #if ENABLE_IP_PKTINFO

 ssize_t
@@ -2215,6 +2323,12 @@
 int
 socket_recv_queue(struct link_socket *sock, int maxsize)
 {
+    if (proto_is_indirect(sock->info.proto))
+    {
+        /* Indirect handler will take care of this, so do nothing. */
+        return IOSTATE_QUEUED;
+    }
+
     if (sock->reads.iostate == IOSTATE_INITIAL)
     {
         WSABUF wsabuf[1];
@@ -2612,7 +2726,16 @@
         /* if persistent is defined, call event_ctl only if rwflags has 
changed since last call */
         if (!persistent || *persistent != rwflags)
         {
-            event_ctl(es, socket_event_handle(s), rwflags, arg);
+#ifdef ENABLE_PLUGIN
+            if (s->indirect)
+            {
+                transport_request_events(s->indirect, es, rwflags);
+            }
+            else
+#endif
+            {
+                event_ctl(es, socket_event_handle(s), rwflags, arg);
+            }
             if (persistent)
             {
                 *persistent = rwflags;
diff --git a/src/openvpn/socket.h b/src/openvpn/socket.h
index 52e09c9..4119330 100644
--- a/src/openvpn/socket.h
+++ b/src/openvpn/socket.h
@@ -78,6 +78,7 @@
     bool connection_established;
     const char *ipchange_command;
     const struct plugin_list *plugins;
+    const char **transport_plugin_argv;
     bool remote_float;
     uint8_t proto;   /* Protocol (PROTO_x defined below) */
     sa_family_t af;  /* Address family like AF_INET, AF_INET6 or AF_UNSPEC*/
@@ -171,6 +172,11 @@
     struct rw_handle listen_handle; /* For listening on TCP socket in server 
mode */
 #endif

+#ifdef ENABLE_PLUGIN
+    /* only valid when info.proto == PROTO_INDIRECT */
+    openvpn_transport_socket_t indirect;
+#endif
+
     /* used for printing status info only */
     unsigned int rwflags_debug;

@@ -612,10 +618,46 @@

 #endif /* ifdef _WIN32 */

+#ifdef ENABLE_PLUGIN
+
+int link_socket_read_indirect(struct link_socket *sock,
+                              struct buffer *buf,
+                              struct link_socket_actual *from);
+
+int link_socket_write_indirect(struct link_socket *sock,
+                               struct buffer *buf,
+                               struct link_socket_actual *from);
+
+#else  /* ifdef ENABLE_PLUGIN */
+
+static inline int
+link_socket_read_indirect(struct link_socket *sock,
+                          struct buffer *buf, struct link_socket_actual *from)
+{
+    return -1;
+}
+
+static inline int
+link_socket_write_indirect(struct link_socket *sock,
+                           struct buffer *buf, struct link_socket_actual *from)
+{
+    return -1;
+}
+
+#endif /* ENABLE_PLUGIN */
+
+unsigned
+socket_indirect_pump(struct link_socket *s, struct event_set_return *esr, int 
*esrlen);
+
 /* read a TCP or UDP packet from link */
 static inline ssize_t
 link_socket_read(struct link_socket *sock, struct buffer *buf, struct 
link_socket_actual *from)
 {
+    if (proto_is_indirect(sock->info.proto))
+    {
+        return link_socket_read_indirect(sock, buf, from);
+    }
+
     if (proto_is_udp(sock->info.proto) || socket_is_dco_win(sock))
     /* unified UDPv4 and UDPv6, for DCO-WIN the kernel
      * will strip the length header */
@@ -735,6 +777,11 @@
 static inline ssize_t
 link_socket_write(struct link_socket *sock, struct buffer *buf, struct 
link_socket_actual *to)
 {
+    if (proto_is_indirect(sock->info.proto))
+    {
+        return link_socket_write_indirect(sock, buf, to);
+    }
+
     if (proto_is_udp(sock->info.proto) || socket_is_dco_win(sock))
     {
         /* unified UDPv4, UDPv6 and DCO-WIN (driver adds length header) */
@@ -811,7 +858,7 @@
 static inline void
 socket_set_listen_persistent(struct link_socket *sock, struct event_set *es, 
void *arg)
 {
-    if (sock && !sock->listen_persistent_queued)
+    if (sock && !sock->listen_persistent_queued && !sock->indirect)
     {
         event_ctl(es, socket_listen_event_handle(sock), EVENT_READ, arg);
         sock->listen_persistent_queued = true;
diff --git a/src/openvpn/socket_util.c b/src/openvpn/socket_util.c
index 879be73..1230542 100644
--- a/src/openvpn/socket_util.c
+++ b/src/openvpn/socket_util.c
@@ -367,16 +367,25 @@
     { "tcp-server", "TCP_SERVER", AF_UNSPEC, PROTO_TCP_SERVER },
     { "tcp-client", "TCP_CLIENT", AF_UNSPEC, PROTO_TCP_CLIENT },
     { "tcp", "TCP", AF_UNSPEC, PROTO_TCP },
+#ifdef ENABLE_PLUGIN
+    { "indirect", "INDIRECT", AF_UNSPEC, PROTO_INDIRECT },
+#endif
     /* force IPv4 */
     { "udp4", "UDPv4", AF_INET, PROTO_UDP },
     { "tcp4-server", "TCPv4_SERVER", AF_INET, PROTO_TCP_SERVER },
     { "tcp4-client", "TCPv4_CLIENT", AF_INET, PROTO_TCP_CLIENT },
     { "tcp4", "TCPv4", AF_INET, PROTO_TCP },
+#ifdef ENABLE_PLUGIN
+    { "indirect4", "INDIRECT_IPv4", AF_INET, PROTO_INDIRECT },
+#endif
     /* force IPv6 */
     { "udp6", "UDPv6", AF_INET6, PROTO_UDP },
     { "tcp6-server", "TCPv6_SERVER", AF_INET6, PROTO_TCP_SERVER },
     { "tcp6-client", "TCPv6_CLIENT", AF_INET6, PROTO_TCP_CLIENT },
     { "tcp6", "TCPv6", AF_INET6, PROTO_TCP },
+#ifdef ENABLE_PLUGIN
+    { "indirect6", "INDIRECT_IPv6", AF_INET6, PROTO_INDIRECT },
+#endif
 };

 int
@@ -486,6 +495,18 @@
         return "TCPv4_CLIENT";
     }

+#ifdef ENABLE_PLUGIN
+    if (proto == PROTO_INDIRECT)
+    {
+        /* FIXME: the string reported here should match the actual transport
+         * protocol being used, however in this function we have no knowledge 
of
+         * what protocol is exactly being used by the transport-plugin.
+         * Therefore we simply return INDIRECT for now.
+         */
+        return "INDIRECT";
+    }
+#endif
+
     ASSERT(0);
     return ""; /* Make the compiler happy */
 }
diff --git a/src/openvpn/socket_util.h b/src/openvpn/socket_util.h
index 40f9334..72432c5 100644
--- a/src/openvpn/socket_util.h
+++ b/src/openvpn/socket_util.h
@@ -26,6 +26,7 @@
 #include "buffer.h"
 #include "env_set.h"
 #include "sig.h"
+#include "transport.h"

 #define PS_SHOW_PORT_IF_DEFINED (1 << 0)
 #define PS_SHOW_PORT            (1 << 1)
@@ -163,6 +164,16 @@
     return proto == PROTO_UDP;
 }

+static inline bool
+proto_is_indirect(int proto)
+{
+#ifdef ENABLE_PLUGIN
+    return proto == PROTO_INDIRECT;
+#else
+    return false;
+#endif
+}
+
 /**
  * @brief Return if the protocol is datagram (UDP)
  *
@@ -170,7 +181,7 @@
 static inline bool
 proto_is_dgram(int proto)
 {
-    return proto_is_udp(proto);
+    return proto_is_udp(proto) || proto_is_indirect(proto);
 }

 /**
diff --git a/src/openvpn/transport.h b/src/openvpn/transport.h
index 72778c5..05422e6 100644
--- a/src/openvpn/transport.h
+++ b/src/openvpn/transport.h
@@ -27,6 +27,11 @@
 #include "plugin.h"
 #include "openvpn-transport.h"

+/* INDIRECT does not have any overhead per se, but it depends on what is
+ * implemented by the transport plugin
+ */
+/*#define INDIRECT_HEADER_SIZE    0 */
+
 /* Given a list of plugins and an argument list for a desired
  * transport plugin instance, prepare to bind new link sockets using
  * that transport plugin and args. If all succeeds, return true, and:

--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/2053?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ibc3194b64f8a69d2877097157311ff5d6cd2911e
Gerrit-Change-Number: 2053
Gerrit-PatchSet: 1
Gerrit-Owner: Gianmarco De Gregori <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: Arne Schwabe <[email protected]>
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
  • [Openvpn-devel] [M] C... Frank Lichtenheld (Code Review) via Openvpn-devel
    • [Openvpn-devel] ... Gianmarco De Gregori (Code Review) via Openvpn-devel

Reply via email to