From 92f396d8e9bf28fc89d6da1e1e0ab9aceaa9e0e9 Mon Sep 17 00:00:00 2001 From: Brendan Creane Date: Thu, 19 Nov 2026 12:12:20 -0800 Subject: [PATCH] util/linuxfw: run without IPv6 rules when ip6tables fails to initialize (#22362) newIPTablesRunner returned the error from the IPv6 go-iptables constructor, which made tailscaled exit at startup. The nft flavor of ip6tables exits 1 from "--version" on kernels without nf_tables even though the binary is present. Treat that like a missing ip6tables and disable tunneled IPv6. Fixes #20348 Signed-off-by: Brendan Creane --- util/linuxfw/iptables.go | 14 +++++---- util/linuxfw/iptables_test.go | 55 +++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 util/linuxfw/iptables_test.go diff --git a/util/linuxfw/iptables.go b/util/linuxfw/iptables.go index 3bd2c2886..dcb0ee868 100644 --- a/util/linuxfw/iptables.go +++ b/util/linuxfw/iptables.go @@ -102,14 +102,18 @@ func newIPTablesRunner(logf logger.Logf) (*iptablesRunner, error) { case ip6terr != nil: logf("disabling tunneled IPv6 due to missing ip6tables: %v", ip6terr) default: - supportsV6 = true ipt6, err = iptables.NewWithProtocol(iptables.ProtocolIPv6) if err != nil { - return nil, err + // ip6tables is present but "ip6tables --version" failed, + // as the nft flavor does on kernels without nf_tables. + // Run without IPv6 rules instead of failing. (#20348) + logf("disabling tunneled IPv6 due to ip6tables init failure: %v", err) + } else { + supportsV6 = true + supportsV6Filter = checkSupportsV6Filter(ipt6, logf) + supportsV6NAT = checkSupportsV6NAT(ipt6, logf) + logf("netfilter running in iptables mode v6 = %v, v6filter = %v, v6nat = %v", supportsV6, supportsV6Filter, supportsV6NAT) } - supportsV6Filter = checkSupportsV6Filter(ipt6, logf) - supportsV6NAT = checkSupportsV6NAT(ipt6, logf) - logf("netfilter running in iptables mode v6 = %v, v6filter = %v, v6nat = %v", supportsV6, supportsV6Filter, supportsV6NAT) } return &iptablesRunner{ ipt4: ipt4, diff --git a/util/linuxfw/iptables_test.go b/util/linuxfw/iptables_test.go new file mode 100644 index 000000000..4ae965f8c --- /dev/null +++ b/util/linuxfw/iptables_test.go @@ -0,0 +1,55 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +//go:build linux && !ts_omit_iptables + +package linuxfw + +import ( + "fmt" + "os" + "path/filepath" + "strings" + "testing" + + "tailscale.com/tstest" + "tailscale.com/types/logger" +) + +// TestNewIPTablesRunnerIP6TablesInitFailure checks that a present but +// non-functional ip6tables disables IPv6 instead of failing runner creation. +// The nft flavor of ip6tables exits 1 from "--version" on kernels without +// nf_tables, which used to make tailscaled exit at startup (#20348). +func TestNewIPTablesRunnerIP6TablesInitFailure(t *testing.T) { + dir := t.TempDir() + writeScript := func(name, body string) { + t.Helper() + if err := os.WriteFile(filepath.Join(dir, name), []byte("#!/bin/sh\n"+body), 0o755); err != nil { + t.Fatal(err) + } + } + writeScript("iptables", "echo 'iptables v1.8.10 (legacy)'\n") + writeScript("ip6tables", "echo 'ip6tables/1.8.10 Failed to initialize nft: Protocol not supported' >&2\nexit 1\n") + t.Setenv("PATH", dir) + + // Make the system IPv6 probe pass so the test reaches the ip6tables + // constructor regardless of the host's IPv6 configuration. + tstest.Replace(t, &checkIPv6ForTest, func(logger.Logf) error { return nil }) + + var logs strings.Builder + r, err := newIPTablesRunner(func(format string, args ...any) { + fmt.Fprintf(&logs, format+"\n", args...) + }) + if err != nil { + t.Fatalf("newIPTablesRunner: %v", err) + } + if r.HasIPV6() { + t.Error("HasIPV6() = true; want false when ip6tables fails to initialize") + } + if r.HasIPV6Filter() || r.HasIPV6NAT() { + t.Error("IPv6 filter/NAT reported available; want neither") + } + if !strings.Contains(logs.String(), "disabling tunneled IPv6 due to ip6tables init failure") { + t.Errorf("log missing ip6tables init failure line; got:\n%s", logs.String()) + } +}