Course contents · Lesson 12 of 22

Routing and NAT Explained: How Packets Find Home

Networks for Engineers · Lesson 12 of 22 · 10 min Engineer

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.

Flowchart worked for a packet to 10.8.3.4. Step 1, a packet arrives with destination 10.8.3.4. Step 2, compare it with every route: is 10.8.3.4 inside the range? Step 3, does any route match? If no, drop it and send back ICMP unreachable. If yes, step 4, pick the longest prefix, because the most specific route wins, and step 5, forward to its next hop, here 10.8.0.1, the VPN. Beside it, an illustrative routing table: 192.168.1.0/24, on-link over Wi-Fi, does not match; 10.8.0.0/16, next hop 10.8.0.1 for the VPN, matches and wins; 0.0.0.0/0, next hop 192.168.1.1, matches. Notes say /16 beats /0 because it covers fewer addresses, and that 0.0.0.0/0, the default route, wins only when nothing else matches, so a packet to 8.8.8.8 goes to the gateway 192.168.1.1
Two routes contain 10.8.3.4; the longer prefix, /16, decides.

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.

Sequence diagram with three columns: your laptop at 192.168.1.5, a home router with the public address 198.51.100.23, and a web server at 203.0.113.7. Going out: step 1, the laptop sends from 192.168.1.5:50000 to 203.0.113.7:443; step 2, the router forwards it from 198.51.100.23:61000 to 203.0.113.7:443, and notes 192.168.1.5:50000 and port 61000 in its NAT table. Coming back: step 3, the server replies from 203.0.113.7:443 to 198.51.100.23:61000; step 4, the router matches port 61000 in the table and delivers the reply to 192.168.1.5:50000. A note says the server only ever sees 198.51.100.23:61000, never the private address
The router swaps the address and port on the way out and swaps them back on the way in.

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.

Illustrative NAT table for a router whose public address is 198.51.100.23, with columns device, inside address, public port, destination and lifetime. Laptop, 192.168.1.5:50000, public port 61000, to 203.0.113.7:443, temporary. Laptop, 192.168.1.5:50001, port 61001, same destination, temporary. Phone, 192.168.1.8:50000, port 61002, same destination, temporary, highlighted. Smart TV, 192.168.1.20:41000, port 61003, to 192.0.2.80:443, temporary. A dashed port-forward row: public port 8080 to 192.168.1.5:8080 from any sender, permanent. Notes say the laptop and phone both used inside port 50000 but got different public ports; a reply to port 61002 goes to the phone; and a new packet matching no row reaches no device unless a port-forward row matches
Same inside port, different public ports: the public port tells the replies apart.

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.