netfilter: add iptables MASQUERADE target with strict hook validation and tests - #14606
Open
hugelgupf wants to merge 1 commit into
Open
netfilter: add iptables MASQUERADE target with strict hook validation and tests#14606hugelgupf wants to merge 1 commit into
hugelgupf wants to merge 1 commit into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
… and tests
The `MASQUERADE` target (xt_nat) is a specialized Source NAT (SNAT)
target valid in the nat table's POSTROUTING chain. It dynamically
rewrites a packet's source IP address to the primary IP address of the
outgoing network interface and establishes a connection-tracking entry so
that return traffic is automatically de-masqueraded back to the original
client.
Kubernetes `kube-proxy` configures MASQUERADE in the KUBE-POSTROUTING
chain for traffic leaving the node or traversing virtual bridge hairpin
paths. Although gVisor's netstack already provided stack.MasqueradeTarget,
netfilter lacked a targetMaker for "MASQUERADE", causing `iptables-restore`
to fail ("MASQUERADE revision 0 not supported") and abort the entire
sync transaction.
### What is implemented:
- Protocol and layout validation: Translates iptables MASQUERADE target
(revisions 0, 1, 2) to stack.MasqueradeTarget for IPv4 and IPv6.
- Strict table and hook validation: Enforces that the MASQUERADE target
is only installed in the `nat` table on the `POSTROUTING` hook. Any
attempt to install MASQUERADE in other tables (filter/mangle/raw) or
other hooks (PREROUTING, INPUT, FORWARD, OUTPUT) is explicitly rejected
during ruleset loading.
- Dynamic egress source translation: stack.MasqueradeTarget queries the
outgoing AddressableEndpoint at action time and executes SNAT.
- Round-trip fidelity: Retains raw target bytes for faithful
iptables-save round-tripping.
- Unit and integration tests:
* Unit test in netfilter_test.go verifying checkLoopsAndChains rejects
MASQUERADE in non-POSTROUTING hooks.
* Integration tests in test/iptables/nat.go verifying UDP and TCP
masquerade rewrites as well as rejection of MASQUERADE in PREROUTING/filter.
### What is still missing / diverges vs. Linux:
- Interface DOWN flushes: Linux instantly deletes all conntrack entries
associated with an interface when the link drops its IP or goes DOWN;
gVisor relies on standard conntrack timeout reaping.
- Port range constraints: Specific port ranges passed via `--to-ports`
are preserved for iptables-save but port selection currently uses
netstack's standard ephemeral port allocator.
hugelgupf
force-pushed
the
pr/iptables-masquerade
branch
from
September 4, 2026 00:17
6c91c6f to
c028adb
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
masqueradeTargetMakerinpkg/sentry/socket/netfilter/masquerade.gofor IPv4 and IPv6, translating the iptables target tostack.MasqueradeTarget.MASQUERADEis only installed in thenattable on thePOSTROUTINGhook. Any rule placingMASQUERADEin other tables (filter/mangle/raw) or other hooks (PREROUTING, INPUT, FORWARD, OUTPUT) is rejected withEINVAL.pkg/sentry/socket/netfilter/netfilter_test.goand integration tests intest/iptables/nat.go(includingNATMasqueradeInvalidHookReject).Motivation & Context
Kubernetes
kube-proxyconfiguresMASQUERADEinKUBE-POSTROUTINGfor traffic leaving the node or traversing virtual bridge hairpin paths. Although netstack hadstack.MasqueradeTarget, netfilter had no targetMaker for"MASQUERADE", causingiptables-restoreto fail ("MASQUERADE revision 0 not supported") and aborting ruleset synchronization.Test Plan
bazel test //pkg/sentry/socket/netfilter:netfilter_test //pkg/tcpip/stack:stack_testtest/iptables:iptables_test(NATPostMasquerade*,NATMasqueradeInvalidHookReject)