Sub topic 1
What a Router Does and Where It Sits
Prerequisites
This chapter assumes you already know the following:
- What an IP address and a subnet are, at the level of "192.0.2.10 is on the network 192.0.2.0/24".
- The difference between a network, a host, and a link.
- That Ethernet frames carry MAC addresses and that a switch moves frames inside one network.
- How to open a terminal and run simple commands.
Later chapters cover prefixes, the routing table, and longest prefix match in detail. Here we only place the router in the picture and describe its job.
The one-sentence job
A router is a device with two or more network interfaces that receives an IP packet on one interface and sends it out another interface, toward the destination address in the packet's IP header. It makes that choice by looking at the Layer 3 (IP) destination address, not the MAC address.
Each interface sits in a different IP network. The router is the member of every one of those networks at once, so it is the point where traffic can leave one network and enter another.
Router, switch, and end host
Three kinds of devices appear in almost every network diagram. They differ in which address they use to make decisions.
| Device | Decides using | Forwards between | Typical state it keeps |
|---|---|---|---|
| End host | Its own routing table, for outgoing packets | Nothing. It is the source or the final destination of packets | Few routes: a connected network and a default route |
| Switch (Layer 2) | Destination MAC address | Ports in the same IP network (same broadcast domain) | MAC address table |
| Router (Layer 3) | Destination IP address | Different IP networks | Routing table and forwarding table |
Three points follow from this table.
- A switch does not change the network. A frame that enters one switch port leaves another port unchanged, and the sender and receiver share an IP subnet. A broadcast frame is flooded to every port.
- A router separates broadcast domains. A router does not forward Ethernet broadcasts. Each router interface is the edge of one broadcast domain. This is why routers are used to divide a large network into smaller ones.
- An end host also makes a routing decision. Before sending, a host asks one question: is the destination on my own network? If so, it sends directly. If not, it sends the packet to a router, usually called its default gateway. Hosts do not forward packets that were meant for someone else, unless you configure them to act as routers.
Where a router sits
A router sits on the boundary between networks. Common places are:
- Between a home or office LAN and an internet service provider (ISP). Home "routers" are usually a router, a switch, a Wi-Fi access point, and often a firewall in one box.
- Between internal subnets in a company, for example between a user network and a server network.
- Inside an ISP and between ISPs, where routers exchange traffic for entire organizations.
The diagram below shows two LANs joined by one router. Each host talks to its own switch, and the router connects the two networks.
Host A and Host B are in 10.1.0.0/16, so they reach each other through Switch 1 alone. Host A reaches Host C only through the router, because Host C is in 10.2.0.0/16.
A packet crossing the router
Suppose Host A sends a packet to Host C. The full per-packet procedure is the subject of a later chapter. At this stage the key idea is which addresses change and which do not.
The IP source and destination addresses stay the same from end to end. The Ethernet source and destination addresses are valid for one link only, so the router builds a new frame for the outgoing link. This is the core difference between Layer 2 delivery (one link) and Layer 3 delivery (across many links).
Host A addressed its frame to the router's MAC address, even though the IP destination was Host C. Host A did this because its routing table told it that 10.2.0.10 is not local and must be sent to the gateway 10.1.0.1.
What a router actually does, in four jobs
- Receives a frame on an interface and checks that it is meant for the router's MAC address (or is a broadcast or multicast it should process).
- Looks up the destination IP address in its routing information to find the outgoing interface and, if needed, the next router.
- Rewrites the packet for the next hop: it decrements the TTL (time to live), updates the header checksum for IPv4, and builds a new Layer 2 header.
- Sends the new frame out the chosen interface.
If the lookup finds no route, the router drops the packet and normally returns an ICMP "destination unreachable" message to the source. Each of these steps gets its own chapter later.
Seeing the role on a real machine
A Linux machine becomes a router when it has more than one interface and IPv4 forwarding is turned on. You can check and change the setting with these commands.
# 0 means this machine drops packets that are not addressed to it
# 1 means it forwards them between interfaces, like a router
sysctl net.ipv4.ip_forward
# Turn forwarding on until the next reboot
sudo sysctl -w net.ipv4.ip_forward=1
# List the interfaces and their addresses
ip -brief address show
# Show the routes this machine uses
ip route showA typical end host prints something like this from ip route show:
default via 192.0.2.1 dev eth0
192.0.2.0/24 dev eth0 proto kernel scope link src 192.0.2.10The second line is the connected network. The first line is the default route that points to the router. Even a host with no forwarding enabled uses this table to decide where to send its own packets.
Common mistakes
- Treating a router and a switch as the same. A switch cannot move traffic between two IP subnets on its own. Some switches can, but only because they include a routing function (a "Layer 3 switch").
- Forgetting the gateway setting. A host with a correct IP address and mask but no default gateway can reach its own subnet only. Pings to other networks fail.
- Expecting forwarding to be on by default. On a general-purpose operating system, forwarding is usually off. A multi-homed Linux server drops transit traffic until you enable it.
- Thinking the router sees only the destination. A router reads the whole IP header. It uses the destination for the lookup, but it also decrements the TTL and may apply policies using the source address or other fields.
- Forgetting the return path. Forwarding works in each direction separately. Host C must also have a route back to 10.1.0.0/16, usually by pointing its default gateway at the router. Without it, requests arrive but replies are lost.
Summary
A router joins different IP networks and moves packets between them by looking at the destination IP address. A switch moves frames inside one network by looking at the MAC address. An end host sends local traffic directly and everything else to its default gateway. IP addresses stay constant end to end, while the Layer 2 header is rebuilt on every link. The next chapters explain how the router knows which networks exist, starting with addresses and prefixes.
Exercise
Host A (10.1.0.10/16) sends a packet to Host C (10.2.0.10) through the router in the diagram. On the link between the router and Host C, which addresses in the packet are different from the ones on the link between Host A and the router: the IP source, the IP destination, the Ethernet source, or the Ethernet destination?
Show answer
Only the Ethernet addresses are different. On the first link the Ethernet source is Host A's MAC and the destination is the router eth0 MAC. On the second link the source is the router eth1 MAC and the destination is Host C's MAC. The IP source (10.1.0.10) and IP destination (10.2.0.10) do not change, apart from the TTL being decremented and the checksum being updated.
Exercise
A new server has IP address 10.2.0.50/16 and can ping Host C at 10.2.0.10, but it cannot ping Host A at 10.1.0.10. The router is working. What is the most likely missing configuration on the server, and which command shows it?
Show answer
The server probably has no default route (or a wrong default gateway), so it does not know to send non-local traffic to the router at 10.2.0.1. Running "ip route show" would show whether a "default via 10.2.0.1" entry exists. Adding it with "sudo ip route add default via 10.2.0.1" fixes the problem.