systemd / systemd/systemd

Masquerade Should be Attached To Routes, Not Addresses

Open
#8,040 3 comments 17 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

network
Dominant language
C
Stars
16.7k
Forks
4.7k
Avg merge
1d 6h
Merged PRs (30d)
140

Description

Unless you use noprefixroute when adding an IP address to an interface, the kernel naturally creates a route for that IP's subnet. Neato.

It seems that systemd takes advantage of this fact, by then inferring the masquerading subnet.

In other words, if you set IPMasquerade=yes, then ip addr add 192.168.77.43/24 dev eth0 will result in iptables -t nat -A POSTROUTING -s 192.168.77.0/24 -j MASQUERADE. This works because noprefixroute is not in use.

But the actual thing that's going on here is that there's a masquerade entry being made related to the route added to the interface, not the address. However, systemd's logic is erroneously hooked up to the address.

Thus this configuration:

[Match]
Name=whatever
[Network]
IPMasquerade=yes
Address=192.168.77.43/24

will result in iptables -t nat -A POSTROUTING -s 192.168.77.0/24 -j MASQUERADE. However, this configuration:

[Match]
Name=whatever
[Network]
IPMasquerade=yes
[Route]
Destination=192.168.77.0/24

will not result in any added masquerading entry. This is incorrect behavior.

The proper behavior would be to add a masquerade entry related to each of the routed subnets, not just the implicit routes attached to addresses.

This is particularly important when using layer 3 tunneling (such as via WireGuard or IPIP), where a common configuration is to not actually assign any specific IP address to the tunnel interface, yet still have it route packets.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how IPMasquerade=yes handles the [Route] Destination setting and how it currently derives masquerading from Address entries. Compare the generated iptables POSTROUTING rules for implicit address routes and explicit routed subnets; done means each routed subnet receives the appropriate masquerade entry, including routes on interfaces without assigned addresses.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, linux
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.