Sub topic 3
Reading a routing table
This chapter assumes you know what an IP address and a subnet are. It also assumes you can run commands in a Linux terminal. By the end you should be able to read any line of a routing table and say what it tells the router to do. Choosing between overlapping entries is covered in the next chapter. Here the goal is to understand what each entry contains.
What a routing table is
A routing table is a list of rules the router consults for every packet. Each rule says: "for destinations inside this prefix, send the packet this way." A prefix is an address block written in CIDR notation, such as 10.2.0.0/16.
An entry answers two questions:
- Which destinations does this rule cover? The prefix answers this.
- Where does the packet go next? The next hop or the outgoing interface answers this.
Hosts have routing tables too. Your laptop uses one to decide whether a destination is on the local link or must go through a gateway. The format is the same on a host and on a router.
Where entries come from
Entries get into the table in three ways, and the table records which way for each one.
- Connected routes appear automatically when an interface has an IP address and is up. If an interface has
192.168.1.10/24, the router adds a route for192.168.1.0/24pointing at that interface. - Static routes are entered by an administrator. They stay in the table until someone removes them.
- Dynamic routes are learned from other routers through a routing protocol such as OSPF or BGP. They change as the network changes.
A host also gets a default route, often from DHCP. A later chapter covers that entry on its own.
Anatomy of an entry
Each entry has several fields. Different systems show them in different layouts, but the content is the same.
| Field | Meaning | Example |
|---|---|---|
| Prefix | The block of destinations the entry covers | `10.2.0.0/16` |
| Next hop | The neighbor router to hand the packet to, if any | `via 192.168.1.2` |
| Interface | The local port the packet leaves through | `dev eth0` |
| Source | How the route was learned | `proto static` |
| Metric | A cost used to compare routes from the same source | `metric 20` |
An entry with a next hop means "the destination is beyond another router." An entry with no next hop means "the destination is directly attached to this interface."
Reading Linux output
On Linux, ip route show prints one entry per line. Here is typical output from a small router:
default via 192.168.1.1 dev eth0 proto dhcp metric 100
10.2.0.0/16 via 192.168.1.2 dev eth0 proto static metric 20
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.10 metric 100Read each line from left to right:
default via 192.168.1.1 dev eth0 proto dhcp metric 100.defaultis the entry that matches anything not matched elsewhere. Packets go to the neighbor192.168.1.1out ofeth0. DHCP installed this route.10.2.0.0/16 via 192.168.1.2 dev eth0 proto static metric 20. Destinations in10.2.0.0/16go to the neighbor192.168.1.2. An administrator added it by hand.192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.10 metric 100. There is novia, so this is a connected network. The kernel created the route from the address oneth0.scope linkmeans the destinations are reachable on the local link.srcis the address the router uses as the source for packets it originates toward this network.
The proto field names the source. Common values are kernel for connected routes, static for manual routes, dhcp, and the name of a routing daemon such as bgp or ospf.
Parsing a table in code
The line format is regular, so a small script can pull out the fields. This script reads the sample output above and prints a summary of each entry:
SAMPLE = """default via 192.168.1.1 dev eth0 proto dhcp metric 100
10.2.0.0/16 via 192.168.1.2 dev eth0 proto static metric 20
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.10 metric 100
"""
KEYS = {"via", "dev", "proto", "metric", "src", "scope"}
def parse_route(line):
words = line.split()
route = {"prefix": words[0]}
i = 1
while i < len(words):
if words[i] in KEYS and i + 1 < len(words):
route[words[i]] = words[i + 1]
i += 2
else:
i += 1
return route
for line in SAMPLE.strip().splitlines():
r = parse_route(line)
next_hop = r.get("via", "directly connected")
print(f"{r['prefix']:<16} next hop: {next_hop:<20} "
f"out: {r['dev']} source: {r.get('proto', '?')}")The output is:
default next hop: 192.168.1.1 out: eth0 source: dhcp
10.2.0.0/16 next hop: 192.168.1.2 out: eth0 source: static
192.168.1.0/24 next hop: directly connected out: eth0 source: kernelThe first word of each line is the prefix. After that, the script treats the line as keyword and value pairs. Any word it does not know, such as a flag, is skipped.
Reading router-style output
Many router operating systems print a table with a code letter at the start of each line. The exact layout varies by vendor, but this style is common:
C 192.168.1.0/24 is directly connected, GigabitEthernet0/0
L 192.168.1.10/32 is directly connected, GigabitEthernet0/0
S 10.2.0.0/16 [1/0] via 192.168.1.2
O 10.3.0.0/16 [110/20] via 192.168.1.3, 00:05:12, GigabitEthernet0/0
B 10.4.0.0/16 [20/0] via 192.168.1.4, 01:10:33The letter is the source: C connected, L local address of the interface, S static, O OSPF, B BGP. The two numbers in brackets are [administrative distance/metric]. Administrative distance ranks how much the router trusts a source. The metric ranks routes within one source. A later chapter explains how the router uses both. For now, just recognize them.
The timer, such as 00:05:12, is how long ago the router learned the route. Connected and static entries have no timer because they do not expire.
Host routes and the local entry
A prefix length of /32 in IPv4 covers exactly one address. Routers use these entries for single hosts, including the router's own interface addresses. The L entry above is an example. It tells the router "this address belongs to me, so deliver the packet to my own software instead of forwarding it." Linux keeps similar entries in a separate table called local. You can see them with ip route show table local.
Asking the table a question
Reading the whole table tells you what rules exist. To see which rule applies to a specific address, ask the kernel:
ip route get 10.2.5.9The kernel answers with the route it would use, the next hop, and the source address. For example: 10.2.5.9 via 192.168.1.2 dev eth0 src 192.168.1.10. You can run this on any Linux host without sending a packet. The next chapter explains how the kernel picks one line when several could match.
Common reading mistakes
- Treating line order as priority. The order in which entries are printed is not the order in which the router tests them. Do not assume the top line wins.
- Confusing the next hop with the destination. The
viaaddress is the neighbor's address on a shared link. It is not where the packet is finally going. - Missing a connected route. If an expected connected entry is absent, the interface is probably down or has no address.
- Reading the prefix length loosely.
10.2.0.0/16and10.2.0.0/24are different entries. They cover different amounts of address space. - Ignoring the source. A route marked
staticstays after a link fails. A dynamic route is withdrawn. Knowing the source tells you which behavior to expect.
Exercise
A router prints the line 172.16.8.0/22 via 10.0.0.2 dev eth1 proto ospf metric 30. Which destinations does it cover, where do matching packets go, and how did the route get into the table?
Show answer
The prefix 172.16.8.0/22 covers 1024 addresses, from 172.16.8.0 to 172.16.11.255. Matching packets are handed to the neighbor router 10.0.0.2 through interface eth1. The route was learned dynamically from OSPF, so it will be removed if that neighbor stops advertising it. It was not entered by hand or created from a local interface address.