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/+/2226?usp=email

to review the following change.


Change subject: Keep MPTCP subflows out of the tunnel
......................................................................

Keep MPTCP subflows out of the tunnel

With --mptcp, a subflow towards an address announced by the server
(MPTCP ADD_ADDR) follows the routing table like any other connection.
When that address is covered by routes into the tunnel, e.g. with
--redirect-gateway, which only excludes the --remote address, the
subflow is routed into the tunnel it belongs to. The server drops its
packets because of their source address, so whenever MPTCP schedules
data on that subflow, the whole connection stalls until the subflow's
retransmission timeout expires, and the timeout doubles every time.

Such subflows are established right after the connection, while with
--pull the routes are added only at the end of the handshake. When the
route lists are initialized, the peers reported by MPTCP_SUBFLOW_ADDRS
are routed like the --remote address, so the subflows never enter the
tunnel. Addresses announced after that are not covered.

Change-Id: Iebd0a97029c53e210641adecd754e31444c5b398
Signed-off-by: Gianmarco De Gregori <[email protected]>
---
M CMakeLists.txt
M config.h.cmake.in
M configure.ac
M doc/man-sections/link-options.rst
M src/openvpn/init.c
M src/openvpn/route.c
M src/openvpn/route.h
M tests/unit_tests/openvpn/test_route.c
8 files changed, 301 insertions(+), 29 deletions(-)



  git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/26/2226/1

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 50b2d68..f57b678 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -222,6 +222,7 @@
 if (HAVE_SYS_EPOLL_H)
     check_symbol_exists(epoll_create sys/epoll.h HAVE_EPOLL_CREATE)
 endif()
+check_include_files(linux/mptcp.h HAVE_LINUX_MPTCP_H)
 check_include_files(syslog.h HAVE_SYSLOG_H)
 if (HAVE_SYSLOG_H)
     check_symbol_exists(openlog syslog.h HAVE_OPENLOG)
diff --git a/config.h.cmake.in b/config.h.cmake.in
index f7818cf..45e0da9 100644
--- a/config.h.cmake.in
+++ b/config.h.cmake.in
@@ -285,6 +285,9 @@
 /* Define to 1 if you have the <sys/epoll.h> header file. */
 #cmakedefine HAVE_SYS_EPOLL_H

+/* Define to 1 if you have the <linux/mptcp.h> header file. */
+#cmakedefine HAVE_LINUX_MPTCP_H
+
 /* Define to 1 if you have the <sys/inotify.h> header file. */
 #cmakedefine HAVE_SYS_INOTIFY_H

diff --git a/configure.ac b/configure.ac
index 585c3bd..239ba3d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -458,6 +458,7 @@
        syslog.h pwd.h grp.h \
        sys/sockio.h sys/uio.h \
        poll.h sys/epoll.h err.h \
+       linux/mptcp.h \
 ])

 SOCKET_INCLUDES="
diff --git a/doc/man-sections/link-options.rst 
b/doc/man-sections/link-options.rst
index ffc8fdd..a8fd8ab 100644
--- a/doc/man-sections/link-options.rst
+++ b/doc/man-sections/link-options.rst
@@ -156,6 +156,12 @@
   kernel (``ip mptcp endpoint``); without them the connection uses a
   single subflow.

+  With ``--pull`` (implied by ``--client``), addresses announced by the
+  server (MPTCP ADD_ADDR) before the routes are added are routed like the
+  ``--remote`` address. An address announced later has to be excluded from
+  the tunnel manually, e.g. with
+  ``--route address 255.255.255.255 net_gateway``.
+
   This option is only supported on Linux. If an MPTCP socket cannot be
   created, OpenVPN logs a warning and falls back to plain TCP. ``--mptcp``
   disables data channel offload, and ``--mtu-disc`` has no effect on MPTCP
diff --git a/src/openvpn/init.c b/src/openvpn/init.c
index e1052c5..bed78ed 100644
--- a/src/openvpn/init.c
+++ b/src/openvpn/init.c
@@ -58,6 +58,10 @@
 #include "resolve.h"
 #include "acc.h"

+#if defined(TARGET_LINUX) && defined(HAVE_LINUX_MPTCP_H)
+#include <linux/mptcp.h>
+#endif
+
 #include "memdbg.h"


@@ -1698,6 +1702,61 @@
     c->c1.tuntap_owned = true;
 }

+#if defined(TARGET_LINUX) && !defined(TARGET_ANDROID) && 
defined(MPTCP_SUBFLOW_ADDRS)
+static void
+do_init_mptcp_subflow_routes(struct context *c)
+{
+    const struct link_socket *sock = c->c2.link_sockets[0];
+    if (!(sock->sockflags & SF_MPTCP))
+    {
+        return;
+    }
+
+    struct mptcp_subflow_data hdr = {
+        .size_subflow_data = sizeof(hdr),
+        .size_user = sizeof(struct mptcp_subflow_addrs),
+    };
+    socklen_t len = sizeof(hdr);
+    if (getsockopt(sock->sd, SOL_MPTCP, MPTCP_SUBFLOW_ADDRS, &hdr, &len) != 0
+        || hdr.size_user != sizeof(struct mptcp_subflow_addrs) || 
hdr.num_subflows == 0)
+    {
+        return;
+    }
+
+    struct
+    {
+        struct mptcp_subflow_data d;
+        struct mptcp_subflow_addrs a[];
+    } *sf;
+    struct gc_arena gc = gc_new();
+    const size_t size = sizeof(*sf) + hdr.num_subflows * sizeof(sf->a[0]);
+    sf = gc_malloc(size, true, &gc);
+    sf->d.size_subflow_data = sizeof(sf->d);
+    sf->d.size_user = sizeof(sf->a[0]);
+    len = (socklen_t)size;
+    if (getsockopt(sock->sd, SOL_MPTCP, MPTCP_SUBFLOW_ADDRS, sf, &len) == 0
+        && len >= sizeof(sf->d))
+    {
+        const size_t n = (len - sizeof(sf->d)) / sizeof(sf->a[0]);
+        for (size_t i = 0; i < n; i++)
+        {
+            const struct mptcp_subflow_addrs *a = &sf->a[i];
+            if (a->sin_remote.sin_family == AF_INET && c->c1.route_list)
+            {
+                route_list_add_extra_remote(c->c1.route_list,
+                                            
ntohl(a->sin_remote.sin_addr.s_addr));
+            }
+            else if (a->sin6_remote.sin6_family == AF_INET6 && 
c->c1.route_ipv6_list)
+            {
+                route_ipv6_list_add_extra_remote(c->c1.route_ipv6_list,
+                                                 &a->sin6_remote.sin6_addr);
+            }
+        }
+    }
+    gc_free(&gc);
+}
+#endif
+
 /*
  * Open tun/tap device, ifconfig, call up script, etc.
  */
@@ -1854,6 +1913,9 @@
             do_init_route_ipv6_list(&c->options, c->c1.route_ipv6_list,
                                     &c->c2.link_sockets[0]->info, c->c2.es, 
&c->net_ctx);
         }
+#if defined(TARGET_LINUX) && !defined(TARGET_ANDROID) && 
defined(MPTCP_SUBFLOW_ADDRS)
+        do_init_mptcp_subflow_routes(c);
+#endif

         /* do ifconfig */
         if (!ifconfig_noexec_enabled(c) && ifconfig_order(c->c1.tuntap) == 
IFCONFIG_BEFORE_TUN_OPEN)
diff --git a/src/openvpn/route.c b/src/openvpn/route.c
index 9086529..bdea368 100644
--- a/src/openvpn/route.c
+++ b/src/openvpn/route.c
@@ -741,6 +741,37 @@
     return false;
 }

+static bool
+add_remote_host_route_ipv6(struct route_ipv6_list *rl6, const struct in6_addr 
*host)
+{
+    if ((rl6->rgi6.flags & (RGI_ADDR_DEFINED | RGI_IFACE_DEFINED))
+        != (RGI_ADDR_DEFINED | RGI_IFACE_DEFINED))
+    {
+        return false;
+    }
+
+    struct route_ipv6 *r6;
+    ALLOC_OBJ_CLEAR_GC(r6, struct route_ipv6, &rl6->gc);
+
+    r6->network = *host;
+    r6->netbits = 128;
+    if (!(rl6->rgi6.flags & RGI_ON_LINK))
+    {
+        r6->gateway = rl6->rgi6.gateway.addr_ipv6;
+    }
+    r6->metric = 1;
+#ifdef _WIN32
+    r6->adapter_index = rl6->rgi6.adapter_index;
+#else
+    r6->iface = rl6->rgi6.iface;
+#endif
+    r6->flags = RT_DEFINED | RT_METRIC_DEFINED;
+
+    r6->next = rl6->routes_ipv6;
+    rl6->routes_ipv6 = r6;
+    return true;
+}
+
 bool
 init_route_ipv6_list(struct route_ipv6_list *rl6, const struct 
route_ipv6_option_list *opt6,
                      const char *remote_endpoint, int default_metric,
@@ -839,36 +870,10 @@
     }

     /* add VPN server host route if needed */
-    if (need_remote_ipv6_route)
+    if (need_remote_ipv6_route && !add_remote_host_route_ipv6(rl6, 
remote_host_ipv6))
     {
-        if ((rl6->rgi6.flags & (RGI_ADDR_DEFINED | RGI_IFACE_DEFINED))
-            == (RGI_ADDR_DEFINED | RGI_IFACE_DEFINED))
-        {
-            struct route_ipv6 *r6;
-            ALLOC_OBJ_CLEAR_GC(r6, struct route_ipv6, &rl6->gc);
-
-            r6->network = *remote_host_ipv6;
-            r6->netbits = 128;
-            if (!(rl6->rgi6.flags & RGI_ON_LINK))
-            {
-                r6->gateway = rl6->rgi6.gateway.addr_ipv6;
-            }
-            r6->metric = 1;
-#ifdef _WIN32
-            r6->adapter_index = rl6->rgi6.adapter_index;
-#else
-            r6->iface = rl6->rgi6.iface;
-#endif
-            r6->flags = RT_DEFINED | RT_METRIC_DEFINED;
-
-            r6->next = rl6->routes_ipv6;
-            rl6->routes_ipv6 = r6;
-        }
-        else
-        {
-            msg(M_WARN,
-                "ROUTE6: IPv6 route overlaps with IPv6 remote address, but 
could not determine IPv6 gateway address + interface, expect failure\n");
-        }
+        msg(M_WARN,
+            "ROUTE6: IPv6 route overlaps with IPv6 remote address, but could 
not determine IPv6 gateway address + interface, expect failure\n");
     }

     gc_free(&gc);
@@ -939,6 +944,65 @@
     }
 }

+#if defined(TARGET_LINUX) && !defined(TARGET_ANDROID)
+void
+route_list_add_extra_remote(struct route_list *rl, in_addr_t host)
+{
+    if (!(rl->flags & RG_ENABLE)
+        || ((rl->spec.flags & RTSA_REMOTE_HOST) && host == 
rl->spec.remote_host)
+        || test_local_addr(host, &rl->rgi) == TLA_LOCAL)
+    {
+        return;
+    }
+
+    struct gc_arena gc = gc_new();
+    if (add_bypass_address(&rl->spec.bypass, host))
+    {
+        msg(M_INFO, "ROUTE: %s is routed like the remote host", 
print_in_addr_t(host, 0, &gc));
+    }
+    else
+    {
+        msg(M_WARN, "ROUTE: no room for bypass route to %s", 
print_in_addr_t(host, 0, &gc));
+    }
+    gc_free(&gc);
+}
+
+void
+route_ipv6_list_add_extra_remote(struct route_ipv6_list *rl6, const struct 
in6_addr *host)
+{
+    if ((rl6->spec_flags & RTSA_REMOTE_HOST) && IN6_ARE_ADDR_EQUAL(host, 
&rl6->remote_host_ipv6))
+    {
+        return;
+    }
+
+    bool overlap = false;
+    for (const struct route_ipv6 *r6 = rl6->routes_ipv6; r6; r6 = r6->next)
+    {
+        if (r6->netbits == 128 && IN6_ARE_ADDR_EQUAL(&r6->network, host))
+        {
+            return;
+        }
+        overlap = overlap || ipv6_net_contains_host(&r6->network, r6->netbits, 
host);
+    }
+    if (!overlap)
+    {
+        return;
+    }
+
+    struct gc_arena gc = gc_new();
+    if (add_remote_host_route_ipv6(rl6, host))
+    {
+        msg(M_INFO, "ROUTE6: %s is routed like the remote host", 
print_in6_addr(*host, 0, &gc));
+    }
+    else
+    {
+        msg(M_WARN, "ROUTE6: could not determine IPv6 gateway address + 
interface for %s",
+            print_in6_addr(*host, 0, &gc));
+    }
+    gc_free(&gc);
+}
+#endif /* if defined(TARGET_LINUX) && !defined(TARGET_ANDROID) */
+
 static bool
 redirect_default_route_to_vpn(struct route_list *rl, const struct tuntap *tt, 
unsigned int flags,
                               const struct env_set *es, openvpn_net_ctx_t *ctx)
diff --git a/src/openvpn/route.h b/src/openvpn/route.h
index 0d33d36..4e585c8 100644
--- a/src/openvpn/route.h
+++ b/src/openvpn/route.h
@@ -311,6 +311,19 @@
                const struct route_gateway_info *rgi, const struct env_set *es,
                openvpn_net_ctx_t *ctx);

+#if defined(TARGET_LINUX) && !defined(TARGET_ANDROID)
+/**
+ * Route @p host like the remote host: via the original default gateway when
+ * it would otherwise be routed into the tunnel.
+ */
+void route_list_add_extra_remote(struct route_list *rl, in_addr_t host);
+
+/**
+ * IPv6 counterpart of route_list_add_extra_remote().
+ */
+void route_ipv6_list_add_extra_remote(struct route_ipv6_list *rl6, const 
struct in6_addr *host);
+#endif
+
 void add_route_to_option_list(struct route_option_list *l, const char 
*network, const char *netmask,
                               const char *gateway, const char *metric, int 
table_id);

diff --git a/tests/unit_tests/openvpn/test_route.c 
b/tests/unit_tests/openvpn/test_route.c
index 3cfee9b..8a0ebea 100644
--- a/tests/unit_tests/openvpn/test_route.c
+++ b/tests/unit_tests/openvpn/test_route.c
@@ -166,11 +166,133 @@
     assert_clear_host_bits("2001:db8::1", 0, "::");
 }

+#if defined(TARGET_LINUX) && !defined(TARGET_ANDROID)
+static in_addr_t
+ip4(const char *s)
+{
+    in_addr_t a;
+    assert_int_equal(inet_pton(AF_INET, s, &a), 1);
+    return ntohl(a);
+}
+
+static void
+test_route_list_add_extra_remote(void **state)
+{
+    struct route_list rl;
+    CLEAR(rl);
+    rl.flags = RG_ENABLE;
+    rl.spec.flags = RTSA_REMOTE_HOST;
+    rl.spec.remote_host = ip4("192.0.2.1");
+    rl.rgi.flags = RGI_ADDR_DEFINED | RGI_NETMASK_DEFINED | RGI_IFACE_DEFINED;
+    rl.rgi.gateway.addr = ip4("10.0.1.1");
+    rl.rgi.gateway.netmask = ip4("255.255.255.0");
+
+    /* the remote host and on-link addresses are skipped */
+    route_list_add_extra_remote(&rl, ip4("192.0.2.1"));
+    route_list_add_extra_remote(&rl, ip4("10.0.1.7"));
+    assert_int_equal(rl.spec.bypass.n_bypass, 0);
+
+    /* added once */
+    route_list_add_extra_remote(&rl, ip4("198.51.100.1"));
+    route_list_add_extra_remote(&rl, ip4("198.51.100.1"));
+    assert_int_equal(rl.spec.bypass.n_bypass, 1);
+    assert_int_equal(rl.spec.bypass.bypass[0], ip4("198.51.100.1"));
+
+    /* full bypass list */
+    for (in_addr_t a = ip4("198.51.100.2"); rl.spec.bypass.n_bypass < 
N_ROUTE_BYPASS; a++)
+    {
+        route_list_add_extra_remote(&rl, a);
+    }
+    route_list_add_extra_remote(&rl, ip4("198.51.100.100"));
+    assert_int_equal(rl.spec.bypass.n_bypass, N_ROUTE_BYPASS);
+    assert_int_not_equal(rl.spec.bypass.bypass[N_ROUTE_BYPASS - 1], 
ip4("198.51.100.100"));
+
+    /* nothing without --redirect-gateway */
+    CLEAR(rl.spec.bypass);
+    rl.flags = 0;
+    route_list_add_extra_remote(&rl, ip4("198.51.100.1"));
+    assert_int_equal(rl.spec.bypass.n_bypass, 0);
+}
+
+static void
+add_test_route_ipv6(struct route_ipv6_list *rl6, const char *network, unsigned 
int netbits)
+{
+    struct route_ipv6 *r6;
+    ALLOC_OBJ_CLEAR_GC(r6, struct route_ipv6, &rl6->gc);
+    assert_int_equal(inet_pton(AF_INET6, network, &r6->network), 1);
+    r6->netbits = netbits;
+    r6->next = rl6->routes_ipv6;
+    rl6->routes_ipv6 = r6;
+}
+
+static int
+count_routes_ipv6(const struct route_ipv6_list *rl6)
+{
+    int n = 0;
+    for (const struct route_ipv6 *r6 = rl6->routes_ipv6; r6; r6 = r6->next)
+    {
+        n++;
+    }
+    return n;
+}
+
+static void
+test_route_ipv6_list_add_extra_remote(void **state)
+{
+    struct route_ipv6_list rl6;
+    struct in6_addr host, gateway;
+
+    CLEAR(rl6);
+    rl6.gc = gc_new();
+    rl6.spec_flags = RTSA_REMOTE_HOST;
+    assert_int_equal(inet_pton(AF_INET6, "2001:db8:1::1", 
&rl6.remote_host_ipv6), 1);
+    rl6.rgi6.flags = RGI_ADDR_DEFINED | RGI_IFACE_DEFINED;
+    assert_int_equal(inet_pton(AF_INET6, "fd00:1::1", &gateway), 1);
+    rl6.rgi6.gateway.addr_ipv6 = gateway;
+    strlcpy(rl6.rgi6.iface, "eth0", sizeof(rl6.rgi6.iface));
+    add_test_route_ipv6(&rl6, "2000::", 3);
+
+    /* the remote host is skipped */
+    route_ipv6_list_add_extra_remote(&rl6, &rl6.remote_host_ipv6);
+    assert_int_equal(count_routes_ipv6(&rl6), 1);
+
+    /* not routed into the tunnel */
+    assert_int_equal(inet_pton(AF_INET6, "fd00:2::1", &host), 1);
+    route_ipv6_list_add_extra_remote(&rl6, &host);
+    assert_int_equal(count_routes_ipv6(&rl6), 1);
+
+    /* added once, as a host route via the original gateway */
+    assert_int_equal(inet_pton(AF_INET6, "2001:db8:2::1", &host), 1);
+    route_ipv6_list_add_extra_remote(&rl6, &host);
+    route_ipv6_list_add_extra_remote(&rl6, &host);
+    assert_int_equal(count_routes_ipv6(&rl6), 2);
+
+    const struct route_ipv6 *r6 = rl6.routes_ipv6;
+    assert_memory_equal(&r6->network, &host, sizeof(host));
+    assert_int_equal(r6->netbits, 128);
+    assert_memory_equal(&r6->gateway, &gateway, sizeof(gateway));
+    assert_string_equal(r6->iface, "eth0");
+    assert_int_equal(r6->flags, RT_DEFINED | RT_METRIC_DEFINED);
+
+    /* nothing without the original gateway */
+    rl6.rgi6.flags = 0;
+    assert_int_equal(inet_pton(AF_INET6, "2001:db8:3::1", &host), 1);
+    route_ipv6_list_add_extra_remote(&rl6, &host);
+    assert_int_equal(count_routes_ipv6(&rl6), 2);
+
+    gc_free(&rl6.gc);
+}
+#endif
+
 static const struct CMUnitTest route_test_list[] = {
     cmocka_unit_test(test_is_special_addr),
     cmocka_unit_test(test_netmask_to_netbits),
     cmocka_unit_test(test_netmask_to_netbits2),
     cmocka_unit_test(test_route_ipv6_clear_host_bits),
+#if defined(TARGET_LINUX) && !defined(TARGET_ANDROID)
+    cmocka_unit_test(test_route_list_add_extra_remote),
+    cmocka_unit_test(test_route_ipv6_list_add_extra_remote),
+#endif
 };

 int

--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/2226?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: Iebd0a97029c53e210641adecd754e31444c5b398
Gerrit-Change-Number: 2226
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] [L] C... Gianmarco De Gregori (Code Review) via Openvpn-devel

Reply via email to