Skip to contentSkip to conversation

dco: don't use NetLink to exchange control packets

PATCH Superseded Antonio Quartulli 3 participants 4 messages Mar 8, 2023 – Mar 13, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dco: don't use NetLink to exchange control packets

Using NetLink has proved to be overkill and performance critical. The amount of control traffic can also easily overrun the NetLink buffer when a server has enough clients connected.

Stop using NetLink to send/receive control packets and just use the transport socket as if DCO was not there at all.

Under the hood DCO will redirect control packets to the transport socket without altering them, so that userspace can happily process them as usual.

Change-Id Ia1297c3ae9a28b188ed21ad21ae96fff3d02ee4d
[[email protected]...: ensure win_dco flag is still exposed]
---
 src/openvpn/dco.c            |  12 -----
 src/openvpn/dco.h            |  16 ------
 src/openvpn/dco_freebsd.c    |  10 ----
 src/openvpn/dco_freebsd.h    |   2 -
 src/openvpn/dco_linux.c      | 101 -----------------------------------
 src/openvpn/dco_linux.h      |   2 -
 src/openvpn/dco_win.c        |   8 ---
 src/openvpn/forward.c        |  63 +++-------------------
 src/openvpn/init.c           |   3 +-
 src/openvpn/mtcp.c           |  22 +-------
 src/openvpn/multi.c          |  39 +-------------
 src/openvpn/ovpn_dco_linux.h |  16 +-----
 src/openvpn/socket.c         |   8 +--
 src/openvpn/socket.h         |  22 ++++----
 14 files changed, 29 insertions(+), 295 deletions(-)
src/openvpn/dco.c +0 -12
index b53332a8..308578b4 100644
@@ -485,7 +485,6 @@ dco_p2p_add_new_peer(struct context *c)
     }
 
     c->c2.tls_multi->dco_peer_id = multi->peer_id;
-    c->c2.link_socket->dco_installed = true;
 
     return 0;
 }
@@ -605,17 +604,6 @@ dco_multi_add_new_peer(struct multi_context *m, struct multi_instance *mi)
 
     c->c2.tls_multi->dco_peer_id = peer_id;
 
-    if (c->mode == CM_CHILD_TCP)
-    {
-        multi_tcp_dereference_instance(m->mtcp, mi);
-        if (close(sd))
-        {
-            msg(D_DCO|M_ERRNO, "error closing TCP socket after DCO handover");
-        }
-        c->c2.link_socket->dco_installed = true;
-        c->c2.link_socket->sd = SOCKET_UNDEFINED;
-    }
-
     return 0;
 }
 
src/openvpn/dco.h +0 -16
index 18a9d78b..2fe671bf 100644
@@ -127,15 +127,6 @@ void close_tun_dco(struct tuntap *tt, openvpn_net_ctx_t *ctx);
  */
 int dco_do_read(dco_context_t *dco);
 
-/**
- * Write data to the DCO communication channel (control packet expected)
- *
- * @param dco       the DCO context
- * @param peer_id   the ID of the peer to send the data to
- * @param buf       the buffer containing the data to send
- */
-int dco_do_write(dco_context_t *dco, int peer_id, struct buffer *buf);
-
 /**
  * Install a DCO in the main event loop
  */
@@ -301,13 +292,6 @@ dco_do_read(dco_context_t *dco)
     return 0;
 }
 
-static inline int
-dco_do_write(dco_context_t *dco, int peer_id, struct buffer *buf)
-{
-    ASSERT(false);
-    return 0;
-}
-
 static inline void
 dco_event_set(dco_context_t *dco, struct event_set *es, void *arg)
 {
src/openvpn/dco_freebsd.c +0 -10
index cd4083c4..92de5f04 100644
@@ -142,7 +142,6 @@ open_fd(dco_context_t *dco)
     {
         dco->open = true;
     }
-    dco->dco_packet_in = alloc_buf(PAGE_SIZE);
 
     return dco->fd;
 }
@@ -560,15 +559,6 @@ dco_do_read(dco_context_t *dco)
     return 0;
 }
 
-int
-dco_do_write(dco_context_t *dco, int peer_id, struct buffer *buf)
-{
-    /* Control packets are passed through the socket, so this should never get
-     * called. See should_use_dco_socket(). */
-    ASSERT(0);
-    return -EINVAL;
-}
-
 bool
 dco_available(int msglevel)
 {
src/openvpn/dco_freebsd.h +0 -2
index 970beca0..a07f9b69 100644
@@ -51,8 +51,6 @@ typedef struct dco_context {
 
     char ifname[IFNAMSIZ];
 
-    struct buffer dco_packet_in;
-
     int dco_message_type;
     int dco_message_peer_id;
     int dco_del_peer_reason;
src/openvpn/dco_linux.c +0 -101

131 lines not on this page · raw message

src/openvpn/dco_linux.h +0 -2
index 4d996d98..d28e3658 100644
@@ -48,8 +48,6 @@ typedef struct
 
     unsigned int ifindex;
 
-    struct buffer dco_packet_in;
-
     int dco_message_type;
     int dco_message_peer_id;
     int dco_del_peer_reason;
src/openvpn/dco_win.c +0 -8
index 0931fb30..a805c2a0 100644
@@ -393,14 +393,6 @@ dco_do_read(dco_context_t *dco)
     return 0;
 }
 
-int
-dco_do_write(dco_context_t *dco, int peer_id, struct buffer *buf)
-{
-    /* no-op on windows */
-    ASSERT(0);
-    return 0;
-}
-
 int
 dco_get_peer_stats_multi(dco_context_t *dco, struct multi_context *m)
 {
src/openvpn/forward.c +8 -55

97 lines not on this page · raw message

src/openvpn/init.c +1 -2
index e67b93d3..124ac76b 100644
@@ -3914,8 +3914,7 @@ do_close_link_socket(struct context *c)
     /* in dco-win case, link socket is a tun handle which is
      * closed in do_close_tun(). Set it to UNDEFINED so
      * we won't use WinSock API to close it. */
-    if (tuntap_is_dco_win(c->c1.tuntap) && c->c2.link_socket
-        && c->c2.link_socket->dco_installed)
+    if (tuntap_is_dco_win(c->c1.tuntap) && c->c2.link_socket)
     {
         c->c2.link_socket->sd = SOCKET_UNDEFINED;
     }
src/openvpn/mtcp.c +2 -20
index 59131ac9..6c56a260 100644
@@ -402,18 +402,6 @@ multi_tcp_wait_lite(struct multi_context *m, struct multi_instance *mi, const in
 
     tv_clear(&c->c2.timeval); /* ZERO-TIMEOUT */
 
-    if (mi && mi->context.c2.link_socket->dco_installed)
-    {
-        /* If we got a socket that has been handed over to the kernel
-         * we must not call the normal socket function to figure out
-         * if it is readable or writable */
-        /* Assert that we only have the DCO exptected flags */
-        ASSERT(action & (TA_SOCKET_READ | TA_SOCKET_WRITE));
-
-        /* We are always ready! */
-        return action;
-    }
-
     switch (action)
     {
         case TA_TUN_READ:
@@ -537,10 +525,7 @@ multi_tcp_dispatch(struct multi_context *m, struct multi_instance *mi, const int
 
         case TA_INITIAL:
             ASSERT(mi);
-            if (!mi->context.c2.link_socket->dco_installed)
-            {
-                multi_tcp_set_global_rw_flags(m, mi);
-            }
+            multi_tcp_set_global_rw_flags(m, mi);
             multi_process_post(m, mi, mpp_flags);
             break;
 
@@ -590,10 +575,7 @@ multi_tcp_post(struct multi_context *m, struct multi_instance *mi, const int act
             }
             else
             {
-                if (!c->c2.link_socket->dco_installed)
-                {
-                    multi_tcp_set_global_rw_flags(m, mi);
-                }
+                multi_tcp_set_global_rw_flags(m, mi);
             }
             break;
 
src/openvpn/multi.c +1 -38
index 59c980b0..53c17b3a 100644
@@ -3202,37 +3202,6 @@ multi_signal_instance(struct multi_context *m, struct multi_instance *mi, const
 #endif
 
 #if defined(ENABLE_DCO) && (defined(TARGET_LINUX) || defined(TARGET_FREEBSD))
-static void
-process_incoming_dco_packet(struct multi_context *m, struct multi_instance *mi,
-                            dco_context_t *dco)
-{
-    if (BLEN(&dco->dco_packet_in) < 1)
-    {
-        msg(D_DCO, "Received too short packet for peer %d",
-            dco->dco_message_peer_id);
-        goto done;
-    }
-
-    uint8_t *ptr = BPTR(&dco->dco_packet_in);
-    uint8_t op = ptr[0] >> P_OPCODE_SHIFT;
-    if ((op == P_DATA_V1) || (op == P_DATA_V2))
-    {
-        msg(D_DCO, "DCO: received data channel packet for peer %d",
-            dco->dco_message_peer_id);
-        goto done;
-    }
-
-    struct buffer orig_buf = mi->context.c2.buf;
-    mi->context.c2.buf = dco->dco_packet_in;
-
-    multi_process_incoming_link(m, mi, 0);
-
-    mi->context.c2.buf = orig_buf;
-
-done:
-    buf_init(&dco->dco_packet_in, 0);
-}
-
 static void
 process_incoming_del_peer(struct multi_context *m, struct multi_instance *mi,
                           dco_context_t *dco)
@@ -3299,11 +3268,7 @@ multi_process_incoming_dco(struct multi_context *m)
     if ((peer_id < m->max_clients) && (m->instances[peer_id]))
     {
         mi = m->instances[peer_id];
-        if (dco->dco_message_type == OVPN_CMD_PACKET)
-        {
-            process_incoming_dco_packet(m, mi, dco);
-        }
-        else if (dco->dco_message_type == OVPN_CMD_DEL_PEER)
+        if (dco->dco_message_type == OVPN_CMD_DEL_PEER)
         {
             process_incoming_del_peer(m, mi, dco);
         }
@@ -3326,8 +3291,6 @@ multi_process_incoming_dco(struct multi_context *m)
         msg(msglevel, "Received DCO message for unknown peer-id: %d, "
             "type %d, del_peer_reason %d", peer_id, dco->dco_message_type,
             dco->dco_del_peer_reason);
-        /* Also clear the buffer if this was incoming packet for a dropped peer */
-        buf_init(&dco->dco_packet_in, 0);
     }
 
     dco->dco_message_type = 0;
src/openvpn/ovpn_dco_linux.h +1 -15
index 96395886..d3fd9a89 100644
@@ -11,7 +11,7 @@
 #ifndef _UAPI_LINUX_OVPN_DCO_H_
 #define _UAPI_LINUX_OVPN_DCO_H_
 
-#define OVPN_NL_NAME "ovpn-dco"
+#define OVPN_NL_NAME "ovpn-dco-v2"
 
 #define OVPN_NL_MULTICAST_GROUP_PEERS "peers"
 
@@ -45,19 +45,6 @@ enum ovpn_nl_commands {
 
 	OVPN_CMD_DEL_KEY,
 
-	/**
-	 * @OVPN_CMD_REGISTER_PACKET: Register for specific packet types to be
-	 * forwarded to userspace
-	 */
-	OVPN_CMD_REGISTER_PACKET,
-
-	/**
-	 * @OVPN_CMD_PACKET: Send a packet from userspace to kernelspace. Also
-	 * used to send to userspace packets for which a process had registered
-	 * with OVPN_CMD_REGISTER_PACKET
-	 */
-	OVPN_CMD_PACKET,
-
 	/**
 	 * @OVPN_CMD_GET_PEER: Retrieve the status of a peer or all peers
 	 */
@@ -105,7 +92,6 @@ enum ovpn_netlink_attrs {
 	OVPN_ATTR_NEW_KEY,
 	OVPN_ATTR_SWAP_KEYS,
 	OVPN_ATTR_DEL_KEY,
-	OVPN_ATTR_PACKET,
 	OVPN_ATTR_GET_PEER,
 
 	__OVPN_ATTR_AFTER_LAST,
src/openvpn/socket.c +4 -4
index eff21ca5..216f2ad7 100644
@@ -2151,7 +2151,7 @@ create_socket_dco_win(struct context *c, struct link_socket *sock,
                       get_server_poll_remaining_time(sock->server_poll_timeout),
                       sig_info);
 
-    sock->dco_installed = true;
+    sock->sockflags |= SF_DCO_WIN;
 
     if (sig_info->signal_received)
     {
@@ -3505,7 +3505,7 @@ link_socket_write_udp_posix_sendmsg(struct link_socket *sock,
 static int
 socket_get_last_error(const struct link_socket *sock)
 {
-    if (sock->dco_installed)
+    if (socket_is_dco_win(sock))
     {
         return GetLastError();
     }
@@ -3546,7 +3546,7 @@ socket_recv_queue(struct link_socket *sock, int maxsize)
         ASSERT(ResetEvent(sock->reads.overlapped.hEvent));
         sock->reads.flags = 0;
 
-        if (sock->dco_installed)
+        if (socket_is_dco_win(sock))
         {
             status = ReadFile((HANDLE)sock->sd, wsabuf[0].buf, wsabuf[0].len,
                               &sock->reads.size, &sock->reads.overlapped);
@@ -3651,7 +3651,7 @@ socket_send_queue(struct link_socket *sock, struct buffer *buf, const struct lin
         ASSERT(ResetEvent(sock->writes.overlapped.hEvent));
         sock->writes.flags = 0;
 
-        if (sock->dco_installed)
+        if (socket_is_dco_win(sock))
         {
             status = WriteFile((HANDLE)sock->sd, wsabuf[0].buf, wsabuf[0].len,
                                &sock->writes.size, &sock->writes.overlapped);
src/openvpn/socket.h +12 -10
index 605b6ad2..cc963a1c 100644
@@ -168,7 +168,6 @@ struct link_socket
 
     socket_descriptor_t sd;
     socket_descriptor_t ctrl_sd; /* only used for UDP over Socks */
-    bool dco_installed;
 
 #ifdef _WIN32
     struct overlapped_io reads;
@@ -207,6 +206,7 @@ struct link_socket
 #define SF_PORT_SHARE (1<<2)
 #define SF_HOST_RANDOMIZE (1<<3)
 #define SF_GETADDRINFO_DGRAM (1<<4)
+#define SF_DCO_WIN (1<<5)
     unsigned int sockflags;
     int mark;
     const char *bind_dev;
@@ -1021,6 +1021,12 @@ stream_buf_read_setup(struct link_socket *sock)
     }
 }
 
+static inline bool
+socket_is_dco_win(const struct link_socket *s)
+{
+    return s->sockflags & SF_DCO_WIN;
+}
+
 /*
  * Socket Read Routines
  */
@@ -1036,7 +1042,7 @@ link_socket_read_udp_win32(struct link_socket *sock,
                            struct link_socket_actual *from)
 {
     sockethandle_t sh = { .s = sock->sd };
-    if (sock->dco_installed)
+    if (socket_is_dco_win(sock))
     {
         *from = sock->info.lsa->actual;
         sh.is_handle = true;
@@ -1058,11 +1064,7 @@ link_socket_read(struct link_socket *sock,
                  struct buffer *buf,
                  struct link_socket_actual *from)
 {
-#ifdef _WIN32
-    if (proto_is_udp(sock->info.proto) || sock->dco_installed)
-#else
-    if (proto_is_udp(sock->info.proto))
-#endif
+    if (proto_is_udp(sock->info.proto) || socket_is_dco_win(sock))
     /* unified UDPv4 and UDPv6, for DCO the kernel
      * will strip the length header */
     {
@@ -1105,7 +1107,7 @@ link_socket_write_win32(struct link_socket *sock,
 {
     int err = 0;
     int status = 0;
-    sockethandle_t sh = { .s = sock->sd, .is_handle = sock->dco_installed };
+    sockethandle_t sh = { .s = sock->sd, .is_handle = socket_is_dco_win(sock) };
     if (overlapped_io_active(&sock->writes))
     {
         status = sockethandle_finalize(sh, &sock->writes, NULL, NULL);
@@ -1179,9 +1181,9 @@ link_socket_write(struct link_socket *sock,
                   struct buffer *buf,
                   struct link_socket_actual *to)
 {
-    if (proto_is_udp(sock->info.proto) || sock->dco_installed)
+    if (proto_is_udp(sock->info.proto) || socket_is_dco_win(sock))
     {
-        /* unified UDPv4 and UDPv6 and DCO (kernel adds size header) */
+        /* unified UDPv4 and UDPv6 */
         return link_socket_write_udp(sock, buf, to);
     }
     else if (proto_is_tcp(sock->info.proto)) /* unified TCPv4 and TCPv6 */
Signature · 1 line
2.39.2
#1 lore raw

PATCH v2 1/1 dco: don't use NetLink to exchange control packets

Using NetLink for control messages did not work out as it did lead to kernel side buffer congestion during heavy client activity.

With this patch DCO will redirect control packets directly to the transport socket without altering them, so that userspace can happily process them as usual.

Change-Id Ia1297c3ae9a28b188ed21ad21ae96fff3d02ee4d
[[email protected]...: ensure win_dco flag is still exposed]

---
Changes from v1:
* improved comments
* improved commit message

This patch was also reviewed and approved on gerrit at:
https://gerrit.openvpn.net/c/openvpn/+/28

 src/openvpn/dco.c            |  12 -----
 src/openvpn/dco.h            |  16 ------
 src/openvpn/dco_freebsd.c    |  10 ----
 src/openvpn/dco_freebsd.h    |   2 -
 src/openvpn/dco_linux.c      | 101 -----------------------------------
 src/openvpn/dco_linux.h      |   2 -
 src/openvpn/dco_win.c        |   8 ---
 src/openvpn/forward.c        |  63 +++-------------------
 src/openvpn/init.c           |   3 +-
 src/openvpn/mtcp.c           |  22 +-------
 src/openvpn/multi.c          |  39 +-------------
 src/openvpn/ovpn_dco_linux.h |  16 +-----
 src/openvpn/socket.c         |   8 +--
 src/openvpn/socket.h         |  29 ++++++----
 14 files changed, 35 insertions(+), 296 deletions(-)
src/openvpn/dco.c +0 -12
index b53332a8..308578b4 100644
@@ -485,7 +485,6 @@ dco_p2p_add_new_peer(struct context *c)
     }
 
     c->c2.tls_multi->dco_peer_id = multi->peer_id;
-    c->c2.link_socket->dco_installed = true;
 
     return 0;
 }
@@ -605,17 +604,6 @@ dco_multi_add_new_peer(struct multi_context *m, struct multi_instance *mi)
 
     c->c2.tls_multi->dco_peer_id = peer_id;
 
-    if (c->mode == CM_CHILD_TCP)
-    {
-        multi_tcp_dereference_instance(m->mtcp, mi);
-        if (close(sd))
-        {
-            msg(D_DCO|M_ERRNO, "error closing TCP socket after DCO handover");
-        }
-        c->c2.link_socket->dco_installed = true;
-        c->c2.link_socket->sd = SOCKET_UNDEFINED;
-    }
-
     return 0;
 }
 
src/openvpn/dco.h +0 -16
index 18a9d78b..2fe671bf 100644
@@ -127,15 +127,6 @@ void close_tun_dco(struct tuntap *tt, openvpn_net_ctx_t *ctx);
  */
 int dco_do_read(dco_context_t *dco);
 
-/**
- * Write data to the DCO communication channel (control packet expected)
- *
- * @param dco       the DCO context
- * @param peer_id   the ID of the peer to send the data to
- * @param buf       the buffer containing the data to send
- */
-int dco_do_write(dco_context_t *dco, int peer_id, struct buffer *buf);
-
 /**
  * Install a DCO in the main event loop
  */
@@ -301,13 +292,6 @@ dco_do_read(dco_context_t *dco)
     return 0;
 }
 
-static inline int
-dco_do_write(dco_context_t *dco, int peer_id, struct buffer *buf)
-{
-    ASSERT(false);
-    return 0;
-}
-
 static inline void
 dco_event_set(dco_context_t *dco, struct event_set *es, void *arg)
 {
src/openvpn/dco_freebsd.c +0 -10
index cd4083c4..92de5f04 100644
@@ -142,7 +142,6 @@ open_fd(dco_context_t *dco)
     {
         dco->open = true;
     }
-    dco->dco_packet_in = alloc_buf(PAGE_SIZE);
 
     return dco->fd;
 }
@@ -560,15 +559,6 @@ dco_do_read(dco_context_t *dco)
     return 0;
 }
 
-int
-dco_do_write(dco_context_t *dco, int peer_id, struct buffer *buf)
-{
-    /* Control packets are passed through the socket, so this should never get
-     * called. See should_use_dco_socket(). */
-    ASSERT(0);
-    return -EINVAL;
-}
-
 bool
 dco_available(int msglevel)
 {
src/openvpn/dco_freebsd.h +0 -2
index 970beca0..a07f9b69 100644
@@ -51,8 +51,6 @@ typedef struct dco_context {
 
     char ifname[IFNAMSIZ];
 
-    struct buffer dco_packet_in;
-
     int dco_message_type;
     int dco_message_peer_id;
     int dco_del_peer_reason;
src/openvpn/dco_linux.c +0 -101

131 lines not on this page · raw message

src/openvpn/dco_linux.h +0 -2
index 4d996d98..d28e3658 100644
@@ -48,8 +48,6 @@ typedef struct
 
     unsigned int ifindex;
 
-    struct buffer dco_packet_in;
-
     int dco_message_type;
     int dco_message_peer_id;
     int dco_del_peer_reason;
src/openvpn/dco_win.c +0 -8
index 0931fb30..a805c2a0 100644
@@ -393,14 +393,6 @@ dco_do_read(dco_context_t *dco)
     return 0;
 }
 
-int
-dco_do_write(dco_context_t *dco, int peer_id, struct buffer *buf)
-{
-    /* no-op on windows */
-    ASSERT(0);
-    return 0;
-}
-
 int
 dco_get_peer_stats_multi(dco_context_t *dco, struct multi_context *m)
 {
src/openvpn/forward.c +8 -55

97 lines not on this page · raw message

src/openvpn/init.c +1 -2
index e67b93d3..124ac76b 100644
@@ -3914,8 +3914,7 @@ do_close_link_socket(struct context *c)
     /* in dco-win case, link socket is a tun handle which is
      * closed in do_close_tun(). Set it to UNDEFINED so
      * we won't use WinSock API to close it. */
-    if (tuntap_is_dco_win(c->c1.tuntap) && c->c2.link_socket
-        && c->c2.link_socket->dco_installed)
+    if (tuntap_is_dco_win(c->c1.tuntap) && c->c2.link_socket)
     {
         c->c2.link_socket->sd = SOCKET_UNDEFINED;
     }
src/openvpn/mtcp.c +2 -20
index 59131ac9..6c56a260 100644
@@ -402,18 +402,6 @@ multi_tcp_wait_lite(struct multi_context *m, struct multi_instance *mi, const in
 
     tv_clear(&c->c2.timeval); /* ZERO-TIMEOUT */
 
-    if (mi && mi->context.c2.link_socket->dco_installed)
-    {
-        /* If we got a socket that has been handed over to the kernel
-         * we must not call the normal socket function to figure out
-         * if it is readable or writable */
-        /* Assert that we only have the DCO exptected flags */
-        ASSERT(action & (TA_SOCKET_READ | TA_SOCKET_WRITE));
-
-        /* We are always ready! */
-        return action;
-    }
-
     switch (action)
     {
         case TA_TUN_READ:
@@ -537,10 +525,7 @@ multi_tcp_dispatch(struct multi_context *m, struct multi_instance *mi, const int
 
         case TA_INITIAL:
             ASSERT(mi);
-            if (!mi->context.c2.link_socket->dco_installed)
-            {
-                multi_tcp_set_global_rw_flags(m, mi);
-            }
+            multi_tcp_set_global_rw_flags(m, mi);
             multi_process_post(m, mi, mpp_flags);
             break;
 
@@ -590,10 +575,7 @@ multi_tcp_post(struct multi_context *m, struct multi_instance *mi, const int act
             }
             else
             {
-                if (!c->c2.link_socket->dco_installed)
-                {
-                    multi_tcp_set_global_rw_flags(m, mi);
-                }
+                multi_tcp_set_global_rw_flags(m, mi);
             }
             break;
 
src/openvpn/multi.c +1 -38
index 59c980b0..53c17b3a 100644
@@ -3202,37 +3202,6 @@ multi_signal_instance(struct multi_context *m, struct multi_instance *mi, const
 #endif
 
 #if defined(ENABLE_DCO) && (defined(TARGET_LINUX) || defined(TARGET_FREEBSD))
-static void
-process_incoming_dco_packet(struct multi_context *m, struct multi_instance *mi,
-                            dco_context_t *dco)
-{
-    if (BLEN(&dco->dco_packet_in) < 1)
-    {
-        msg(D_DCO, "Received too short packet for peer %d",
-            dco->dco_message_peer_id);
-        goto done;
-    }
-
-    uint8_t *ptr = BPTR(&dco->dco_packet_in);
-    uint8_t op = ptr[0] >> P_OPCODE_SHIFT;
-    if ((op == P_DATA_V1) || (op == P_DATA_V2))
-    {
-        msg(D_DCO, "DCO: received data channel packet for peer %d",
-            dco->dco_message_peer_id);
-        goto done;
-    }
-
-    struct buffer orig_buf = mi->context.c2.buf;
-    mi->context.c2.buf = dco->dco_packet_in;
-
-    multi_process_incoming_link(m, mi, 0);
-
-    mi->context.c2.buf = orig_buf;
-
-done:
-    buf_init(&dco->dco_packet_in, 0);
-}
-
 static void
 process_incoming_del_peer(struct multi_context *m, struct multi_instance *mi,
                           dco_context_t *dco)
@@ -3299,11 +3268,7 @@ multi_process_incoming_dco(struct multi_context *m)
     if ((peer_id < m->max_clients) && (m->instances[peer_id]))
     {
         mi = m->instances[peer_id];
-        if (dco->dco_message_type == OVPN_CMD_PACKET)
-        {
-            process_incoming_dco_packet(m, mi, dco);
-        }
-        else if (dco->dco_message_type == OVPN_CMD_DEL_PEER)
+        if (dco->dco_message_type == OVPN_CMD_DEL_PEER)
         {
             process_incoming_del_peer(m, mi, dco);
         }
@@ -3326,8 +3291,6 @@ multi_process_incoming_dco(struct multi_context *m)
         msg(msglevel, "Received DCO message for unknown peer-id: %d, "
             "type %d, del_peer_reason %d", peer_id, dco->dco_message_type,
             dco->dco_del_peer_reason);
-        /* Also clear the buffer if this was incoming packet for a dropped peer */
-        buf_init(&dco->dco_packet_in, 0);
     }
 
     dco->dco_message_type = 0;
src/openvpn/ovpn_dco_linux.h +1 -15
index 96395886..d3fd9a89 100644
@@ -11,7 +11,7 @@
 #ifndef _UAPI_LINUX_OVPN_DCO_H_
 #define _UAPI_LINUX_OVPN_DCO_H_
 
-#define OVPN_NL_NAME "ovpn-dco"
+#define OVPN_NL_NAME "ovpn-dco-v2"
 
 #define OVPN_NL_MULTICAST_GROUP_PEERS "peers"
 
@@ -45,19 +45,6 @@ enum ovpn_nl_commands {
 
 	OVPN_CMD_DEL_KEY,
 
-	/**
-	 * @OVPN_CMD_REGISTER_PACKET: Register for specific packet types to be
-	 * forwarded to userspace
-	 */
-	OVPN_CMD_REGISTER_PACKET,
-
-	/**
-	 * @OVPN_CMD_PACKET: Send a packet from userspace to kernelspace. Also
-	 * used to send to userspace packets for which a process had registered
-	 * with OVPN_CMD_REGISTER_PACKET
-	 */
-	OVPN_CMD_PACKET,
-
 	/**
 	 * @OVPN_CMD_GET_PEER: Retrieve the status of a peer or all peers
 	 */
@@ -105,7 +92,6 @@ enum ovpn_netlink_attrs {
 	OVPN_ATTR_NEW_KEY,
 	OVPN_ATTR_SWAP_KEYS,
 	OVPN_ATTR_DEL_KEY,
-	OVPN_ATTR_PACKET,
 	OVPN_ATTR_GET_PEER,
 
 	__OVPN_ATTR_AFTER_LAST,
src/openvpn/socket.c +4 -4
index eff21ca5..216f2ad7 100644
@@ -2151,7 +2151,7 @@ create_socket_dco_win(struct context *c, struct link_socket *sock,
                       get_server_poll_remaining_time(sock->server_poll_timeout),
                       sig_info);
 
-    sock->dco_installed = true;
+    sock->sockflags |= SF_DCO_WIN;
 
     if (sig_info->signal_received)
     {
@@ -3505,7 +3505,7 @@ link_socket_write_udp_posix_sendmsg(struct link_socket *sock,
 static int
 socket_get_last_error(const struct link_socket *sock)
 {
-    if (sock->dco_installed)
+    if (socket_is_dco_win(sock))
     {
         return GetLastError();
     }
@@ -3546,7 +3546,7 @@ socket_recv_queue(struct link_socket *sock, int maxsize)
         ASSERT(ResetEvent(sock->reads.overlapped.hEvent));
         sock->reads.flags = 0;
 
-        if (sock->dco_installed)
+        if (socket_is_dco_win(sock))
         {
             status = ReadFile((HANDLE)sock->sd, wsabuf[0].buf, wsabuf[0].len,
                               &sock->reads.size, &sock->reads.overlapped);
@@ -3651,7 +3651,7 @@ socket_send_queue(struct link_socket *sock, struct buffer *buf, const struct lin
         ASSERT(ResetEvent(sock->writes.overlapped.hEvent));
         sock->writes.flags = 0;
 
-        if (sock->dco_installed)
+        if (socket_is_dco_win(sock))
         {
             status = WriteFile((HANDLE)sock->sd, wsabuf[0].buf, wsabuf[0].len,
                                &sock->writes.size, &sock->writes.overlapped);
src/openvpn/socket.h +18 -11
index 605b6ad2..bfc1253b 100644
@@ -168,7 +168,6 @@ struct link_socket
 
     socket_descriptor_t sd;
     socket_descriptor_t ctrl_sd; /* only used for UDP over Socks */
-    bool dco_installed;
 
 #ifdef _WIN32
     struct overlapped_io reads;
@@ -207,6 +206,7 @@ struct link_socket
 #define SF_PORT_SHARE (1<<2)
 #define SF_HOST_RANDOMIZE (1<<3)
 #define SF_GETADDRINFO_DGRAM (1<<4)
+#define SF_DCO_WIN (1<<5)
     unsigned int sockflags;
     int mark;
     const char *bind_dev;
@@ -1021,6 +1021,17 @@ stream_buf_read_setup(struct link_socket *sock)
     }
 }
 
+/**
+ * Returns true if we are on Windows and this link is running on DCO-WIN.
+ * This helper is used to enable DCO-WIN specific logic that is not relevant
+ * to other platforms.
+ */
+static inline bool
+socket_is_dco_win(const struct link_socket *s)
+{
+    return s->sockflags & SF_DCO_WIN;
+}
+
 /*
  * Socket Read Routines
  */
@@ -1036,7 +1047,7 @@ link_socket_read_udp_win32(struct link_socket *sock,
                            struct link_socket_actual *from)
 {
     sockethandle_t sh = { .s = sock->sd };
-    if (sock->dco_installed)
+    if (socket_is_dco_win(sock))
     {
         *from = sock->info.lsa->actual;
         sh.is_handle = true;
@@ -1058,12 +1069,8 @@ link_socket_read(struct link_socket *sock,
                  struct buffer *buf,
                  struct link_socket_actual *from)
 {
-#ifdef _WIN32
-    if (proto_is_udp(sock->info.proto) || sock->dco_installed)
-#else
-    if (proto_is_udp(sock->info.proto))
-#endif
-    /* unified UDPv4 and UDPv6, for DCO the kernel
+    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 */
     {
         int res;
@@ -1105,7 +1112,7 @@ link_socket_write_win32(struct link_socket *sock,
 {
     int err = 0;
     int status = 0;
-    sockethandle_t sh = { .s = sock->sd, .is_handle = sock->dco_installed };
+    sockethandle_t sh = { .s = sock->sd, .is_handle = socket_is_dco_win(sock) };
     if (overlapped_io_active(&sock->writes))
     {
         status = sockethandle_finalize(sh, &sock->writes, NULL, NULL);
@@ -1179,9 +1186,9 @@ link_socket_write(struct link_socket *sock,
                   struct buffer *buf,
                   struct link_socket_actual *to)
 {
-    if (proto_is_udp(sock->info.proto) || sock->dco_installed)
+    if (proto_is_udp(sock->info.proto) || socket_is_dco_win(sock))
     {
-        /* unified UDPv4 and UDPv6 and DCO (kernel adds size header) */
+        /* unified UDPv4, UDPv6 and DCO-WIN (driver adds length header) */
         return link_socket_write_udp(sock, buf, to);
     }
     else if (proto_is_tcp(sock->info.proto)) /* unified TCPv4 and TCPv6 */
Signature · 1 line
2.39.2
Acked-by on v2 1/1
#2 lore raw

Am 09.03.2023 um 22:03 schrieb Antonio Quartulli:

Quoted 6 lines

Using NetLink for control messages did not work out as it did lead to kernel side buffer congestion during heavy client activity.

With this patch DCO will redirect control packets directly to the transport socket without altering them, so that userspace can happily process them as usual.

Patch looks good and does what it promises.

I ran this through the torture test on Ubuntu 22.04 and Bookworm (Debian 13) and saw no issues.

#3 lore raw
In reply to Antonio Quartulli

I have tested this on ubuntu 20.04 against the kernel DCO (v2) module from "next", commit 580608e. All client and server side tests pass (with and without DCO), and it survived UDP and TCP gremlin attacks for ~1h each. This is better than we ever had, so yay :-)

UDP gremlins still seems to make it lose track of a few clients (kernel thinks "nothing left" and sends no more keepalive, userland claims "6 clients left") - this is still not perfect, but no *real* issue - if one of the clients reconnects, the session will be flushed, and if key renegotiation comes up, it will eventually be expired.

Stare-at-code also looks good. Taking out lots of stuff that was just complicating things. I also like that this obsoletes quite a bit of extra comments that we fought about quite a while to make them understandable :-) - plus the #ifdef _WIN32 from my bandaid patch...

I have also compile-tested this for Windows (MinGW), not actually runtime-tested it - but the logic wrt "dco_installed" -> "SF_DCO_WIN" is clear enough (one could argue endlessly on "flag" or "bool", but it does the same thing).

Uncrustify complains about ovpn_dco_linux.h, but since this is a "foreign import" I ignored it (as previously).

I have added a note to the commit message that this is an API breaking change and needs a newer kernel module (as agreed on IRC).

Your patch has been applied to the master and release/2.6 branch.

commit ac1d24286ad4788415ce6f56e97c18562d1cadbd (master) commit 321b04fac8aaaad254fe884472109042d8fb83d7 (release/2.6)

Author: Antonio Quartulli
Date:   Thu Mar 9 22:03:44 2023 +0100
     dco: don't use NetLink to exchange control packets

     Signed-off-by: Antonio Quartulli <[email protected]...>
     Acked-by: Arne Schwabe <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg26384.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering