Meet the neighbours
Every 10 seconds, each router sends a small Hello out of every link to say "I am here". R1 says hello to R2 and R3, the routers at the other end of its two links. OSPF stands for Open Shortest Path First.
R2 and R3 answer with Hellos that list who they hear, R1 included. Seeing its own name, R1 knows the link works both ways: they are neighbours. If no Hello arrives for 40 seconds, the neighbour is declared dead.
Build the map
Each link gets a cost: lower is better. By default, cost = 100 Mb/s ÷ link speed. A fast 100 Mb/s link costs 1; a slow 10 Mb/s link costs 10. A path's cost is the sum of its links.
Each router writes one piece of the map, a link-state advertisement (LSA). R1's piece says: I link to R2 at cost 1 and to R3 at cost 10. Every router writes its own piece the same way.
Before sharing pieces, two neighbours become adjacent. They swap a list of the pieces each holds, ask for any they are missing, and send them across. When both lists match, the neighbour state is Full.
R1 sends its piece to its neighbours. Each router passes a new piece on out of every other link, and confirms it got it. Soon R1's piece reaches all five routers, and so does everyone else's.
Now every router holds the same 5 pieces: the link-state database. Together they form a full map of routers, links and costs. No router just trusts a neighbour's directions; each works out routes from the map itself.
Shortest path first
R1 runs Dijkstra's algorithm on the map. It always takes the cheapest router not yet reached, then checks that router's links. The result is a tree of best paths from R1. The slow R1–R3 link is not in it.
R1 installs routes. To reach R5, it sends via R2: the path R1, R2, R3, R5 costs 3. The two-hop path through R3 costs 11. More hops, yet cheaper: OSPF counts cost, not hops.
When a link fails
The R2–R3 link fails. R2 and R3 notice at once if the port goes dark, or after 40 seconds without Hellos. Each writes a new piece without that link and floods it to every router.
With the new map, every router reruns Dijkstra. R1's best path to R5 is now through R3, cost 11, just ahead of the path through R2 and R4 at 12. When all maps agree and routes settle, the network has converged.
Large networks split routers into areas, so each map stays small. Every area connects to the backbone, area 0, through a border router that passes on summaries, not whole maps. Our five routers all sit in area 0.
What is happening
;; step 1/12 · hello ;; r1 → r2 packet Hello (OSPF type 1), IP protocol 89 from R1, Router ID 192.0.2.1 to 224.0.0.5 (all OSPF routers) area 0.0.0.0 every 10 s · dead after 40 s out of R1–R2 and R1–R3
OSPF version 2 (RFC 2328) routes IPv4; OSPFv3 (RFC 5340) does the same for IPv6. OSPF packets ride directly on IP as protocol 89. Hellos go to 224.0.0.5; HelloInterval 10 s and RouterDeadInterval 40 s are the RFC's sample values for a LAN and the usual defaults. Neighbours must agree on the area and on both timers. RFC 2328 leaves each interface's cost (1 to 65535) to the operator. Cost = 100 Mb/s ÷ bandwidth, at least 1, is the long-standing default in common router software. With that reference, 1 Gb/s and 10 Gb/s links also get cost 1, so operators often raise the reference bandwidth.