Sub topic 1
How a Router Decides Where to Send a Packet
Prerequisites
This chapter assumes you understand IPv4 addresses, subnet masks or CIDR notation, and the difference between a host, switch, and router. You should also be comfortable running basic commands on Linux or a network device.
The forwarding decision
A router receives a packet and examines its destination IP address. It compares that address with entries in its routing table. Each matching entry specifies a next hop or an outgoing interface.
The router normally selects the matching route with the longest prefix. If competing routes have the same prefix length, it compares the route source, using administrative distance, and then the protocol metric. The selected entry becomes the forwarding decision.
Longest-prefix match: the most specific route wins
A prefix length states how many leading bits identify a network. A /24 route is more specific than a /16 route because it matches a smaller address range.
For destination 10.2.5.7, these routes both match:
| Route | Matching range | Result |
|---|---|---|
| `10.0.0.0/8` | `10.0.0.0`–`10.255.255.255` | Less specific |
| `10.2.0.0/16` | `10.2.0.0`–`10.2.255.255` | Wins |
The /16 route wins even if the /8 route was learned from a different source. Longest-prefix matching happens before comparing route sources and metrics.
Static routes: configuring paths by hand
A static route is a route an administrator enters manually. It is predictable and has little protocol overhead, but it does not automatically adapt when the topology changes.
On Linux, this command sends traffic for 10.2.0.0/16 to next hop 192.168.1.2 through eth0:
sudo ip route add 10.2.0.0/16 via 192.168.1.2 dev eth0
ip route show
ip route get 10.2.5.7The final command shows the route the Linux kernel would use for that destination.
The default route as a catch-all
A default route is 0.0.0.0/0. It matches every IPv4 destination, but it loses to any more specific matching route. Hosts commonly use a default route toward their local router. Routers often use one toward an upstream provider.
Exercise
Which route handles 10.2.5.7: 10.0.0.0/8 or 10.2.0.0/16?
Show answer
10.2.0.0/16, because it has the longer and more specific prefix.