Skip to contentSkip to conversation

Workaround FreeBSD 12+ race condition on tun/tap open with IPv6.

PATCH New Gert Doering 2 participants 4 messages Jul 22, 2020 – Aug 23, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Workaround FreeBSD 12+ race condition on tun/tap open with IPv6.

On FreeBSD 12 (tested and verified on 12.1-RELEASE-p2), after "ifconfig inet6" for a tun/tap interface, there sometimes is a race condition where the "IFDISABLED" flag shows up after a short time frame, under a second, and never clears itself. This disables use of the configured IPv6 address on the interface, breaking IPv6 over tun/tap operation.

As a workaround until this can be fixed on the FreeBSD side (or a better workaround is found), sleep(1) after ifconfig, then call "ifconfig $dev inet6 -ifdisable".

Yes, this is massively ugly but makes the problem completely go away for my test systems.

(The same effect can be achieved with an --up script that does this, but it's even less pretty - see trac ticket)

Trac: 1226

---
 src/openvpn/tun.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
src/openvpn/tun.c +17 -0
index e96368ca..2d3ba623 100644
@@ -1038,6 +1038,23 @@ do_ifconfig_ipv6(struct tuntap *tt, const char *ifname, int tun_mtu,
     openvpn_execve_check(&argv, es, S_FATAL,
                          "generic BSD ifconfig inet6 failed");
 
+#if defined(TARGET_FREEBSD) && __FreeBSD_version >= 1200000
+    /* FreeBSD 12 grew some sort of race condition where sometimes
+     * the IPv6 side of a tun interface is stuck in "IFDISABLED"
+     * in "ifconfig tun0" output, and the assigned IPv6 address is not
+     * working ("tentative").  Workaround in here, for now.
+     *
+     * the sleep is needed because there is a race condition, so turning
+     * it off immediately can lead to "kernel turns it on again, stuck!!"
+     */
+    sleep(1);
+    argv_printf(&argv, "%s %s inet6 -ifdisabled", IFCONFIG_PATH, ifname);
+    argv_msg(M_INFO, &argv);
+
+    openvpn_execve_check(&argv, es, S_FATAL,
+                         "FreeBSD BSD 'ifconfig inet6 -ifdisabled' failed");
+#endif
+
 #if defined(TARGET_OPENBSD) || defined(TARGET_NETBSD) \
     || defined(TARGET_DARWIN)
     /* and, hooray, we explicitly need to add a route... */
Signature · 1 line
2.27.0
#1 lore raw

PATCH 1/1 Workaround FreeBSD 12+ race condition on tun/tap open with IPv6.

On FreeBSD 12 (tested and verified on 12.1-RELEASE-p2), after "ifconfig inet6" for a tun/tap interface, there sometimes is a race condition where the "IFDISABLED" flag shows up after a short time frame, under a second, and never clears itself. This disables use of the configured IPv6 address on the interface, breaking IPv6 over tun/tap operation.

This only happens if ipv6_activate_all_interfaces="YES" is not set in /etc/rc.conf - but there might be reasons why this is not so.

As a workaround until this can be fixed on the FreeBSD side (or a better workaround is found), sleep(1) after ifconfig, then call "ifconfig $dev inet6 -ifdisable".

Yes, this is massively ugly but makes the problem completely go away for my test systems.

(The same effect can be achieved with an --up script that does this, but it's even less pretty - see trac ticket)

FreeBSD: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=248172

v2: reword text, refer to FreeBSD bug with much more details

Trac: 1226

---
 src/openvpn/tun.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
src/openvpn/tun.c +23 -0
index e96368ca..bbccc1cc 100644
@@ -1038,6 +1038,29 @@ do_ifconfig_ipv6(struct tuntap *tt, const char *ifname, int tun_mtu,
     openvpn_execve_check(&argv, es, S_FATAL,
                          "generic BSD ifconfig inet6 failed");
 
+#if defined(TARGET_FREEBSD) && __FreeBSD_version >= 1200000
+    /* On FreeBSD 12 and up, there is ipv6_activate_all_interfaces="YES"
+     * in rc.conf, which is not set by default.  If it is *not* set,
+     * "all new interfaces that are not already up" are configured by
+     * devd + /etc/pccard_ether as "inet6 ifdisabled".
+     *
+     * The "is this interface already up?" test is a non-zero time window
+     * which we manage to hit with our ifconfig often enough to cause 
+     * frequent fails in the openvpn test environment.
+     *
+     * Thus: assume that the system might interfere, wait for things to
+     * settle (it's a very short time window), and remove -ifdisable again.
+     *
+     * See: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=248172
+     */
+    sleep(1);
+    argv_printf(&argv, "%s %s inet6 -ifdisabled", IFCONFIG_PATH, ifname);
+    argv_msg(M_INFO, &argv);
+
+    openvpn_execve_check(&argv, es, S_FATAL,
+                         "FreeBSD BSD 'ifconfig inet6 -ifdisabled' failed");
+#endif
+
 #if defined(TARGET_OPENBSD) || defined(TARGET_NETBSD) \
     || defined(TARGET_DARWIN)
     /* and, hooray, we explicitly need to add a route... */
Signature · 1 line
2.27.0
Acked-by on v1 1/1
#2 lore raw

Am 23.07.20 um 14:19 schrieb Gert Doering:

Quoted 16 lines

On FreeBSD 12 (tested and verified on 12.1-RELEASE-p2), after "ifconfig inet6" for a tun/tap interface, there sometimes is a race condition where the "IFDISABLED" flag shows up after a short time frame, under a second, and never clears itself. This disables use of the configured IPv6 address on the interface, breaking IPv6 over tun/tap operation.

This only happens if ipv6_activate_all_interfaces="YES" is not set in /etc/rc.conf - but there might be reasons why this is not so.

As a workaround until this can be fixed on the FreeBSD side (or a better workaround is found), sleep(1) after ifconfig, then call "ifconfig $dev inet6 -ifdisable".

Yes, this is massively ugly but makes the problem completely go away for my test systems.

Quoted 6 lines

(The same effect can be achieved with an --up script that does this, but it's even less pretty - see trac ticket)

FreeBSD: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=248172

v2: reword text, refer to FreeBSD bug with much more details

This looks like a ugly workaround but if it is the best we can, so be it.

Arne

#3 lore raw
In reply to Gert Doering

Your patch has been applied to the master and release/2.5 branch.

commit 5e19cc2c1bf22d44f10dc585ba69d48951dc7a86 (master) commit 6dac3342d851995bdee75db4b52cfb3a3f9232e3 (release/2.5)

Author: Gert Doering
Date:   Thu Jul 23 14:19:49 2020 +0200
     Workaround FreeBSD 12+ race condition on tun/tap open with IPv6.

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

--
kind regards,

Gert Doering