What you’ll get
Check conntrack table usage on a Linux host and compare the same outbound TCP flow on both sides of a NAT
router (10.0.10.1 on LAN, 203.0.113.1 on WAN):
sysctl net.netfilter.nf_conntrack_count net.netfilter.nf_conntrack_max
# net.netfilter.nf_conntrack_count = 22
# net.netfilter.nf_conntrack_max = 65536
Gateway nftables rules — one to forward an inbound port (dnat) and one to translate the LAN outbound
(masquerade):
table ip nat {
chain prerouting {
type nat hook prerouting priority dstnat; policy accept;
iifname "wan0" tcp dport 8080 dnat to 10.0.10.10:80
}
chain postrouting {
type nat hook postrouting priority srcnat; policy accept;
ip saddr 10.0.10.0/24 oifname "wan0" masquerade
}
}
Unlike an ordinary router in Packets and encapsulation that swaps only MAC addresses and decrements TTL, a NAT
router rewrites L3 and L4 headers and records the mapping in conntrack:
Src IP in the postrouting hook after routing lookupDst IP in the prerouting hook before routing lookupSrc Port, the router reassigns the second oneNAT evaluates rules only on the first packet of a flow. Every later packet and reply is translated automatically from the conntrack entry.
SNAT and masquerade: how a whole network shares one IP
ISPs do not route private ranges 10.0.0.0/8, 172.16.0.0/12, and 192.168.0.0/16 (IPv4, masks, and CIDR) on the
public internet. To let dozens of devices behind a home router — or Docker containers behind docker0 — reach
the internet through one public IP 203.0.113.1, the gateway performs SNAT (Source NAT; combined with port
translation, also called PAT or NAPT).
Src IP translation runs in postrouting — after the routing decision has already picked the egress
interface. Capturing one connection from 10.0.10.10 to 203.0.113.10:9000 on both sides of the gateway shows
the rewrite directly:
# Internal interface (LAN): client sends from its own 10.0.10.10
# 18:50:13.539320 IP 10.0.10.10.56992 > 203.0.113.10.9000: Flags [S], seq 1583712191, length 0
# 18:50:13.539371 IP 203.0.113.10.9000 > 10.0.10.10.56992: Flags [S.], ack 1583712192, length 0
# External interface (WAN): router substituted its own 203.0.113.1
# 18:50:13.539355 IP 203.0.113.1.56992 > 203.0.113.10.9000: Flags [S], seq 1583712191, length 0
# 18:50:13.539368 IP 203.0.113.10.9000 > 203.0.113.1.56992: Flags [S.], ack 1583712192, length 0
Remote server 203.0.113.10 sees only the router’s 203.0.113.1 and replies to 203.0.113.1:56992. When the
reply arrives on WAN, conntrack matches the flow and rewrites Dst IP back to 10.0.10.10. If a second LAN
client connects to the same server from the same ephemeral port 56992 at the same time, the router assigns it a
free port (say 56993) to keep replies separate.
Both nftables and iptables offer two outbound NAT actions:
snat to 203.0.113.1— use with a static public IP so the kernel does not look up the interface address on every new flow.masquerade— picks up the egress interface’s current IP automatically; fits dynamic ISP addresses (DHCP/PPPoE) or a NAT bridge on a virtualization host (Proxmox networking).
DNAT (port forwarding) and firewall hook order
To accept an inbound internet connection for an internal service behind a router (or a Docker container), use
DNAT (Destination NAT). It rewrites Dst IP and Dst Port in the prerouting hook — before the routing
table and before input or forward.
Same live lab, now with external client 203.0.113.10 connecting to 203.0.113.1:8080, forwarded to
10.0.10.10:80:
# External interface (WAN): client connects to 203.0.113.1:8080
# 18:50:14.900395 IP 203.0.113.10.57514 > 203.0.113.1.8080: Flags [S], seq 1608493753, length 0
# 18:50:14.900447 IP 203.0.113.1.8080 > 203.0.113.10.57514: Flags [S.], ack 1608493754, length 0
# Internal interface (LAN): after DNAT, packet targets 10.0.10.10:80
# 18:50:14.900422 IP 203.0.113.10.57514 > 10.0.10.10.80: Flags [S], seq 1608493753, length 0
# 18:50:14.900441 IP 10.0.10.10.80 > 203.0.113.10.57514: Flags [S.], ack 1608493754, length 0
Because DNAT runs in prerouting, two consequences follow:
- The packet enters
forward, notinput. Insideforward, the destination port is already the internal port (80, not8080). That is why a rule ininputdoes not block published Docker ports (Docker ports bypass the input chain, nftables). - The client’s
Src IPstays unchanged. Internal server10.0.10.10sees the real client IP203.0.113.10in its logs, yet its reply must go back through the same NAT router10.0.10.1. If the reply takes a different path, the router never translates10.0.10.10:80back to203.0.113.1:8080, and the client drops the unmatched packet.
| NAT type | netfilter hook |
What it rewrites | When it runs | Typical use |
|---|---|---|---|---|
SNAT / masquerade |
postrouting |
Src IP (and Src Port on collision) |
After routing, right before WAN egress | Home LAN or containers reaching the internet |
| DNAT | prerouting |
Dst IP and Dst Port |
Before routing and forward |
Port forwarding to a home server, Docker ports: |
conntrack, Hairpin NAT, CGNAT, and firewall boundaries
The conntrack table backs both NAT and stateful firewall rules (ct state established,related). On Ubuntu
24.04, check usage via sysctl net.netfilter.nf_conntrack_count and nf_conntrack_max (default 65536), and
list active flows with sudo conntrack -L from the conntrack package. For each flow, the kernel stores the
original and reply address/port tuples, connection state, and expiration timer.
Four common NAT scenarios — from standard outbound SNAT to Hairpin NAT and ISP CGNAT:
- LAN 10.0.10.10✓ SYN :56992 > :9000
- postrouting✓ Src > 203.0.113.1
- conntrack✓ saved tuple pair
- Server :9000✓ replies to 203.0.113.1
LAN: 10.0.10.10.56992 > 203.0.113.10.9000: Flags [S] WAN: 203.0.113.1.56992 > 203.0.113.10.9000: Flags [S]
The router replaced the private Src IP with its public IP, stored the mapping in conntrack, and translated the reply back to the LAN client.
sysctl net.netfilter.nf_conntrack_count and sudo nft list ruleset
- WAN client✓ > 203.0.113.1:8080
- prerouting✓ Dst > 10.0.10.10:80
- forward✓ accept dport 80
- Server :80✓ replies via gateway
WAN: 203.0.113.10.57514 > 203.0.113.1.8080: Flags [S] LAN: 203.0.113.10.57514 > 10.0.10.10.80: Flags [S]
prerouting rewrote Dst IP and port, forward allowed it through, and conntrack rewrote the reply back to come from 203.0.113.1:8080.
sudo nft list ruleset | grep -E "dnat|forward"
- LAN .20✓ > 203.0.113.1:8080
- Router DNAT✓ Dst > 10.0.10.10:80
- Server .10✕ replies straight to .20
- LAN .20✕ RST (wrong Src IP)
10.0.10.20.51200 > 203.0.113.1.8080: Flags [S] 10.0.10.10.80 > 10.0.10.20.51200: Flags [S.] # bypassed router! 10.0.10.20.51200 > 10.0.10.10.80: Flags [R]
Without Hairpin SNAT, server .10 sees Src IP = 10.0.10.20 on its own subnet and replies straight over L2, bypassing the router. The client expects 203.0.113.1 and sends RST.
Enable Hairpin NAT on the router or configure internal DNS
- Internet✓ > ISP public IP
- ISP NAT✕ no DNAT to customer
- WAN 100.64.x.x— silence
- Home server— never arrived
# On router (WAN): 100.64.18.42/24 # External IP (curl -4 ifconfig.me): 203.0.113.77
The ISP shares one public IPv4 across many subscribers via CGNAT (100.64.0.0/10). You cannot open inbound ports on the home router.
Compare the router’s WAN address with curl -4 ifconfig.me
Why NAT is not a firewall. On a home router, NAT creates the illusion of security because unsolicited packets
from the internet never reach your PC. In practice, that is only a side effect of having no matching conntrack
entry or DNAT rule. NAT does not restrict outbound traffic (a compromised host or camera can phone home freely),
does not isolate devices inside the LAN, and disappears the moment you enable IPv6, where every device gets a
global address. Access control belongs in explicit firewall rules (policy drop on inbound and transit chains,
nftables).
If it doesn’t work
Forwarded port 8080 to internal service 10.0.10.10:80, yet external connections time out
- Cause
- The DNAT rule fired in
prerouting, then either the firewall’sforwardchain dropped the traffic (where the destination port is already80, not8080) or10.0.10.10’s default gateway bypasses this router. - Fix
- Inspect the
forwardchain insudo nft list ruleset(allow internal port80orct status dnat) and checkip route get <client-ip>on10.0.10.10.
Your self-hosted site opens over mobile data by its public domain, yet fails on home Wi-Fi
- Cause
- Hairpin NAT (NAT Loopback) is missing: a client in
10.0.10.0/24hits the router’s public IP, the router DNATs to10.0.10.10, and the server replies directly over LAN with an unexpectedSrc IP(10.0.10.10instead of203.0.113.1), so the client drops it. - Fix
- Enable Hairpin NAT (SNAT/masquerade for LAN traffic hitting the forwarded port) or serve the local IP for that domain from internal DNS (split-horizon DNS).
Port forwarding is configured correctly on the router, yet packets from the internet never reach WAN
- Cause
- Your connection sits behind ISP CGNAT (the WAN port holds an address in
100.64.0.0/10or10.0.0.0/8), so inbound connections stop at the provider’s NAT. - Fix
- Compare the router’s WAN address with
curl -4 ifconfig.me. Options: request a public IPv4 from the ISP, switch to IPv6, or use an outbound tunnel (WireGuard to a VPS, Cloudflare Tunnel).
Blocked a container port in the firewall’s input chain, yet the service is still reachable from outside
- Cause
- Docker publishes ports with DNAT in
prerouting, which sends packets intoforwardand skipsinput. - Fix
- Filter published container ports in
forwardorDOCKER-USER, or bind published ports to127.0.0.1(Docker ports bypass the input chain).
Under load on a router or Docker host, new connections drop and dmesg logs nf_conntrack: table full, dropping packet
- Cause
- The
conntrackstate table is full (Linux often defaults to65536entries): without a free slot, the kernel drops new packets. - Fix
- Check usage with
sysctl net.netfilter.nf_conntrack_count net.netfilter.nf_conntrack_max, find what is generating flows viasudo conntrack -L(from theconntrackpackage), and raisenf_conntrack_maxin/etc/sysctl.d/.