[Openvpn-devel] multi: call client-disconnect handlers when late client setup fails

Message ID CADJo8AbsiVxntM2bXe+RsjA756MvwiTqw9FLa7d5KT1q0YT3+g@mail.gmail.com
State New
Headers
Series [Openvpn-devel] multi: call client-disconnect handlers when late client setup fails |

Commit Message

Farshid Rezaiyan Nov. 25, 2026, 8:45 p.m. UTC
  multi_client_connect_late_setup() is only reached after all
client-connect handlers (scripts and plugins) have succeeded. It sets
the state to CAS_CONNECT_DONE and then downgrades it to CAS_FAILED if
setting the protocol options, adding the DCO peer or generating the
data channel keys fails.

multi_close_instance() only calls the client-disconnect handlers for
instances in state CAS_CONNECT_DONE, so for such a client the
client-connect handlers have run but client-disconnect never does.
Anything that pairs the two calls gets out of sync. For example, a
RADIUS accounting plugin that sends Accounting-Start on connect and
Accounting-Stop on disconnect keeps the session open forever.

This is easy to hit with DCO on Linux and TCP: if the client closes
the TCP connection while a client-connect handler is running, the
kernel refuses to attach the socket ("provided TCP socket is not in
ESTABLISHED state") and dco_multi_add_new_peer() fails.

Remember in the new did_client_connect flag that a client-connect
plugin or script was called with success. This is the case if one is
defined, because multi_client_connect_late_setup() is only reached
when all client-connect handlers have succeeded.
multi_close_instance() then also calls the disconnect handlers for an
instance in CAS_FAILED that has this flag set. So they run on instance
shutdown and exactly once, as for any other client, and only after a
successful client-connect.

GitHub: closes OpenVPN/openvpn#1155
Signed-off-by: Farshid Rezaiyan <farshidrezaiyan@gmail.com>
---
This is the squashed patch of GitHub PR #1156, as asked there:
https://github.com/OpenVPN/openvpn/pull/1156

The patch made with git format-patch is attached, because the Gmail
web interface wraps long lines. It applies to master (99ee0917).

Tested on Linux 6.8 with DCO (ovpn-backports), TCP and UDP, with
client-connect / client-disconnect scripts and a small test plugin.
The test cases are listed in the PR.

 src/openvpn/multi.c | 14 +++++++++++++-
 src/openvpn/multi.h |  3 +++
 2 files changed, 16 insertions(+), 1 deletion(-)
From 34c1f0ef2bf1ad536131ddd6052ef80f616b65cd Mon Sep 17 00:00:00 2001
From: Farshid Rezaiyan <farshidrezaiyan@gmail.com>
Date: Wed, 25 Nov 2026 23:56:59 +0330
Subject: [PATCH] multi: call client-disconnect handlers when late client setup
 fails

multi_client_connect_late_setup() is only reached after all
client-connect handlers (scripts and plugins) have succeeded. It sets
the state to CAS_CONNECT_DONE and then downgrades it to CAS_FAILED if
setting the protocol options, adding the DCO peer or generating the
data channel keys fails.

multi_close_instance() only calls the client-disconnect handlers for
instances in state CAS_CONNECT_DONE, so for such a client the
client-connect handlers have run but client-disconnect never does.
Anything that pairs the two calls gets out of sync. For example, a
RADIUS accounting plugin that sends Accounting-Start on connect and
Accounting-Stop on disconnect keeps the session open forever.

This is easy to hit with DCO on Linux and TCP: if the client closes
the TCP connection while a client-connect handler is running, the
kernel refuses to attach the socket ("provided TCP socket is not in
ESTABLISHED state") and dco_multi_add_new_peer() fails.

Remember in the new did_client_connect flag that a client-connect
plugin or script was called with success. This is the case if one is
defined, because multi_client_connect_late_setup() is only reached
when all client-connect handlers have succeeded.
multi_close_instance() then also calls the disconnect handlers for an
instance in CAS_FAILED that has this flag set. So they run on instance
shutdown and exactly once, as for any other client, and only after a
successful client-connect.

GitHub: closes OpenVPN/openvpn#1155
Signed-off-by: Farshid Rezaiyan <farshidrezaiyan@gmail.com>
---
 src/openvpn/multi.c | 14 +++++++++++++-
 src/openvpn/multi.h |  3 +++
 2 files changed, 16 insertions(+), 1 deletion(-)
  

Patch

diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c
index 900b5a3b..706195ac 100644
--- a/src/openvpn/multi.c
+++ b/src/openvpn/multi.c
@@ -718,7 +718,10 @@  multi_close_instance(struct multi_context *m, struct multi_instance *mi, bool sh
     set_cc_config(mi, NULL);
 #endif
 
-    if (mi->context.c2.tls_multi->multi_state >= CAS_CONNECT_DONE)
+    /* A client that failed in multi_client_connect_late_setup() is in
+     * CAS_FAILED, but its client-connect handlers did succeed */
+    if (mi->context.c2.tls_multi->multi_state >= CAS_CONNECT_DONE
+        || (mi->did_client_connect && mi->context.c2.tls_multi->multi_state == CAS_FAILED))
     {
         multi_client_disconnect_script(mi);
     }
@@ -2414,6 +2417,15 @@  multi_client_connect_late_setup(struct multi_context *m, struct multi_instance *
     /* set context-level authentication flag */
     mi->context.c2.tls_multi->multi_state = CAS_CONNECT_DONE;
 
+    /* We only get here if all client-connect handlers succeeded, so a
+     * defined client-connect plugin or script has been called with
+     * success. Remember this, as the steps below may still set
+     * CAS_FAILED, and the client-disconnect handlers must then be called
+     * when the instance is closed. */
+    mi->did_client_connect = plugin_defined(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_CONNECT)
+                             || plugin_defined(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_CONNECT_V2)
+                             || mi->context.options.client_connect_script != NULL;
+
     /* Since dco-win maintains iroute routing table (subnet -> peer),
      * peer must be added before iroutes. For other platforms it doesn't matter. */
 
diff --git a/src/openvpn/multi.h b/src/openvpn/multi.h
index 3d2e9321..9cc986c9 100644
--- a/src/openvpn/multi.h
+++ b/src/openvpn/multi.h
@@ -145,6 +145,9 @@  struct multi_instance
     struct buffer_list *cc_config;
 #endif
     bool did_iroutes;
+    /** a client-connect plugin or script was called and succeeded, so
+     * client-disconnect must be called when the instance is closed */
+    bool did_client_connect;
     int n_clients_delta;    /* added to multi_context.n_clients when instance is closed */
 
     struct context context; /**< The context structure storing state