basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    NAT, port forwarding, and conntrack: SNAT, DNAT, CGNAT, and firewall boundaries

    NAT rewrites IP addresses and TCP/UDP ports on the fly, while the conntrack state table records each translation so replies return to the right host. Covers SNAT and masquerade, port forwarding (DNAT), Hairpin NAT, ISP CGNAT, and why NAT does not replace a firewall.

    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:

    What a NAT router changes in a packet · SNAT and DNATPick a layer
    MAC addresses change just like on any routerRewritten at every hop
    Src / Dst MACnew on egressStandard frame rewrite when routing between networks
    SNAT changes Src IP, DNAT changes Dst IPRewritten at the NAT gateway
    SNAT / masquerade10.0.10.10 > 203.0.113.1Rewrites Src IP in the postrouting hook after routing lookup
    DNAT (forwarding)203.0.113.1 > 10.0.10.10Rewrites Dst IP in the prerouting hook before routing lookup
    TTL and ChecksumTTL − 1 · new sumAfter rewriting the IP, the kernel recalculates the IPv4 header checksum
    Port translation (PAT) and checksum recalculationChanges on forwarding or collision
    On DNATdport 8080 > 80Maps the external port to the internal service’s actual port
    On SNAT (PAT)sport 56992 > 56993If two LAN clients pick the same Src Port, the router reassigns the second one
    TCP/UDP ChecksumrecalculatedThe L4 checksum covers the IP pseudo-header, so it changes alongside the address
    Stores the tuple pair for reverse translationKept until the session closes

    NAT 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:

    1. The packet enters forward, not input. Inside forward, the destination port is already the internal port (80, not 8080). That is why a rule in input does not block published Docker ports (Docker ports bypass the input chain, nftables).
    2. The client’s Src IP stays unchanged. Internal server 10.0.10.10 sees the real client IP 203.0.113.10 in its logs, yet its reply must go back through the same NAT router 10.0.10.1. If the reply takes a different path, the router never translates 10.0.10.10:80 back to 203.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:

    1. LAN 10.0.10.10✓ SYN :56992 > :9000
    2. postrouting✓ Src > 203.0.113.1
    3. conntrack✓ saved tuple pair
    4. 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]
    What happened

    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.

    Check with

    sysctl net.netfilter.nf_conntrack_count and sudo nft list ruleset

    1. WAN client✓ > 203.0.113.1:8080
    2. prerouting✓ Dst > 10.0.10.10:80
    3. forward✓ accept dport 80
    4. 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]
    What happened

    prerouting rewrote Dst IP and port, forward allowed it through, and conntrack rewrote the reply back to come from 203.0.113.1:8080.

    Check with

    sudo nft list ruleset | grep -E "dnat|forward"

    1. LAN .20✓ > 203.0.113.1:8080
    2. Router DNAT✓ Dst > 10.0.10.10:80
    3. Server .10✕ replies straight to .20
    4. 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]
    Cause

    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.

    Check with

    Enable Hairpin NAT on the router or configure internal DNS

    1. Internet✓ > ISP public IP
    2. ISP NAT✕ no DNAT to customer
    # On router (WAN): 100.64.18.42/24
    # External IP (curl -4 ifconfig.me): 203.0.113.77
    Cause

    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.

    Check with

    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).

    Does a rule in `input` block a published Docker port?
    docker compose: ports: ["8080:80"]
    nftables (input): tcp dport 8080 drop
    Does a rule in `input` block a published Docker port?
    Answer:
    Port 8080 is still reachable from outside: the packet went through prerouting (DNAT) and forward

    Docker publishes container ports with DNAT rules in prerouting. Once Dst IP changes to the container’s address, the kernel sees the packet is not for the host itself and routes it into forward — input never sees it.

    Why does a port-forwarded site fail to open from inside its own LAN?
    Client 10.0.10.20 ──► router 203.0.113.1:8080 (DNAT) ──► server 10.0.10.10:80
                                                              (Hairpin NAT off)
    Why does a port-forwarded site fail to open from inside its own LAN?
    Answer:
    10.0.10.10.80 > 10.0.10.20.51200: Flags [S.] -> client sends Flags [R]

    Plain DNAT changes Dst IP only and leaves Src IP = 10.0.10.20. Server .10 recognizes .20 as a local neighbor and replies straight over L2, skipping the router’s reverse NAT. Fix it with Hairpin SNAT (rewriting Src IP to the router’s IP) or a local DNS record pointing directly to 10.0.10.10.

    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’s forward chain dropped the traffic (where the destination port is already 80, not 8080) or 10.0.10.10’s default gateway bypasses this router.
    Fix
    Inspect the forward chain in sudo nft list ruleset (allow internal port 80 or ct status dnat) and check ip route get <client-ip> on 10.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/24 hits the router’s public IP, the router DNATs to 10.0.10.10, and the server replies directly over LAN with an unexpected Src IP (10.0.10.10 instead of 203.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/10 or 10.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 into forward and skips input.
    Fix
    Filter published container ports in forward or DOCKER-USER, or bind published ports to 127.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 conntrack state table is full (Linux often defaults to 65536 entries): 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 via sudo conntrack -L (from the conntrack package), and raise nf_conntrack_max in /etc/sysctl.d/.