Skip to content

Conversation

@Fredi-raspall
Copy link
Contributor

This goes on top of #1257

@Fredi-raspall Fredi-raspall requested a review from qmonnet February 4, 2026 12:44
@qmonnet qmonnet changed the title Pr/fredi/port forwarding Add support for port forwarding with masquerading (stateful NAT) Feb 4, 2026
@qmonnet qmonnet added the area/nat Related to Network Address Translation (NAT) label Feb 4, 2026
@qmonnet qmonnet linked an issue Feb 4, 2026 that may be closed by this pull request
@Fredi-raspall Fredi-raspall added the dont-merge Do not merge this Pull Request label Feb 4, 2026
Differentiate between 2 cases when logging:
  - no flow entry is found in table for a given key
  - did not build a flow key (because of  the type of packet)

Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
Add srcVpcd to FlowTuple so that it gets displayed/logged too.
The flowTuple type could be used as the single input to the
lookup function.

Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
Make it such that no string needs to be created.

Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
Flow-filter should be the first stage so that other stages
can use the flow-info. Up until now, the flow-filter couldn't
because it preceeded the flow-lookup stage.

Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
Augment FlowInfoLocked to include a VpcDiscriminant so that we
can determine the dst VPC from a flow entry as a fallback.

Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
Rename the type (NF) so that it is clear its purpose.

Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
If, in the flow filter, the destination vpc (discriminant) cannot
be determined for a packet, try to discover it from the flow-info
attached to the packet. Ofc, the correctness of this depends on
whether we store a flow-entry for the corresponding flow and
what information we keep there.

Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
Signed-off-by: Quentin Monnet <qmo@qmon.net>
Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
@qmonnet qmonnet force-pushed the pr/fredi/nat_use_cases branch from 50d68da to 5b0b995 Compare February 6, 2026 21:30
@qmonnet
Copy link
Member

qmonnet commented Feb 6, 2026

Note that I rebased the base branch on main. I didn't rebase this PR because I don't know whether you have local work in progress on your branch.

Also, extend the metadata to 32 bits and the Display impl.

Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
Extend the enum with PortForwarding. The expectation is that from
the API, we'll be explicitly told about port forwarding. As a
result, the contents of the flow-filter will include that infor-
mation which will allow us to annotate the packet accordingly
to steer the packet through the PortForwarder.

Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
Do not unnecessarily propagate src/dst vpc discriminants.
The stateful NAT NF requires packets to have been annotated with
both of them, but it only needs them if there is no session.
Instead of always retrieving them and passing them along, keep
the check that they are present, but just retrieve them from the
packet given that we need to pass a reference to it to modify it.

Also, given that the flow-filter is the one responsible for
determining src & dst vpcd, and annotating that nat is needed,
the stateful nat function should never get a packet without those
annotations. Therefore, add a debug assert to the existing check.

Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
Extend FlowInfoLocked object with optional state for port
forwarding.

Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
Implement TryFrom/From methods for [Tcp|Udp]ProtoKey from
port tuples.

Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
The port-forwarding table is the table that keeps the rules to
perform port-forwarding (and address translation) in the datapath.
The table is wrapped in a PortFwTableRw object that allows repla-
cing it depending on the configuration. A single type is used to
both read and replace.

Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
Implement Display for port forwarding table, key and entries.

Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
This avoids polluting the datapath with error checking and makes
sense from a practical standpoint.

Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
The PortFwState is the custom state associated to a flow entry
used to perform src ip/port translation on the reverse path
of a port-forwarded flow.

Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
Adds functions to nat source IPs and ports or destination IPs
and ports. The functions are specific for port-forwrading since
they expect packets to be IP and contain either TCP or UDP payloads.
The functions return true if the packet could be updated and false
otherwise.

Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
Make FlowInfo::update_status() infallible as it always returns
Ok().

Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
@Fredi-raspall Fredi-raspall force-pushed the pr/fredi/port-forwarding branch 3 times, most recently from 4764633 to 7087c7c Compare February 9, 2026 16:39
@qmonnet qmonnet force-pushed the pr/fredi/nat_use_cases branch 2 times, most recently from c7b016c to 0ade936 Compare February 9, 2026 17:18
Adds functions to:
  - retrieve port-forwarding state associated to a packet.
  - create entry on reverse path with port-forwarding state.

Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
This NF will attempt to perform port-forwarding for all packets.
This behavior needs to be changed depending on what the flow-filter
does for those packets.

Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
Udp::empty() sets the length of the UDP header to the minimum.
This avoids panics when printing test packets since the previous
method does not set the length. This will be fixed in another PR.

Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
Signed-off-by: Fredi Raspall <fredi@githedgehog.com>
@Fredi-raspall Fredi-raspall force-pushed the pr/fredi/port-forwarding branch from 7087c7c to d46f0ac Compare February 9, 2026 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/nat Related to Network Address Translation (NAT) dont-merge Do not merge this Pull Request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support port forwarding for masquerading (stateful NAT)

2 participants