Intermediate · Routing: static routes and OSPF · topic 31 of 57 · 30 min
How a router forwards: the routing table
Explanation
What a router really does
A router joins different IP networks. For every packet that arrives, it does the same three things: read the destination IP address, look for the best matching entry in its routing table, and send the packet out of the interface that entry names, towards the next hop. The next hop is the IP address of the next router on the way. The source address does not matter for normal forwarding.
The routing table is a list of networks the router knows about. Each entry says: this network, learnt from this source, reachable through this next hop or out of this interface. If no entry matches and there is no default route, the router drops the packet and sends back an ICMP "destination unreachable" message.
Where routes come from
| Code | Meaning | How it got into the table |
|---|---|---|
| C | Connected | An interface has an IP address and is up/up. The whole subnet is added. |
| L | Local | The router's own address on that interface, as a /32, so it knows which packets are for itself. |
| S | Static | An administrator typed an ip route command. |
| S* | Static default | A static route to 0.0.0.0/0. The star means candidate default. |
| O | OSPF | Learnt from OSPF neighbours (O IA means from another OSPF area). |
| D, R, B | EIGRP, RIP, BGP | Learnt from those routing protocols. |
Reading show ip route
R1# show ip route
Codes: L - local, C - connected, S - static, R - RIP, B - BGP
D - EIGRP, O - OSPF, IA - OSPF inter area, * - candidate default
Gateway of last resort is 203.0.113.1 to network 0.0.0.0
S* 0.0.0.0/0 [1/0] via 203.0.113.1
10.0.0.0/8 is variably subnetted, 5 subnets, 4 masks
C 10.1.12.0/30 is directly connected, GigabitEthernet0/1
L 10.1.12.1/32 is directly connected, GigabitEthernet0/1
O 10.2.0.0/16 [110/2] via 10.1.12.2, 00:05:12, GigabitEthernet0/1
S 10.2.5.0/24 [1/0] via 10.1.12.2
O 10.3.3.0/24 [110/3] via 10.1.12.2, 00:05:12, GigabitEthernet0/1Read each line from left to right. The code says where the route came from. Then the prefix (network and prefix length). The numbers in square brackets are [administrative distance/metric]. "via" gives the next hop. For learnt routes you also see how long ago the route was learnt and the exit interface. "Gateway of last resort" tells you the default route is set.
Rule 1: longest prefix match
A destination often matches several entries. The router always picks the entry with the longest prefix, meaning the most network bits, because it is the most specific. Take these routes: 0.0.0.0/0 to the ISP, 10.0.0.0/8 to R2, 10.1.0.0/16 to R3, 10.1.1.0/24 to R4.
| Destination | Routes that match | Winner |
|---|---|---|
| 10.1.1.20 | /0, /8, /16, /24 | 10.1.1.0/24 to R4 |
| 10.1.9.9 | /0, /8, /16 | 10.1.0.0/16 to R3 |
| 10.200.0.1 | /0, /8 | 10.0.0.0/8 to R2 |
| 8.8.8.8 | /0 only | Default route to the ISP |
To check a match yourself, write the destination and the network in binary and compare only the first N bits, where N is the prefix length. 10.1.9.9 and 10.1.0.0 share the first 16 bits (10.1), so the /16 matches. They differ inside the first 24 bits (9 versus 1 in the third octet), so 10.1.1.0/24 does not match.
Rule 2: administrative distance
Administrative distance (AD) is how much the router trusts a source of routing information. Lower is more trusted. AD is used only when two different sources offer the exact same prefix with the same length, for example a static route and an OSPF route both for 10.2.0.0/16. Only the winner goes into the routing table.
| Route source | Default AD |
|---|---|
| Connected interface | 0 |
| Static route | 1 |
| External BGP (eBGP) | 20 |
| EIGRP (internal) | 90 |
| OSPF | 110 |
| RIP | 120 |
| Internal BGP (iBGP) | 200 |
| Unknown, unusable | 255 (never installed) |
Rule 3: the metric
A metric is how one routing protocol measures the length of a path. OSPF uses cost (based on bandwidth), RIP uses hop count, EIGRP uses a formula based mainly on bandwidth and delay. Lower is better. Metrics are compared only between routes from the same protocol: an OSPF cost of 20 and a RIP hop count of 2 cannot be compared. If two paths have the same best metric, the router installs both and shares traffic between them (equal-cost load balancing).
The whole decision, in order
- Building the table: for each prefix, if several sources offer it, keep the lowest AD. Inside one protocol, keep the lowest metric.
- Forwarding a packet: among the installed entries that match the destination, use the longest prefix.
- No match and no default route: drop the packet.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
How a router forwards: the routing table · 1/8
In this lesson you will see how a router decides where to send every packet it receives.
Prefer a video by a teacher? Find free videos on “ccna routing table longest prefix match administrative distance explained” on YouTube ↗
In real life: A bank branch with two kinds of links
A branch of a bank in Pune has a private MPLS link to the data centre and a broadband line for internet. The branch router has 10.0.0.0/8 pointing to the MPLS router and a default route pointing to the broadband modem. A teller's payment request to the core banking server at 10.20.5.10 matches both routes, but the /8 is more specific, so it goes over the private link. A visit to a government website matches only the default route and leaves through broadband.
Branch# show ip route
S* 0.0.0.0/0 [1/0] via 192.168.100.1
S 10.0.0.0/8 [1/0] via 172.16.1.1Practice (0/3 done)
A router has S 10.0.0.0/8 [1/0] and O 10.5.5.0/24 [110/20]. A packet arrives for 10.5.5.9. Which route is used?
This lookup(routes, ip) function has a bug: it returns the next hop of the FIRST route that matches, not the most specific one. routes is a list of (prefix, next_hop) tuples in any order. Fix it so it uses longest prefix match, and returns None when nothing matches. Use the ipaddress module.
Several sources offer a route to the SAME prefix. Each candidate is a tuple (source, metric, next_hop). Write best_route(candidates) that returns the next hop of the route the router installs: the lowest administrative distance first (use the AD dictionary), and if the AD is equal, the lowest metric. The starter wrongly compares only metrics.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Static and default routesRouting: static routes and OSPF · same module
- OSPF concepts: link state, areas and neighboursRouting: static routes and OSPF · same module
- Configuring and verifying single-area OSPFRouting: static routes and OSPF · same module
- Routing: sending each request to the right codeBackend Development · APIs and request handling
- Network basics: IP addresses, routes, DNS and the ip commandLinux and Red Hat (RHCSA to RHCE) · Software and networking
- Access control lists (ACLs)Network security
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What is the default administrative distance of OSPF?
Answer: 110
OSPF is 110. EIGRP is 90, RIP is 120.
In "O 10.2.0.0/16 [110/2] via 10.1.12.2", what is the 2?
Answer: The OSPF cost (metric)
The brackets hold [administrative distance/metric], so 2 is the OSPF cost.
Which routes match destination 172.16.5.9? A) 172.16.0.0/16 B) 172.16.4.0/23 C) 172.16.5.0/24 D) 172.16.6.0/24. Which one is used?
Answer: C
A, B and C all match (172.16.4.0/23 covers 4.0 to 5.255). The /24 is the longest, so C wins. D does not match.
What does the code L mean in show ip route?
Answer: The router's own interface address as a /32
L (local) is the /32 host route of the router's own IP on that interface.
A route has administrative distance 255. What happens?
Answer: It is never installed in the routing table
AD 255 means the source is not trusted at all, so the route is never used.
What is the default administrative distance of a static route?Related
Answer: 1
Static routes have AD 1 unless you add a different number at the end of the command.
Which route handles GET /products?sort=price?Related
Answer: GET /products
The query string is not part of routing. The handler of GET /products reads it.
What is the default OSPF reference bandwidth on Cisco routers?Related
Answer: 100 Mbps
The default is 100 Mbps, which is why every link of 100 Mbps or faster costs 1.
Which command shares a router's static default route with its OSPF neighbours?Related
Answer: default-information originate
default-information originate, under router ospf, advertises the default route as O*E2.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 4 more topics in “Routing: static routes and OSPF” before its exam: Static and default routes, OSPF concepts: link state, areas and neighbours, Configuring and verifying single-area OSPF, First hop redundancy: HSRP, VRRP and GLBP.
- Module 7 exam: static routes, OSPF and HSRP · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, IP services: DHCP, DNS, NAT and more. 57 topics left in the course before the final exam.