From: "cron2 (Code Review)" Date: Sat, 8 Mar 2025 08:39:16 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, ralf@...2726..., frank@...2641... Message-ID: <476087e71d118f21a2d6b0a02f0f7af6d297876e-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="QcAZEQb376c="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: Handle missing DCO peer by restarting the session List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: ralf_lici , plaisthos , flichtenheld Cc: openvpn-devel --QcAZEQb376c= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has uploaded a new patch set (#4) to the change originally created by= ralf_lici. ( http://gerrit.openvpn.net/c/openvpn/+/831?usp=3Demail ) The following approvals got outdated and were removed: Code-Review+2 by flichtenheld Change subject: Handle missing DCO peer by restarting the session ...................................................................... Handle missing DCO peer by restarting the session Occasionally, CMD_DEL_PEER is not delivered to userspace, preventing the openvpn process from registering the event. To handle this case, we check if calls to the Linux DCO module return an error, and, if so, send a SIGUSR1 signal to reset the session. Most DCO commands that return an error already trigger a SIGUSR1 signal or even call _exit(1). This commit extends that behavior to include dco_get_peer_stats_multi() and dco_get_peer_stats(). Change-Id: Ib118426c5a69256894040c69856a4003d9f4637c Signed-off-by: Ralf Lici Acked-by: Frank Lichtenheld Message-Id: <20250305171730.250444-1-frank@...2641...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg31= 022.html Signed-off-by: Gert Doering --- M src/openvpn/dco.h M src/openvpn/dco_freebsd.c M src/openvpn/dco_linux.c M src/openvpn/dco_win.c M src/openvpn/forward.c M src/openvpn/manage.c M src/openvpn/multi.c M src/openvpn/sig.c 8 files changed, 63 insertions(+), 21 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/31/831/4 diff --git a/src/openvpn/dco.h b/src/openvpn/dco.h index 35ceace..ed194cc 100644 --- a/src/openvpn/dco.h +++ b/src/openvpn/dco.h @@ -231,17 +231,20 @@ /** * Update traffic statistics for all peers * - * @param dco DCO device context - * @param m the server context + * @param dco DCO device context + * @param m the server context + * @param raise_sigusr1_on_err whether to raise SIGUSR1 on error **/ -int dco_get_peer_stats_multi(dco_context_t *dco, struct multi_context *m); +int dco_get_peer_stats_multi(dco_context_t *dco, struct multi_context *m, + const bool raise_sigusr1_on_err); /** * Update traffic statistics for single peer * - * @param c instance context of the peer + * @param c instance context of the peer + * @param raise_sigusr1_on_err whether to raise SIGUSR1 on error **/ -int dco_get_peer_stats(struct context *c); +int dco_get_peer_stats(struct context *c, const bool raise_sigusr1_on_err); /** * Retrieve the list of ciphers supported by the current platform @@ -373,13 +376,14 @@ } static inline int -dco_get_peer_stats_multi(dco_context_t *dco, struct multi_context *m) +dco_get_peer_stats_multi(dco_context_t *dco, struct multi_context *m, + const bool raise_sigusr1_on_err) { return 0; } static inline int -dco_get_peer_stats(struct context *c) +dco_get_peer_stats(struct context *c, const bool raise_sigusr1_on_err) { return 0; } diff --git a/src/openvpn/dco_freebsd.c b/src/openvpn/dco_freebsd.c index 0e536de..b8816c6 100644 --- a/src/openvpn/dco_freebsd.c +++ b/src/openvpn/dco_freebsd.c @@ -713,7 +713,8 @@ } int -dco_get_peer_stats_multi(dco_context_t *dco, struct multi_context *m) +dco_get_peer_stats_multi(dco_context_t *dco, struct multi_context *m, + const bool raise_sigusr1_on_err) { struct ifdrv drv; @@ -781,7 +782,7 @@ } int -dco_get_peer_stats(struct context *c) +dco_get_peer_stats(struct context *c, const bool raise_sigusr1_on_err) { /* Not implemented. */ return 0; diff --git a/src/openvpn/dco_linux.c b/src/openvpn/dco_linux.c index 68c1a8d..b0a85fd 100644 --- a/src/openvpn/dco_linux.c +++ b/src/openvpn/dco_linux.c @@ -952,7 +952,8 @@ } int -dco_get_peer_stats_multi(dco_context_t *dco, struct multi_context *m) +dco_get_peer_stats_multi(dco_context_t *dco, struct multi_context *m, + const bool raise_sigusr1_on_err) { msg(D_DCO_DEBUG, "%s", __func__); @@ -963,6 +964,14 @@ int ret =3D ovpn_nl_msg_send(dco, nl_msg, dco_parse_peer_multi, m, __f= unc__); nlmsg_free(nl_msg); + + if (raise_sigusr1_on_err && ret < 0) + { + msg(M_WARN, "Error retrieving DCO peer stats: the underlying DCO p= eer" + "may have been deleted from the kernel without notifying " + "userspace. Restarting the session"); + register_signal(m->top.sig, SIGUSR1, "dco peer stats error"); + } return ret; } @@ -1008,9 +1017,14 @@ } int -dco_get_peer_stats(struct context *c) +dco_get_peer_stats(struct context *c, const bool raise_sigusr1_on_err) { - uint32_t peer_id =3D c->c2.tls_multi->dco_peer_id; + int peer_id =3D c->c2.tls_multi->dco_peer_id; + if (peer_id =3D=3D -1) + { + return 0; + } + msg(D_DCO_DEBUG, "%s: peer-id %d", __func__, peer_id); if (!c->c1.tuntap) @@ -1030,6 +1044,14 @@ nla_put_failure: nlmsg_free(nl_msg); + + if (raise_sigusr1_on_err && ret < 0) + { + msg(M_WARN, "Error retrieving DCO peer stats: the underlying DCO p= eer" + "may have been deleted from the kernel without notifying " + "userspace. Restarting the session"); + register_signal(c->sig, SIGUSR1, "dco peer stats error"); + } return ret; } diff --git a/src/openvpn/dco_win.c b/src/openvpn/dco_win.c index 45cb919..8b47124 100644 --- a/src/openvpn/dco_win.c +++ b/src/openvpn/dco_win.c @@ -712,14 +712,15 @@ } int -dco_get_peer_stats_multi(dco_context_t *dco, struct multi_context *m) +dco_get_peer_stats_multi(dco_context_t *dco, struct multi_context *m, + const bool raise_sigusr1_on_err) { /* Not implemented. */ return 0; } int -dco_get_peer_stats(struct context *c) +dco_get_peer_stats(struct context *c, const bool raise_sigusr1_on_err) { struct tuntap *tt =3D c->c1.tuntap; diff --git a/src/openvpn/forward.c b/src/openvpn/forward.c index b025344..fce7ac8 100644 --- a/src/openvpn/forward.c +++ b/src/openvpn/forward.c @@ -488,7 +488,7 @@ static void check_inactivity_timeout(struct context *c) { - if (dco_enabled(&c->options) && dco_get_peer_stats(c) =3D=3D 0) + if (dco_enabled(&c->options) && dco_get_peer_stats(c, true) =3D=3D 0) { int64_t tot_bytes =3D c->c2.tun_read_bytes + c->c2.tun_write_bytes; int64_t new_bytes =3D tot_bytes - c->c2.inactivity_bytes; @@ -497,7 +497,6 @@ { c->c2.inactivity_bytes =3D tot_bytes; event_timeout_reset(&c->c2.inactivity_interval); - return; } } diff --git a/src/openvpn/manage.c b/src/openvpn/manage.c index 484042a..0e73942 100644 --- a/src/openvpn/manage.c +++ b/src/openvpn/manage.c @@ -4146,8 +4146,13 @@ counter_type dco_read_bytes =3D 0; counter_type dco_write_bytes =3D 0; - if (dco_enabled(&c->options) && (dco_get_peer_stats(c) =3D=3D 0)) + if (dco_enabled(&c->options)) { + if (dco_get_peer_stats(c, true) < 0) + { + return; + } + dco_read_bytes =3D c->c2.dco_read_bytes; dco_write_bytes =3D c->c2.dco_write_bytes; } @@ -4166,7 +4171,8 @@ void man_persist_client_stats(struct management *man, struct context *c) { - if (dco_enabled(&c->options) && (dco_get_peer_stats(c) =3D=3D 0)) + /* no need to raise SIGUSR1 since we are already closing the instance = */ + if (dco_enabled(&c->options) && (dco_get_peer_stats(c, false) =3D=3D 0= )) { management_bytes_client(man, c->c2.dco_read_bytes, c->c2.dco_write= _bytes); } diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index 85a1712..9d244be 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -548,7 +548,10 @@ { if (dco_enabled(&m->top.options)) { - dco_get_peer_stats_multi(&m->top.c1.tuntap->dco, m); + if (dco_get_peer_stats_multi(&m->top.c1.tuntap->dco, m, false) < 0) + { + return; + } } setenv_counter(c->c2.es, "bytes_received", c->c2.link_read_bytes + c->= c2.dco_read_bytes); @@ -856,7 +859,10 @@ if (dco_enabled(&m->top.options)) { - dco_get_peer_stats_multi(&m->top.c1.tuntap->dco, m); + if (dco_get_peer_stats_multi(&m->top.c1.tuntap->dco, m, true) = < 0) + { + return; + } } if (version =3D=3D 1) diff --git a/src/openvpn/sig.c b/src/openvpn/sig.c index 8323f0d..b0f8935 100644 --- a/src/openvpn/sig.c +++ b/src/openvpn/sig.c @@ -489,7 +489,10 @@ if (dco_enabled(&c->options)) { - dco_get_peer_stats(c); + if (dco_get_peer_stats(c, true) < 0) + { + return; + } } status_printf(so, "OpenVPN STATISTICS"); -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/831?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: Ib118426c5a69256894040c69856a4003d9f4637c Gerrit-Change-Number: 831 Gerrit-PatchSet: 4 Gerrit-Owner: ralf_lici Gerrit-Reviewer: cron2 Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-MessageType: newpatchset --QcAZEQb376c= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 uploaded patch set #4 to the change originally cr= eated by ralf_lici.

View Change

The following approvals got outdated= and were removed: Code-Review+2 by flichtenheld

Handle missing DCO peer by restarting the ses=
sion

Occasionally, CMD_DEL_PEER is not delivered to userspace, preve= nting the
openvpn process from registering the event. To handle this cas= e, we
check if calls to the Linux DCO module return an error, and, if so= , send
a SIGUSR1 signal to reset the session.

Most DCO commands t= hat return an error already trigger a SIGUSR1 signal
or even call _exit(= 1). This commit extends that behavior to include
dco_get_peer_stats_mult= i() and dco_get_peer_stats().

Change-Id: Ib118426c5a69256894040c6985= 6a4003d9f4637c
Signed-off-by: Ralf Lici <ralf@...2726...>
Acked= -by: Frank Lichtenheld <frank@...2641...>
Message-Id: <20250305= 171730.250444-1-frank@...2641...>
URL: https://www.mail-archive.com/o= penvpn-devel@lists.sourceforge.net/msg31022.html
Signed-off-by: Gert Doe= ring <gert@...1296...>
---
M src/openvpn/dco.h
M src/openvpn= /dco_freebsd.c
M src/openvpn/dco_linux.c
M src/openvpn/dco_win.c
M= src/openvpn/forward.c
M src/openvpn/manage.c
M src/openvpn/multi.cM src/openvpn/sig.c
8 files changed, 63 insertions(+), 21 deletions(-)=

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/31/=
831/4
diff --git a/src/openvpn/dco.h b/src/openvpn/dco.h
= index 35ceace..ed194cc 100644
--- a/src/openvpn/dco.h=
+++ b/src/openvpn/dco.h
@@ -231,17 +231,20= @@
/**
* Update traffic statistics for = all peers
*
- * @param dco DCO device context
- * @param m the server context
+ * @param dco DCO dev= ice context
+ * @para= m m the server context
+ * @param raise_sigusr1_on_err whether to raise SIG= USR1 on error
**/
-int dco_get_peer_stats_multi(dco_context_t *dco, struct multi= _context *m);
+int dc= o_get_peer_stats_multi(dco_context_t *dco, struct multi_context *m,<= br>+ = const bool raise_sigusr1_on_err);

/**<= /span>
* Update traffic statistics for single peer
*

- * @param c i= nstance context of the peer
+ * @param c instance context of the peer=
+ * @param raise_sigusr1_on= _err whether to raise SIGUSR1 on error
**/
-int dco_get_peer_stats(struct conte= xt *c);
+int dco_get_= peer_stats(struct context *c, const bool raise_sigusr1_on_err);
<= span>
/**
* Retrieve the list of cipher= s supported by the current platform
@@ -373,13 +376,14 @@
}

static inline int
-dco_get_peer_stats_multi(d= co_context_t *dco, struct multi_context *m)
+dco_get_peer_stats_multi(dco_context_t *dco, struct= multi_context *m,
+ = const bool raise_sigusr1_on_err)
{=
return 0;
}
=
static inline int
-dco_get_peer_stats(struct context *c)
+dco_get_peer_stats(struct context *c, const bool r= aise_sigusr1_on_err)
{
return 0;
}
diff --git a/src/openvpn/dco_freebsd.c b/src= /openvpn/dco_freebsd.c
index 0e536de..b8816c6 100644=
--- a/src/openvpn/dco_freebsd.c
+++ b/src/openvpn= /dco_freebsd.c
@@ -713,7 +713,8 @@
}

int
-dco_get_peer_stats_multi(dco_context_t *dco, struct multi_conte= xt *m)
+dco_get_peer_= stats_multi(dco_context_t *dco, struct multi_context *m,
+ const bool ra= ise_sigusr1_on_err)
{

= struct ifdrv drv;
@@ -781,7 +782,7 @@
}=

int
-dco_get_peer_stats(struct context *c)
+dco_get_peer_stats(struct context *c, c= onst bool raise_sigusr1_on_err)
{
/* = Not implemented. */
return 0;
diff --g= it a/src/openvpn/dco_linux.c b/src/openvpn/dco_linux.c
inde= x 68c1a8d..b0a85fd 100644
--- a/src/openvpn/dco_linux.c

+++ b/src/openvpn/dco_linux.c
@@ -952,7 +952,8= @@
}

int
-dco_get_peer_stats_multi(dco_contex= t_t *dco, struct multi_context *m)
+dco_get_peer_stats_multi(dco_context_t *dco, struct multi_co= ntext *m,
+ = const bool raise_sigusr1_on_err)
{ msg(D_DCO_DEBUG, "%s", __func__);
<= /span>
@@ -963,6 +964,14 @@
int ret =3D ovpn_= nl_msg_send(dco, nl_msg, dco_parse_peer_multi, m, __func__);

nlmsg_free(nl_msg);
+
+ if (raise_sigusr1_on_err && ret < 0)
+ {
+ msg(M_WARN, "Error retrieving DCO peer= stats: the underlying DCO peer"
+ "may have been deleted from the kernel w= ithout notifying "
+ "userspace. Restarting the session");
<= span style=3D"color: hsl(120, 100%, 40%);">+ register_signal(m->t= op.sig, SIGUSR1, "dco peer stats error");
+ }
return ret;
}

@@ -1008,9 +1017,14 @@
}

int
-dco_get_peer_stats(struct context *c)
+dco_get_peer_stats(str= uct context *c, const bool raise_sigusr1_on_err)
{<= br>- uint32_t peer_id =3D c-&g= t;c2.tls_multi->dco_peer_id;
+ int peer_id =3D c->c2.tls_multi->dco_peer_id;<= br>+ if (peer_id =3D=3D -1)<= /span>
+ {
+ return 0;

+ }
+
msg(D_DCO_DEBUG, "%s: p= eer-id %d", __func__, peer_id);

= if (!c->c1.tuntap)
@@ -1030,6 +1044,14 @@

nla_put_failure:
nlmsg_free(nl_m= sg);
+
+ if (raise_sigusr1_on_err &= & ret < 0)

+ = {
+ msg(M_WA= RN, "Error retrieving DCO peer stats: the underlying DCO peer"
+ "may = have been deleted from the kernel without notifying "
+ "userspace. Restart= ing the session");
+ register_signal(c->sig, SIGUSR1, "dco peer stats error&= quot;);
+ }=
return ret;
}

<= span>diff --git a/src/openvpn/dco_win.c b/src/openvpn/dco_win.c

<= span>index 45cb919..8b47124 100644

--- a/src/openvpn/dco_wi= n.c
+++ b/src/openvpn/dco_win.c
@@ -712,14 = +712,15 @@
}

int=
-dco_get_peer_stats_multi(dco= _context_t *dco, struct multi_context *m)
+dco_get_peer_stats_multi(dco_context_t *dco, struct m= ulti_context *m,
+ = const bool raise_sigusr1_on_err)
{
/* Not implemented. */
return 0;
}

int
-dco_get_peer_stats(struct context *c)
+dco_get_peer_stats(str= uct context *c, const bool raise_sigusr1_on_err)
{<= br> struct tuntap *tt =3D c->c1.tuntap;

diff --git a/src/openvpn/forward.c b/src/openvpn/forward.c
index b025344..fce7ac8 100644
--- a/src/openvpn= /forward.c
+++ b/src/openvpn/forward.c
@@ -= 488,7 +488,7 @@
static void
check_inactiv= ity_timeout(struct context *c)
{
- if (dco_enabled(&c->options) &&a= mp; dco_get_peer_stats(c) =3D=3D 0)
+ if (dco_enabled(&c->options) && dco_get_= peer_stats(c, true) =3D=3D 0)
{
= int64_t tot_bytes =3D c->c2.tun_read_bytes + c->c2.tun_write_bytes= ;
int64_t new_bytes =3D tot_bytes - c->c2.inact= ivity_bytes;
@@ -497,7 +497,6 @@
{=
c->c2.inactivity_bytes =3D tot_bytes;
event_timeout_reset(&c->c2.inactivity_inter= val);
-
return;

}
}

diff --git a/src/openvpn/manage.c b/src/openvpn/manage.c
index 484042a..0e73942 100644
--- a/src/openvpn/= manage.c
+++ b/src/openvpn/manage.c
@@ -414= 6,8 +4146,13 @@
counter_type dco_read_bytes =3D 0;=
counter_type dco_write_bytes =3D 0;

- if (dco_e= nabled(&c->options) && (dco_get_peer_stats(c) =3D=3D 0))
+ if (dco_enabled= (&c->options))
{
+ if (dco_get_peer_stats(c, true) &l= t; 0)
+ {<= /span>
+ retu= rn;
+ }
+
= dco_read_bytes =3D c->c2.dco_read_bytes;
= dco_write_bytes =3D c->c2.dco_write_bytes;
= }
@@ -4166,7 +4171,8 @@
void
<= span> man_persist_client_stats(struct management *man, struct context *c)
{
- = if (dco_enabled(&c->options) && (dco_get_peer_stats(c) =3D= =3D 0))
+ /* no ne= ed to raise SIGUSR1 since we are already closing the instance */
= + if (dco_enabled(&c->= ;options) && (dco_get_peer_stats(c, false) =3D=3D 0))
{

management_bytes_client(man, c->c2.dc= o_read_bytes, c->c2.dco_write_bytes);
}
<= span>diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c

index 85a1712..9d244be 100644

--- a/src/openvpn/multi.c
+++ b/src/openvpn/multi.c
@@ -548,7 +548,10 @= @
{
if (dco_enabled(&m->top.op= tions))
{
- dco_get_peer_stats_multi(&m->top.c1.tuntap->dco,= m);
+ if (dco= _get_peer_stats_multi(&m->top.c1.tuntap->dco, m, false) < 0)
+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ return;

<= span style=3D"color: hsl(120, 100%, 40%);">+ }

= }

setenv_counter(c->c2.es, "= bytes_received", c->c2.link_read_bytes + c->c2.dco_read_bytes);<= /span>
@@ -856,7 +859,10 @@

= if (dco_enabled(&m->top.options))
{
- dco_get_peer= _stats_multi(&m->top.c1.tuntap->dco, m);
+ if (dco_get_peer_stats_multi(&am= p;m->top.c1.tuntap->dco, m, true) < 0)
+ {
+ return;
+ }
}
if (version =3D=3D 1)
di= ff --git a/src/openvpn/sig.c b/src/openvpn/sig.c
index 8323= f0d..b0f8935 100644
--- a/src/openvpn/sig.c
+++ b/src/openvpn/sig.c

@@ -489,7 +489,10 @@

if (dco_enabled(&c->options))
{
- dco= _get_peer_stats(c);
+= if (dco_get_peer_stats(c, true) < 0)
+ {
+ return;
+ }
}
=
status_printf(so, "OpenVPN STATISTICS");

To view, visit change 831. To unsubscribe, or for hel= p writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ib118426c5a69256894040c69856= a4003d9f4637c
Gerrit-Change-Number: 831
Gerrit-PatchSet: 4
Gerrit-Owner: ralf_lici <ralf@...2726...>= ;
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-MessageType: newpatchset
--QcAZEQb376c=--