util/linuxfw,wgengine/router: add connmark rules for rp_filter workaround (#18860)

When a Linux system acts as an exit node or subnet router with strict
reverse path filtering (rp_filter=1), reply packets may
be dropped because they fail the RPF check. Reply packets arrive on the
WAN interface but the routing table indicates they should have arrived
on the Tailscale interface, causing the kernel to drop them.

This adds firewall rules in the mangle table to save outbound packet
marks to conntrack and restore them on reply packets before the routing
decision. When reply packets have their marks restored, the kernel uses
the correct routing table (based on the mark) and the packets pass the
rp_filter check.

Implementation adds two rules per address family (IPv4/IPv6):

- mangle/OUTPUT: Save packet marks to conntrack for NEW connections
with non-zero marks in the Tailscale fwmark range (0xff0000)

- mangle/PREROUTING: Restore marks from conntrack to packets for
ESTABLISHED,RELATED connections before routing decision and rp_filter
check

The workaround is automatically enabled when UseConnmarkForRPFilter is
set in the router configuration, which happens when subnet routes are
advertised on Linux systems.

Both iptables and nftables implementations are provided, with automatic
backend detection.

Fixes #3310
Fixes #14409
Fixes #12022
Fixes #15815
Fixes #9612

Signed-off-by: Mike O'Driscoll <mikeo@tailscale.com>
This commit is contained in:
Mike O'Driscoll
2026-03-04 14:09:11 -05:00
committed by GitHub
parent dab8922fcf
commit 26ef46bf81
6 changed files with 814 additions and 12 deletions
+98
View File
@@ -527,6 +527,104 @@ func (i *iptablesRunner) DelStatefulRule(tunname string) error {
return nil
}
// AddConnmarkSaveRule adds conntrack marking rules to save and restore marks.
// These rules run in mangle/PREROUTING (to restore marks from conntrack) and
// mangle/OUTPUT (to save marks to conntrack) before rp_filter checks, enabling
// proper routing table lookups for exit nodes and subnet routers.
func (i *iptablesRunner) AddConnmarkSaveRule() error {
// Check if rules already exist (idempotency)
for _, ipt := range []iptablesInterface{i.ipt4, i.ipt6} {
rules, err := ipt.List("mangle", "PREROUTING")
if err != nil {
continue
}
// Look for existing connmark restore rule
for _, rule := range rules {
if strings.Contains(rule, "CONNMARK") &&
strings.Contains(rule, "restore-mark") &&
strings.Contains(rule, "ctmask 0xff0000") {
// Rules already exist, skip adding
return nil
}
}
}
// mangle/PREROUTING: Restore mark from conntrack for ESTABLISHED/RELATED connections
// This runs BEFORE routing decision and rp_filter check
for _, ipt := range []iptablesInterface{i.ipt4, i.ipt6} {
args := []string{
"-m", "conntrack",
"--ctstate", "ESTABLISHED,RELATED",
"-j", "CONNMARK",
"--restore-mark",
"--nfmask", fwmarkMask,
"--ctmask", fwmarkMask,
}
if err := ipt.Insert("mangle", "PREROUTING", 1, args...); err != nil {
return fmt.Errorf("adding %v in mangle/PREROUTING: %w", args, err)
}
}
// mangle/OUTPUT: Save mark to conntrack for NEW connections with non-zero marks
for _, ipt := range []iptablesInterface{i.ipt4, i.ipt6} {
args := []string{
"-m", "conntrack",
"--ctstate", "NEW",
"-m", "mark",
"!", "--mark", "0x0/" + fwmarkMask,
"-j", "CONNMARK",
"--save-mark",
"--nfmask", fwmarkMask,
"--ctmask", fwmarkMask,
}
if err := ipt.Insert("mangle", "OUTPUT", 1, args...); err != nil {
return fmt.Errorf("adding %v in mangle/OUTPUT: %w", args, err)
}
}
return nil
}
// DelConnmarkSaveRule removes conntrack marking rules added by AddConnmarkSaveRule.
func (i *iptablesRunner) DelConnmarkSaveRule() error {
for _, ipt := range []iptablesInterface{i.ipt4, i.ipt6} {
// Delete PREROUTING rule
args := []string{
"-m", "conntrack",
"--ctstate", "ESTABLISHED,RELATED",
"-j", "CONNMARK",
"--restore-mark",
"--nfmask", fwmarkMask,
"--ctmask", fwmarkMask,
}
if err := ipt.Delete("mangle", "PREROUTING", args...); err != nil {
if !isNotExistError(err) {
return fmt.Errorf("deleting connmark rule in mangle/PREROUTING: %w", err)
}
// Rule doesn't exist - this is fine for idempotency
}
// Delete OUTPUT rule
args = []string{
"-m", "conntrack",
"--ctstate", "NEW",
"-m", "mark",
"!", "--mark", "0x0/" + fwmarkMask,
"-j", "CONNMARK",
"--save-mark",
"--nfmask", fwmarkMask,
"--ctmask", fwmarkMask,
}
if err := ipt.Delete("mangle", "OUTPUT", args...); err != nil {
if !isNotExistError(err) {
return fmt.Errorf("deleting connmark rule in mangle/OUTPUT: %w", err)
}
// Rule doesn't exist - this is fine for idempotency
}
}
return nil
}
// buildMagicsockPortRule generates the string slice containing the arguments
// to describe a rule accepting traffic on a particular port to iptables. It is
// separated out here to avoid repetition in AddMagicsockPortRule and