Skip to contentSkip to conversation

Don't "undo" ifconfig on exit if it wasn't done

PATCH In review Max Fillinger 2 participants 4 messages Aug 10, 2022 – Aug 20, 2022

lore
Share & tools
lore
#0 lore raw

PATCH v6 1/1 Don't "undo" ifconfig on exit if it wasn't done

When running with --ifconfig-noexec, OpenVPN does not execute ifconfig, but on exit, it still tries to "undo" the configuration it would have done. This patch fixes it by extracting an undo_ifconfig() function from close_tun(). The undo function is called before close_tun(), but only if --ifconfig-noexec isn't set. This is symmetric to how open_tun() and do_ifconfig() are used.

v2: Fix tabs-vs-spaces.
v3: Fix another style mistake.
v4: Move undo_ifconfig{4,6}() out of #ifdef TARGET_LINUX.
v5: Keep ctx argument in close_tun().
v6: Fix bug in non-Linux non-Windows version of undo_ifconfig_ipv6

---
 src/openvpn/init.c |   4 ++
 src/openvpn/tun.c  | 159 +++++++++++++++++++++++----------------------
 src/openvpn/tun.h  |   8 +++
 3 files changed, 95 insertions(+), 76 deletions(-)
src/openvpn/init.c +4 -0
index b6705921..ee800880 100644
@@ -1913,6 +1913,10 @@ do_close_tun_simple(struct context *c)
     msg(D_CLOSE, "Closing TUN/TAP interface");
     if (c->c1.tuntap)
     {
+        if (!c->options.ifconfig_noexec)
+        {
+            undo_ifconfig(c->c1.tuntap, &c->net_ctx);
+        }
         close_tun(c->c1.tuntap, &c->net_ctx);
         c->c1.tuntap = NULL;
     }
src/openvpn/tun.c +83 -76

176 lines not on this page · raw message

src/openvpn/tun.h +8 -0
index 8ec8f51f..3fdfcce6 100644
@@ -300,6 +300,14 @@ void do_ifconfig_setenv(const struct tuntap *tt,
 void do_ifconfig(struct tuntap *tt, const char *ifname, int tun_mtu,
                  const struct env_set *es, openvpn_net_ctx_t *ctx);
 
+/**
+ * undo_ifconfig - undo configuration of the tunnel interface
+ *
+ * @param tt    the tuntap interface context
+ * @param ctx   the networking API opaque context
+ */
+void undo_ifconfig(struct tuntap *tt, openvpn_net_ctx_t *ctx);
+
 bool is_dev_type(const char *dev, const char *dev_type, const char *match_type);
 
 int dev_type_enum(const char *dev, const char *dev_type);
Signature · 1 line
2.30.2
Acked-by on v6 1/1
#1 lore raw

[Openvpn-devel] [PATCH applied] Re: Don't undo ifconfig on exit if it wasn't done

I had to apply a bit of pressure to get the patch to apply - the context in tun.c got changed ( || TARGET_FREEBSD) in the meantime... fairly trivial, and no actual code change.

Pushed to the new and renovated buildbot army, to see if it would break any platforms (very convenient, this ;-) ). Didn't! Thanks for the v6 update!

The change is as we have discussed, and I still think it's a good and clean way forward. The "undo_ifconfig_ipv4() / _ipv6()" functions are as they have been before (except that they now get compiled for all platforms except WIN32 - the "linux only" part was a 2.5.0 oversight), and the calls moved 1:1 from (TARGET_LINUX) close_tun() to a new undo_ifconfig() function. tun.c is still a mess, but this helps a bit.

While staring at the diff, I kicked a spurious blank line from the end of undo_ifconfig()... :-)

I'm a bit unsure if we want to pull this up to release/2.5 - it won't apply "as is" (due to all the DCO bits around it), but the actual change could be done the same. It's a bugfix, and brings back the 2.4 "undo ifconfig" behaviour to all non-linux/non-Win32 platforms (which was an unintended change in 2.5.0). So maybe we should?

Your patch has been applied to the master branch.

commit 0c4d40cb838eb0fad4a82bbed1198f6d359d445a

Author: Max Fillinger
Date:   Wed Aug 10 17:30:06 2022 +0200
     Don't undo ifconfig on exit if it wasn't done

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

--
kind regards,

Gert Doering

#2 lore raw

Re: [Openvpn-devel] Followup work on "Don't undo ifconfig on exit if it wasn't done"

Hi,

Quoted 6 lines · Gert Doering

The change is as we have discussed, and I still think it's a good and clean way forward. The "undo_ifconfig_ipv4() / _ipv6()" functions are as they have been before (except that they now get compiled for all platforms except WIN32 - the "linux only" part was a 2.5.0 oversight), and the calls moved 1:1 from (TARGET_LINUX) close_tun() to a new undo_ifconfig() function. tun.c is still a mess, but this helps a bit.

I did lots of testing on FreeBSD today, and it turns out that the generic "undo" code is just a mess and clutters the log with garbage...

2022-08-20 22:58:02 /sbin/ifconfig tun4 0.0.0.0
ifconfig: ioctl (SIOCAIFADDR): Destination address required
2022-08-20 22:58:02 Generic ip addr del failed: external program exited with error status: 1
2022-08-20 22:58:02 /sbin/ifconfig tun4 del fd00:abcd:194:2::103d/64
ifconfig: del: bad value
2022-08-20 22:58:02 Generic ip -6 addr del failed: external program exited with error status: 1

... not Max' fault, but I wonder if this ever did something useful...

(I'm fairly sure "set ipv4 address to 0.0.0.0" was a valid way to undo IPv4 ifconfig, but I do not think "del" was ever a valid ifconfig IPv6 syntax on *BSD)

So - this needs fixing, and platform-by-platform testing...

Technically, for most cases this won't be actually needed - if the tun/tap interface is destroyed at program end ("ifconfig tun4 destroy"),
the IPv4/IPv6 config is gone anyway. Only for persistant interfaces
it makes a difference.

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]...
#3 lore raw

Re: [Openvpn-devel] Followup work on "Don't undo ifconfig on exit if it wasn't done"

HI,

Quoted 5 lines · Gert Doering

I did lots of testing on FreeBSD today, and it turns out that the generic "undo" code is just a mess and clutters the log with garbage...

2022-08-20 22:58:02 /sbin/ifconfig tun4 0.0.0.0

ifconfig: ioctl (SIOCAIFADDR): Destination address required

... interesting enough, on *tap* interfaces, OpenVPN can do that...

2022-08-20 23:05:34 /sbin/ifconfig tap0 0.0.0.0

ifconfig: WARNING: setting interface address without mask is deprecated, default mask may not be correct.

... but the kernel logs a warning...

Aug 20 23:05:34 fbsd14 kernel: tap0: set address: WARNING: network mask should be specified; using historical default

*shake head*

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]...