Sub topic 1
What a Router Does and Where It Sits
Prerequisites
This chapter assumes you know the following:
- What an IP address and a subnet are.
- The difference between a network, a host, and a link.
- What an Ethernet frame and a MAC address are.
- How to read output in a command-line terminal.
The role of a router
A router is a device that moves IP packets from one network to another. Each of its interfaces connects to a different network. When a packet arrives, the router reads the destination IP address, decides which interface the packet should leave through, and sends it on.
Compare this with two other devices:
| Device | Works with | Decides using | Connects |
|---|---|---|---|
| End host | Application data | Its own routing table, mostly a default route | Itself to one network |
| Switch | Ethernet frames | Destination MAC address | Hosts inside one network |
| Router | IP packets | Destination IP address | Different networks |
A switch keeps traffic inside one Layer 2 network, called a broadcast domain. A router sits on the border between such networks. Each interface of a router has its own IP address and belongs to the network attached to that interface.
An end host can also forward in principle. A Linux machine does so only if you enable it, as the next section shows. By default a host drops packets that are not addressed to it.
Where a router sits
Consider a host on network A that sends a packet to a host on network C.
Host A cannot reach Host C directly, because they are on different networks. Host A sends the packet to its gateway, which is the router on its own network. Router 1 passes the packet to Router 2. Router 2 delivers it to Host C. Each router makes its own decision, so no single device knows the whole path.
Seeing it on a Linux machine
You can check whether a Linux machine acts as a router. The ip_forward setting controls whether it forwards packets between interfaces.
# 0 means the machine drops packets not addressed to it, 1 means it forwards them
sysctl net.ipv4.ip_forward
# Turn forwarding on (lasts until reboot)
sudo sysctl -w net.ipv4.ip_forward=1
# Show the networks this machine is attached to, and its default gateway
ip route showThe ip route show output lists the machine's directly attached networks. It usually also lists a default via ... line, which names the router the host uses for everything else. Later chapters explain how to read that output.
Common mistakes
- Treating a router as a switch. A router does not forward frames by MAC address. It removes the incoming frame and builds a new one for the next link.
- Forgetting the host's gateway. A host with no default route can reach only its own network.
- Enabling forwarding and assuming it is finished. Forwarding also needs routes that point at the right networks.
Exercise
A host at 10.0.1.5 sends a packet to 10.0.2.9, which is on a different network. Which device must forward it, and why can a switch not do this job?
Show answer
A router must forward it. A switch only moves frames between hosts on the same Layer 2 network, using MAC addresses. The two hosts are on different IP networks, so the packet has to go to the gateway router. The router chooses the outgoing interface using the destination IP address.
Exercise
On a Linux machine, sysctl net.ipv4.ip_forward prints net.ipv4.ip_forward = 0. What does this mean for packets that arrive on one interface and are addressed to a host on another network?
Show answer
The machine drops them. With forwarding off, the kernel accepts only packets addressed to itself, so it does not act as a router.