Skip to content

Intermediate · Routing: static routes and OSPF · topic 31 of 57 · 30 min

How a router forwards: the routing table

Explanation

A router reads the destination IP address of each packet and looks it up in its routing table. If several routes match, the most specific one (the longest prefix) wins. When two sources offer the same prefix, the router trusts the one with the lower administrative distance, and inside one source it picks the lower metric.

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.

Think of it like this: Think of a post office sorting room. One bag is for PIN codes starting with 4000, one for PIN codes starting with 40, and one big bag marked "rest of India". A letter for 400001 matches both the 4000 bag and the 40 bag, but the sorter uses the 4000 bag because it is more specific. A letter for 110001 matches only "rest of India". A router sorts packets in exactly this way.

Where routes come from

CodeMeaningHow it got into the table
CConnectedAn interface has an IP address and is up/up. The whole subnet is added.
LLocalThe router's own address on that interface, as a /32, so it knows which packets are for itself.
SStaticAn administrator typed an ip route command.
S*Static defaultA static route to 0.0.0.0/0. The star means candidate default.
OOSPFLearnt from OSPF neighbours (O IA means from another OSPF area).
D, R, BEIGRP, RIP, BGPLearnt from those routing protocols.

Reading show ip route

output
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/1
Cisco IOS: a shortened routing table. [110/2] means administrative distance 110, metric 2.

Read 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.

DestinationRoutes that matchWinner
10.1.1.20/0, /8, /16, /2410.1.1.0/24 to R4
10.1.9.9/0, /8, /1610.1.0.0/16 to R3
10.200.0.1/0, /810.0.0.0/8 to R2
8.8.8.8/0 onlyDefault 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 sourceDefault AD
Connected interface0
Static route1
External BGP (eBGP)20
EIGRP (internal)90
OSPF110
RIP120
Internal BGP (iBGP)200
Unknown, unusable255 (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

  1. Building the table: for each prefix, if several sources offer it, keep the lowest AD. Inside one protocol, keep the lowest metric.
  2. Forwarding a packet: among the installed entries that match the destination, use the longest prefix.
  3. No match and no default route: drop the packet.
Careful: Common mistakes. (1) Thinking a lower AD beats a longer prefix. It never does: a /24 learnt by OSPF (AD 110) beats a /16 static route (AD 1) for addresses inside that /24, because longest match is checked first. (2) Comparing metrics of different protocols. (3) Forgetting that L routes are /32 host routes for the router itself, not real networks. (4) Expecting [1/0] to mean "one hop": it means AD 1, metric 0.
Tip: When a route is missing, ask "which source should have given it?" Then check that source: the interface status for C, the ip route line for S, the neighbours for O.

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

How a router forwards

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.

output
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.1
Cisco IOS: the /8 wins for bank traffic, the default route takes everything else.

Practice (0/3 done)

Task 1 · Question

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?

Task 2 · Code · Python

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.

Task 3 · Code · Python

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

Question 1

1.What is the default administrative distance of OSPF?

Question 2

2.In "O 10.2.0.0/16 [110/2] via 10.1.12.2", what is the 2?

Question 3

3.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?

Question 4

4.What does the code L mean in show ip route?

Question 5

5.A route has administrative distance 255. What happens?

Read more (free)

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.

From Static and default routes

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.

From Routing: sending each request to the right code

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.

From OSPF concepts: link state, areas and neighbours

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.

From Configuring and verifying single-area OSPF

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

  1. 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.
  2. Module 7 exam: static routes, OSPF and HSRP · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, IP services: DHCP, DNS, NAT and more. 57 topics left in the course before the final exam.

← Previous: EtherChannel: bundling links