Labs / IP Addressing & NAT / NAT

Lab 3 of 4 ยท NAT

NAT explained: static, dynamic and PAT on one router

Build static NAT, a pool and overload on one router, and see what each costs.

Start this lab 7 steps

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.

One address out, a port for each desk
One address out, a port for each desk
The rule hands over the whole address
The rule hands over the whole address
The first rule that fits wins
The first rule that fits wins
Two addresses, three desks
Two addresses, three desks
Two places, both taken
Two places, both taken
Three answers to one question
Three answers to one question
Whose address, and who's looking
Whose address, and who's looking

Steps

  1. 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, behind ferryRouter.

    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. addressCheck is 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/
  2. Give the mail server its own address

    Travel agents send Harbor Line their bookings by mail, so mailServer has 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.16 to 203.0.113.31, and sends all traffic for them to ferryRouter. 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.20 means 10.40.1.20. The rule rewrites the receiver, so it's destination NAT (DNAT). It goes in PREROUTING, 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: nc opens a connection to a port and prints what comes back, so point it at port 25, where mail programs wait, and ping tests the address while the lab records at mailServer.

    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
  3. Send the server's mail from its own address

    Mail runs both ways. When mailServer sends 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 listed 203.0.113.20, so that's the address its mail has to leave from.

    mailServer has that address now. Find out what the outside sees when mailServer starts the conversation: fetch addressCheck from it.

    If it shows the wrong address, the fix is source NAT (SNAT), which rewrites the sender as a packet leaves. It goes in POSTROUTING, like the MASQUERADE rule.

    The order matters, because the router stops at the first rule that fits. MASQUERADE covers the whole office, mailServer included, so the new rule has to sit above it. -I POSTROUTING 1 puts it in first place, where -A would 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/
  4. 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.21 and 203.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 SNAT with a range after --to-source. -I POSTROUTING 2 puts the rule in second place, under the mail server's rule and above MASQUERADE.

    Add it. The lab then clears the router's old notes and starts a long download on northDesk and on southDesk, so two desks are busy, and the pool holds two addresses. Fetch the page from pierDesk, 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
  5. 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-40001 means 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 2 replaces the rule in second place.

    After your rule, the lab starts the two long downloads again, and they take both places. Then pierDesk asks for a page. The internet is fine, the router is fine, and nothing has changed on pierDesk.

    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
  6. 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.21 is busy carrying the downloads, so ping it from travelAgent.

    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
  7. 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 outside
    

    Take the mail server's conversation. The lab clears the old notes. Fetch addressCheck from mailServer, 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

Keep reading

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab