Sub topic 9
Link-State Protocols and OSPF
This chapter assumes you know how a routing table and longest-prefix match work, what administrative distance and metrics are, and how to read basic Cisco IOS configuration. It covers how link-state protocols build routes, then how OSPF (Open Shortest Path First) implements that model, including its common failure modes.
The link-state idea
A distance-vector router shares its routing table with neighbors, so it only knows what its neighbors tell it. A link-state router shares facts about its own links instead. Each router describes who it is connected to and what each link costs. Every router in the area receives all of these descriptions and builds the same map of the network. Each router then calculates its own best paths from that map.
The process has four steps:
- Each router discovers its neighbors by sending small hello packets.
- Each router advertises its links in a link-state advertisement (LSA).
- LSAs are flooded: every router forwards new LSAs to its neighbors, so all routers in the area receive them.
- Each router stores the LSAs in its link-state database (LSDB) and runs the shortest path first (SPF) algorithm, also called Dijkstra's algorithm, with itself as the root.
Because every router in an area holds an identical LSDB, routing loops are rare. Convergence is fast. A router that detects a failure floods one small update, and each router reruns SPF locally. The cost is memory and CPU, since every router stores the whole map and recomputes it on every change.
What SPF computes
SPF produces a tree of lowest-cost paths from the local router to every other router. The routing table keeps only the first hop of each path. The following script runs the same calculation on a four-router topology.
import heapq
def spf(graph, root):
"""Dijkstra with first-hop tracking, as an OSPF router would do it."""
dist = {}
first_hop = {}
done = set()
heap = [(0, root, None)]
while heap:
cost, node, hop = heapq.heappop(heap)
if node in done:
continue
done.add(node)
dist[node] = cost
if hop is not None:
first_hop[node] = hop
for neighbor, link_cost in graph[node].items():
if neighbor not in done:
next_hop = neighbor if node == root else hop
heapq.heappush(heap, (cost + link_cost, neighbor, next_hop))
return dist, first_hop
lsdb = {
"R1": {"R2": 10, "R3": 1},
"R2": {"R1": 10, "R4": 1},
"R3": {"R1": 1, "R4": 20},
"R4": {"R2": 1, "R3": 20},
}
dist, hops = spf(lsdb, "R1")
for router in sorted(dist):
print(router, "cost", dist[router], "via", hops.get(router, "-"))The output is:
R1 cost 0 via -
R2 cost 10 via R2
R3 cost 1 via R3
R4 cost 11 via R2R4 is reached through R2 at cost 11, not through R3 at cost 21. The low-cost link between R2 and R4 outweighs the extra hop. Every router in the area runs this same computation on the same data, with itself as the root.
OSPF basics
OSPF is the most widely used link-state IGP in enterprise networks. OSPFv2 carries IPv4 and OSPFv3 carries IPv6. The properties that matter in daily work are:
- OSPF runs directly on IP as protocol number 89. It does not use TCP or UDP.
- Routers send hellos to the multicast address 224.0.0.5 (all OSPF routers). On broadcast networks, DR and BDR routers also listen on 224.0.0.6.
- The default administrative distance is 110.
- The metric is cost, derived from interface bandwidth.
- Each router has a 32-bit router ID, written like an IPv4 address. It identifies the router in the LSDB.
Cost and the reference bandwidth
OSPF cost is the reference bandwidth divided by the interface bandwidth. On Cisco IOS the reference is 100 Mbps by default. A 10 Mbps link costs 10. FastEthernet, Gigabit and 10 Gigabit links all cost 1, so OSPF cannot tell them apart. Raise the reference bandwidth on every router in the domain so costs stay consistent.
Neighbor states
Two routers must become fully adjacent before they exchange routing information. The neighbor relationship moves through these states.
In the Exchange state, routers swap summaries of their databases. In Loading, they request the LSAs they are missing. Neighbors that stay in 2-Way are normal on broadcast networks, as the next section explains.
Designated routers on shared segments
On an Ethernet segment with many routers, a full mesh of adjacencies would create a lot of flooding. OSPF elects a designated router (DR) and a backup designated router (BDR). The other routers form a full adjacency only with the DR and BDR. They stay in 2-Way with each other, which is expected.
The router with the highest interface priority wins the election, and the highest router ID breaks ties. The election is not preemptive. A new router with a higher priority does not displace the current DR. On point-to-point links there is no election, so you can set ip ospf network point-to-point on router-to-router Ethernet links to skip it and speed up adjacency.
Areas and scaling
A large LSDB makes SPF slow and floods wide. OSPF limits this by dividing the network into areas. Routers inside an area share full LSDB detail. Between areas, only summarized reachability crosses the boundary.
- Area 0 is the backbone. All other areas must connect to it.
- An area border router (ABR) has interfaces in more than one area and passes route information between them.
- An autonomous system boundary router (ASBR) injects routes from outside OSPF, such as static routes or routes from another protocol.
A topology change in one area triggers SPF only inside that area. A flapping link in a branch area does not make the whole domain recalculate. For small networks, a single area 0 is fine and simpler to operate.
Configuring OSPF
This configuration enables OSPF on a router with interfaces in 10.1.0.0/16 and keeps it quiet on a user-facing interface.
router ospf 1
router-id 1.1.1.1
auto-cost reference-bandwidth 10000
passive-interface GigabitEthernet0/2
network 10.1.0.0 0.0.255.255 area 0The details:
router-idfixes the identity. Without it, IOS picks the highest loopback address, then the highest active interface address. Setting it explicitly avoids surprises when interfaces change.auto-cost reference-bandwidth 10000sets the reference to 10 Gbps. Configure the same value on every router.passive-interfacestops hellos on an interface but still advertises its network. Use it on links toward users, where no neighbor should form.- The
networkstatement uses a wildcard mask, the inverse of a subnet mask. It selects the interfaces whose addresses fall in that range and places them in the given area. - The process number (1) is local to the router and does not have to match on neighbors.
OSPF routes appear in the routing table with code O. A route to another area appears as O IA. If a static route to the same prefix exists, the static route has administrative distance 1 and wins. A static route configured with distance 200 only takes over when the OSPF route disappears, as in the floating static route covered earlier.
Verifying and troubleshooting
These commands answer most OSPF questions:
show ip ospf neighbor
show ip ospf interface brief
show ip ospf database
show ip route ospfshow ip ospf neighbor lists each neighbor with its state, such as FULL/DR, FULL/BDR or 2WAY/DROTHER. An empty list or a state stuck below Full tells you where to look.
Common causes of failure
For two routers to become neighbors, several parameters must match on the shared link.
| Symptom | Likely cause |
|---|---|
| No neighbor appears | Mismatched hello or dead timers, different area IDs, different subnet or mask, or authentication mismatch |
| Neighbor stuck in ExStart or Exchange | MTU mismatch between the two interfaces |
| Neighbor stuck in Init | Hellos arrive in one direction only, often because of an ACL or a one-way link problem |
| Neighbors stay in 2-Way | Normal between two non-DR routers on a broadcast segment |
| Routes missing despite Full adjacency | Network statement does not cover the interface, or the interface is passive |
| Unstable adjacency | Duplicate router IDs, which cause LSAs to overwrite each other |
Default timers are a 10-second hello and a 40-second dead interval on broadcast and point-to-point links. The dead interval is the time a router waits without hearing a hello before it declares the neighbor down. That wait sets the failure detection time unless a faster mechanism, such as a link-down event or BFD, signals the loss sooner.
Link-state versus distance-vector in practice
| Property | Link-state (OSPF) | Distance-vector (RIP) |
|---|---|---|
| What routers share | Their own links, flooded to the whole area | Their routing table, sent to neighbors |
| View of the network | Full map in every router | Only neighbors' advertisements |
| Convergence | Fast, driven by flooding and local SPF | Slower, depends on update intervals |
| Resource use | More memory and CPU | Light |
| Scaling tool | Areas | Hop limits |
OSPF suits medium and large enterprise networks that need fast, loop-free convergence and want control over how the network is divided. It adds design work: area planning, summarization, and consistent configuration across routers.
Exercise
Two routers on the same Ethernet link show their OSPF neighbor state stuck in ExStart. Hello timers, area IDs and masks match. Name the most likely cause and the command that helps you confirm it.
Show answer
The most likely cause is an MTU mismatch between the two interfaces. The routers cannot complete the exchange of database descriptions when the MTU values differ. Compare the MTU on both sides, for example with show ip ospf interface and show interfaces, then make the values match.
Exercise
A router has an OSPF route to 10.2.0.0/16 and a static route to the same prefix with default administrative distance. Which one enters the routing table, and what must you configure so the static route acts only as a backup?
Show answer
The static route enters the routing table because its administrative distance is 1, lower than OSPF's 110. To make it a backup, give it a higher administrative distance than OSPF, such as 200: ip route 10.2.0.0 255.255.0.0 192.168.2.2 200. It then stays out of the table until the OSPF route is lost.
Exercise
In the Python example, the link between R2 and R4 changes from cost 1 to cost 15. Which first hop does R1 use to reach R4 afterwards, and why?
Show answer
R1 uses R3. The path through R2 now costs 10 + 15 = 25, and the path through R3 costs 1 + 20 = 21. SPF picks the lower total, so the first hop for R4 becomes R3.