Support nftables and iptables #15
Labels
No labels
Bug
BuildIssue
Claude-fixed
Enhancement
In Progress
Low priority
On Hold
Rejected
Security
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
keyop-go/nfq_forwarder#15
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Support whichever is available, preferring nftables
Plan
Today the entire packet-interception/NAT mechanism is hard-wired to
iptablesviagithub.com/coreos/go-iptablesand a bundled, capability-enabled private copy of theiptablesbinary. This adds an
nftablesbackend alongside it, auto-preferring nftables when available(with an explicit override), while leaving the well-tested existing iptables path unchanged in
substance.
Verified empirically against a real kernel first (this sandbox has real
nft+ root viasudo, cleaned up afterward): confirmed atype filter hook output priority manglechainresolves to priority
-150andtype nat hook output priority dstnatresolves to-100-preserving the exact same hook-ordering guarantee issue #4 depends on (interception strictly
before DNAT, within one packet's
OUTPUT-hook traversal). Also confirmednft -j listgivesclean structured JSON with
comment/handle/counter fields, and that deletion is by stablehandlerather than positional index - a genuine improvement over iptables' index-baseddeletion (which is why
deleteIPTablesTableChainTagRuleshas to delete in reverse order, andpart of why
iptablesMuexists at all).Design
firewallBackendinterface (internal/nfq_forwarder/firewall.go) consolidating the fourexisting test-only indirection vars (
initNfqIptablesFunc,deleteNfqIptablesRulesFunc,addDNATRulesFunc,deleteDNATRuleForTunnelFunc) into one seam:initQueueRule,deleteQueueRules,addDNATRule,deleteDNATRule,dnatCounters.iptablesBackend(firewall_iptables.go): a near-mechanical move of the existinginitNfqIptables/deleteNfqIptablesRules/addDNATRules/deleteDNATRuleForTunnel/refreshStatsbodies into methods - same tables (mangle/nat/OUTPUT), same comment-tagscheme, same crash-recovery scan of
filtertoo. Nothing about this path changes in substance.nftablesBackend(firewall_nftables.go, new): owns one dedicatedinet nfq_forwardertablewith an
outputchain (priority mangle,queue num <n>rules) and anoutput_natchain(
priority dstnat,counter ... dnat to ...rules) - shells out tonft -j(JSON), matchinggo-iptables's own CLI-wrapping approach rather than adding a new netlink-level dependency(none is cached/already a dependency). Still comment-tags rules per queue/tunnel exactly like
today, since the table is shared across connections/processes the same way iptables'
mangle/nat/OUTPUTis.selectFirewallBackend(override string): prefers nftables ifnftis on$PATHand aharmless
nft list tablesactually runs without error; falls back to iptables otherwise. Newfirewall_backend: auto|iptables|nftablesconfig field lets--configforce a specificbackend, skipping detection entirely (an invalid override fails loudly, matching how other
config validation errors behave). Logged once at startup either way.
iptablesandnftprivate copies get bundled andsetcap'd the same way(
CAP_DAC_READ_SEARCH,CAP_NET_ADMIN,CAP_NET_RAW- confirmedNET_ADMIN/NET_RAWare thegenuine netlink capabilities nftables needs too, not iptables-legacy-specific) - runtime
detection then picks whichever's actually usable on the installed host.
cmd/setup/setup.go'sCLI description text still says the NFQUEUE rule lives in the
'filter'table - stale sinceissue #4 moved it to
'mangle'.Testing
New
firewall_nftables_test.gomirrors the existing iptables tests' philosophy (safe regardlessof CI's privilege level, doc-comment-driven log-output assertions for table/chain/priority
choices).
TestIptablesMutatingFunctionsSerializeOnSharedLockbecomes table-driven across bothbackends. Given this sandbox's real root access, I'll also do genuine real-host verification
(force
firewall_backend: nftables, confirm actual rule creation/counters/teardown viasudo nft -j list table inet nfq_forwarder) before considering this done - not just passingunit tests.
Branch:
issue15_nftables_support.Implemented in PR #92 - see the PR description for the full design summary and real-host verification status. Requested review from @danny.
Pausing this for now - it's a large one (full firewall-backend abstraction to support both iptables and nftables). I've done the investigation groundwork (mapped every current iptables touchpoint, empirically verified nftables'
nft -jJSON output covers everything needed - comments, counters, hook-priority ordering matching mangle/nat - and confirmed the existing capability-grant mechanism generalizes to a bundlednftcopy the same way it works foriptablestoday), but haven't finalized a design yet. Will pick this back up later - marked On Hold in the meantime.Correction to my last comment: this is actually already fully implemented, not just investigated. See PR #92 (branch
issue15_nftables_support) - afirewallBackendinterface withiptablesBackend/nftablesBackendimplementations, real end-to-end nftables verification against this sandbox's actualnft, and packaging updates to bundle+setcap both binaries. I lost track of that context and redundantly redid some investigation just now (no code changes, purely read-only - apologies for the noise). Labeling as On Hold per Danny's request; PR #92 remains open awaiting review.