Routing and NAT Explained: How Packets Find Home
Your laptop, phone and TV are all online at once, yet every website sees one address for your whole home, and a friend cannot reach the test server on your laptop. This lesson explains routing and NAT: how each router picks where a packet goes next, and how your home router shares one public address among your devices and still delivers every reply.
Routing tables: the most specific match wins
As lesson 1 showed, a router looks up each packet's destination in its routing table and picks only the next hop.
Each line is a route: a destination range in CIDR notation, from lesson 11, plus a next hop, the neighbouring router that should get matching packets. For a network the device is plugged into, the route says to deliver directly, often shown as "on-link".
Your laptop has a routing table too: typically its home network (such as 192.168.1.0/24, on-link), routes added by software such as a VPN (virtual private network, an encrypted link into another network), and one catch-all.
A destination often fits several ranges. The rule is longest prefix match: the route with the longest prefix wins, because it covers the fewest addresses and so is the most specific. A packet for 10.8.3.4 fits both 10.8.0.0/16 and the catch-all, and the /16 wins.
The catch-all is the default route, 0.0.0.0/0. Its prefix length is 0, so it matches every IPv4 address but loses to any other match. Its next hop is the default gateway, the router that takes everything else: at home, your home router.
If nothing matches and there is no default route, the router drops the packet and usually says so with an ICMP (Internet Control Message Protocol) message, a short error or status note between routers and hosts: here, "destination unreachable". In the flowchart, follow 10.8.3.4 through the table on the right.
Hop by hop: the TTL and traceroute
Every router on the path repeats this lookup in its own table. Before forwarding, each also lowers the packet's TTL, the hop counter from lesson 1, by one. When it reaches zero, the router discards the packet and sends the sender an ICMP "time exceeded" message from its own address. IPv6 calls the field the hop limit.
Traceroute turns that rule into a map. It sends test packets, called probes, with a TTL of 1, then 2, then 3: the first router discards the first probe and reports back, the second router the next one, and so on until a probe reaches the destination. Lesson 19 covers reading its output.
Private addresses behind one public address
As lesson 2 showed, private addresses (in CIDR terms 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16) work only inside one local network, and internet routers do not carry packets to them.
Homes use them because IPv4's 4.3 billion addresses are too few for every device. Your ISP usually gives your router one public IPv4 address, and the router lends each device a private one, such as 192.168.1.5, with DHCP (Dynamic Host Configuration Protocol), which also tells the device its gateway.
That leaves a problem: a reply to 192.168.1.5 could never come back, because no internet router carries that address and millions of homes use it. So the home router rewrites each packet on its way out.
NAT: rewriting the address and the port
NAT (network address translation) means rewriting addresses in packet headers as they pass through a router. Home routers also rewrite ports, which is strictly NAPT (network address port translation), but most people simply say NAT.
Follow one connection in the diagram. Your laptop sends from 192.168.1.5:50000 to a web server at 203.0.113.7:443. The router replaces the source with its public address and a port of its own, 198.51.100.23:61000, and records the pair in its NAT table, the list of current translations.
The server sees only 198.51.100.23:61000 and replies there. The router finds port 61000 in its table, rewrites the destination back to 192.168.1.5:50000 and delivers the reply. In the analogy, the table is the desk's register and the public port is the reference number.
Entries are temporary: the router removes one after the connection closes or stays idle for a while, which is why some quiet connections, such as SSH sessions, send small keep-alive packets.
Why change the port too? Two devices may pick the same one. In the table below, the laptop and the phone both used port 50000, so the router gave them public ports 61000 and 61002, and each reply's port says which device it is for. A router may keep the original port when no other device is using it.
What NAT changes: out is easy, in needs port forwarding
Outgoing traffic just works: any device can start a connection, and the router adds a table entry automatically.
Incoming connections are different. Say you run a test web server on your laptop's port 8080. A friend's packet to 198.51.100.23:8080 matches no entry, so the router cannot tell which device should get it, and no device does.
Port forwarding fixes that: a permanent rule, set on your router's admin page, such as "public port 8080 goes to 192.168.1.5:8080". Forward only what you need, since anyone online can then reach that program; firewalls, in lesson 13, control who may.
Some ISPs, mobile ones especially, add a second NAT of their own, called carrier-grade NAT, and then a port forward at home cannot help. On IPv6, devices usually get public addresses of their own with no NAT, and the router's firewall blocks unasked-for traffic instead.
Check yourself
Your laptop routes 10.8.0.0/16 via the VPN and everything else via 192.168.1.1. Where do packets for 10.8.20.4 and 8.8.8.8 go?
10.8.20.4 goes into the VPN: it matches both routes, and /16 is the longer prefix. 8.8.8.8 matches only 0.0.0.0/0, so it goes to the home router.
Your laptop and phone both use source port 50000 to reach the same server. How does the router send each reply to the right device?
It gives each connection its own public port and records the inside address and port behind it, so the port a reply arrives on picks the device.
A test server on your laptop's port 8080 works from your phone at home, but a friend elsewhere cannot reach it at your public address. Why?
Your phone reaches the laptop's private address directly, without NAT. The friend's packet matches no NAT entry, so no device gets it; a port-forwarding rule fixes that, unless your ISP uses carrier-grade NAT.