Skip to content

hotfix(netlink): fix dropping connections on firewall setup for all systems - #3158

Open
qdm12 wants to merge 21 commits into
masterfrom
conntrack
Open

hotfix(netlink): fix dropping connections on firewall setup for all systems#3158
qdm12 wants to merge 21 commits into
masterfrom
conntrack

Conversation

@qdm12

@qdm12 qdm12 commented Feb 25, 2026

Copy link
Copy Markdown
Member

Description

WIP

Do this: #3152 (comment)

Issue

#3152

Assertions

  • I am aware that we do not accept manual changes to the servers.json file
  • I am aware that any changes to settings should be reflected in the wiki

@qdm12 qdm12 changed the title hotfix(netlink): fix conntrack behavior on older kernels 4.4.x hotfix(netlink): fix dropping connections on firewall setup for all systems Feb 25, 2026
@qdm12
qdm12 force-pushed the conntrack branch 3 times, most recently from efe0cf6 to 02d2ed6 Compare February 26, 2026 19:56
@qdm12
qdm12 force-pushed the conntrack branch 2 times, most recently from c2a8ef9 to 39800c7 Compare February 26, 2026 20:52
… requirement since some systems don't show what they support reliably
@qdm12 qdm12 added Category: kernel 🧠 Status: 📌 Before next release Has to be done before the next release labels Mar 7, 2026
@qdm12
qdm12 force-pushed the master branch 2 times, most recently from 2c06921 to 9a5995f Compare March 16, 2026 13:48
@qdm12
qdm12 force-pushed the master branch 4 times, most recently from 40f126b to 44d5104 Compare May 3, 2026 04:29
qdm12 added 3 commits August 19, 2026 20:19
On older kernels (4.4.x), the conntrack netlink delete message is not
supported, so the flush fails with a raw netlink error like
"netlink receive: invalid argument", even when the module probe
succeeds. That error used to abort the firewall enabling, preventing
gluetun from starting (#3152).

flushExistingConnections is now fully best-effort: any try failure
falls back to the next one, and if all tries fail, a warning is logged
instead of an error being returned, since killing the existing
connections is an optimization, not a requirement for the firewall
to function.

Also:
- add Warnf to the firewall Logger interface
- fix the stale doc comment on AcceptOutputPublicOnlyNewTraffic
- fix the broken firewall.NewConfig call in the pmtud tcp integration test
- add unit tests for the fallback behavior
Master refactored the iptables package (single mutex, inline errors,
removed sentinels) after the conntrack branch forked, so the merge
left conntrack's code referencing removed symbols:

- AcceptOutputPublicOnlyNewTraffic locked the removed ip6tablesMutex
  (master unified locking on a single iptablesMutex)
- parse.go and list.go referenced the removed
  ErrIptablesCommandMalformed and ErrChainRuleMalformed sentinels;
  inlined the errors in master's style (final messages unchanged)
- pmtud now checks both ErrKernelModuleMissing and
  ErrMarkMatchModuleMissing, so TCP PMTUD gracefully aborts whether
  the mark module is missing at the libxt_mark.so stat precheck or at
  iptables runtime
- fix the stale 6-arg firewall.NewConfig call in
  pmtud_integration_test.go, which master added after the branch forked
- fix stale ip6tablesMutex comments in atomic.go

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR moves conntrack cleanup into firewall setup and adds iptables fallbacks when netlink flushing is unavailable.

Changes:

  • Adds conntrack probing and AF_UNSPEC flushing.
  • Adds iptables marking, reject/drop fallbacks, and parser support.
  • Updates firewall wiring, PMTUD handling, and tests.

Review blockers (critical):

  • iptables.go must return unclassified IPv4 setup errors instead of falling through to the IPv6 no-op.
  • Replace or safely manage the global PUBLIC_ONLY chain name.
  • Return ErrConntrackNetlinkNotSupported on unsupported platforms instead of panicking.
  • Avoid unconditionally calling the panic-prone non-Linux mod.Probe.

Reviewed changes

Copilot reviewed 22 out of 22 changed files in this pull request and generated 4 comments.

Show a summary per file
File Summary
internal/pmtud/tcp/tcp_integration_test.go Updates firewall construction and skip diagnostics.
internal/pmtud/tcp/mss.go Handles missing kernel modules.
internal/pmtud/tcp/helpers_test.go Updates test firewall construction.
internal/pmtud/pmtud.go Handles kernel-module failures.
internal/pmtud/pmtud_integration_test.go Updates firewall construction.
internal/netlink/netlink.go Probes conntrack netlink support.
internal/netlink/conntrack_unspecified.go Defines unsupported-platform conntrack behavior.
internal/netlink/conntrack_linux.go Updates conntrack flushing.
internal/firewall/iptables/tcp.go Refines mark-module error handling.
internal/firewall/iptables/parse.go Parses new iptables operations and targets.
internal/firewall/iptables/parse_test.go Updates parser expectations.
internal/firewall/iptables/list.go Parses connmark and reject rules.
internal/firewall/iptables/iptables.go Adds fallback firewall rules.
internal/firewall/iptables/ip6tables.go Handles missing kernel modules.
internal/firewall/iptables/firewall.go Adds a kernel-module error sentinel.
internal/firewall/iptables/atomic.go Updates mutex documentation.
internal/firewall/interfaces.go Extends firewall and logger interfaces.
internal/firewall/flush.go Implements connection-cleanup fallback orchestration.
internal/firewall/flush_test.go Tests fallback orchestration.
internal/firewall/firewall.go Injects the netlink dependency.
internal/firewall/enable.go Runs connection cleanup during enablement.
cmd/gluetun/main.go Wires netlink into firewall setup.
Suppressed comments (3)

internal/firewall/iptables/iptables.go:259

  • These exceptions only recognize RFC1918/ULA/link-local/loopback, but routing.LocalNetwork is built from arbitrary directly connected route prefixes (including globally routed IPv6 LANs). When conntrack flushing fails and the permanent mark fallback is installed, an existing connection to such a configured local network hits PUBLIC_ONLY and is dropped before the later local-subnet ACCEPT rule. Include the configured local prefixes in the exceptions.
	ipv6PrivatePrefixes := []netip.Prefix{
		netip.MustParsePrefix("fc00::/7"),
		netip.MustParsePrefix("fe80::/10"),
		netip.MustParsePrefix("::1/128"),
	}

internal/firewall/iptables/parse.go:310

  • For the new -m connmark --mark 0x567 syntax, this branch returns at the strings.HasPrefix case before consuming the mark value. The outer parser then stores 0x567 in instruction.mark, while parseChainRuleOptionalFields stores the listed rule in rule.connMark, so deleteIPTablesRule cannot match connmark rules. Parse the --mark value into instruction.connMark here, including the optional inversion marker.
	case "connmark":
		consumed++
		switch {
		case len(fields[consumed:]) == 0 || strings.HasPrefix(fields[consumed], "-"):
			// end or another flag

internal/pmtud/tcp/mss.go:46

  • This new missingMarkModule branch does not reach callers. When every destination returns this error, mss remains zero and the function returns the generic all ... unreachable error below; when any destination succeeds, it returns nil. Consequently tcp.PathMTUDiscover never receives ErrKernelModuleMissing, so the new ICMP fallback in pmtud.go is ineffective for this case. Preserve and return the stored module error in the no-MSS path, and cover it with a test.
			missingMarkModule := errors.Is(result.err, iptables.ErrKernelModuleMissing) ||
				errors.Is(result.err, iptables.ErrMarkMatchModuleMissing)
			switch {
			case err != nil: // error already occurred for another findMSS goroutine
			case missingMarkModule:

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread internal/firewall/iptables/iptables.go
Comment thread internal/firewall/iptables/iptables.go Outdated
Comment thread internal/netlink/conntrack_unspecified.go
Comment thread internal/netlink/netlink.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants