Labs / IP Addressing & NAT / NAT

Lab 1 of 4 · NAT

NAT: share one public address across a whole building

Hide a network behind one public address, and see how replies find the way back.

Start this lab 6 steps

The idea

Mira's radio station runs on private addresses, and a request from the news desk never gets an answer. Make the whole station share one public address.

Dropped for its sender
Dropped for its sender
One request, two senders
One request, two senders
The note is the way home
The note is the way home
One address, two ports
One address, two ports
Two knocks, two different refusals
Two knocks, two different refusals
NAT lets it straight through
NAT lets it straight through

Steps

  1. Try the news wire

    Mira looks after the computers at a small radio station. Every morning the news desk fetches a bulletin from a news wire, a service that sends news to stations. This morning the fetch fails.

    The station's machines have private addresses, such as 10.30.1.10. Anyone may use those numbers, so thousands of buildings use the same ones at once. The news wire's 198.51.100.10 is a public IP address, which belongs to one machine in the world.

    A request travels as a packet, a small piece of data that carries two addresses: the sender's and the receiver's.

    Fetch the page from newsDesk with curl, a program that asks a web server, the machine behind a website, for a page. The lab records what reaches newsWire while you do.

    Fetch it a second time while the lab records at providerRouter, the Router at the station's internet provider, one stop before newsWire. Then read that router's list of routes. A route is one line that says where to send a block of addresses.

    newsDesk:~# curl -sS -m 3 http://198.51.100.10/
    newsDesk:~# curl -sS -m 3 http://198.51.100.10/
    providerRouter:~# ip route
  2. Share the router's address

    providerRouter won't carry a packet from 10.30.1.10, but it has a line for a public address, and the station has one: 203.0.113.2, on stationRouter's network card toward the provider. A network card is the part of a machine that a cable plugs into, and each one has its own address.

    Network address translation (NAT) builds on this. As a packet leaves, the router rewrites the sender's address to its own public one, so providerRouter sees a sender it can reach, and the rest of the internet sees only the router.

    It began as a short-term fix. By 1994 it was clear that the internet's addresses would run out.

    RFC 1631 from May of that year, the first of the internet's technical papers to describe NAT, said it "may be a good short term solution" while bigger fixes were built. Over 30 years later, it's in nearly every home router.

    stationRouter runs Linux, an operating system like Windows that's common on routers and servers. One rule in its iptables tool turns NAT on. Add the rule, fetch the page again while the lab records at newsWire, then ask newsDesk what address it thinks it has.

    stationRouter:~# iptables -v -t nat -A POSTROUTING -s 10.30.1.0/24 -j MASQUERADE
    newsDesk:~# curl -sS -m 3 http://198.51.100.10/
    newsDesk:~# ip -br addr show dev studio
  3. How the answer finds the desk

    To tell conversations apart, the router needs one more number: the Port. An address gets a packet to a machine, and a port gets it to the right program there. A web server waits on port 80, and curl picks a free number for each fetch, such as 55968.

    Every packet carries two ports, the source port and the destination port. An answer swaps them, as it swaps the addresses, so it names the same conversation from the other end.

    conntrack, short for connection tracking, shows what a Linux router knows about the conversations through it. The lab clears what your failed tries left behind. Fetch the page once more, then list what stationRouter holds.

    newsDesk:~# curl -sS -m 3 http://198.51.100.10/
    stationRouter:~# conntrack -L
  4. Two desks, one port

    A note for each conversation works until two machines pick the same number. Each desk chooses its source port with no idea what the others chose, and they all share one public address.

    If studioDesk and newsDesk both talk to newsWire from port 40000, the two conversations look the same from outside: same public address, same port, same server. An answer would fit both notes.

    The lab starts a long download on studioDesk, sent from its port 40000, and leaves it running. Then fetch the page on newsDesk from the same port, and list the notes.

    newsDesk:~# curl -sS -m 3 --local-port 40000 http://198.51.100.10/
    stationRouter:~# conntrack -L
  5. Knock from outside

    Every conversation so far started inside the station. Now turn it around.

    The news wire wants an old recording from archive, the station's own web server at 10.30.1.200. newsWire has two addresses to try: the station's public one, 203.0.113.2, and archive's private one.

    Try both from newsWire, and watch how each one fails.

    newsWire:~# curl -sS -m 3 http://203.0.113.2/
    newsWire:~# curl -sS -m 3 http://10.30.1.200/
  6. Test whether NAT keeps strangers out

    Look again at why the second knock failed. stationRouter never saw it. The request stopped at providerRouter, because one line was missing, and NAT had nothing to do with that.

    A private address can't cross the internet, but it can cross one cable. Whoever controls the next router along can add a line to the station, and that's the provider, or an attacker who has broken into the provider's router.

    Add that line on providerRouter. Then try archive from newsWire once more, and read the notes on stationRouter.

    providerRouter:~# ip -echo route add 10.30.1.0/24 via 203.0.113.2
    newsWire:~# curl -sS -m 3 http://10.30.1.200/
    stationRouter:~# conntrack -L

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