How OSPF Works: Link-State Routing and Dijkstra's Algorithm

Open Shortest Path First (OSPF) is a link-state interior gateway protocol (IGP) used to distribute routing information within a single autonomous system. Unlike BGP, which routes between autonomous systems using policy-driven path selection, OSPF computes shortest paths through a network by having every router build a complete topological map of the routing domain and then running Dijkstra's Shortest Path First (SPF) algorithm against that map. OSPF is defined in RFC 2328 (OSPFv2 for IPv4) and RFC 5340 (OSPFv3 for IPv6) and is one of the two dominant IGPs in production networks, alongside IS-IS.

OSPF matters to BGP operators because IGP reachability is a prerequisite for iBGP sessions. If the IGP cannot provide a path between two BGP speakers within an AS, their iBGP session will not come up, and external routes learned via eBGP will not propagate internally. OSPF also feeds next-hop resolution for BGP routes — the BGP next-hop attribute must be resolvable via the IGP for a BGP route to be installed in the forwarding table. In MPLS networks, OSPF extensions (RFC 3630) carry traffic engineering information used by RSVP-TE to compute constrained paths.

Link-State Fundamentals

OSPF is a link-state protocol, which means every router in the OSPF domain independently discovers its neighbors, measures the cost of each link, and floods this information to all other routers. Each router ends up with an identical copy of the Link-State Database (LSDB) — a complete graph of the network topology. The router then runs Dijkstra's SPF algorithm on this graph to compute a shortest-path tree rooted at itself, producing a routing table.

This contrasts with distance-vector protocols like RIP, where routers only share their routing tables with immediate neighbors and have no knowledge of the full topology. Link-state protocols converge faster, avoid count-to-infinity problems, and scale better because each router makes forwarding decisions based on a globally consistent view of the network.

OSPF Link-State Operation 1. Discover Neighbors Send Hello packets on all OSPF interfaces 224.0.0.5 (AllSPFRouters) Hello interval: 10s (broadcast) Dead interval: 40s 2. Flood LSAs Each router originates LSAs describing its links Reliable flooding via LS Update / LS Ack LSA refresh every 30 min 3. Compute SPF Tree Run Dijkstra's algorithm on the LSDB Produces shortest-path tree rooted at this router Install routes into RIB Every router has an identical LSDB but computes a unique SPF tree because each tree is rooted at a different router Convergence: topology change → LSA flood → SPF recalculation → RIB update Typical sub-second with SPF throttling (initial: 50ms, hold: 200ms, max: 5s)

OSPF Packet Types

OSPF uses IP protocol number 89 (not TCP or UDP) and defines five packet types. All OSPF packets share a common 24-byte header containing the version number, packet type, packet length, router ID of the sender, area ID, checksum, and authentication data.

Neighbor Discovery and Adjacency Formation

OSPF routers go through a well-defined state machine when forming adjacencies with their neighbors. Understanding these states is essential for troubleshooting OSPF problems:

  1. Down: No Hellos have been received from the neighbor. This is the initial state.
  2. Init: A Hello has been received from the neighbor, but the neighbor has not yet listed this router in its Hello packet's neighbor list. Communication is one-way.
  3. 2-Way: Bidirectional communication is established — each router sees its own router ID in the neighbor's Hello. On broadcast and NBMA networks, DR/BDR election occurs at this point. Not all neighbor pairs form full adjacencies on multi-access networks; only adjacencies with the DR and BDR proceed past this state.
  4. ExStart: The two routers negotiate master/slave roles and initial sequence numbers for the DBD exchange. The router with the higher router ID becomes the master.
  5. Exchange: Routers exchange DBD packets containing LSA headers. Each router compares received headers against its LSDB to identify missing or outdated LSAs.
  6. Loading: Routers send LSR packets to request missing LSAs and receive them in LSU packets. The LSDB synchronization process is in progress.
  7. Full: LSDBs are synchronized. The adjacency is fully operational, and the routers can participate in SPF computation together. This is the steady-state for healthy adjacencies.

On point-to-point links, both routers always form a full adjacency. On broadcast multi-access networks (like Ethernet), OSPF elects a DR and BDR to reduce the number of adjacencies from O(n²) to O(n). Each router on the segment forms an adjacency only with the DR and BDR, not with every other router.

Designated Router and Backup Designated Router

On broadcast and NBMA network segments, OSPF elects a Designated Router (DR) and a Backup Designated Router (BDR) to reduce flooding overhead and the number of adjacencies. Without a DR, a segment with N routers would require N(N-1)/2 adjacencies; with a DR, only N-1 adjacencies are needed (each non-DR/BDR router, called a DROther, forms adjacencies only with the DR and BDR).

The DR serves two purposes: it represents the multi-access network as a pseudonode in the LSDB (via a Type 2 Network LSA), and it is responsible for flooding LSAs to all routers on the segment. When a DROther router has a new LSA to flood, it sends it to the multicast address 224.0.0.6 (AllDRouters), and the DR then refloods it to 224.0.0.5 (AllSPFRouters) so that all other routers on the segment receive it.

The BDR exists for redundancy. If the DR fails, the BDR promotes to DR without requiring a new election among all routers (only a new BDR is elected). This minimizes convergence time after a DR failure.

DR Election Rules

DR/BDR election is based on router priority (configurable, 0-255, default 1) and router ID (tiebreaker). The rules are:

OSPF Areas

OSPF uses a hierarchical area structure to improve scalability. Every OSPF domain has a backbone area (Area 0), and all other areas must connect to Area 0 either directly or via virtual links. This two-level hierarchy limits LSA flooding and SPF computation to within each area, reducing CPU and memory consumption on routers.

OSPF Area Hierarchy Area 0 (Backbone) ABR-1 Core ABR-2 Area 1 (Regular) Full LSDB Inter-area: Type 3 LSA External: Type 5 LSA Router Area 2 (Stub) No Type 5 LSAs ABR injects default route for externals Router Area 3 (NSSA) No Type 5 LSAs Type 7 for local external routes (ASBR) Rtr ASBR Regular: all LSA types Stub: no external LSAs NSSA: local externals via Type 7

Area Types

Area Border Routers (ABRs)

An ABR is a router with interfaces in multiple OSPF areas, including Area 0. ABRs maintain separate LSDBs for each area they belong to and run independent SPF calculations per area. They generate Type 3 Summary LSAs to advertise inter-area routes: routes learned within one area are summarized and advertised into adjacent areas. This is where OSPF route summarization occurs — an ABR can aggregate multiple specific prefixes into a single summary before advertising it into another area, reducing LSDB size and improving stability.

LSA Types

OSPF uses different LSA types to describe different aspects of the network topology. Understanding LSA types is critical for troubleshooting and design:

Dijkstra's SPF Algorithm in OSPF

At the heart of OSPF is Dijkstra's SPF algorithm, which computes the shortest-path tree from the running router to every other router and network in the area. The algorithm works as follows:

  1. Initialize the SPF tree with only the root (this router). Place it on the candidate list (also called the OPEN set).
  2. Move the root to the shortest-path tree (CLOSED set). Examine all links described in the root's Type 1 Router LSA. For each neighbor, calculate the cumulative cost from the root. Add each neighbor to the candidate list with its computed cost.
  3. From the candidate list, select the vertex with the lowest cumulative cost. Move it to the shortest-path tree.
  4. For the newly added vertex, examine its LSA to discover its neighbors. For each neighbor not yet in the shortest-path tree, compute the cumulative cost via this vertex. If the neighbor is not in the candidate list, add it. If it is already in the candidate list with a higher cost, update its cost and parent pointer.
  5. Repeat steps 3-4 until the candidate list is empty.

The resulting tree gives the shortest path from this router to every other router and network prefix in the area. The next hop for each destination is determined by tracing back through the tree to find the first hop along each shortest path.

OSPF uses interface cost as the metric, typically calculated as the reference bandwidth divided by the interface bandwidth (the default reference bandwidth is 100 Mbps, so a 100 Mbps link has cost 1, a 10 Mbps link has cost 10, and a 1 Gbps link also has cost 1 unless the reference bandwidth is increased). Administrators should adjust the reference bandwidth to differentiate between modern high-speed links (e.g., setting it to 100 Gbps so that 10G links have cost 10 and 100G links have cost 1).

OSPF Route Types and Preference

OSPF routes have a strict preference order. When multiple routes to the same destination exist, OSPF selects based on route type (not just metric):

  1. Intra-area routes (O): Routes within the same area. Always preferred over inter-area routes, regardless of metric.
  2. Inter-area routes (O IA): Routes from other areas, learned via Type 3 Summary LSAs. Preferred over external routes.
  3. External Type 1 routes (O E1): External routes where the metric includes the internal OSPF cost to the ASBR plus the external metric.
  4. External Type 2 routes (O E2): External routes where only the external metric is considered (default type). If two E2 routes have equal external metric, the route with the lower internal cost to the ASBR is preferred.
  5. NSSA Type 1 routes (O N1): Like E1 but within an NSSA.
  6. NSSA Type 2 routes (O N2): Like E2 but within an NSSA.

This hierarchy means an intra-area route with a metric of 1000 is preferred over an inter-area route with a metric of 1. This design prevents suboptimal routing through external paths and ensures area boundaries are respected in path selection.

OSPF Authentication

OSPF supports three authentication modes to prevent unauthorized routers from injecting routes:

RFC 5709 introduced SHA-based HMAC authentication for OSPF, providing stronger cryptographic protection. In practice, many networks still use MD5 because the threat model for IGP authentication is typically limited to on-link attackers (someone with physical or Layer 2 access to the network segment), and MD5 is sufficient for this use case.

OSPF Equal-Cost Multi-Path (ECMP)

When Dijkstra's algorithm discovers multiple paths to the same destination with equal cost, OSPF can install all of them in the routing table for load balancing. This is called Equal-Cost Multi-Path (ECMP). Most router implementations support up to 4, 8, 16, or even 64 equal-cost next hops per destination.

ECMP traffic distribution is typically per-flow (based on a hash of source/destination IP, protocol, and port numbers) rather than per-packet, to avoid out-of-order delivery. ECMP is one of OSPF's significant advantages over simple static routing, as it automatically discovers and utilizes parallel paths.

OSPFv2 vs. OSPFv3

OSPFv2 (RFC 2328) was designed for IPv4 and carries IPv4 prefixes. OSPFv3 (RFC 5340) was originally designed for IPv6 but has been extended (RFC 5838) to support multiple address families, including IPv4. Key differences:

OSPF in Service Provider Networks

In large ISP networks, OSPF serves as the IGP that provides reachability between iBGP speakers and MPLS LSRs. The typical design pattern is:

Keeping the IGP lean (tens or hundreds of prefixes rather than thousands) is a best practice that ensures fast convergence. When the IGP carries too many routes, SPF computation time increases, and convergence after a link failure slows down.

OSPF Traffic Engineering Extensions

RFC 3630 defines OSPF-TE (Traffic Engineering) extensions that allow OSPF to carry link attributes needed for MPLS RSVP-TE path computation. These attributes are carried in Opaque LSAs (Type 10) and include:

This information allows the head-end router of an RSVP-TE tunnel to run a Constrained Shortest Path First (CSPF) computation that considers not just link metrics but also available bandwidth and administrative constraints. OSPF-TE is being gradually superseded by Segment Routing extensions, which provide similar traffic engineering capabilities without the per-tunnel state overhead of RSVP-TE.

OSPF Convergence and Tuning

OSPF convergence — the time from a topology change to the routing table being updated — involves several components:

With BFD for fast failure detection, aggressive SPF timers, and modern hardware, OSPF convergence can be achieved in under 100 milliseconds for typical topologies. However, in very large areas with thousands of prefixes, the SPF computation itself can take significant time, which is one reason for careful area design and keeping the IGP topology small.

OSPF Graceful Restart and Non-Stop Routing

RFC 3623 defines OSPF Graceful Restart (GR), which allows a router to restart its OSPF process (e.g., for a software upgrade) without causing its neighbors to treat it as failed. During a graceful restart, neighboring routers continue to forward traffic to the restarting router using existing routes, and the restarting router has a grace period to re-establish adjacencies and synchronize its LSDB without triggering network-wide SPF recalculations.

Non-Stop Routing (NSR) takes a different approach: the standby supervisor maintains a synchronized copy of the OSPF state, so when the active supervisor fails, the standby takes over without any protocol disruption visible to neighbors. NSR is platform-specific and does not require protocol-level support from neighbors, unlike GR which requires helper mode on neighboring routers.

Common OSPF Design Mistakes

Several design errors frequently cause OSPF operational problems:

OSPF and BGP Interaction

OSPF and BGP serve complementary roles. OSPF handles internal routing within an autonomous system, while BGP handles routing between autonomous systems. Their interaction follows a clear pattern:

Explore OSPF-Carried BGP Routes

OSPF provides the foundation that makes BGP work within an AS. To see real BGP routing data — the AS paths, prefixes, and origin ASes that BGP carries on top of the IGP underlay — try the god.ad BGP Looking Glass. Look up any IP address or ASN to see live BGP routes and understand how inter-domain routing builds on top of the intra-domain routing that OSPF provides.

See BGP routing data in real time

Open Looking Glass
More Articles
What is BGP? The Internet's Routing Protocol Explained
What is an Autonomous System (AS)?
What is a BGP Looking Glass?
How to Look Up an IP Address's BGP Route
Understanding BGP AS Paths
What is RPKI? Securing BGP Routing