Skip to content

fix: make masquerade to ports take effect, with its flags - #373

Open
bored-engineer wants to merge 1 commit into
google:mainfrom
bored-engineer:masq-ports-flags
Open

bored-engineer wants to merge 1 commit into
google:mainfrom
bored-engineer:masq-ports-flags

Conversation

@bored-engineer

Copy link
Copy Markdown

Masq with ToPorts wrote the port registers but never set NF_NAT_RANGE_PROTO_SPECIFIED, which the kernel's masq expression needs to apply them (nft sets it too), so the range was ignored. It also only wrote NFTA_MASQ_FLAGS when ToPorts was false, so Random, FullyRandom and Persistent were dropped as soon as a range was set.

Always write the flags that apply, and set PROTO_SPECIFIED with ToPorts. Masq without ToPorts is unchanged.

Masq with ToPorts wrote the port registers but never set
NF_NAT_RANGE_PROTO_SPECIFIED, which the kernel's masq expression needs
to apply them (nft sets it too), so the range was ignored. It also only
wrote NFTA_MASQ_FLAGS when ToPorts was false, so Random, FullyRandom and
Persistent were dropped as soon as a range was set.

Always write the flags that apply, and set PROTO_SPECIFIED with ToPorts.
Masq without ToPorts is unchanged.
@moracca

moracca commented Nov 15, 2026

Copy link
Copy Markdown

We hit this too, on kernel 6.8. A rule built with expr.Masq{ToPorts: true} for masquerade to :1024-16383 looks right in nft list ruleset, but a client's original source port (35819) survived the masquerade, since the kernel only applies the range when NF_NAT_RANGE_PROTO_SPECIFIED is in NFTA_MASQ_FLAGS. We're carrying the same change as this PR on top of v0.3.0 and it fixes it. Would be great to see this merged and in a release so we can drop our patched copy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants