19.1. Building Routed Networks

A routed network needs three things from each participating host: the kernel must be willing to forward, the route table must send each destination toward a reachable gateway, and replies must be able to come back. This section builds all three, then breaks the third on purpose to show how the kernel's reverse-path filter reacts.

19.1.1. Forwarding, Route Selection, and Gateway Reachability

Start on the router, as john, with the full interface inventory:

$ ip -br addr
lo               UNKNOWN        127.0.0.1/8 ::1/128
eth0             UP             192.168.121.117/24 fe80::5054:ff:fedf:6221/64
eth1             UP             192.168.219.2/24 fe80::5054:ff:fe4b:42fc/64
eth2             UP             192.168.220.2/24 fe80::5054:ff:fe8e:c7d0/64
eth3             UP             192.168.221.2/24 fe80::5054:ff:fe1e:737/64
eth4             UP             192.168.222.2/24 fe80::5054:ff:fe23:ecd/64

Four networks are directly connected: LAN A on eth1, LAN B on eth2, the transit link to rtpeer on eth3, and the trunk on eth4. The kernel derives one connected route per address prefix - traffic for 192.168.219.0/24 goes out eth1 by construction, with no route entry needed from anyone. Everything else needs an explicit route.

A router differs from an ordinary host in one kernel setting: IP forwarding. Without it, a packet that arrives on one interface but is addressed to another network is dropped, no matter how correct the route table is. Enable it persistently with a sysctl drop-in, as Chapter 7 showed:

# /etc/sysctl.d/90-ch19-forward.conf
net.ipv4.ip_forward = 1

Apply it once with sudo sysctl --system (silent on success) and confirm the live value, on rtrouter as john:

$ sudo sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 1

This file belongs on the two routers only. A multi-homed server should normally leave forwarding off: a host with several addresses is not a router just because it has several cables, and a misconfigured one will happily forward between networks that were meant to be isolated.

Route selection is the kernel's longest-prefix match. For any destination it walks the routing table - local, then main, then default, with policy rules able to insert other tables in between, which is exactly what Section 19.1.3 does - and picks the entry whose prefix covers the destination with the most bits. A host route (/32) beats a /24, a /24 beats the default route, and the default route, 0.0.0.0/0, loses only to nothing. Among entries with the same prefix, the lowest metric wins, and equal metrics load-balance across the gateways.

Gateways have one hard rule: the next hop must be reachable directly, on a network the host itself is connected to. ip route add ... via 192.168.5.1 fails with Nexthop has invalid gateway if 192.168.5.1 sits behind another router - the kernel refuses a route whose first step is itself another lookup. Every gateway in this chapter is therefore an address on one of the connected networks from the inventory above.

Watch the rule work against rtclient. The client has an address on LAN A but no route to LAN B, so a ping to the server has nowhere to go except the default route. On rtclient, as john:

$ ping -c 2 -W 1 192.168.220.10
PING 192.168.220.10 (192.168.220.10) 56(84) bytes of data.
From 192.168.121.1 icmp_seq=1 Destination Port Unreachable
From 192.168.121.1 icmp_seq=2 Destination Port Unreachable

--- 192.168.220.10 ping statistics ---
2 packets transmitted, 0 received, +2 errors, 100% packet loss, time 1014ms

The packets left through eth0 toward the administration network's router, 192.168.121.1, which has no route back into the isolated practice networks and said so. The default route is not "the internet connection"; it is the route for everything the host knows nothing about, and this is what knowing nothing looks like. The fix is a route through the router the client already shares a LAN with.

ping option Description
-c count Stop after count packets and print statistics.
-W seconds Per-reply timeout; without it a missing route costs ten seconds per packet.
-s size Payload size in bytes; used for MTU probing in Section 19.5.5.
-M do Set the Don't Fragment bit; combined with -s, finds the path MTU.

19.1.2. Persistent Static Routes and Route Metrics

A static route says: for this destination prefix, send packets via this gateway, on this interface, with this preference. With systemd-networkd it lives in a [Route] section of the same .network file that owns the interface - no separate file, no other owner. Extend rtclient's eth1 file, replacing the address-only starting state:

# /home/john/debian-handbook/routing/rtclient-eth1.network
[Match]
Name=eth1

[Network]
Address=192.168.219.10/24
ConfigureWithoutCarrier=yes

[Route]
Destination=192.168.220.0/24
Gateway=192.168.219.2
Metric=100

[Route]
Destination=10.19.0.0/24
Gateway=192.168.219.2
Metric=100

The first [Route] sends LAN B traffic to the router's LAN A address, which is on the client's directly connected network, satisfying the gateway rule. The second does the same for the WireGuard tunnel network used from Section 19.5 on; installing it now costs nothing. Create the file in the workspace with the editor, then put it in place on rtclient, as john:

$ sudo install -m 0644 rtclient-eth1.network /etc/systemd/network/10-ch19-eth1.network

The command prints nothing on success. install copies and sets the mode in one step:

install option Description
-m mode Permission bits for the copied file (0644: world-readable, owner-writable).
-d Create a directory (with -m) instead of copying a file.
-o user / -g group Set ownership at the same time.
-T Treat the destination as a plain file, never a directory prefix.

Make systemd-networkd read the new files and reapply the interface's configuration, each as a separate step:

$ sudo networkctl reload
$ sudo networkctl reconfigure eth1

Both are silent. reload re-reads /etc/systemd/network/ and applies new or changed files; reconfigure pushes an interface's file again, which is what makes a modified [Route] or [Network] section take effect on a link that was already up.

The route is now in the kernel's main table:

$ ip route show 192.168.220.0/24
192.168.220.0/24 via 192.168.219.2 dev eth1 proto static metric 100

proto static records that systemd-networkd (not DHCP, not a routing daemon) owns this entry, and metric 100 is the preference written into the [Route] section. The subcommands used in this chapter:

ip route form Description
show [prefix] List matching routes; show with no argument lists the main table.
add / del Change the table at runtime; gone at reboot unless persisted.
get address Show which route the kernel would use for one destination, including the chosen table.
show table N List a specific routing table; table all shows every table.
via, dev, metric Selectors that qualify add/del and fields in get output.

The server needs the mirror-image route back to LAN A, plus a route to the OpenVPN service network used in Section 19.6. Its eth1 file, which replaces rtserver's address-only starting state:

# /home/john/debian-handbook/routing/rtserver-eth1.network
[Match]
Name=eth1

[Network]
Address=192.168.220.10/24
ConfigureWithoutCarrier=yes

[Route]
Destination=192.168.219.0/24
Gateway=192.168.220.2
Metric=100

[Route]
Destination=10.19.0.0/24
Gateway=192.168.220.2
Metric=100

[Route]
Destination=10.19.1.0/24
Gateway=192.168.220.20
Metric=100

On rtserver, as john, install it to the same path and apply it with the same two networkctl commands shown above:

$ sudo install -m 0644 rtserver-eth1.network /etc/systemd/network/10-ch19-eth1.network

networkctl reload and networkctl reconfigure eth1 follow exactly as on rtclient. Return to rtclient, as john, and the path is complete:

$ ping -c 2 192.168.220.10
PING 192.168.220.10 (192.168.220.10) 56(84) bytes of data.
64 bytes from 192.168.220.10: icmp_seq=1 ttl=63 time=1.09 ms
64 bytes from 192.168.220.10: icmp_seq=2 ttl=63 time=0.548 ms

--- 192.168.220.10 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.548/0.817/1.086/0.269 ms

ttl=63 is the fingerprint of one router hop: the client sent with TTL 64 and rtrouter decremented it. Both directions work because both ends have routes - a static route on one side alone produces precisely the silent timeout that makes routing problems feel like cable problems.

Metrics matter when more than one route could carry a prefix. The kernel accepts several routes to the same destination and uses the lowest metric; with identical metrics it load-balances across them (equal-cost multipath). Demonstrate on rtclient, as john, with the documentation prefix that Section 19.2.3 makes rtpeer advertise - any prefix the host does not already know works the same way:

$ sudo ip route add 203.0.113.0/24 via 192.168.219.2 metric 100
$ sudo ip route add 203.0.113.0/24 via 192.168.219.2 metric 200

Both commands are silent. Listing the prefix shows both entries:

$ ip route show 203.0.113.0/24
203.0.113.0/24 via 192.168.219.2 dev eth1 metric 100
203.0.113.0/24 via 192.168.219.2 dev eth1 metric 200

Traffic uses the metric 100 entry; the metric 200 entry is the fallback that takes over automatically if the first is deleted or its gateway disappears. Delete them in either order, since the metric distinguishes the entries:

$ sudo ip route del 203.0.113.0/24 via 192.168.219.2 metric 200
$ sudo ip route del 203.0.113.0/24 via 192.168.219.2 metric 100
$ ip route show 203.0.113.0/24

The final show prints nothing: no route matches, and the prefix is unknown again. These runtime ip route commands were for observing behavior; the persistent routes remain the [Route] sections in the .network files. The same preference logic underlies a pattern from Chapter 17: NetworkManager's ipv4.route-metric ranks a DHCP interface's routes below others, and a deliberately worse backup uplink is simply a second default route with a higher metric.

Longest prefix and metric choose between routes within a table. Policy routing chooses between tables, and the selector can be anything the kernel can match - most usefully the source address. The classic case is a router with two uplinks: packets that arrived from LAN A should leave through uplink A, packets from LAN B through uplink B, and neither should fall back to the other's gateway. Plain main-table routing cannot express that, because both uplinks lead to the same destinations and the table would just pick one.

The lab builds the same shape in miniature: rtrouter can reach rtpeer's test network 198.51.100.0/24 through two next hops - 192.168.220.20 on LAN B (eth2) and 192.168.221.20 on the transit link (eth3). Policy routing sends traffic from LAN A sources out the first and from LAN B sources out the second.

Two pieces of configuration are needed, and both go into the interface files. First, the rules that select a table by source, on rtrouter's eth1 file (replacing the address-only starting state):

# /home/john/debian-handbook/routing/rtrouter-eth1.network
[Match]
Name=eth1

[Network]
Address=192.168.219.2/24
ConfigureWithoutCarrier=yes

[RoutingPolicyRule]
From=192.168.219.0/24
Table=100
Priority=100

[RoutingPolicyRule]
From=192.168.220.0/24
Table=200
Priority=200

Second, the tables themselves. Table 100 holds the LAN-B uplink route and lives in eth2's file; table 200 holds the transit uplink route in eth3's file:

# /home/john/debian-handbook/routing/rtrouter-eth2.network
[Match]
Name=eth2

[Network]
Address=192.168.220.2/24
ConfigureWithoutCarrier=yes

[Route]
Destination=198.51.100.1/32
Gateway=192.168.220.20
Table=100
# /home/john/debian-handbook/routing/rtrouter-eth3.network
[Match]
Name=eth3

[Network]
Address=192.168.221.2/24
ConfigureWithoutCarrier=yes

[Route]
Destination=198.51.100.1/32
Gateway=192.168.221.20
Table=200

A [Route] without Table= lands in main; with it, the route exists only in the named table. The rules are global even though they are written in eth1's file - the file is just where networkd happens to read them. On rtrouter, as john, install all three files and apply them:

$ sudo install -m 0644 rtrouter-eth1.network /etc/systemd/network/10-ch19-eth1.network
$ sudo install -m 0644 rtrouter-eth2.network /etc/systemd/network/10-ch19-eth2.network
$ sudo install -m 0644 rtrouter-eth3.network /etc/systemd/network/10-ch19-eth3.network
$ sudo networkctl reload
$ sudo networkctl reconfigure eth1 eth2 eth3

All five commands are silent. The rules are now in the kernel, in priority order:

$ ip rule show
0:  from all lookup local
100:    from 192.168.219.0/24 lookup 100 proto static
200:    from 192.168.220.0/24 lookup 200 proto static
32766:  from all lookup main
32767:  from all lookup default

The kernel walks this list from the smallest priority upward. Rules 0, 32766, and 32767 are the built-ins: local first (the host's own addresses), then main and default as the fallbacks everyone shares. The two inserted rules sit in between. A packet whose source falls in 192.168.219.0/24 looks up table 100 first; if that table has a matching route the lookup ends there, and if it does not, the walk continues to rule 200 and eventually main - falling through is what makes partial policy routing safe.

ip route get shows the whole decision for one destination, including which table answered:

$ ip route get 198.51.100.1 from 192.168.219.2
198.51.100.1 from 192.168.219.2 via 192.168.220.20 dev eth2 table 100 uid 1001
    cache
$ ip route get 198.51.100.1 from 192.168.220.2
198.51.100.1 from 192.168.220.2 via 192.168.221.20 dev eth3 table 200 uid 1001
    cache

Same destination, two different next hops and interfaces, chosen only by the source address. (uid 1001 is john's user ID; the kernel reports who triggered the lookup.) The tables themselves hold exactly what the .network files wrote:

$ ip route show table 100
198.51.100.1 via 192.168.220.20 dev eth2 proto static
$ ip route show table 200
198.51.100.1 via 192.168.221.20 dev eth3 proto static

Source-based route selection through policy rules

Figure 19.2. Conceptual view of the rule walk on rtrouter: rules are tried by priority, and the first rule whose table contains a matching route wins. Sources outside both LAN prefixes fall through to the main table, which has no route to 198.51.100.0/24 and therefore no opinion.

Two practical notes for real deployments. A source-based rule only matches traffic the host originates or forwards with those source addresses, so the router itself - sending from its own addresses - falls through to main, which is usually what you want for management traffic. And the two uplinks here lead to the same place, so no route is ever wrong, only suboptimal; when uplinks lead to different places, add a default route per table as well, or sources whose table has no default will leak out main.

19.1.4. Reverse Path Filtering and Asymmetric Routing

The kernel guards route tables with a sanity check: when a packet arrives on an interface, would a packet to that source leave through the same interface? If the route table and the arrival disagree, the packet may be spoofed, and rp_filter decides what to do about it:

rp_filter value Behavior
0 No check; packets accepted regardless of the reverse path.
1 Strict: drop unless the reverse route uses the arrival interface.
2 Loose: drop only if no route to the source exists at all.

Debian 13's systemd ships /usr/lib/sysctl.d/50-default.conf, which sets net.ipv4.conf.default.rp_filter=2 and applies loose mode to every interface: arriving packets must at least be routable, but asymmetry is tolerated. For an ordinary host that is the right default. On a router with policy routing, strict mode is actively dangerous - Section 19.1.3 created a topology where the return path may legitimately differ from the arrival path, and strict filtering would drop exactly the traffic the policy was built to carry.

The kernel computes the effective value per interface as the larger of all and the interface's own setting, so a demonstration of strict mode on one interface needs all lowered first. On rtrouter, as john:

$ sudo sysctl -w net.ipv4.conf.all.rp_filter=0 net.ipv4.conf.eth1.rp_filter=1
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.eth1.rp_filter = 1

These -w writes are runtime-only; nothing is persisted, which keeps the experiment contained. Now manufacture an asymmetric path: give the router a route that says rtclient's address is reachable through the trunk instead of LAN A:

$ sudo ip route add 192.168.219.10/32 dev eth4

The command is silent, and the route lookup now prefers it - a /32 beats the connected /24:

$ ip route get 192.168.219.10
192.168.219.10 dev eth4 src 192.168.222.2 uid 1001
    cache

Packets to the client would leave through eth4, so packets from the client arriving on eth1 now fail the strict check. On rtclient, as john:

$ ping -c 2 -W 1 192.168.219.2
PING 192.168.219.2 (192.168.219.2) 56(84) bytes of data.

--- 192.168.219.2 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1015ms

The client's requests die on rtrouter's eth1, silently - reverse-path drops produce no ICMP message, which is why they are so confusing to diagnose: every route the sender consults looks correct. Recover by undoing both changes in reverse order, on rtrouter, as john:

$ sudo ip route del 192.168.219.10/32
$ sudo sysctl -w net.ipv4.conf.eth1.rp_filter=0
net.ipv4.conf.eth1.rp_filter = 0

And on rtclient, as john:

$ ping -c 2 192.168.219.2
PING 192.168.219.2 (192.168.219.2) 56(84) bytes of data.
64 bytes from 192.168.219.2: icmp_seq=1 ttl=64 time=0.337 ms
64 bytes from 192.168.219.2: icmp_seq=2 ttl=64 time=0.303 ms

--- 192.168.219.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1022ms
rtt min/avg/max/mdev = 0.303/0.320/0.337/0.017 ms

The lesson generalizes: when a multi-path router drops traffic that ip route get says should work, check sysctl net.ipv4.conf.<iface>.rp_filter before anything else. Loose mode (2) is the appropriate setting wherever routes are deliberately asymmetric; strict mode belongs on hosts whose routing is expected to be symmetric - an ordinary server, or a router without policy routing.