Skip to contentSkip to conversation

--explicit-exit-notify and P2P mode (trac #1337)

Openvpn-devel Superseded Gert Doering 4 participants 11 messages Sep 18, 2022 – Oct 17, 2022

lore
Share & tools
lore
#0 lore raw

Hi,

we have a long standing open ticket, #1337, about --explicit-exit-notify together with peer-to-peer mode.

My understanding of --explicit-exit-notify comes from p2mp mode, where it notifies the other end "I have been SIGINTR'ed, and will go away now", so the other end can stop sending packets (multi-instance is killed on the server, and client reconnects / goes to next server).

I have learned much later that --explicit-exit-notify in p2p mode does something else - it kills the peer. So you press ctrl-c on one end of the OpenVPN connection, and *both* sides exit.

David tells me "it has always been that way", but I wonder if we should keep it (= close #1337, do not change anything), or do something else, like "ignore incoming EEN (control-channel EXIT or OCC_EXIT) if not in server mode" (*)

gert

(*) p2mp server-to-client is not signaled as EXIT/OCC_EXIT but with control-channel restart

(**) https://community.openvpn.net/openvpn/ticket/1337

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#1 lore raw

Am 18.09.2022 um 18:06 schrieb Gert Doering:

Quoted 10 lines

Hi,

we have a long standing open ticket, #1337, about --explicit-exit-notify together with peer-to-peer mode.

My understanding of --explicit-exit-notify comes from p2mp mode, where it notifies the other end "I have been SIGINTR'ed, and will go away now", so the other end can stop sending packets (multi-instance is killed on the server, and client reconnects / goes to next server).

Quoted 8 lines

I have learned much later that --explicit-exit-notify in p2p mode does something else - it kills the peer. So you press ctrl-c on one end of the OpenVPN connection, and *both* sides exit.

David tells me "it has always been that way", but I wonder if we should keep it (= close #1337, do not change anything), or do something else, like "ignore incoming EEN (control-channel EXIT or OCC_EXIT) if not in server mode" (*)

From the implement and the fact that it is a an OCC message (basically the rudimentary predecessor to control channel), I think it is older. I think this feature fits nicely to the weird inetd + openvpn mode that seems to have far to many hacks still left in our code. With inetd, it makes sense that the server instance quits if you press C-c on the client.

In our current state where inetd is no longer supported, this behaviour to exit makes little sense and we should probably change the behaviour to SIGUSR1.

Arne

#2 lore raw

Hi,

Quoted 1 line · Arne Schwabe

In our current state where inetd is no longer supported, this behaviour to exit makes little sense and we should probably change the behaviour to SIGUSR1.

I agree with the above.
Alternatively, we should just ignore in P2P mode, but I think SIGUSR1 is a cleaner handler.

Cheers,

Signature · 1 line
Antonio Quartulli
#3 lore raw
In reply to Arne Schwabe

Hi,

Quoted 3 lines · Arne Schwabe

In our current state where inetd is no longer supported, this behaviour to exit makes little sense and we should probably change the behaviour to SIGUSR1.

What does SIGUSR1 do in p2mp mode on the server end? If it does what SIGTERM does ("terminate this client instance") this sounds like a good way forward :-)

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#4 lore raw

PATCH 1/1 Change explicit exit notification to be a SIGUSR1 instead of a SIGTERM signal

In reply to Gert Doering

From the implemention and the fact that it is a an OCC message (basically the rudimentary predecessor to control channel), this message is very old.

I think in the past this feature fit nicely to the weird inetd + openvpn mode that seems to have far to many hacks still left in our code. With inetd, it made sense that the server instance quits if you press C-c on the client.

In our current state where inetd is no longer supported, this behaviour to exit makes little sense and this patch changes the behaviour to SIGUSR1.

---
 src/openvpn/occ.c  | 2 +-
 src/openvpn/push.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
src/openvpn/occ.c +1 -1
index 1ed0d3771..eb1f2fae7 100644
@@ -431,7 +431,7 @@ process_received_occ_msg(struct context *c)
 
         case OCC_EXIT:
             dmsg(D_PACKET_CONTENT, "RECEIVED OCC_EXIT");
-            c->sig->signal_received = SIGTERM;
+            c->sig->signal_received = SIGUSR1;
             c->sig->signal_text = "remote-exit";
             break;
     }
src/openvpn/push.c +1 -1
index 26259c6b8..da8c456b1 100644
@@ -193,7 +193,7 @@ void
 receive_exit_message(struct context *c)
 {
     dmsg(D_STREAM_ERRORS, "Exit message received by peer");
-    c->sig->signal_received = SIGTERM;
+    c->sig->signal_received = SIGUSR1;
     c->sig->signal_text = "remote-exit";
 #ifdef ENABLE_MANAGEMENT
     if (management)
Signature · 1 line
2.37.0 (Apple Git-136)
#5 lore raw

Re: [Openvpn-devel] [PATCH] Change explicit exit notification to be a SIGUSR1 instead of a SIGTERM signal

Hi,

Quoted 10 lines · Arne Schwabe

From the implemention and the fact that it is a an OCC message (basically the rudimentary predecessor to control channel), this message is very old.

I think in the past this feature fit nicely to the weird inetd + openvpn mode that seems to have far to many hacks still left in our code. With inetd, it made sense that the server instance quits if you press C-c on the client.

In our current state where inetd is no longer supported, this behaviour to exit makes little sense and this patch changes the behaviour to SIGUSR1.

I am very confused about v2 of this patch - and would be happy to ACK v1.

I have applied v1, and tested

- p2p without the patch -> client sends EEN -> server exits

    2022-10-14 16:56:56 us=334517 Exit message received by peer
    2022-10-14 16:56:56 us=334753 TCP/UDP: Closing socket
    2022-10-14 16:56:56 us=334814 Closing TUN/TAP interface
    2022-10-14 16:56:56 us=334836 net_addr_v4_del: 10.204.11.1 dev tun11
    2022-10-14 16:56:56 us=334987 net_addr_v6_del: fd00:abcd:204:11::1/64 dev tun11
    2022-10-14 16:56:56 us=342133 SIGTERM[soft,remote-exit] received, process exiting

- p2p with the v1 patch -> client sends EEN -> server logs, and restarts

    2022-10-14 16:57:30 us=151079 Exit message received by peer
    2022-10-14 16:57:30 us=151254 TCP/UDP: Closing socket
    2022-10-14 16:57:30 us=151316 Closing TUN/TAP interface
    2022-10-14 16:57:30 us=151343 net_addr_v4_del: 10.204.11.1 dev tun11
    2022-10-14 16:57:30 us=151465 net_addr_v6_del: fd00:abcd:204:11::1/64 dev tun11
    2022-10-14 16:57:30 us=155086 SIGUSR1[soft,remote-exit] received, process restarting
    2022-10-14 16:57:30 us=155149 Restart pause, 5 second(s)

(and then it rearms itself and is ready to server another p2p client)

- p2mp server without patch -> client sends EEN -> server logs, and terminates the instance

    2022-10-14 16:58:26 us=544309 cron2-freebsd-tc-amd64/194.97.140.21:17570 Exit message received by peer
    2022-10-14 16:58:26 us=544365 cron2-freebsd-tc-amd64/194.97.140.21:17570 SIGTERM[soft,remote-exit] received, client-instance exiting

- p2mp server with the v1 patch -> client sends EEN -> server logs, and says it will "restart" the instance

    2022-10-14 16:59:02 us=113506 cron2-freebsd-tc-amd64/194.97.140.21:41691 Exit message received by peer
    2022-10-14 16:59:02 us=113573 cron2-freebsd-tc-amd64/194.97.140.21:41691 SIGUSR1[soft,remote-exit] received, client-instance restarting

- with a 2.4 client, it has no "Exit message recived by peer" (because that is the new control channel thing), and just logs

    2022-10-14 17:03:47 us=455929 cron2-freebsd-tc-amd64/194.97.140.21:13152 SIGUSR1[soft,remote-exit] received, client-instance restarting

Trying to understand the code is an interesting maze... that message above comes from sig.c::print_signal(), which is called from "multi::multi_close_instance_on_signal()".

*That* one is called (mostly) from multi_process_post(), which says

" * Also close context on signal."

and basically does

    if (IS_SIG(&mi->context))
    {
        if (flags & MPP_CLOSE_ON_SIGNAL)
        {
            multi_close_instance_on_signal(m, mi);
            ret = false;
        }
    }

... so, the nature of the (soft) signal raised does not seem to play any role to the instance - "if signal, then close instance". Just the logging is a bit off now ("restarting" vs. "exiting").

If we care about logging, I think we should do the signal remap in multi_close_instance_on_signal() (... which already remaps SIGUSR1 by means of remap_signal(), but only if c->options.remap_sigusr1 is set).

OTOH, maybe it should just *not* call remap_signal(), as the per-instance signals and the global signals do different things, and always remap SIGUSR1-in-instance to SIGTERM...

What I do not think will make us happy in future is the extra conditionals in v2...

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#6 lore raw

Re: [Openvpn-devel] [PATCH] Change explicit exit notification to be a SIGUSR1 instead of a SIGTERM signal

Quoted 7 lines

If we care about logging, I think we should do the signal remap in multi_close_instance_on_signal() (... which already remaps SIGUSR1 by means of remap_signal(), but only if c->options.remap_sigusr1 is set).

Quoted 7 lines

OTOH, maybe it should just *not* call remap_signal(), as the per-instance signals and the global signals do different things, and always remap SIGUSR1-in-instance to SIGTERM...

What I do not think will make us happy in future is the extra conditionals in v2...

A completely different effect screwed up my testing and lead to believe that signals are handled different in p2p and p2mp. There is different behaviour in p2p and p2mp but it is triggered by the HMAC cookie approach that only p2mp has. Detailed explaination is in the code in patch v3.

Arne

#7 lore raw

PATCH v2 1/1 Change exit notification in P2P to be a SIGUSR1 instead of a SIGTERM signal

In reply to Gert Doering

From the implemention and the fact that it is a an OCC message (basically the rudimentary predecessor to control channel), this message is very old.

I think in the past this feature fit nicely to the weird inetd + openvpn mode that seems to have far to many hacks still left in our code. With inetd, it made sense that the server instance quits if you press C-c on the client.

In our current state where inetd is no longer supported, this behaviour to exit makes little sense and this patch changes the behaviour to SIGUSR1.

The P2MP mode nees to keep the SIGTERM behaviour to terminate that client session, so we use different signals based on the mode.

Patch v2: use different signals for p2mp and p2p

---
 src/openvpn/occ.c  | 12 ++++++++++--
 src/openvpn/push.c | 11 ++++++++++-
 2 files changed, 20 insertions(+), 3 deletions(-)
src/openvpn/occ.c +10 -2
index 1ed0d3771..75703fc70 100644
@@ -431,8 +431,16 @@ process_received_occ_msg(struct context *c)
 
         case OCC_EXIT:
             dmsg(D_PACKET_CONTENT, "RECEIVED OCC_EXIT");
-            c->sig->signal_received = SIGTERM;
-            c->sig->signal_text = "remote-exit";
+            /* in server mode we want the instance to be terminated. But for a
+             * standalone p2p, SIGTERM would terminate the program */
+            if (c->options.mode == MODE_SERVER)
+            {
+                c->sig->signal_received = SIGTERM;
+            }
+            else
+            {
+                c->sig->signal_received = SIGUSR1;
+            }            c->sig->signal_text = "remote-exit";
             break;
     }
     c->c2.buf.len = 0; /* don't pass packet on */
src/openvpn/push.c +10 -1
index 26259c6b8..ec909c630 100644
@@ -193,7 +193,16 @@ void
 receive_exit_message(struct context *c)
 {
     dmsg(D_STREAM_ERRORS, "Exit message received by peer");
-    c->sig->signal_received = SIGTERM;
+    /* in server mode we want the instance to be terminated. But for a
+     * standalone p2p, SIGTERM would terminate the program */
+    if (c->options.mode == MODE_SERVER)
+    {
+        c->sig->signal_received = SIGTERM;
+    }
+    else
+    {
+        c->sig->signal_received = SIGUSR1;
+    }
     c->sig->signal_text = "remote-exit";
 #ifdef ENABLE_MANAGEMENT
     if (management)
Signature · 1 line
2.37.0 (Apple Git-136)
#8 lore raw

Re: [Openvpn-devel] [PATCH v2] Change exit notification in P2P to be a SIGUSR1 instead of a SIGTERM signal

Quoted 8 lines · Arne Schwabe

From the implemention and the fact that it is a an OCC message (basically the rudimentary predecessor to control channel), this message is very old.

I think in the past this feature fit nicely to the weird inetd + openvpn mode that seems to have far to many hacks still left in our code. With inetd, it made sense that the server instance quits if you press C-c on the client.

Quoted 7 lines

In our current state where inetd is no longer supported, this behaviour to exit makes little sense and this patch changes the behaviour to SIGUSR1.

The P2MP mode nees to keep the SIGTERM behaviour to terminate that client session, so we use different signals based on the mode.

Patch v2: use different signals for p2mp and p2p

IMHO the amount of copied code and logic is big enough to warrant moving it to a shared function.

Regards,

Signature · 1 line
  Frank Lichtenheld
#9 lore raw

PATCH v3 1/1 Change exit signal in P2P to be a SIGUSR1 and delay CC exit in P2MP

In reply to Gert Doering

From the implemention of explicit-notify and the fact that it is a an OCC message (basically the rudimentary predecessor to control channel), this message is very old.

I think in the past this feature fit nicely to the weird inetd + openvpn mode that seems to have far to many hacks still left in our code. With inetd, it made sense that the server instance quits if you press C-c on the client.

In our current state where inetd is no longer supported, this behaviour to exit makes little sense and this patch changes the behaviour to SIGUSR1.

Testing this lead to a confused v2 of the patch and also finally the insight that if a CC channel exit is triggered too early the remaining control channel packets that come after that can trigger the HMAC code to open a sessions again if the whole session lasted less than two minutes (with default settings).

Patch v2: use different signals for p2mp and p2p
Patch v3: use dealyed exit for P2MP/CC exit and USR1 for everything else

---
 src/openvpn/occ.c  |  2 +-
 src/openvpn/push.c | 20 +++++++++++++++++++-
 2 files changed, 20 insertions(+), 2 deletions(-)
src/openvpn/occ.c +1 -1
index 1ed0d3771..eb1f2fae7 100644
@@ -431,7 +431,7 @@ process_received_occ_msg(struct context *c)
 
         case OCC_EXIT:
             dmsg(D_PACKET_CONTENT, "RECEIVED OCC_EXIT");
-            c->sig->signal_received = SIGTERM;
+            c->sig->signal_received = SIGUSR1;
             c->sig->signal_text = "remote-exit";
             break;
     }
src/openvpn/push.c +19 -1
index 26259c6b8..d9fa64870 100644
@@ -193,7 +193,25 @@ void
 receive_exit_message(struct context *c)
 {
     dmsg(D_STREAM_ERRORS, "Exit message received by peer");
-    c->sig->signal_received = SIGTERM;
+    /* With control channel exit notification, we want the session to give
+     * enough time to handle retransmits and acknowledgment, so lat coming
+     * retries from the client to resend the exit or ACKs do not trigger
+     * a new session since we already killed it but the packet still has
+     * a valid HMAC. This can only happen for the period for the HMAC
+     * timeslot is still valid but waiting five seconds here does not
+     * hurt much and is the better alternative.
+     *
+     * This does not affect OCC exit since the HMAC session code will
+     * ignore DATA packets
+     * */
+    if (c->options.mode == MODE_SERVER)
+    {
+        schedule_exit(c, c->options.scheduled_exit_interval, SIGTERM);
+    }
+    else
+    {
+        c->sig->signal_received = SIGUSR1;
+    }
     c->sig->signal_text = "remote-exit";
 #ifdef ENABLE_MANAGEMENT
     if (management)
Signature · 1 line
2.37.0 (Apple Git-136)
author Acked-by on v3 1/1
#10 lore raw

[Openvpn-devel] [PATCH applied] Re: Change exit signal in P2P to be a SIGUSR1 and delay CC exit in P2MP

Tested the whole lot again. Only difference to v1 is in p2mp mode with incoming TLS EEN, which now logs

10:15:34 cron2-freebsd-tc-amd64/194.97.140.21:53341 Exit message received by peer
10:15:34 cron2-freebsd-tc-amd64/194.97.140.21:53341 Delayed exit in 5 seconds
10:15:38 read UDPv6 [ECONNREFUSED]: Connection refused (fd=5,code=111)
10:15:39 us=240199 cron2-freebsd-tc-amd64/194.97.140.21:53341 SIGTERM[soft,delayed-exit] received, client-instance exiting

(the ECONNREFUSED is "the client exited right away, so never saw the control-plane ACK" - calling "--explicit-exit-notify 10" on the client makes those last packets succeed, with no adverse effects on unwanted restarts)

I have worked a bit on the commit message, and a lot on the new comment block - I hope it is now clearer what could happen, and how this change avoids the problem.

I have also added a Changes.rst entry (user-visible changes), pointing out that "--remap-usr1 SIGTERM" will bring back the old behaviour, if someone relies on it.

Your patch has been applied to the master branch.

commit d468dff7bdfd79059818c190ddf41b125bb658de

Author: Arne Schwabe
Date:   Sun Oct 16 17:49:53 2022 +0200
     Change exit signal in P2P to be a SIGUSR1 and delay CC exit in P2MP

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

--
kind regards,

Gert Doering