Dynamic routing: OSPF step by step
Lesson 5 of 6
5Dynamic routing: OSPF step by step
The big pictureWith three routers you type the routes by hand. With three hundred, and a link dropping or being added every day, typing by hand is impossible. This is where routers talk among themselves and draw the map, and redraw it, on their own.
What the picture shows: This is how two routers become full OSPF neighbours: they meet with Hello messages, swap a summary of what they know, then each asks for what it is missing until their maps match.
Map of this part
The concepts in order, click any one to jump to it:
Two ways into the same idea
Picture a group of delivery drivers in a city. Static routing is handing each driver a sheet of roads; if a street closes, nobody knows. OSPF is every driver calling their neighbours: "the street between us is open and fast". The news spreads until every driver holds the whole city map, then each one works out the shortest road alone. If a street closes, the news reaches everyone and they recalculate within seconds. "Shortest" here is not fewest streets but the fastest road: a wide street costs less.
OSPF is a link-state protocol inside one autonomous system (an IGP), defined in RFC 2328 for IPv4 (OSPFv2). Routers send Hello to the multicast address 224.0.0.5 every 10 seconds on Ethernet and declare a neighbour dead after 40 seconds (Dead). To become neighbours they must match on area, subnet and mask, Hello and Dead timers, authentication and MTU. Neighbours swap LSAs stored in an LSDB that is identical across the area, then each router runs SPF (Dijkstra) with cost = reference bandwidth ÷ interface bandwidth (Cisco's default reference is 100 Mbps, so every interface at 100 Mbps or faster costs 1; that is why you raise it with auto-cost reference-bandwidth). On shared segments a DR and BDR are elected by highest priority, then highest router ID, to cut the number of adjacencies. The design is hierarchical: every area connects to area 0 (the backbone). The administrative distance is 110.
Before You Start
🧭Routing and NAT
You know the routing table, longest match and administrative distance.
🧮Subnets
You read prefixes and wildcard masks.
The Core Idea
1Why static routes are not enough
A static route is simple and predictable, and fits a small network and the default route towards your provider. Its problem is that it does not see faults: if the link drops, the route stays in the table and packets are lost. And every new network means editing every router by hand. A dynamic protocol solves both: it discovers networks and routes around faults on its own.
| Static | OSPF | |
|---|---|---|
| Who writes it | The engineer, line by line | Routers learn it themselves |
| When a link drops | Stays and traffic is lost | Recalculated within seconds |
| Fits | Small networks and the default route | Medium and large enterprise networks |
| Load on the router | Almost none | Memory and CPU for the map |
2Neighbours and the shared map
Each router sends Hello on its interfaces. If another router answers with the same settings (area, subnet, timers, authentication) they become neighbours. Then they swap LSAs: messages saying "I am R1, I have a link to 10.1.1.0/24 at cost 1, and my neighbour is R2". Every LSA floods through the whole area, so every router ends up with an identical copy of the map, called the LSDB. The state you want between two neighbours is FULL.
3Dijkstra and cost: who wins?
Once a router holds the map, it runs Dijkstra to find the lowest total cost to every network. Cost comes from interface speed: reference ÷ speed. So a road across three fast links can beat a direct road over one slow link. Run the script and watch exactly that.
The direct R1 to R2 link at 10 Mbps costs 10000, so OSPF prefers going round through R3 and R4 at a cost of 120. Change the speeds and run it again.
import heapq
REFERENCE = 100_000 # Mbps: raised from Cisco's default 100 so fast links differ
def cost(mbps):
return max(1, REFERENCE // mbps)
# links: (router, router, speed in Mbps)
links = [("R1", "R2", 10), ("R1", "R3", 10_000), ("R3", "R4", 10_000), ("R4", "R2", 1_000)]
graph = {}
for a, b, speed in links:
graph.setdefault(a, []).append((b, cost(speed)))
graph.setdefault(b, []).append((a, cost(speed)))
def shortest(start):
best = {start: (0, [start])}
queue = [(0, start, [start])]
while queue:
total, node, path = heapq.heappop(queue)
if total > best[node][0]:
continue
for nxt, c in graph[node]:
if nxt not in best or total + c < best[nxt][0]:
best[nxt] = (total + c, path + [nxt])
heapq.heappush(queue, (total + c, nxt, path + [nxt]))
return best
for dest, (total, path) in sorted(shortest("R1").items()):
print(f"R1 -> {dest}: cost {total:<5} via {' > '.join(path)}")4Areas, the DR, and verification
In a large network the map grows and the calculation gets heavy, so it is split into areas, each connected to area 0. On a segment shared by many routers a DR (and a backup BDR) is elected, so everyone swaps maps with it instead of every router with every other. The winner: the highest priority (default 1, and zero means never stand), then the highest router ID.
passive-interface stops Hello messages towards the user network (safer and quieter) while still advertising it. FULL means a complete adjacency, and [110/120] is administrative distance/cost.
R1(config)# router ospf 1 R1(config-router)# router-id 1.1.1.1 R1(config-router)# auto-cost reference-bandwidth 100000 R1(config-router)# network 10.1.0.0 0.0.255.255 area 0 R1(config-router)# passive-interface GigabitEthernet0/2 R1# show ip ospf neighbor Neighbor ID Pri State Dead Time Address Interface 2.2.2.2 1 FULL/DR 00:00:34 10.1.12.2 Gi0/0 3.3.3.3 1 FULL/BDR 00:00:38 10.1.13.3 Gi0/1 R1# show ip route ospf O 10.2.0.0/24 [110/120] via 10.1.13.3, Gi0/1
Worked example
The task: after adding a new router R5, show ip ospf neighbor on R1 never shows R5, even though ping between them works. How do you investigate?
The answer: ping works, so layers 1 to 3 are fine and the problem is in the adjacency settings. Compare both ends with show ip ospf interface: (1) the same area? (2) the same mask? (3) matching Hello and Dead timers? (4) authentication? (5) is the interface passive by mistake? (6) a duplicate router ID? Here R5 was in area 1 and R1 in area 0 on the same link. After fixing it the state went FULL and O routes appeared in the table.
Myth vs Reality
| Myth | Reality |
|---|---|
| OSPF picks the path with the fewest hops. | It picks the lowest total cost, and cost comes from link speed. Counting hops is RIP's way. |
| If two routers can ping, they will become neighbours. | An adjacency also needs matching area, mask, timers and authentication. |
| The router with the highest IP address is always the DR. | Priority first, then router ID. And the election does not change if a higher router joins later. |
Where you meet it at work
🏢A company with branches
OSPF joins headquarters and branches and routes around a failed link by itself.
🎓The CCNA
Single-area OSPF is a core part of the IP Routing domain.
☁️Cloud and data centres
The same idea (link state and shortest path) runs inside data-centre networks.
Test your understanding
On what basis does OSPF choose a path?
Dijkstra over costs, and cost comes from link speed.
With Cisco's default reference, what does a 10 Mbps interface cost?
100 ÷ 10 = 10.
Which setting does NOT need to match between two OSPF neighbours?
The hostname plays no part; the area, timers and mask do.
Which state means two neighbours have identical maps?
FULL is the end of the exchange.
How is the DR elected?
And priority zero means it never stands.
Why use passive-interface on the user-facing interface?
There are no routers on the user network, and Hello messages there are noise and risk.
Quick review cards
Check Your Understanding
Why do we need a DR on an Ethernet segment with six routers?
Without it every router forms a full adjacency with every other one (15 adjacencies). With a DR and BDR everyone swaps only with them, cutting load and messages.
The link between R1 and R3 drops. What happens in OSPF?
Both ends notice (at once from the interface, or after the Dead timer), send a new LSA that floods the area, and every router reruns Dijkstra and updates its table.
When are static routes still better than OSPF?
In a small branch with one way out, and for the default route towards the provider: simple, predictable and free of overhead.
Resources for this lesson
Grouped by type. Start with the official docs for precision, or an article or video for the explanation.
Articles2
Done with this lesson?
Sign in to save your progress.