Sub topic 5
The default route
This chapter assumes you can read an IPv4 prefix such as 10.1.0.0/16 and that you know how longest prefix match picks the most specific route. It also assumes you are comfortable running commands in a Linux terminal.
What the default route is
A router cannot list every network on the internet in its routing table. The default route covers every destination that no other entry matches. It is written 0.0.0.0/0 in IPv4 and ::/0 in IPv6. Many tools and router configurations simply call it default.
The prefix length is 0, so no bits of the destination address have to match. Every address falls inside 0.0.0.0/0. Because longest prefix match prefers the longest prefix, the default route wins only when nothing more specific matches. It is the last resort.
Seeing it in a lookup
The following code keeps every matching route and picks the longest prefix. The default route is always in the match list, but it is chosen only when it is the only entry there.
import ipaddress
routes = [
(ipaddress.ip_network("0.0.0.0/0"), "192.0.2.1"),
(ipaddress.ip_network("10.0.0.0/8"), "192.0.2.2"),
(ipaddress.ip_network("10.1.0.0/16"), "192.0.2.3"),
]
def lookup(destination):
addr = ipaddress.ip_address(destination)
matches = [(net, hop) for net, hop in routes if addr in net]
if not matches:
return None
return max(matches, key=lambda m: m[0].prefixlen)
print(lookup("10.1.2.3")) # (IPv4Network('10.1.0.0/16'), '192.0.2.3')
print(lookup("10.9.9.9")) # (IPv4Network('10.0.0.0/8'), '192.0.2.2')
print(lookup("8.8.8.8")) # (IPv4Network('0.0.0.0/0'), '192.0.2.1')Remove the first route and lookup("8.8.8.8") returns None. A real router handles that case by dropping the packet and sending an ICMP Destination Unreachable message to the source.
Where default routes are used
Most end hosts and small-site routers have exactly one default route. It points at the next hop that leads toward the rest of the world, usually an upstream router or an ISP. The device only needs specific entries for its directly connected networks. Everything else goes to the gateway.
Setting and checking it on Linux
# Add a default route through the gateway 192.0.2.1
sudo ip route add default via 192.0.2.1 dev eth0
# Show the table; the default entry is listed first as "default via ..."
ip route show
# Confirm which route a destination uses
ip route get 8.8.8.8Common errors
- No default route. Destinations outside the connected networks fail. On a Linux host this shows as "Network is unreachable".
- Wrong gateway. The default next hop must be on a directly connected network. Otherwise the router cannot resolve its Layer 2 address.
- Default route loops. If router A's default points at B and B's default points back at A, a packet for an unknown destination bounces between them until its TTL reaches zero. The result is an ICMP Time Exceeded message.
- Two competing defaults. Two equal default routes on one host can cause unpredictable path choice, or a failure when the route you meant to use is not the one selected.
Exercise
A router has these routes: 0.0.0.0/0 via 192.0.2.1, 172.16.0.0/12 via 192.0.2.2, and 172.16.5.0/24 via 192.0.2.3. Which next hop does it use for 172.16.5.9, for 172.20.1.1, and for 203.0.113.7?
Show answer
172.16.5.9 matches all three routes, and the /24 is the longest, so the next hop is 192.0.2.3. 172.20.1.1 is inside 172.16.0.0/12 (172.16.0.0 to 172.31.255.255) but not the /24, so it uses 192.0.2.2. 203.0.113.7 matches only the default route, so it uses 192.0.2.1.