Skip to content

ElementaryLesson 6 of 9

Sharing one address: NAT ​

Every device in your home has a private address, such as 192.168.20.10. The internet does not carry packets to private addresses, so the router swaps each one for its own public address on the way out, and back on the way in. This is Network address translation: rewriting the addresses in packets as they pass through a router, so that many devices can share one public address..

Why one address is shared ​

An IPv4 address is 32 bits, so there are about 4.3 billion of them. There are more devices in the world than that. Many addresses are also set aside for other uses, so not every device can have one of its own.

The private ranges from Reading an IP address are the way out: 10.0.0.0/8, 172.16.0.0/12, and 192.168.0.0/16. Any home or office may use them, so millions of homes use 192.168.1.0/24 at the same time. That is why they cannot be used on the internet: a reply to 192.168.20.10 could be meant for any of those homes. Routers on the internet have no routes to private addresses at all.

Your internet provider gives your router one An address that belongs to one place on the whole internet, so a reply to it can find its way back. Your internet provider gives you one.. In the single-site network, it is 203.0.113.2, on the router's port to the provider. Every device at home shares it.

What the router rewrites ​

An address and a port are often written together with a colon, such as 192.168.20.10:51000. When the laptop opens a website:

  1. The laptop sends a packet from 192.168.20.10:51000 to the website at 198.51.100.7:443.
  2. The router replaces the sender with its public address and a port it picks, such as 203.0.113.2:40001. It writes a row in its The list a NAT router keeps of every connection it rewrote: which inside address and port became which outside port, and who is at the other end.: 192.168.20.10:51000 is 203.0.113.2:40001, talking to 198.51.100.7:443.
  3. The website sees a packet from 203.0.113.2:40001, and answers it there.
  4. The answer reaches the router. The router finds port 40001 in its table, puts 192.168.20.10:51000 back as the receiver, and passes the packet in.

The port is what makes sharing work. With one public address for everyone, the address alone cannot say which device an answer is for, but the router's port can.

Try itOpen connections through one address
Private: 192.168.20.0/24PublicLaptop192.168.20.10Phone192.168.20.11Camera192.168.20.50Routeroutside 203.0.113.2The internetWebsite198.51.100.7Video service198.51.100.20Stranger192.0.2.66
The router’s translation table
RowInsideOutsideOther end
Empty.

Open a connection from the laptop, then one from the phone. Then connect from outside, first to port 8443, then to a port in the table.

router.ts
firewall: {
    nat: {
        source: [{ description: "Share the public address", if: { out_interface: "1g-1" }, then: "masquerade" }],
    },
},

The router in the widget picks a new outside port for every connection, so the two sides are easy to tell apart. Some routers keep the device's own port when no other connection uses it, and pick a new one only when they must.

The table does not grow forever. A row is removed when its connection ends, or after it has been quiet for a while.

CheckAn answer arrives at 203.0.113.2, port 40002. How does the router know which device at home it is for?

Nobody can start a connection in ​

Every row in the table is written by a packet going out. A packet from the internet for a port that is in no row has nowhere to go. The router cannot know which device at home it is for, so the packet stops there.

A row also remembers who is at the other end, so a port in the table does not help a stranger either. Some home routers are looser and let anyone use a row once it exists. Either way, the row was opened from inside.

To let a connection in on purpose, you add a A rule that sends everything arriving at one port of the public address to one device inside. Also called destination NAT.: "whatever arrives at 203.0.113.2, port 8443, goes to the camera at 192.168.20.50, port 443". It is also called destination NAT, because it rewrites the receiver of a packet. The rewriting so far changed the sender.

A port forward is open to everyone

Anyone on the internet who tries port 8443 reaches the camera, not only you. Forward only what must be reachable from outside, and remove a forward you no longer need.

CheckYour router has no port forward. Someone on the internet opens a connection to your public address, port 22. What happens?

NAT is not a firewall ​

Because nobody can start a connection in, NAT is often said to protect a home. On its own, it does not:

  • It decides nothing about what goes out. Any device at home can connect to anything.
  • It does not stop a packet sent straight to a private address. A computer in your provider's network can send a packet for 192.168.20.10 to your router's outside port. NAT has no row for it, and needs none: the router knows the way to 192.168.20.0/24, so it routes the packet in.
  • It does nothing for the router itself. A login attempt to 203.0.113.2, port 22, is for the router, and NAT never touches it.

Deciding what may pass is the job of a firewall. In the single-site router, its forward chain drops that packet for 192.168.20.10.

CheckWhich of these does NAT stop on its own?

In Circuit ​

NAT is written in the router's firewall, under nat. The single-site router has one rule:

ts
firewall: {
    // filter: the rules of the next lesson.
    nat: {
        source: [{ description: "Share the public address", if: { out_interface: "1g-1" }, then: "masquerade" }],
    },
},
  • source lists the rules that rewrite the sender, on the way out.
  • if: { out_interface: "1g-1" } limits the rule to packets leaving through 1g-1, the port to the provider. Traffic between the VLANs at home keeps its own addresses.
  • then: "masquerade" means: use the address of the port the packet leaves through, whatever it is now. If your provider changes your public address, the rule still works. With a fixed address, then: "snat" and to_address: "203.0.113.2" write it out instead.

A port forward goes in destination, with then: "dnat", to_address, and to_port. If the router's forward chain drops by default, as the single-site router's does, the forwarded connection also needs a rule there that accepts it.

Circuit turns each rule into the vendor's own command. For the single-site router, which runs RouterOS, that is:

text
add action=masquerade chain=srcnat comment="Share the public address" out-interface=ether1

1g-1 became ether1, the router's own name for its first 1G port: see A device.

Discussion

Stuck, or found a mistake? Ask here. You sign in with GitHub.

Released under the Business Source License 1.1.