Chapters

Putting it together

Kadé opens an office with a small warehouse at the back. Two networks, one router between them. We follow three messages and watch every decision from chapters 2 to 5 happen in order.

Addressing and local delivery · chapter 6 of 34 · 4 min read

The Kadé office

Staff computers and warehouse devices are kept on separate networks, so a problem on a warehouse device stays in the warehouse. Both networks use the mask 255.255.255.0. One router sits between them, with an address in each.

Office routereth0 198.51.100.20 (to ISP)eth1 172.16.1.1 · eth2 172.16.2.1eth1eth2eth0ISP → internetkade-api 34.87.120.15staff · 172.16.1.0/24warehouse · 172.16.2.0/24switchswitchFront desk PC.10Manager laptop.11Office printer.20Stock scanner.10Packing PC.11Label printer.20
  • devices
  • router and switches: they pass traffic on
  • the internet side, where kade-api is

Three networks meet at the router: staff on eth1, warehouse on eth2, the ISP on eth0.

Staff network · 172.16.1.0/24Warehouse network · 172.16.2.0/24
Front desk PC · 172.16.1.10Stock scanner · 172.16.2.10
Manager laptop · 172.16.1.11Packing PC · 172.16.2.11
Office printer · 172.16.1.20Label printer · 172.16.2.20
gateway 172.16.1.1 (router eth1)gateway 172.16.2.1 (router eth2)

A router has several interfaces

This is the key idea of the chapter. A router has more than one network interface (chapter 2), and each has its own IP on a different network. That is what lets one box join separate networks.

InterfaceIPNetworkFaces
eth0198.51.100.20198.51.100.0/24The ISP (WAN)ISP router at 198.51.100.1
eth1172.16.1.1172.16.1.0/24Staff networkstaff devices' default gateway
eth2172.16.2.1172.16.2.0/24Warehouse networkwarehouse devices' default gateway
Your home router is the same idea with two interfaces: LAN side 192.168.1.1 and WAN side 203.0.113.45 (chapter 3).

Switch and router

Inside each network, devices are joined by a switch. The switch and the router do different jobs:

SwitchRouter
JoinsDevices inside one networkSeparate networks
ReadsMAC addresses (the frame)IP addresses (the packet)
Used whenTarget is on the same networkTarget is on a different network
Speed costTiny, microsecondsSmall, but it does more work
A home "router" is really several devices in one box: a router, a small switch (the LAN ports), a WiFi access point, a DHCP server and a DNS forwarder.

Case 1: same network

The front desk PC prints an order on the office printer.

Office routereth0 198.51.100.20 (to ISP)eth1 172.16.1.1 · eth2 172.16.2.1eth1eth2eth0ISP → internetkade-api 34.87.120.15staff · 172.16.1.0/24warehouse · 172.16.2.0/24switchswitchFront desk PC.10Manager laptop.11Office printer.20Stock scanner.10Packing PC.11Label printer.20
  • sender
  • target and path
  • router used
  1. Front desk PC first checks: is the office printer on my own network? Its mask, 255.255.255.0, says only the first three numbers name the network. Its own address gives 172.16.1.0; the office printer’s gives 172.16.1.0. They match: same network.
  2. It sends straight to the office printer (172.16.1.20). The staff switch passes the frame across.
  3. The router is not used.

Case 2: the other office network

The front desk PC sends a shipping label to the label printer in the warehouse.

Office routereth0 198.51.100.20 (to ISP)eth1 172.16.1.1 · eth2 172.16.2.1eth1eth2eth0ISP → internetkade-api 34.87.120.15staff · 172.16.1.0/24warehouse · 172.16.2.0/24switchswitchFront desk PC.10Manager laptop.11Office printer.20Stock scanner.10Packing PC.11Label printer.20
  • sender
  • target and path
  • router used
  1. Front desk PC first checks: is the label printer on my own network? Its mask, 255.255.255.0, says only the first three numbers name the network. Its own address gives 172.16.1.0; the label printer’s gives 172.16.2.0. They differ: different networks.
  2. It hands the packet to its default gateway, 172.16.1.1 (router eth1).
  3. The router looks up 172.16.2.20: 172.16.2.0/24 is directly connected on eth2.
  4. The router sends it out eth2, and the warehouse switch delivers it to the label printer.

Step 3 is where the router uses its own routing table (chapter 5). It knows both office networks because it is directly connected to them:

DestinationSend toWhy
172.16.1.0/24directly, out eth1Connected: eth1 is on this network
172.16.2.0/24directly, out eth2Connected: eth2 is on this network
198.51.100.0/24directly, out eth0Connected: the link to the ISP
0.0.0.0/0198.51.100.1, out eth0Everything else: the router's own default gateway

Case 3: out to the internet

The front desk PC loads an order from Kadé's API.

Office routereth0 198.51.100.20 (to ISP)eth1 172.16.1.1 · eth2 172.16.2.1eth1eth2eth0ISP → internetkade-api 34.87.120.15staff · 172.16.1.0/24warehouse · 172.16.2.0/24switchswitchFront desk PC.10Manager laptop.11Office printer.20Stock scanner.10Packing PC.11Label printer.20
  • sender
  • target and path
  • router used
  1. Front desk PC first checks: is kade-api on my own network? Its mask, 255.255.255.0, says only the first three numbers name the network. Its own address gives 172.16.1.0; kade-api’s gives 34.87.120.0. They differ: different networks.
  2. It hands the packet to its default gateway, 172.16.1.1 (router eth1).
  3. The router looks up 34.87.120.15. None of its connected networks match, so it uses its default route 0.0.0.0/0 → the ISP (198.51.100.1), out eth0.
  4. The ISP’s routers pass it on, hop by hop, to kade-api in GCP. On the way out, NAT swaps the private source 172.16.1.10 for 198.51.100.20.

Try any pair

Pick a sender and a target. The map shows the path and the steps below it explain each decision.

Sender
Target
Office routereth0 198.51.100.20 (to ISP)eth1 172.16.1.1 · eth2 172.16.2.1eth1eth2eth0ISP → internetkade-api 34.87.120.15staff · 172.16.1.0/24warehouse · 172.16.2.0/24switchswitchFront desk PC.10Manager laptop.11Office printer.20Stock scanner.10Packing PC.11Label printer.20
  • sender
  • target and path
  • router used
  1. Packing PC first checks: is the office printer on my own network? Its mask, 255.255.255.0, says only the first three numbers name the network. Its own address gives 172.16.2.0; the office printer’s gives 172.16.1.0. They differ: different networks.
  2. It hands the packet to its default gateway, 172.16.2.1 (router eth2).
  3. The router looks up 172.16.1.20: 172.16.1.0/24 is directly connected on eth1.
  4. The router sends it out eth1, and the staff switch delivers it to the office printer.

The three cases side by side

#From front desk PC toTarget AND maskSame network?First stepRouter's job
1Office printer 172.16.1.20172.16.1.0yesStraight to the printer, via the switchnone
2Label printer 172.16.2.20172.16.2.0noGateway 172.16.1.1Connected route → out eth2
3kade-api 34.87.120.1534.87.120.0noGateway 172.16.1.1Default route → ISP, out eth0
The PC's own network is 172.16.1.0 in every row. Only one comparison decides everything that follows.

What we skipped

A few things were quietly assumed in this chapter. Each gets its own chapter next:

MAC addressTo put a frame on the wire toward the next device, we need that device's physical address. In case 2 the PC's frame goes to eth1's MAC, and the router builds a new frame for the label printer's MAC.
ARPHow the PC finds a MAC from an IP: the printer's in case 1, the router's (172.16.1.1) in cases 2 and 3.
NAT172.16.1.10 is private. In case 3 the router swaps it for 198.51.100.20 before the packet leaves eth0, and swaps it back on the reply.
Hop-by-hop routingHow the ISP and the routers after it find their way to 34.87.120.15, and what changes in the packet at each hop.

Summary

#IdeaIn one lineNew terms
1The setupTwo /24 networks joined by one router
2InterfacesA router has one interface per network, each with its own IProuter interface
3Switch vs routerSwitch joins devices in a network (MAC); router joins networks (IP)switch
4Same networkAND results match → direct, through the switch
5Other networkNo match → gateway → router's connected routeconnected route
6InternetNo match → gateway → router's default route → ISP
7NextMAC, ARP, NAT and hop-by-hop routing fill the gaps

Try it yourself

Path to a same-network device

  • What to look for: One hop: the device itself. No router in between.
  • Runs on: Windows: tracert · any device on your own network
  • From section: 4. Case 1: same network
traceroute 192.168.1.60

Path out to the internet

traceroute 1.1.1.1

Which way a packet would go

  • What to look for: via 172.16.1.1 for another network; no via for your own.
  • Runs on: Linux, on a machine with these networks
  • From section: 5. Case 2: the other office network
ip route get 172.16.2.20 from 172.16.1.10

One line per interface

  • What to look for: Several interfaces, each with an address on a different network.
  • Runs on: Linux, run on a router or a VM with several NICs
  • From section: 2. A router has several interfaces
ip -brief addr

Spotted a mistake, or something unclear? Suggest an edit on GitHub