{"canonical_url":"https://engineer.company/glossary/nat/","content_markdown":"# NAT\n\n## What it is\n\nNAT, short for network address translation, is the rewriting of IP addresses in\npackets as they pass through a router, so that hosts numbered in one address\nspace can talk to hosts numbered in another. RFC 2663, the 1999 document that\nfixed the vocabulary, describes it as mapping IP addresses from one realm to\nanother, with the aim of transparent routing: the hosts on either side should\nnot need to know that it happens.\n\nThe form almost everyone meets is what RFC 3022, published in January 2001,\ncalls Traditional NAT. Its typical case is a small office or home with a single\nIP address from its provider, while the machines behind the router use private\naddresses from the RFC 1918 blocks 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16,\nwhich are not routed between organisations. The router rewrites the source\naddress of each outgoing packet to its own public one and reverses the change on\nthe replies. Basic NAT translates addresses alone, so only as many inside hosts\ncan be out at once as there are public addresses in its pool. NAPT, network\naddress port translation, also rewrites the TCP or UDP port or the ICMP\nidentifier, which lets many inside hosts share one public address. RFC 3022\nnotes that Linux offered NAT under the name IP masquerade, and its firewall\ntools still use the word.\n\nChanging an address breaks everything computed from it, so the router has to\nrepair that as well. RFC 3022 requires the IP header checksum to be adjusted,\nand the TCP and UDP checksums too, because they cover a pseudo-header that\ncontains the source and destination addresses. To send each reply to the right\ninside host, the router keeps a table of mappings: inside address and port on\none side, outside address and port on the other. That table is state held in the\nmiddle of the network, and most of what goes wrong with NAT comes from it.\n\nTraditional NAT works in one direction. RFC 3022 describes its sessions as\noutbound from the private network, with traffic the other way allowed only\nthrough static address maps for hosts chosen in advance. The idea now also runs\none level up: RFC 6598, from April 2012, reserved 100.64.0.0/10 for\ncarrier-grade NAT, numbering the links between a provider's NAT and its\ncustomers' equipment, which may translate once more.\n\n## Why it matters\n\nRFC 3022 is candid about the trade. NAT can be installed without changing hosts\nor routers, but it takes away, in the RFC's words, \"the end-to-end significance\nof an IP address\", and makes up for it with more state in the network. Nearly\nevery practical consequence of NAT follows from that sentence.\n\nFor a server, the source address of a connection is often not one machine but a\nrouter with a crowd behind it. A block or a rate limit aimed at one visitor\nlands on everyone who shares the address, and an abuse report names the router,\nnot the culprit. RFC 3022 makes the same point from the inside: hiding private\naddresses, which it counts as a privacy benefit, also makes debugging harder,\nbecause a host that attacks other machines or sends spam has its address hidden\nin the NAT router.\n\nState also means timers. A mapping lives only as long as the router remembers\nit. RFC 4787, a Best Current Practice from January 2007, says a UDP mapping must\nremain valid for at least two minutes and recommends five minutes or more. RFC\n5382, its TCP counterpart from October 2008, sets 2 hours and 4 minutes for an\nidle established connection: keepalives go out every two hours by default, and\nthe four minutes cover packets still in flight. These are floors for devices\nthat follow the documents, and not every device does. A connection that stays\nquiet for longer than the device's own timer loses its mapping, and the next\npacket from outside has nowhere to go.\n\nTwo further constraints follow. RFC 3022 makes it mandatory that every packet of\na session pass through the same NAT router, and where a network has more than\none exit, it calls it essential for the NAT devices to share an identical table\nof translations. And protocols that carry addresses inside their payload need\nhelp from the NAT device, which works only while it can read that payload.\n\n## What it looks like in practice\n\nOn a Linux router, nftables performs NAT in chains of the type `nat`. The\nruleset below is this article's illustration, built on the NAT examples in the\nnft(8) manual. Here 198.51.100.0/24 stands in for the inside network and\n203.0.113.5 for the router's public address; both come from the documentation\nranges, and a real inside network would normally use one of the RFC 1918 blocks.\n\n```text\nadd table nat\nadd chain nat prerouting { type nat hook prerouting priority dstnat; }\nadd chain nat postrouting { type nat hook postrouting priority srcnat; }\nadd rule nat postrouting oif eth0 ip saddr 198.51.100.0/24 snat to 203.0.113.5\nadd rule nat prerouting iif eth0 tcp dport 443 dnat to 198.51.100.20\n```\n\nThe first rule is the everyday case: packets from the inside network that leave\nthrough eth0 get 203.0.113.5 as their source. For a dynamic public address, the\nmanual offers `masquerade`, which uses whatever address the outgoing interface\nhas at the moment. The second rule is a static map the other way: TCP\nconnections that arrive on eth0 for port 443 go to 198.51.100.20 inside. So few\nrules are enough because only the first packet of a connection passes through a\nnat chain; its decision is stored in the connection tracking entry, and every\nlater packet is rewritten the same way.\n\nThe other side of the picture is a host behind a NAT that it does not control,\nand WireGuard has a setting for exactly that. In this second illustration of\nmine, an inside host keeps a tunnel open to a peer on the outside:\n\n```ini\n[Peer]\nPublicKey = \u003cthe outside peer's public key\u003e\nEndpoint = 192.0.2.44:51820\nAllowedIPs = 192.0.2.128/25\nPersistentKeepalive = 25\n```\n\nThe wg(8) manual describes `PersistentKeepalive` as an interval of 1 to 65535\nseconds for sending an authenticated empty packet, so that a stateful firewall\nor NAT mapping stays valid. It suggests 25 seconds for an interface behind NAT\nthat rarely sends anything itself and may receive traffic at any time, and\nleaves the setting off by default. The outside peer needs nothing special: its\n`Endpoint` for this host is updated to the most recent source address and port\nof correctly authenticated packets, which is exactly what the NAT assigned.\n\nTunnels like these are where NAT trouble tends to surface. The portfolio\ndescribes\n[eight WireGuard and IPsec connections managed and troubleshot from 2022 to 2024](/portfolio/managed-8-wireguard-and-ipsec-vpn-connections/)\nacross Google Cloud and Linux systems. That page says the tunnels were fragile\nand awkward to diagnose when they dropped, and that recurring routing and\nkey-exchange problems were fixed at the root instead of with a restart, after\nwhich the interruptions stopped. It does not say whether NAT played a part, so\nthe two examples above belong to this article, not to that project.\n\n## Where it goes wrong\n\n**Quiet connections that stop answering.** A tunnel, a database connection or a\npush channel that sends nothing for a while loses its mapping and breaks without\neither end having closed it. The symptom looks random, because it depends on how\nlong the line was idle. Send keepalives from the inside more often than the\nshortest timer on the path expires, as `PersistentKeepalive` does, and on a NAT\nyou run, keep the timers at or above the RFC 4787 and RFC 5382 minimums. RFC\n4787 requires outbound packets to refresh a mapping and warns that inbound\nrefresh lets an attacker keep one alive forever, so the inside end should do the\nrefreshing.\n\n**Expecting connections to come in.** Traditional NAT is outbound by design, so\na service on an inside host is unreachable until someone writes a static map for\nit. Add a `dnat` rule for exactly the ports the service needs, or turn the\ndirection around and let the inside host connect out, as the keepalive above\ndoes.\n\n**Missing hairpinning.** Two inside hosts that reach each other through the\noutside address of one of them need the NAT to turn the packet around. RFC 4787\nrequires NAT devices to support this hairpinning and to present the\nturned-around packet with the external source address and port. Test that path\ndeliberately, and where a device fails it, have inside hosts use the inside\naddress.\n\n**Addresses inside the payload.** FTP is the classic case: RFC 2663 notes that\nits `PORT` command and `PASV` response carry an IP address and port as text,\nwhich a NAT that only rewrites headers leaves wrong. NAT devices patch such\nprotocols with an Application Level Gateway (ALG), and RFC 3022 calls the one\nfor FTP the most popular. An ALG must read the payload, so encryption disables\nit unless the gateway holds the key, and RFC 3022 adds that end-to-end IPsec\ncannot be assumed across a NAT. Prefer protocols that keep addresses out of the\npayload; where an old one must cross a NAT you run, the nft(8) manual shows a\nconnection tracking helper for FTP.\n\n**Logs that only name the router.** Behind NAT, a server's access log records\nthe shared address, which on its own cannot single out one inside host. Log the\nsource port and an exact timestamp next to the address, and keep clocks\nsynchronised, so that a complaint about one connection can be matched against\nthe translation on the router.\n\n**Fragments under NAPT.** Only the first fragment of a packet carries the TCP or\nUDP header, so RFC 3022 warns that translating outgoing fragments with NAPT is\nbound to fail, and that two inside hosts sending fragments with the same\nidentifier to the same destination can corrupt both sessions. Keep packets small\nenough to cross the path whole.\n\n**Treating NAT as a firewall.** The one-way nature of NAT is easy to mistake for\nprotection. RFC 4787 acknowledges that NATs are often deployed for security\ngoals, but says plainly that the way a NAT maps makes no difference to its\nsecurity, which is fully determined by its filtering behaviour: which packets it\nlets in. Write explicit filter rules for what may enter, and pair every `dnat`\nrule with a rule that allows only the traffic it was meant for.\n\n## Further reading\n\n- [RFC 3022: Traditional IP Network Address Translator (Traditional NAT)](https://www.rfc-editor.org/rfc/rfc3022.html)\n  — the 2001 description of Basic NAT and NAPT, with an honest list of their\n  costs.\n- [RFC 2663: IP Network Address Translator (NAT) Terminology and Considerations](https://www.rfc-editor.org/rfc/rfc2663.html)\n  — the vocabulary of NAT, from address realms to transparent routing and ALGs.\n- [RFC 4787: Network Address Translation (NAT) Behavioral Requirements for Unicast UDP](https://www.rfc-editor.org/rfc/rfc4787.html)\n  — how a NAT should map, time out and hairpin UDP traffic.\n- [RFC 5382: NAT Behavioral Requirements for TCP](https://www.rfc-editor.org/rfc/rfc5382.html)\n  — the TCP counterpart, including the idle timeouts for established and closing\n  connections.\n- [wg(8): set and retrieve configuration of WireGuard interfaces](https://git.zx2c4.com/wireguard-tools/about/src/man/wg.8)\n  — the WireGuard manual, with `PersistentKeepalive` and the advice for hosts\n  behind NAT.\n- [nft(8): nftables manual](https://www.netfilter.org/projects/nftables/manpage.html)\n  — the reference for nftables, including nat chains and the `snat`, `dnat` and\n  `masquerade` statements.\n\n\u003chttps://engineer.company/glossary/nat/\u003e\n","date_modified":"2026-10-30T02:51:31Z","description":"What NAT is, how it lets a whole network share one public IPv4 address, and where it breaks: expired mappings, inbound traffic, payloads and logs.","formats":{"gemtext":"https://engineer.company/glossary/nat/index.gmi","gopher":"https://engineer.company/glossary/nat/gophermap","html":"https://engineer.company/glossary/nat/","json":"https://engineer.company/glossary/nat/index.json","markdown":"https://engineer.company/glossary/nat/index.md","pdf":"https://engineer.company/glossary/nat/index.pdf"},"kind":"page","language":"en","title":"What is NAT?","url":"https://engineer.company/glossary/nat/","version":1}