SkillPilotAI

NAT, PAT and port forwarding

NAT (Network Address Translation) lets a whole home or office of private IPs share one public IPv4 address. Knowing SNAT, masquerade, PAT, DNAT (port forwarding), hairpin NAT, CGNAT and the conntrack table explains most 'it works inside but not from outside' problems.

94.1 Why is it used?

  • IPv4 has only about 4 billion addresses, far fewer than the devices in the world, so homes use private IPs (192.168.x.x, 10.x, 172.16-31.x) inside.
  • Private ranges are not routed on the internet: a reply to 192.168.114.10 gets dropped at the ISP because no router knows a path to it.
  • NAT rewrites the private address to the router's public IP on the way out and back again on the way in.
SNAT on the way out, reverse on the way back
  1. 1.PC 1 sends src 192.168.114.10:40000 -> dst 198.51.100.80:80

  2. 2.Home router SNAT: src becomes 203.0.113.10:40000 (stored in conntrack)

  3. 3.Web server replies to dst 203.0.113.10:40000

  4. 4.Router looks up conntrack and rewrites dst back to 192.168.114.10:40000

  5. 5.PC 1 gets the reply as if it talked directly

Masquerade = SNAT that picks the WAN interface's current IP by itself

94.2 How it works: SNAT, masquerade and PAT

TypeWhat changesTypical use
SNATSource IP of outgoing packets (fixed public IP)Office with a static public IP
MasqueradeSource IP, using the interface's current IPHome router whose ISP IP changes on reboot
PAT (NAT overload)Source port too, so one IP is sharedThousands of devices behind one IP
DNATDestination IP/port of incoming packetsPort forwarding to an inside server
CGNATISP does a second NAT (100.64.0.0/10)Mobile and many broadband ISPs
  • If PC 1 and Laptop 2 both use source port 41000 to the same server, the router keeps PC 1 at 41000 and changes the laptop's port to 12654.
  • Every mapping is written to the NAT table; on Linux this is the conntrack table.
  • Each conntrack line has two halves: the original direction (what the PC sent) and the reply direction (what the router expects back).

94.3 Practical: no NAT vs masquerade

Lab: one privileged Docker container with network namespaces: PC 1 (192.168.114.10), Laptop 2 (.11), home router (WAN 203.0.113.10), ISP router and web server 198.51.100.80.

Terminal
# home routeriptables -t nat -A POSTROUTING \  -s 192.168.114.0/24 -o wan0 \  -j MASQUERADEiptables -t nat -S POSTROUTING# PC 1nc -v -w 3 -p 40000 198.51.100.80 80
Output
$ nc -v -w 3 -p 40000 198.51.100.80 80nc: connect to 198.51.100.80 port 80 (tcp) timed out: Operation in progressweb$ cat /tmp/w1.txt23:07:08.666319 IP 192.168.114.10.40000 > 198.51.100.80.80: Flags [S], seq 1360199522, win 64240, options [mss 1460,sackOK,TS val 1061945411 ecr 0,nop,wscale 10], length 023:07:08.666338 IP 198.51.100.80.80 > 192.168.114.10.40000: Flags [S.], seq 3371886239, ack 1360199523, win 65160, options [mss 1460,sackOK,TS val 230638312 ecr 1061945411,nop,wscale 10], length 0$ iptables -t nat -S POSTROUTING-A POSTROUTING -s 192.168.114.0/24 -o wan0 -j MASQUERADE$ nc -v -w 3 -p 40000 198.51.100.80 80Connection to 198.51.100.80 80 port [tcp/http] succeeded!web saw 203.0.113.10 port 40000
  • Without NAT: the server's SYN-ACK goes to a private IP and never comes back, so nc times out.
  • With the MASQUERADE rule in nat/POSTROUTING on wan0, the server sees source 203.0.113.10.

94.4 Practical: PAT and the conntrack table

Terminal
# PC 1 and Laptop 2, same source portnc -v -w 3 -p 41000 198.51.100.80 80# home routerconntrack -L -p tcp --dport 80
Output
pc1$ nc -v -w 3 -p 41000 198.51.100.80 80web saw 203.0.113.10 port 41000pc2$ nc -v -w 3 -p 41000 198.51.100.80 80web saw 203.0.113.10 port 12654$ conntrack -L -p tcp --dport 80tcp      6 115 TIME_WAIT src=192.168.114.10 dst=198.51.100.80 sport=40000 dport=80 src=198.51.100.80 dst=203.0.113.10 sport=80 dport=40000 [ASSURED] mark=0 use=1tcp      6 117 TIME_WAIT src=192.168.114.10 dst=198.51.100.80 sport=41000 dport=80 src=198.51.100.80 dst=203.0.113.10 sport=80 dport=41000 [ASSURED] mark=0 use=1tcp      6 118 TIME_WAIT src=192.168.114.11 dst=198.51.100.80 sport=41000 dport=80 src=198.51.100.80 dst=203.0.113.10 sport=80 dport=12654 [ASSURED] mark=0 use=1conntrack v1.4.8 (conntrack-tools): 3 flow entries have been shown.
  • Laptop's line: reply half has dst=203.0.113.10 dport=12654, which is how the router knows the reply belongs to the laptop.
  • TIME_WAIT is the connection state; 115/117/118 are seconds left before the entry is deleted.
  • [ASSURED] means traffic was seen in both directions.

94.5 DNAT: port forwarding

  • Use case: a camera or NAS web server on PC 1 port 80 that you want to open from outside.
  • A client sends to the router's public IP 203.0.113.10:8080; DNAT rewrites the destination to 192.168.114.10:80.
  • DNAT goes in nat/PREROUTING (before routing); SNAT/masquerade goes in nat/POSTROUTING (after routing).
Terminal
# web server: before the rulenc -v -w 3 203.0.113.10 8080# home routeriptables -t nat -A PREROUTING -i wan0 \  -p tcp --dport 8080 -j DNAT \  --to-destination 192.168.114.10:80# web server: after the rulenc -v -w 3 203.0.113.10 8080# home routerconntrack -L -p tcp --orig-port-dst 8080
Output
$ nc -v -w 3 203.0.113.10 8080nc: connect to 203.0.113.10 port 8080 (tcp) failed: Connection refused$ nc -v -w 3 203.0.113.10 8080Connection to 203.0.113.10 8080 port [tcp/http-alt] succeeded!pc1 server sees 198.51.100.80$ conntrack -L -p tcp --orig-port-dst 8080tcp      6 117 TIME_WAIT src=198.51.100.80 dst=203.0.113.10 sport=34342 dport=8080 src=192.168.114.10 dst=198.51.100.80 sport=80 dport=34342 [ASSURED] mark=0 use=1

94.6 Hairpin NAT: your own public IP from the LAN

Why hairpin NAT is needed
  1. 1.Laptop 192.168.114.11 opens 203.0.113.10:8080 (public IP, same LAN)

  2. 2.Router DNAT: dst becomes 192.168.114.10:80, src stays .11

  3. 3.PC 1 sees a LAN client and replies directly to .11

  4. 4.Laptop gets a SYN-ACK from .10, not from 203.0.113.10: sends RST

  5. 5.Fix: hairpin MASQUERADE makes src 192.168.114.1, so the reply returns via the router

Terminal
# DNAT on the public IP, any interfaceiptables -t nat -R PREROUTING 1 \  -d 203.0.113.10 -p tcp --dport 8080 \  -j DNAT --to-destination 192.168.114.10:80# hairpin SNATiptables -t nat -A POSTROUTING \  -s 192.168.114.0/24 -d 192.168.114.10 \  -p tcp --dport 80 -j MASQUERADE# Laptop 2nc -v -w 3 203.0.113.10 8080
Output
$ nc -v -w 3 203.0.113.10 8080nc: connect to 203.0.113.10 port 8080 (tcp) timed out: Operation in progress$ head -6 /tmp/h1.txt23:07:57.849453 IP 192.168.114.11.56306 > 203.0.113.10.8080: Flags [S], seq 1266608612, win 64240, options [mss 1460,sackOK,TS val 3850051453 ecr 0,nop,wscale 10], length 023:07:57.849619 IP 192.168.114.10.80 > 192.168.114.11.56306: Flags [S.], seq 1783427465, ack 1266608613, win 65160, options [mss 1460,sackOK,TS val 3078085609 ecr 3850051453,nop,wscale 10], length 023:07:57.849630 IP 192.168.114.11.56306 > 192.168.114.10.80: Flags [R], seq 1266608613, win 0, length 0$ nc -v -w 3 203.0.113.10 8080Connection to 203.0.113.10 8080 port [tcp/http-alt] succeeded!pc1 server sees 192.168.114.1$ conntrack -L -p tcp --orig-port-dst 8080tcp      6 117 TIME_WAIT src=192.168.114.11 dst=203.0.113.10 sport=56266 dport=8080 src=192.168.114.10 dst=192.168.114.1 sport=80 dport=56266 [ASSURED] mark=0 use=1

94.7 CGNAT: the ISP's NAT (double NAT)

  • Many ISPs (especially mobile) give your router a shared address from 100.64.0.0/10, not a public IP.
  • NAT 1 at your router (to 100.64.0.2), NAT 2 at the ISP's CGNAT box (to 203.0.113.1).
  • Inbound requests are blocked at the ISP, so port forwarding does not work behind CGNAT.
  • Masquerade on the home router needed no change: it picked up the new WAN IP automatically.
Terminal
# ISP router: CGNAT to 203.0.113.1iptables -t nat -A POSTROUTING \  -s 100.64.0.0/10 -o out0 \  -j SNAT --to-source 203.0.113.1# PC 1nc -v -w 3 -p 43000 198.51.100.80 80# web server (internet)nc -v -w 3 203.0.113.1 8080
Output
home$ ip -4 -br addr show wan0wan0@if9         UP             100.64.0.2/24$ nc -v -w 3 -p 43000 198.51.100.80 80web saw 203.0.113.1 port 43000home$ conntrack -L -p tcp --dport 80tcp      6 118 TIME_WAIT src=192.168.114.10 dst=198.51.100.80 sport=43000 dport=80 src=198.51.100.80 dst=100.64.0.2 sport=80 dport=43000 [ASSURED] mark=0 use=1isp$ conntrack -L -p tcp --dport 80tcp      6 116 TIME_WAIT src=100.64.0.2 dst=198.51.100.80 sport=43000 dport=80 src=198.51.100.80 dst=203.0.113.1 sport=80 dport=43000 [ASSURED] mark=0 use=1$ nc -v -w 3 203.0.113.1 8080nc: connect to 203.0.113.1 port 8080 (tcp) failed: Connection refused

94.8 Common mistakes

  • Thinking NAT is a firewall: NAT only changes addresses; once you port-forward, that port is open to the whole internet. Use a stateful firewall.
  • Port forwarding behind CGNAT: if the router's WAN IP is 100.64.x.x and 'what is my IP' shows another IP, inbound will never reach you.
  • Forgetting hairpin NAT: the service works from outside but not from inside the LAN via the public IP.
  • Using SNAT with a fixed IP on a dynamic WAN link: use MASQUERADE instead.

94.9 Interview tips

  • SNAT/masquerade: outgoing, source changes; DNAT: incoming, destination changes (port forwarding); PAT: ports change so one IP is shared.
  • All mappings live in the conntrack table (original + reply tuple) until their timeout.
  • SNAT is in POSTROUTING, DNAT in PREROUTING.
  • Hairpin NAT = DNAT + SNAT so LAN clients can use the public address.
  • CGNAT (100.64.0.0/10) = double NAT: no inbound port forwarding; fixes are IPv6, a public IP from the ISP, or a tunnel/VPN.