Sub topic 1
How a Router Decides Where to Send a Packet
Prerequisites
This chapter assumes you already know three things:
- IPv4 addressing, including subnet masks and CIDR prefix notation such as
10.2.0.0/16. - The difference between a router, a switch and a host. A router forwards packets between networks, a switch forwards frames inside one network, and a host is an end device.
- Basic command-line use on a router or a Linux host.
You do not need to know any routing protocol yet. Later chapters cover static routes, dynamic protocols and how to choose between them. This chapter covers the decision every router makes, whichever way its table was filled.
The forwarding decision in one sentence
A router looks at the destination IP address of each packet, finds the best matching entry in its routing table, and sends the packet out of the interface named in that entry, toward the next hop it names. Everything else in routing exists to put correct entries in that table.
The router repeats this decision for every packet and every hop. It does not know the whole path. It only knows the next step. The path is the sum of many independent local decisions.
Control plane and data plane
Two jobs run on a router, and keeping them apart makes the rest of the topic easier.
- The control plane builds the routing table. Static routes entered by an administrator and routes learned from protocols such as OSPF or BGP are inputs to it.
- The data plane forwards packets using the result. It looks up each destination address and moves the packet.
Static and dynamic routing differ only in the control plane: how entries get into the table. The data-plane lookup is the same for both.
On many routers the control plane compiles the table into a faster structure for forwarding. Cisco calls it the FIB (Forwarding Information Base). Linux has a similar split between the routing tables and the kernel's forwarding cache. For this chapter, treat "routing table" and "forwarding table" as the same thing.
What a routing table entry contains
Each entry is a route. A route has these parts:
| Field | Meaning | Example |
|---|---|---|
| Destination prefix | The range of addresses the route covers | `10.2.0.0/16` |
| Next hop | The IP address of the next router, or none if the network is directly attached | `192.168.1.2` |
| Outgoing interface | Where the packet leaves the router | `eth0` |
| Source | How the router learned the route | connected, static, OSPF |
| Metric | A cost used to compare routes from the same source | `20` |
Routes get into the table from three kinds of source:
- Connected routes. When you assign
192.168.1.1/24to an interface and bring it up, the router adds a route for192.168.1.0/24automatically. - Static routes. An administrator types them in.
- Dynamic routes. A routing protocol learns them from neighbouring routers.
The lookup steps
For one incoming packet, the router does the following:
- Read the destination IP address from the packet header.
- Compare it against every prefix in the table, and keep the entries whose prefix contains the address.
- Among those, pick the one with the longest prefix length. The next chapter covers this rule, longest-prefix match, in detail.
- If several routes have the same prefix, pick by administrative distance, then by metric. These are covered in later chapters.
- Decrement the packet's TTL. If it reaches zero, drop the packet and send an ICMP Time Exceeded message to the sender.
- Send the packet toward the next hop on the chosen interface.
- If no entry matches at all, drop the packet and send an ICMP Destination Unreachable message.
A worked example
Suppose router R1 has this table:
| Destination | Next hop | Interface | Source |
|---|---|---|---|
| `10.1.0.0/16` | none | `eth1` | connected |
| `192.168.1.0/24` | none | `eth0` | connected |
| `10.2.0.0/16` | `192.168.1.2` | `eth0` | static |
| `0.0.0.0/0` | `192.168.1.1` | `eth0` | static |
A packet arrives with destination 10.2.5.7. Walk through the lookup:
10.1.0.0/16does not contain10.2.5.7. No match.192.168.1.0/24does not contain it. No match.10.2.0.0/16contains it. Match.0.0.0.0/0contains every address. Match.
Two entries match. The /16 is more specific than the /0, so R1 sends the packet to 192.168.1.2 out of eth0. A packet for 8.8.8.8 matches only 0.0.0.0/0, so it goes to 192.168.1.1.
Seeing the decision on a Linux host
Linux lets you add a route, list the table, and ask the kernel which route it would pick for one address.
# Linux host or router: add a static route to 10.2.0.0/16 via a next hop
sudo ip route add 10.2.0.0/16 via 192.168.1.2 dev eth0
# Show the routing table
ip route show
# Ask the kernel which route it would use for one destination
ip route get 10.2.5.7The first command adds a static route. The second lists the table. The third shows the route chosen for 10.2.5.7, and its output names the next hop, the interface and the source address the kernel would use.
A hypothetical output of ip route get looks like this:
10.2.5.7 via 192.168.1.2 dev eth0 src 192.168.1.10 uid 1000
cacheRead it from left to right. The destination is 10.2.5.7. The next hop is 192.168.1.2. The packet leaves through eth0, with source address 192.168.1.10.
Adding that route requires the next hop to be reachable first. The kernel refuses a route whose next hop is not on a connected network, and returns Network is unreachable. Check that 192.168.1.0/24 is attached to eth0 before you add the route.
The same table on a Cisco router
The Cisco IOS equivalents use the same ideas with different syntax. The first line sends traffic for 10.2.0.0/16 to a fixed next hop. The second line (0.0.0.0/0) matches any destination that no more specific route covers.
! Cisco IOS: static route to 10.2.0.0/16 via next hop 192.168.1.2
ip route 10.2.0.0 255.255.0.0 192.168.1.2
! Default route toward the upstream router
ip route 0.0.0.0 0.0.0.0 192.168.1.1To inspect the result, use these commands:
! Cisco IOS: inspect the routing table
show ip route
show ip route static
show ip route ospf
show ip route 10.2.5.7The last command is the IOS counterpart of ip route get. It shows the specific route the router would use for one destination.
Next hop resolution
A route that names a next hop IP address is not the end of the lookup. To transmit the packet on Ethernet, the router needs the next hop's MAC address. It finds it with ARP for IPv4, using the interface the route names, and builds a new Ethernet frame addressed to that MAC.
Two consequences follow:
- The IP destination address stays the same across hops, but the Ethernet source and destination addresses change at every hop.
- If ARP fails, the route can look correct in the table while packets are still dropped. A next hop that is down or misaddressed causes this.
This is why "the route is in the table" does not prove that traffic flows.
Common errors
These problems appear often when someone starts working with routing tables:
- Asymmetric routing. The forward path and return path use different routes. Each router's decision is local, so a missing route on the return side breaks a conversation even though forward packets arrive. Always check both directions.
- A next hop that is not reachable. The route is rejected, or installed but unusable, because the router has no way to reach the next hop address.
- No route back to the source. A ping fails because the reply has nowhere to go, not because the request was lost.
- Routing loops. Two routers each point at the other for the same prefix. The TTL field limits the damage. A packet circulates until TTL reaches zero, and then it is dropped.
- A missing default route. Traffic to unknown destinations is dropped instead of sent upstream.
- Confusing the table with reachability. A route proves only that the router has a rule, not that the next hop is alive.
What this chapter sets up
The lookup above uses one tie-breaking idea, the most specific prefix, and mentions two others, administrative distance and metric. The following chapters cover these in turn:
- Longest-prefix match, to select among overlapping prefixes.
- Static routes and the default route, to fill the table by hand.
- Dynamic routing, to let routers fill it for each other.
- Administrative distance and metrics, to choose between competing sources.
Exercise
Router R1 has these routes: 10.0.0.0/8 via 192.168.1.2, 10.2.0.0/16 via 192.168.1.3, and 0.0.0.0/0 via 192.168.1.1. Which next hop does R1 use for a packet to 10.2.5.7, and which for a packet to 10.9.1.1?
Show answer
For 10.2.5.7, three routes match, and the /16 is the most specific, so R1 uses 192.168.1.3. For 10.9.1.1, the /8 and the default route match, but the /8 is more specific, so R1 uses 192.168.1.2.
Exercise
A router has a correct route to 10.2.0.0/16 via 192.168.1.2, yet packets to 10.2.5.7 are dropped. Name two things you would check that are not visible from the route entry itself.
Show answer
Check that the next hop 192.168.1.2 is alive and that the router can resolve its MAC address with ARP. Also check that the next hop router has a route to 10.2.5.7, and that a return route to the sender exists on the far side, because asymmetric or missing return routes also break traffic.