18.4. Filtering Routed and Translated Traffic
A router's firewall answers a question a host never faces: what may
pass through? The forward chain decides that, but two other kernel
mechanisms must be in place first - packet forwarding itself, which is
a routing function netfilter does not provide, and the address
rewriting that makes a private network reach an untrusted one. This
section enables forwarding, builds the forward policy on fwrouter,
and then adds IPv4 masquerading and destination NAT, keeping IPv6
untranslated throughout.
18.4.1. Forwarding Policy Between Trusted and Untrusted Networks
Debian leaves packet forwarding off: both net.ipv4.ip_forward and
net.ipv6.conf.all.forwarding default to zero, and no filtering rule
can change that. On fwrouter, in a root shell, create
/etc/sysctl.d/90-forwarding.conf with the two enabling settings:
net.ipv4.ip_forward = 1
net.ipv6.conf.all.forwarding = 1
Apply it the way Chapter 7 applies sysctl files -
sysctl --system walks every file in order and prints what it sets:
# sysctl --system
* Applying /usr/lib/sysctl.d/10-coredump-debian.conf ...
* Applying /usr/lib/sysctl.d/50-default.conf ...
* Applying /usr/lib/sysctl.d/50-pid-max.conf ...
* Applying /etc/sysctl.d/90-forwarding.conf ...
kernel.core_pattern = core
kernel.sysrq = 0x01b6
kernel.core_uses_pid = 1
net.ipv4.conf.default.rp_filter = 2
net.ipv4.conf.eth0.rp_filter = 2
net.ipv4.conf.eth1.rp_filter = 2
net.ipv4.conf.eth2.rp_filter = 2
net.ipv4.conf.eth3.rp_filter = 2
net.ipv4.conf.lo.rp_filter = 2
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.eth0.accept_source_route = 0
net.ipv4.conf.eth1.accept_source_route = 0
net.ipv4.conf.eth2.accept_source_route = 0
net.ipv4.conf.eth3.accept_source_route = 0
net.ipv4.conf.lo.accept_source_route = 0
net.ipv4.conf.default.promote_secondaries = 1
net.ipv4.conf.eth0.promote_secondaries = 1
net.ipv4.conf.eth1.promote_secondaries = 1
net.ipv4.conf.eth2.promote_secondaries = 1
net.ipv4.conf.eth3.promote_secondaries = 1
net.ipv4.conf.lo.promote_secondaries = 1
net.ipv4.ping_group_range = 0 2147483647
net.core.default_qdisc = fq_codel
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
fs.protected_regular = 2
fs.protected_fifos = 1
vm.max_map_count = 1048576
kernel.pid_max = 4194304
net.ipv4.ip_forward = 1
net.ipv6.conf.all.forwarding = 1
The banners and the applied values interleave; the last two lines are
the new file's contribution, and the rp_filter values reasserted
along the way matter later, in Section 18.5.3. Check the two settings
directly:
# sysctl net.ipv4.ip_forward net.ipv6.conf.all.forwarding
net.ipv4.ip_forward = 1
net.ipv6.conf.all.forwarding = 1
One side effect is worth knowing: with forwarding enabled, the kernel
stops configuring its own addresses from router advertisements on
forwarded interfaces, which suits a router with static addresses. A
host that must both forward and learn addresses from RAs needs
accept_ra=2, as the kernel's ip-sysctl documentation describes.
Now the policy. On fwrouter, as john, create
~/debian-handbook/firewall/router-base.nft - the full routed
ruleset, filter and nat tables together:
flush ruleset
define mgmt_if = "eth0"
define lan_if = "eth1"
define wan_if = "eth2"
define dmz_if = "eth3"
define mgmt_net = 192.168.121.0/24
define lan_net = 192.168.218.0/24
define wan_net = 192.168.219.0/24
define dmz_net = 192.168.220.0/24
define server4 = 192.168.220.10
define server6 = 2001:db8:18:3::10
table inet filter {
counter forwarded_web {}
counter forward_drops {}
counter input_drops {}
chain input {
type filter hook input priority filter; policy drop;
ct state invalid counter name input_drops drop
ct state established,related accept
iifname "lo" accept
iifname $mgmt_if ip saddr $mgmt_net tcp dport 22 accept
iifname $lan_if ip saddr $lan_net tcp dport 22 accept
icmp type { echo-request, destination-unreachable, time-exceeded, parameter-problem } accept
icmpv6 type { echo-request, destination-unreachable, packet-too-big, time-exceeded, parameter-problem, nd-router-solicit, nd-router-advert, nd-neighbor-solicit, nd-neighbor-advert } accept
counter name input_drops
limit rate 5/minute burst 2 packets log prefix "ch18-router-input: " level info
drop
}
chain forward {
type filter hook forward priority filter; policy drop;
ct state invalid counter name forward_drops drop
ct state established,related accept
iifname $lan_if oifname $wan_if ip saddr $lan_net ip daddr 192.168.219.20 tcp dport { 80, 8080 } counter name forwarded_web accept
iifname $lan_if oifname $dmz_if ip saddr $lan_net ip daddr $server4 tcp dport 80 counter name forwarded_web accept
iifname $wan_if oifname $dmz_if ct status dnat tcp dport 80 counter name forwarded_web accept
iifname $lan_if oifname $wan_if ip6 saddr 2001:db8:18:1::/64 ip6 daddr 2001:db8:18:2::20 tcp dport 80 accept
iifname $lan_if oifname $dmz_if ip6 saddr 2001:db8:18:1::/64 ip6 daddr $server6 tcp dport 80 accept
iifname $lan_if oifname $wan_if icmp type echo-request accept
iifname $lan_if oifname $dmz_if icmp type echo-request accept
iifname $lan_if oifname $wan_if icmpv6 type { echo-request, destination-unreachable, packet-too-big, time-exceeded } accept
iifname $lan_if oifname $dmz_if icmpv6 type { echo-request, destination-unreachable, packet-too-big, time-exceeded } accept
counter name forward_drops
limit rate 5/minute burst 2 packets log prefix "ch18-router-forward: " level info
drop
}
chain output {
type filter hook output priority filter; policy accept;
}
}
table ip nat {
chain prerouting {
type nat hook prerouting priority dstnat; policy accept;
iifname $wan_if ip daddr 192.168.219.2 tcp dport 8080 dnat to $server4:80
}
chain postrouting {
type nat hook postrouting priority srcnat; policy accept;
oifname $wan_if ip saddr $lan_net masquerade
}
}
In the root shell on fwrouter, replace the running policy - install,
check and load, all silent on success:
# install -m 0644 /home/john/debian-handbook/firewall/router-base.nft /etc/nftables.conf
# nft -c -f /etc/nftables.conf
# nft -f /etc/nftables.conf
This is the atomic-replacement pattern from Section 18.3.1 applied to a bigger file: the flush and both tables commit as one transaction, and a mistake in either table would leave the router running the previous ruleset unchanged. Read back the forward chain:
# nft list chain inet filter forward
table inet filter {
chain forward {
type filter hook forward priority filter; policy drop;
ct state invalid counter name "forward_drops" drop
ct state established,related accept
iifname "eth1" oifname "eth2" ip saddr 192.168.218.0/24 ip daddr 192.168.219.20 tcp dport { 80, 8080 } counter name "forwarded_web" accept
iifname "eth1" oifname "eth3" ip saddr 192.168.218.0/24 ip daddr 192.168.220.10 tcp dport 80 counter name "forwarded_web" accept
iifname "eth2" oifname "eth3" ct status dnat tcp dport 80 counter name "forwarded_web" accept
iifname "eth1" oifname "eth2" ip6 saddr 2001:db8:18:1::/64 ip6 daddr 2001:db8:18:2::20 tcp dport 80 accept
iifname "eth1" oifname "eth3" ip6 saddr 2001:db8:18:1::/64 ip6 daddr 2001:db8:18:3::10 tcp dport 80 accept
iifname "eth1" oifname "eth2" icmp type echo-request accept
iifname "eth1" oifname "eth3" icmp type echo-request accept
iifname "eth1" oifname "eth2" icmpv6 type { destination-unreachable, packet-too-big, time-exceeded, echo-request } accept
iifname "eth1" oifname "eth3" icmpv6 type { destination-unreachable, packet-too-big, time-exceeded, echo-request } accept
counter name "forward_drops"
limit rate 5/minute burst 2 packets log prefix "ch18-router-forward: " level info
drop
}
}
The forward rules are directional by construction: each names the
incoming and outgoing interface, the source prefix and the
destination, so traffic is admitted one (network, network) pair at a
time. The LAN may reach the WAN host's web ports and the DMZ server's
web port; the WAN may reach the DMZ server on port 80 only when the
connection was destination-NATted - the ct status dnat match - which
Section 18.4.2 turns into the published-address rule; and no rule at
all lets the WAN or DMZ initiate toward the LAN. Replies need no
rules: ct state established,related accept returns everything the
forward rules admitted. The input chain is the router's own host
policy from Section 18.2.4 with counters and logging added, unchanged
in substance. The nat table is new, and small enough to read whole:
# nft list table ip nat
table ip nat {
chain prerouting {
type nat hook prerouting priority dstnat; policy accept;
iifname "eth2" ip daddr 192.168.219.2 tcp dport 8080 dnat to 192.168.220.10:80
}
chain postrouting {
type nat hook postrouting priority srcnat; policy accept;
oifname "eth2" ip saddr 192.168.218.0/24 masquerade
}
}
# nft list tables
table inet filter
table ip nat
Two chains, each at the hook Figure 18.1 places it at: prerouting at
dstnat (-100), where a packet's destination can be rewritten before
routing decides, and postrouting at srcnat (100), where the source
can be rewritten as the packet leaves. The destination rule matches
the router's own WAN address and port 8080, and rewrites the
destination to the DMZ server's port 80. The scope matters: a rule
matching only the port, without ip daddr 192.168.219.2, would
redirect any port-8080 packet entering eth2 - including transit
traffic bound for the LAN - to the DMZ server. The source rule
masquerades the whole LAN prefix on its way out the WAN interface.
18.4.2. IPv4 Masquerading and Destination NAT
Both translations are easiest to believe by watching what the endpoints record. Figure 18.2 maps the two flows this ruleset translates.
Figure 18.2. A conceptual view of the two translated flows: a LAN request masqueraded out the WAN interface, and a WAN request to the router's published address destination-NATted to the DMZ server. What each far endpoint puts in its log is the evidence the text reads.
The masquerade first. On fwclient, as john, request the WAN host's
page:
$ curl -sS --max-time 3 http://192.168.219.20/
hello from remote 80
The request crossed the router and came back. On fwremote, in a root
shell, read what the web server saw:
# tail -n 1 /var/log/nginx/access.log
192.168.219.2 - - [21/Nov/2026:13:55:41 +0000] "GET / HTTP/1.1" 200 21 "-" "curl/8.14.1"
The client's log-worthy address is 192.168.219.2 - the router's WAN
address, not 192.168.218.10. The postrouting rule rewrote the source
on the way out, and conntrack stored the translation so the reply
found its way back to fwclient with no reverse rule of any kind.
The same request toward the DMZ shows the opposite case, no rewriting
at all. On fwclient, as john:
$ curl -sS --max-time 3 http://192.168.220.10/
hello from server 80
On fwserver, in a root shell:
# tail -n 1 /var/log/nginx/access.log
192.168.218.10 - - [21/Nov/2026:13:55:47 +0000] "GET / HTTP/1.1" 200 21 "-" "curl/8.14.1"
Directly routed traffic crosses the router with both addresses untouched: the DMZ is reachable without NAT, so the ruleset does not translate it.
Destination NAT is the published-service pattern: the WAN host asks
for the router's address on port 8080, and the router delivers the
connection to the DMZ server's port 80. On fwremote, as john:
$ curl -sS --max-time 3 http://192.168.219.2:8080/
hello from server 80
On fwserver, in a root shell:
# tail -n 1 /var/log/nginx/access.log
192.168.219.20 - - [21/Nov/2026:13:55:53 +0000] "GET / HTTP/1.1" 200 21 "-" "curl/8.14.1"
The server answered a request from the WAN host's own address, on port
80 - the destination was rewritten in prerouting, the original client
source was preserved, and the forward chain admitted it through the
ct status dnat rule.
What the policy denies is equally explicit. Three attempts that no
forward rule matches, each from the network that must not make them.
On fwremote, as john, toward the LAN host:
$ curl -sS --max-time 3 http://192.168.218.10:8080/
curl: (28) Connection timed out after 3002 milliseconds
Still on fwremote, toward the DMZ server's unlisted port:
$ curl -sS --max-time 3 http://192.168.220.10:8081/
curl: (28) Connection timed out after 3002 milliseconds
On fwserver, as john, toward the LAN host:
$ curl -sS --max-time 3 http://192.168.218.10:8080/
curl: (28) Connection timed out after 3002 milliseconds
All three timed out - dropped at the router's forward chain, the same
silence a host firewall produces. The router records them the same way
the host did. On fwrouter, in the root shell, the accept counter
first:
# nft list counter inet filter forwarded_web
table inet filter {
counter forwarded_web {
packets 3 bytes 180
}
}
Three packets - one from each of the accepted connections above. The drop log next:
# journalctl -k -g ch18-router-forward --no-pager | tail -n 4
Nov 21 13:55:57 fwrouter kernel: ch18-router-forward: IN=eth2 OUT=eth1 MAC=52:54:00:d0:d6:a1:52:54:00:72:c5:c2:08:00 SRC=192.168.219.20 DST=192.168.218.10 LEN=60 TOS=0x00 PREC=0x00 TTL=63 ID=33893 DF PROTO=TCP SPT=38446 DPT=8080 WINDOW=64240 RES=0x00 SYN URGP=0
Nov 21 13:55:58 fwrouter kernel: ch18-router-forward: IN=eth2 OUT=eth1 MAC=52:54:00:d0:d6:a1:52:54:00:72:c5:c2:08:00 SRC=192.168.219.20 DST=192.168.218.10 LEN=60 TOS=0x00 PREC=0x00 TTL=63 ID=33894 DF PROTO=TCP SPT=38446 DPT=8080 WINDOW=64240 RES=0x00 SYN URGP=0
The log lines name the direction a curl's timeout could only imply:
IN=eth2 OUT=eth1, source 192.168.219.20, destination port 8080 -
a WAN host trying the LAN, one second apart, the burst allowance
logging the first two of the three SYN retransmissions the request
made. The counter that counts every one:
# nft list counter inet filter forward_drops
table inet filter {
counter forward_drops {
packets 9 bytes 540
}
}
Nine packets, three per denied request, every retransmission counted
whether the rate limit let it be logged or not. The forward log line
differs from the host's in one useful field: OUT=eth1 records the
route the kernel had already chosen for the packet when the drop
happened, which for a router is half the diagnosis.
Masquerading has one alternative worth knowing. masquerade reads
the outgoing interface's address at translation time, which is exactly
right when that address is dynamic - a residential or cloud uplink
that can change without notice. When the source address is static,
snat to address says so explicitly:
| Option | When to use | Address handling |
|---|---|---|
masquerade |
Dynamic or unknown outgoing address | Reads the interface's current address per flow |
snat to address |
Static outgoing address | Fixed address compiled into the rule |
Both record the same conntrack entry and need no reverse rule; the
choice is about stating what is already true. snat on a dynamic
uplink silently keeps translating to a stale address after the
interface's address changes, which is the failure mode masquerade
exists to avoid.
18.4.3. IPv6 Forwarding Without Assuming NAT
The routed ruleset forwards IPv6 in the same forward chain, with
explicit ip6 rules, and no nat table for it at all -
nft list tables above showed exactly two tables, inet filter and
ip nat. The practice network's IPv6 prefixes are global-scope
documentation addresses, and each host has a real address of its own;
there is no address shortage to work around and no second addressing
realm to bridge. The tests look like their IPv4 counterparts. On
fwclient, as john, request the DMZ server by its IPv6 address - the
-g flag turns off curl's glob interpretation so the brackets in the
URL are literal:
$ curl -sS --max-time 3 -g "http://[2001:db8:18:3::10]/"
hello from server 80
On fwserver, in a root shell:
# tail -n 1 /var/log/nginx/access.log
2001:db8:18:1::10 - - [21/Nov/2026:13:56:20 +0000] "GET / HTTP/1.1" 200 21 "-" "curl/8.14.1"
The client's own global address appears in the log: forwarded,
untranslated, exactly like the routed IPv4 case. On fwclient, as
john, the WAN host's page:
$ curl -sS --max-time 3 -g "http://[2001:db8:18:2::20]/"
hello from remote 80
On fwremote, in a root shell:
# tail -n 1 /var/log/nginx/access.log
2001:db8:18:1::10 - - [21/Nov/2026:13:56:26 +0000] "GET / HTTP/1.1" 200 21 "-" "curl/8.14.1"
And the denial works the same way - on fwremote, as john, the LAN
host's unlisted port times out:
$ curl -sS --max-time 3 -g "http://[2001:db8:18:1::10]:8080/"
curl: (28) Connection timed out after 3002 milliseconds
No rule admits WAN-to-LAN IPv6, so the forward chain dropped it -
firewall policy, not address translation, is the control that matters
here. NAT for IPv6 does exist: nftables can masquerade or DNAT the
ip6 family, and RFC 6296 defines network prefix translation (NPTv6)
for the one case it was designed for - making two sites' different
prefixes look alike to each other. Neither is needed for ordinary
routing, and neither is in this ruleset; reaching for IPv6 NAT by
analogy with IPv4 habit adds state and failure modes for no gain. What
IPv6 genuinely requires is the neighbour discovery already allowed in
every ruleset of this chapter, and the ICMPv6 forwarding rules on the
router - packet-too-big above all, because path-MTU discovery is
mandatory in IPv6 and a filtered packet-too-big message produces
connections that stall on large transfers
(Chapter 17 covers the failure from
the host side).
18.4.4. Interaction with Container and Virtualization Rules
Container runtimes and virtualization managers configure the same
kernel tables this chapter has been writing. Docker, for example,
creates its own chains - DOCKER among them, plus DOCKER-USER
specifically so that site rules can sit in Docker's path - and adds
NAT rules whenever a container publishes a port; libvirt adds
forwarding and NAT rules when a managed network with NAT mode starts.
On Debian 13 both speak through iptables-nft by default, so what they
create lands in the same nftables tables nft list reads. The names
below imitate what those daemons create; no container runtime is
needed to observe the mechanism. On fwrouter, in the root shell,
create the chain and one rule, both silent on success:
# iptables -t nat -N DOCKER
# iptables -t nat -A DOCKER -d 172.17.0.2/32 -p tcp --dport 80 -j ACCEPT
The nat table now holds three chains with two authors:
# nft list table ip nat
table ip nat {
chain prerouting {
type nat hook prerouting priority dstnat; policy accept;
iifname "eth2" ip daddr 192.168.219.2 tcp dport 8080 dnat to 192.168.220.10:80
}
chain postrouting {
type nat hook postrouting priority srcnat; policy accept;
oifname "eth2" ip saddr 192.168.218.0/24 masquerade
}
chain DOCKER {
ip daddr 172.17.0.2 tcp dport 80 counter packets 0 bytes 0 accept
}
}
The DOCKER chain sits beside the routed ruleset's chains in the same
table, added through a different command family, visible to both.
Remove it again through the same interface that created it; both
commands print nothing:
# iptables -t nat -F DOCKER
# iptables -t nat -X DOCKER
The coexistence is the lesson, and it has three practical
consequences on a host that runs containers or virtual machines.
First, the blunt flush ruleset that heads this chapter's ruleset
files is safe on a dedicated firewall but destructive on a container
host: it removes the runtime's chains too, breaking published ports
and network isolation until the daemon re-creates them. Scope such a
host's replacement to the tables it owns - flush table inet filter
clears the filter table and nothing else - or keep the site policy in
a table with its own name. Second, filtering decisions interleave by
chain priority, so a site rule and a runtime rule at the same hook are
ordered by their priorities whether either author intended it or not;
Docker's DOCKER-USER chain exists exactly
to give the site a defined place in that order.
Chapter 24 covers the virtualization side and
Chapter 25 the container side of the same
boundary, including where each runtime expects site rules to go.
Third, the iptables tool remains the right emergency read interface
on such a host - iptables-save and iptables-translate speak the
older vocabulary many runtimes still document, while nft list ruleset
shows everything regardless of which tool wrote it.