NAT and Internet access¶
Practical 3.7 nat-and-internet-access-annex.pdf · PDF
Open your phone's Wi-Fi settings and look at your IP address. Probably something like 192.168.1.33. Now open the web page ifconfig.me, which tells you the address the world sees. It is a completely different number. Which one is really "your" IP? And why are there two?
On the Routing page you had to ask another group to add a route back to you. Now think bigger. For a server in California to answer your PC, every router on the Internet would need a route back to 10.3.0.0. And to your neighbour's 10.3.0.0, and to millions of identical home networks. That cannot work. This page is about what the world did instead.
What you will learn
- Tell public from private addresses, and why private ones cannot cross the Internet
- Tell the story of IPv4 exhaustion with its dates, and what NAT was invented for
- Explain what masquerade changes in a packet and how the answer finds its way back
- Give a RouterOS router Internet access: WAN address, default route, DNS and masquerade
- Publish a service from inside the LAN with port forwarding
- Why
192.168.1.33is not the address the world sees, and why NAT is not a firewall
Not enough addresses¶
An IPv4 address has 32 bits. How many different addresses is that?
2³² = 4,294,967,296: about 4.3 billion.
It sounds like a lot until you count. There are more than 8 billion people, and many of them have a phone, a laptop, a console and a smart TV. It never fitted.
The story, with dates:
| When | What happened |
|---|---|
| May 1994 | Kjeld Egevang and Paul Francis publish RFC 1631, "The IP Network Address Translator (NAT)". They call it a short-term solution. |
| February 1996 | RFC 1918 sets aside three private ranges that anyone may use inside their own network. |
| 3 February 2011 | IANA, the world's central stock of addresses, hands out its last blocks to the five regional registries. The stock is empty. |
| 14 September 2012 | Europe's registry, RIPE NCC, starts on its last block and rations it. |
| 25 November 2019 | RIPE NCC allocates its very last IPv4 addresses. |
The "short-term solution" of 1994 is, more than thirty years later, in every home on the planet. The real solution is a bigger address: IPv6, on a later page.
Careful: IPv4 running out does not mean the Internet is full
It means there are no free blocks left to hand out. The Internet keeps running thanks to NAT and to addresses being reused and resold. IPv6 is the way out.
Public and private addresses¶
A public address is unique in the whole world and can be reached across the Internet. A private address can be used by anyone inside their own network, so it is not unique and Internet routers refuse to carry it.
The three private ranges of RFC 1918:
| Range | From | To |
|---|---|---|
10.0.0.0/8 |
10.0.0.0 |
10.255.255.255 |
172.16.0.0/12 |
172.16.0.0 |
172.31.255.255 |
192.168.0.0/16 |
192.168.0.0 |
192.168.255.255 |
Your group's 10.G.0.0/24, the backbone 172.16.0.0/24 and your home's 192.168.1.0/24 are all private. Thousands of homes use exactly 192.168.1.0/24 at the same time. That is fine inside each home, and it is exactly why nobody outside can send a packet "to 192.168.1.33": which one?
The 172 trap
172.16.0.0/12 ends at 172.31.255.255. So 172.20.5.9 is private, but 172.32.0.1 is public. Check the second number: only 16 to 31 are private.
The trick: an office with a reception desk¶
We keep one analogy for the whole page: an office with a reception desk.
Inside the office, people have extension numbers: 101, 102, 103. The same extensions exist in every office in the world, so nobody outside can call "extension 101". The office has one public phone number.
When someone inside calls out, the receptionist makes the call from the office number and writes in a notebook: "line 7 = extension 101, talking to the dentist". When the dentist calls back on line 7, the receptionist checks the notebook and puts the call through to 101.
| In the office | In the network |
|---|---|
| Extension numbers | Private addresses of the PCs |
| The office phone number | The router's public address |
| The receptionist | The router, doing NAT |
| The notebook | The translation table (RouterOS calls it connection tracking) |
| The line number | The port number |
NAT (Network Address Translation) is the router rewriting the addresses of packets as they cross it. The usual form, masquerade, replaces the private source address of every packet leaving the LAN with the router's own public address.
The port is a number inside every packet that identifies one conversation (more on ports on the next page). It is the "line" in the notebook, and it is how one public address can serve hundreds of PCs at once.
Be the receptionist¶
A router has the LAN address 10.3.0.1 and the WAN address 172.16.0.3. Two PCs open the same web page at the same moment, and by chance both pick port 51000 for their side:
| Packet from the PC | Leaves the router as | Line in the notebook |
|---|---|---|
10.3.0.199:51000 → 1.1.1.1:443 |
172.16.0.3:51000 → 1.1.1.1:443 |
:51000 ↔ 10.3.0.199:51000 |
10.3.0.198:51000 → 1.1.1.1:443 |
172.16.0.3:51001 → 1.1.1.1:443 |
:51001 ↔ 10.3.0.198:51000 |
The second row is the clever bit. Both PCs chose the same port, so the router changed one of them to a free port. NAT translates address and port.
- An answer arrives for
172.16.0.3:51001. The notebook says: deliver to10.3.0.198:51000. - A packet arrives for
172.16.0.3:40000that nobody asked for. There is no line in the notebook, so it is dropped.
flowchart LR
subgraph LAN ["LAN: private extensions"]
PC1["<b>PC</b><br>10.3.0.199:51000"]
PC2["<b>PC</b><br>10.3.0.198:51000"]
end
R["<b>Router</b><br>the receptionist<br>WAN 172.16.0.3"]
NB[("<b>Translation table</b><br>the notebook")]
S["<b>Web server</b><br>1.1.1.1:443"]
PC1 -->|"source rewritten<br>to :51000"| R
PC2 -->|"source rewritten<br>to :51001"| R
R --- NB
R -->|"both look like<br>172.16.0.3"| S
Two receptionists in a row: CGNAT¶
Run traceroute -n 1.1.1.1 at home. If after your home router you see an address starting with 100., your Internet provider is also using NAT for its customers. That range, 100.64.0.0/10, was reserved in 2012 for exactly this. It is called CGNAT (Carrier-Grade NAT): your home router is a receptionist, and the provider has another one in front of it.
Careful: my IP is not 192.168.1.33
That is your extension. The address the world sees is your router's public one, and with CGNAT not even that one is yours alone: you share it with other customers.
The WAN side of the router¶
The WAN (Wide Area Network) side of a router is the interface that leads out, towards the provider. On the group routers it is ether1. The LAN side is where your PCs are.
A provider gives you the WAN address in one of three ways:
| Method | How it works | Where you find it |
|---|---|---|
| DHCP | The router asks for an address automatically, like a PC | The most common: cable, fibre, 4G routers |
| Static | The provider tells you the numbers and you type them | Business lines; our backbone 172.16.0.G/24 |
| PPPoE | The router logs in with a user name and password | Many fibre and DSL lines |
Three things for Internet¶
With a WAN address in place, the router needs three more lines:
/ip route add gateway=172.16.0.254
/ip dns set servers=172.16.0.254
/ip firewall nat add chain=srcnat out-interface=ether1 action=masquerade
- A default route: everything unknown goes to the provider's router (here, the teacher's router
172.16.0.254). Nodst-addressmeans0.0.0.0/0. - A DNS server to ask, so names like
www.debian.orgwork. - The receptionist.
chain=srcnat: we change the source.out-interface=ether1: only for packets leaving by the WAN.action=masquerade: use the router's own WAN address, whatever it is. The answers are translated back automatically.
In WinBox the masquerade rule is IP → Firewall → NAT → +: Chain srcnat, Out. Interface ether1, Action tab masquerade. You can watch the notebook at IP → Firewall → Connections, or in the CLI:
Checking it from a PC¶
ping -c 3 1.1.1.1
dig +short www.debian.org
traceroute -n 1.1.1.1
curl -s https://ifconfig.me ; echo
pingproves the packets get out and the answers come back.digproves DNS works.tracerouteshows the first hops:10.3.0.1(your group router),172.16.0.254(the teacher's router), then the school's network.curl ifconfig.measks a web site "which address do you see?". The answer is neither10.3.0.xnor172.16.0.3: there are at least three receptionists in a row before the Internet.
Before you add the masquerade rule, try ping -c 3 172.16.0.254 from a PC: it fails, while the same ping from the router works. The teacher's router has no route back to 10.3. After the rule, the PC's packets leave with the router's address 172.16.0.3, which the teacher's router can answer.
A door in: port forwarding¶
The notebook only has lines for conversations started from inside. So a web server running on a PC of your LAN is invisible from outside: a visitor's packet arrives at the router and finds no line.
The fix is a standing order for the receptionist: "whatever arrives at the WAN for TCP port 8080, send it to 10.3.0.199".
Port forwarding (in RouterOS, a
dstnatrule withaction=dst-nat) rewrites the destination of packets arriving at the WAN, so that one port of the router leads to one machine inside the LAN.
/ip firewall nat add chain=dstnat in-interface=ether1 protocol=tcp dst-port=8080 \
action=dst-nat to-addresses=10.3.0.199 to-ports=8080
A quick web server to test it, on the PC:
Then another group, from outside, runs:
curl http://172.16.0.3:8080 # Hello from group 3
curl -m 3 http://10.3.0.199:8080 # fails: no route to a private network
The second line fails because nobody outside has a route to a private address: that is the whole point. In the server's terminal, each visit prints the visitor's address. It is the partner's router, 172.16.0.4: their PC is hidden behind their own masquerade.
| Rule | Chain | Changes | Direction | Who starts the conversation |
|---|---|---|---|---|
| Masquerade | srcnat |
The source address | Packets going out | Someone inside |
| Port forwarding | dstnat |
The destination address | Packets coming in | Someone outside |
Careful: NAT is not a firewall
NAT hides your LAN as a side effect: nobody outside can start a connection in, because the notebook has no line for it. But it was designed to save addresses, not to decide what is allowed. And port forwarding opens exactly one port to one machine: whatever is listening on that port is now on the Internet. Deciding what may pass is the job of the firewall, on the next page.
Seeing NAT with your own eyes¶
On the real routers NAT works, but you never see the address change: the far side is the real Internet. In the virtual lab you own both sides. A node called isp plays the Internet with the address 203.0.113.1 (a range reserved for documentation, safe to use as a fake Internet), and it has no route to private networks, just like every real Internet router.
On isp you leave a capture running:
Before the masquerade rule, a ping from pc1 gets 100 % loss, yet the capture shows the requests arriving:
The question arrives with a private source. isp cannot answer: it has no route to 10.3.10.11. After the rule, the same ping works and the capture shows IP 203.0.113.2 > 203.0.113.1: same PC, same ping, but the source is now the router's public address. Open a web page from pc1 and pc2, and the web server on isp logs 203.0.113.2 for both. Two PCs, one address.
The Practical 3.7 sheet walks you through both parts: Internet and a door for your group's real router, then the lab. Keep the five configuration lines of the lab router: the next page starts from them.
Key takeaways¶
- IPv4 has only 2³² ≈ 4.3 billion addresses; IANA ran out on 3 February 2011 and RIPE NCC on 25 November 2019.
- Private addresses (
10.0.0.0/8,172.16.0.0/12,192.168.0.0/16) can be reused by anyone and do not cross the Internet; public addresses are unique. - NAT lets a whole LAN share one public address; masquerade rewrites the source of outgoing packets and the translation table sends each answer to the right PC, by port.
- Internet on RouterOS = a WAN address + a default route + a DNS server + a
srcnatmasquerade rule. - Port forwarding (
dst-nat) rewrites the destination so someone outside can reach one service inside. - NAT hides by accident; it is not a firewall. CGNAT means your provider adds a second NAT in front of yours.
Quick check¶
1. Which of these addresses is private?
- 172.20.5.9
- 172.32.0.1
- 8.8.4.4
- 193.146.1.1
The private block 172.16.0.0/12 goes from 172.16.0.0 to 172.31.255.255, so 172.20.5.9 is inside it. 172.32.0.1 is just outside: the classic trap.
2. What does masquerade change in a packet leaving the LAN?
- The source address
- The destination address
- The MAC address of the PC
- The DNS name being visited
Masquerade is source NAT: it replaces the private source with the router's public WAN address (and the port, if needed). Changing the destination is dst-nat, used for port forwarding.
3. An answer arrives at the router's public address. How does the router know which PC of the LAN it is for?
- It looks it up in its translation table, by port
- It asks every PC with ARP
- It checks the DHCP leases
- It reads it from its routing table
When the PC's packet went out, the router wrote a line in its translation table (the receptionist's notebook): this port belongs to that private address and port. The answer comes back to that port, so the router knows where to deliver it.
4. When do you need a port forwarding rule?
- When someone outside must start a connection to a server inside
- When a PC inside wants to open a web page
- When the DNS server stops answering
- When two VLANs of the same router talk
Conversations started from inside work with masquerade alone. A conversation started from outside finds no line in the notebook, so it needs a standing order: port forwarding (dst-nat).
5. When did IANA hand out its last blocks of IPv4 addresses?
- 2011
- 1994
- 2019
- 1996
On 3 February 2011 IANA gave its last blocks to the five regional registries. 1994 is when NAT was published (RFC 1631), 1996 the private ranges (RFC 1918), and 2019 when RIPE NCC ran out.
6. Why can a packet with the source 10.3.10.11 not get an answer from a server on the Internet?
- Internet routers have no route back to private addresses
- Private addresses are too long to fit in a packet
- The server only answers to addresses ending in .1
- Packets from private addresses are encrypted
Thousands of networks use the same private ranges, so no Internet router can have a route back to "10.3.10.11". That is why the router must replace it with its own public address before the packet leaves.
7. At home your phone shows 192.168.1.33 and the site ifconfig.me shows 100.72.14.9. Pick the two that are true.
- 192.168.1.33 is a private address used only inside your home
- Your provider is also doing NAT, so this is CGNAT
- 100.72.14.9 is a public address that belongs only to your phone
- NAT at home acts as a complete firewall, so no other protection is needed
192.168.1.33 is your extension inside the LAN. An address in 100.64.0.0/10 is the range reserved for CGNAT: a second receptionist at your provider, shared with other customers. And NAT hides by accident; deciding what may pass is the firewall's job.
Next: you opened one door on purpose. How many others are open that you never opened? Find out in Ports and firewalls.