The idea
Harbor Line's office shares one public address, which suits its ticket desks and fails its mail server. Build all three kinds of NAT on one Router, and see what each one gives and what it costs.
Steps
-
Share one address across the desks
Harbor Line runs ferries. Its office sits on private addresses in
10.40.1.0/24: three ticket desks and a mail server, behindferryRouter.Private addresses mean nothing on the internet, so the router has to put a public address on whatever leaves. That rewrite is network address translation (NAT).
NAT comes in three kinds, and they differ in one thing: how many machines get how many public addresses. This lab builds all three on one router, then shows how to pick one and how to read the four names that exams and Cisco manuals use.
Start with the kind a home router uses, overload, also called port address translation (PAT). Every machine shares one public address, here the router's own,
192.0.2.2.Add the rule. Then fetch a page from two desks.
addressCheckis a website out on the internet that prints the address and Port it sees.ferryRouter:~# iptables -v -t nat -A POSTROUTING -s 10.40.1.0/24 -j MASQUERADE northDesk:~# curl -sS -m 3 http://192.0.2.60/ southDesk:~# curl -sS -m 3 http://192.0.2.60/
-
Give the mail server its own address
Travel agents send Harbor Line their bookings by mail, so
mailServerhas to take connections from strangers, at an address that never changes. Overload can't do that.Harbor Line's provider has also sold it a block of 16 public addresses,
203.0.113.16to203.0.113.31, and sends all traffic for them toferryRouter. None of them sits on a card. They exist only in the router's rules, and an address with no rule leads nowhere.Static NAT ties one public address to one private address, for good:
203.0.113.20means10.40.1.20. The rule rewrites the receiver, so it's destination NAT (DNAT). It goes inPREROUTING, the list read as a packet arrives at the router.If you've met a port forward, this rule looks like one with a piece missing: it names no port.
Add it. Then test from
travelAgent, a machine on the internet, in two ways:ncopens a connection to a port and prints what comes back, so point it at port 25, where mail programs wait, andpingtests the address while the lab records atmailServer.ferryRouter:~# iptables -v -t nat -A PREROUTING -d 203.0.113.20 -j DNAT --to-destination 10.40.1.20 travelAgent:~# nc -w 2 203.0.113.20 25 travelAgent:~# ping -c 2 203.0.113.20
-
Send the server's mail from its own address
Mail runs both ways. When
mailServersends a message, the server at the far end looks at the sending address, because many mail systems refuse mail from an address that isn't listed for that domain. Harbor Line has listed203.0.113.20, so that's the address its mail has to leave from.mailServerhas that address now. Find out what the outside sees whenmailServerstarts the conversation: fetchaddressCheckfrom it.If it shows the wrong address, the fix is source NAT (
SNAT), which rewrites the sender as a packet leaves. It goes inPOSTROUTING, like theMASQUERADErule.The order matters, because the router stops at the first rule that fits.
MASQUERADEcovers the whole office,mailServerincluded, so the new rule has to sit above it.-I POSTROUTING 1puts it in first place, where-Awould add it at the end.Add the rule, and fetch the page again.
mailServer:~# curl -sS -m 3 http://192.0.2.60/ ferryRouter:~# iptables -v -t nat -I POSTROUTING 1 -s 10.40.1.20 -j SNAT --to-source 203.0.113.20 mailServer:~# curl -sS -m 3 http://192.0.2.60/
-
Give the desks a pool of addresses
On busy days, Harbor Line's card payment company slows down any one address that opens too many connections, and all three desks arrive from one address. Spreading them over a few would keep them under the limit. Harbor Line sets aside two more:
203.0.113.21and203.0.113.22.That's a pool, a list of public addresses that the router hands out as they're needed. It's the idea behind dynamic NAT: nobody owns an address. A machine gets one when it starts talking, and the address goes back to the pool when the machine falls silent, which suits a network with more machines than addresses.
On Linux, a pool is
SNATwith a range after--to-source.-I POSTROUTING 2puts the rule in second place, under the mail server's rule and aboveMASQUERADE.Add it. The lab then clears the router's old notes and starts a long download on
northDeskand onsouthDesk, so two desks are busy, and the pool holds two addresses. Fetch the page frompierDesk, then list the router's notes.ferryRouter:~# iptables -v -t nat -I POSTROUTING 2 -s 10.40.1.0/24 -j SNAT --to-source 203.0.113.21-203.0.113.22 pierDesk:~# curl -sS -m 3 http://192.0.2.60/ ferryRouter:~# conntrack -L
-
Fill the pool and try once more
A strict pool runs out of places. In a pool of two addresses there are two places, each one a whole address for one machine, however many conversations that machine holds.
Linux can't build that, so build the closest thing: a rule with only two places to land.
--to-source 203.0.113.21:40000-40001means one address and two ports, so the router can hold two conversations and no more. It's a stand-in, because a strict pool counts machines and this rule counts conversations. What the third desk sees is the same.Ports belong to TCP and UDP, so a rule with a port range has to say
-p tcp.-R POSTROUTING 2replaces the rule in second place.After your rule, the lab starts the two long downloads again, and they take both places. Then
pierDeskasks for a page. The internet is fine, the router is fine, and nothing has changed onpierDesk.ferryRouter:~# iptables -v -t nat -R POSTROUTING 2 -s 10.40.1.0/24 -p tcp -j SNAT --to-source 203.0.113.21:40000-40001 pierDesk:~# curl -sS -m 3 http://192.0.2.60/ ferryRouter:~# conntrack -L
-
Let the ports do the sharing
Go back to the pool rule with no port limit. Each address gets its full set of ports back, so two addresses at about 65,000 ports each make about 130,000 places. A strict pool of two had two.
That sum explains why overload took over. NAT was proposed in 1994 as a short-term fix for the shortage of addresses, and the internet's central pool of IPv4 addresses ran out in 2011. Billions of devices still get online, nearly all of them behind overload.
Replace the rule, and fetch from
pierDesk. Then look from the other side.203.0.113.21is busy carrying the downloads, sopingit fromtravelAgent.ferryRouter:~# iptables -v -t nat -R POSTROUTING 2 -s 10.40.1.0/24 -j SNAT --to-source 203.0.113.21-203.0.113.22 pierDesk:~# curl -sS -m 3 http://192.0.2.60/ travelAgent:~# ping -c 1 -W 1 203.0.113.21
-
Name the four addresses
Cisco's manuals and most network exams give each address in a NAT conversation a two-word name. The names stop being hard once you split them.
The first word says whose address it is: inside is a machine on your side of the router, and outside is one beyond it. The second says who's looking: local is the view from inside, and global is the view from outside.
inside local inside machine, seen from inside inside global inside machine, seen from outside outside local outside machine, seen from inside outside global outside machine, seen from outsideTake the mail server's conversation. The lab clears the old notes. Fetch
addressCheckfrommailServer, then read the router's note for it.mailServer:~# curl -sS -m 3 http://192.0.2.60/ ferryRouter:~# conntrack -L -s 10.40.1.20
Run these steps on real machines