Sub topic 1
How a Router Decides Where to Send a Packet
Prerequisites
This chapter assumes you know the following:
- IPv4 addressing and CIDR prefix notation, such as 10.2.0.0/16.
- The difference between a router, a switch and a host.
- Basic command-line use on a Linux host or a router.
The forwarding decision
A router connects networks. For every packet it receives, it asks one question: which neighbor should get this packet next? The answer comes from the routing table, a list of known destination networks and the way to reach each one.
The router does not plan the whole journey. It picks only the next step, called the next hop. The next router repeats the process. This is hop-by-hop forwarding.
What a routing table entry contains
Each entry is a route. A route usually holds these fields:
| Field | Meaning |
|---|---|
| Destination prefix | The network the route covers, such as 10.2.0.0/16 |
| Next hop | The neighbor router's address, or none if the network is directly attached |
| Outgoing interface | The port the packet leaves through, such as eth0 |
| Source | How the route was learned: connected, static or a routing protocol |
| Metric | A cost used to compare routes from the same protocol |
A route to a directly connected network appears automatically when you assign an address to an interface and bring it up. Other routes come from an administrator (static routes) or from other routers (dynamic routing). Later chapters cover both.
The steps for each packet
- The router reads the destination IP address in the packet header.
- It compares that address against the prefix of every route in the table.
- It selects the best matching route. If several prefixes match, the most specific one wins. The next chapter explains this rule.
- It lowers the packet's TTL (time to live) by one. If the TTL reaches zero, the router drops the packet and sends back an ICMP "time exceeded" message.
- It sends the packet out of the chosen interface toward the next hop.
- If no route matches, the router drops the packet and sends back an ICMP "destination unreachable" message.
Seeing the table on a Linux host
A Linux host also has a routing table. You can inspect it with the ip tool.
# Show the routing table
ip route show
# Ask the kernel which route it would use for one destination
ip route get 10.2.5.7Typical output from the first command looks like this:
default via 192.168.1.1 dev eth0
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.10The first line says that traffic with no better match goes to 192.168.1.1. The second line is the directly connected network. Its proto kernel tag shows that the system created it when the address was assigned.
A worked example
Suppose a router has these routes:
- 192.168.1.0/24 is directly connected on eth0.
- 10.2.0.0/16 is reachable via next hop 192.168.1.2.
A packet arrives for 10.2.5.7. The address falls inside 10.2.0.0/16, so the router forwards the packet to 192.168.1.2 through eth0. A packet for 172.16.0.9 matches nothing. With no other route, the router drops it and reports "destination unreachable".
Common errors
- A missing return route. The router knows how to reach the destination, but the remote side has no route back. Pings then fail even though the forward path works.
- An unreachable next hop. A route points to a next hop that is not on a connected network. The router cannot send to it, so the route is not usable.
- Assuming a router knows the whole path. Each router decides only its own next hop. A fault several hops away appears as a loss further down the path, not at the router that has the problem.
Exercise
A router has the connected route 192.168.1.0/24 on eth0 and one static route, 10.2.0.0/16 via 192.168.1.2. What does it do with a packet for 10.3.0.5, and what does it send back to the sender?
Show answer
The address 10.3.0.5 is not inside 192.168.1.0/24 or 10.2.0.0/16, so no route matches. The router drops the packet and sends an ICMP "destination unreachable" message to the sender.
Exercise
Why must every router along a path have its own routes, rather than only the first router?
Show answer
Each router chooses only the next hop from its own routing table. If any router on the path lacks a route to the destination, the packet is dropped there.