Sub topic 5
The default route
This chapter assumes you can read a routing table and apply longest prefix match from the earlier chapters. It also assumes you know CIDR notation, such as /24 meaning a 24-bit network prefix. Here you will learn what the default route is, how it behaves during lookup, and how it fails.
What the default route is
A default route is a routing table entry whose prefix length is zero. In IPv4 it is written 0.0.0.0/0. In IPv6 it is written ::/0. A prefix of length zero requires no bits of the destination to match, so every address matches it.
Under longest prefix match, this makes the default route the entry of last resort. Any entry with a longer prefix beats it. The router uses the default route only when no other entry matches.
This is how routers and hosts reach destinations they have no specific knowledge of. A home router does not know where 8.8.8.8 is. It only knows that anything not on the local network goes to its internet provider. One entry covers the whole rest of the internet.
| Property | Specific route (for example `10.1.0.0/16`) | Default route (`0.0.0.0/0`) |
|---|---|---|
| Addresses matched | 65,536 | All 4,294,967,296 |
| Prefix length | 16 | 0 |
| Priority in lookup | Higher | Lowest |
| Typical use | A known network | Everything else |
Where the default route sits in the lookup
The router first finds every entry that contains the destination. It then picks the longest prefix among them. The default route is always in the candidate set, so a router that has one never fails to find a match.
The next code block extends the lookup function from the longest prefix match chapter. It shows what happens when the default route is present and when it is removed.
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(table, destination):
addr = ipaddress.ip_address(destination)
matches = [(net, hop) for net, hop in table if addr in net]
if not matches:
return None
return max(matches, key=lambda m: m[0].prefixlen)
without_default = [r for r in routes if r[0].prefixlen != 0]
print(lookup(routes, "10.1.2.3")) # (IPv4Network('10.1.0.0/16'), '192.0.2.3')
print(lookup(routes, "8.8.8.8")) # (IPv4Network('0.0.0.0/0'), '192.0.2.1')
print(lookup(without_default, "8.8.8.8")) # NoneThe last line is the failure case. With no match, the real router discards the packet.
Default routes on hosts
An end host has a routing table too, and it is usually tiny. A typical host has two entries. One is a connected route for its own subnet. The other is a default route pointing at the local router, often called the default gateway.
The host's decision for each outgoing packet is simple. If the destination is inside the connected subnet, the host sends it directly to that address. If not, the host sends it to the gateway. The destination IP stays the same in both cases. Only the Layer 2 destination changes.
These Linux commands show and change the default route. The addresses come from the documentation range 192.0.2.0/24.
# Show only the default route
ip route show default
# Example: default via 192.0.2.1 dev eth0 proto dhcp metric 100
# Ask which route a destination would use
ip route get 8.8.8.8
# Install or replace the default route
sudo ip route replace default via 192.0.2.1 dev eth0
# Remove it
sudo ip route del defaultUse replace instead of add when scripting. add fails with RTNETLINK answers: File exists if a default route with the same metric is already present. replace is safe to run repeatedly.
After you delete the default route, a ping to an outside address fails immediately with Network is unreachable. The host's own kernel produces that error and sends no packet. Hosts on the local subnet still work, because the connected route is still there.
Where default routes come from
A default route enters the table in one of these ways:
- DHCP. The server hands the host a router address, and the host installs a default route to it. This is the usual case for end hosts.
- Static configuration. An administrator adds it by hand, as in the
ip route replacecommand above. This suits a small office router with one uplink. - A dynamic routing protocol. An upstream router advertises
0.0.0.0/0to its neighbors, and they install it. OSPF and BGP can both do this. A later chapter covers how.
Any of these sources can be present on the same router. Metrics and administrative distance, covered later, decide which one wins.
Requirements and failure modes
The next hop must be reachable
A default route says "send everything to this next hop." The next hop must be an address on a directly connected network. If the router cannot resolve that next hop to a MAC address, it cannot build the Layer 2 frame, and packets that match the default route are dropped. Many routers will not install a static route whose next hop is unreachable. Linux rejects it with Nexthop has invalid gateway.
Missing default route
Without a default route, a destination that matches nothing is unreachable. A router drops the packet and sends an ICMP Destination Unreachable message with the code "network unreachable" back to the source. A host reports the error locally instead. This is a common cause of "I can reach the local network but nothing beyond it."
Wrong default route
A default route that points to the wrong next hop is harder to find. Traffic flows, but it takes a poor path or reaches a router that cannot forward it further. Check with ip route get for a specific destination, then run traceroute to see where the path stops.
Loops
If two routers each use the other as their default, any destination that neither knows about bounces between them. The packet's TTL falls by one at each hop until it reaches zero. A router then discards the packet and sends an ICMP Time Exceeded message.
Multiple default routes
Two default routes with different metrics give a primary and a backup. The lower metric is used. If the primary's interface goes down, its route is removed, and the backup takes over. Two defaults with equal metrics may share load, depending on the router's equal-cost multipath settings. A multihomed host with two interfaces, such as Wi-Fi and Ethernet, often has two defaults and sends everything out of the one with the lower metric.
Default routes and the internet core
Routers in the core of the internet do not use a default route. They carry a full table of every public prefix, and this zone is called the default-free zone. A packet to an unrouted address is dropped there instead of being passed on. Smaller networks lean on defaults precisely because they cannot or do not need to carry that table.
Summary of behavior
| Situation | Result |
|---|---|
| Destination matches a specific route | Specific route is used, default ignored |
| Destination matches nothing, default present | Packet goes to the default next hop |
| Destination matches nothing, no default | Packet dropped, ICMP Destination Unreachable sent |
| Default next hop cannot be resolved | Packet dropped at Layer 2 |
| Two routers default to each other | Loop until TTL reaches zero |
Exercise
A router has these IPv4 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 a packet to 172.16.5.9, and for one to 172.32.0.1? What happens to a packet to 172.32.0.1 if the default route is deleted?
Show answer
172.16.5.9 matches all three entries, and the longest prefix is /24, so the next hop is 192.0.2.3. 172.32.0.1 is outside 172.16.0.0/12, which covers only 172.16.0.0 to 172.31.255.255, and outside 172.16.5.0/24. Only the default route matches, so the next hop is 192.0.2.1. With the default deleted, nothing matches, so the router drops the packet and sends an ICMP Destination Unreachable (network unreachable) message to the source.
Exercise
A Linux host at 192.0.2.10/24 can ping 192.0.2.50 but gets "Network is unreachable" when pinging 8.8.8.8. Which command shows the likely cause, and what command fixes it if the gateway is 192.0.2.1 on eth0?
Show answer
Run "ip route show default" (or "ip route get 8.8.8.8"). It will show no default route. Fix it with "sudo ip route replace default via 192.0.2.1 dev eth0". The local ping works because the connected route for 192.0.2.0/24 still exists.