Files
Brendan Creane 92f396d8e9 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 <[email protected]>
2026-11-19 12:12:20 -08:00

56 lines
1.8 KiB
Go

// 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())
}
}