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

Message ID 20261126101550.28784-1-gert@greenie.muc.de
State New
Headers
Series [Openvpn-devel,v1] multi: call client-disconnect handlers when late client setup fails |

Commit Message

Gert Doering Nov. 26, 2026, 10:15 a.m. UTC
  From: Farshid Rezaiyan <farshidrezaiyan@gmail.com>

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
Change-Id: Ic667de6f830fa86e2842f1da2820e1250b9d6054
Signed-off-by: Farshid Rezaiyan <farshidrezaiyan@gmail.com>
Acked-by: Ralf Lici <ralf@mandelbit.com>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2350
---

This change was reviewed on Gerrit and approved by at least one
developer. I request to merge it to master.

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2350
This mail reflects revision 1 of this Change.

Acked-by according to Gerrit (reflected above):
Ralf Lici <ralf@mandelbit.com>
  

Patch

diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c
index 900b5a3..706195a 100644
--- a/src/openvpn/multi.c
+++ b/src/openvpn/multi.c
@@ -718,7 +718,10 @@ 
     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 @@ 
     /* 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 3d2e932..9cc986c 100644
--- a/src/openvpn/multi.h
+++ b/src/openvpn/multi.h
@@ -145,6 +145,9 @@ 
     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