Course contents

Module 1: Understanding OSPFLesson 1.4 (4 of 4 in this module)4 of 29 in the OSPF course

How does OSPF work?

What has to happen before R1 can learn a route to a network it has never visited? Let's follow the OSPF messages, the information they carry and the route they produce.

Intermediate · 13 min read

What does R1 need to learn?

A user on LAN A wants to reach the server on LAN B, 192.168.4.0/24. R1 connects to LAN A, and R4 connects to LAN B. R2 and R3 provide two possible routes between them. R1 already knows its directly connected networks, but it needs information about the rest of the network before it can choose a path.

OSPF (Open Shortest Path First) exchanges that information between routers. A link-state advertisement (LSA) is a record describing links or reachable networks. The link-state database (LSDB) stores those records. Each router uses shortest path first (SPF), a lowest-cost path calculation, to turn its database into its own best paths.

Learning a route and forwarding a user's packet are separate jobs. OSPF prepares the information that forwarding uses; a user packet does not travel inside an OSPF update.

Follow the control traffic first, then the route it produces. The five stages below are a learning model: Hellos continue during database exchange, and SPF may run while the network is still converging.

From neighbours to installed routes

Inside R1: neighbours → map → routes
Inside R1: neighbours → map → routesA processing flow, not three network cables. Neighbours supply link-state information; SPF uses the LSDB to calculate paths. The routing table selects routes from OSPF and other sources. R1 has neighbours R2 and R3. The area-0 LSDB contains link information from R1 through R4. The selected route to 192.168.4.0/24 goes via R2 at cost 3. Inspect with show ip ospf neighbor, show ip ospf database and show ip route ospf.R1 neighboursR2 · R3show ip ospf neighborLSDB · area 0R1 linksR2 linksR3 linksR4 linksshow ip ospf databaseRouting table192.168.4.0/24via R2 · cost 3Next hop selectedshow ip route ospfSPF**SPF calculates OSPF paths; route selection decides what is installed.
A processing flow, not three network cables. Neighbours supply link-state information; SPF uses the LSDB to calculate paths. The routing table selects routes from OSPF and other sources.

Step 1: start the process

Use OSPFv2 for this IPv4 lab. OSPFv3 and its IPv6 examples are covered separately. The commands use Cisco IOS/IOS XE syntax; check interface names and feature support on your image. The four routers already have the shown addresses, operational links and compatible area 0 settings. This page explains the process, rather than supplying a complete initial configuration for every device.

From privileged EXEC, R1#, enter configure terminal to reach global configuration, R1(config)#. The router ospf 1 command then enters R1(config-router)#, where the network statements belong. Use end to return to privileged EXEC for show commands. Set router IDs before forming adjacencies in a new lab; changing an existing process identity can require a disruptive restart.

Process ID and router ID

router ospf 1 starts process 1. The number is local to the router (1 to 65535), so neighbours don't need to match, although most networks use the same number everywhere to keep things tidy. The process then needs a unique router ID, chosen as described in What is OSPF?; set it explicitly with router-id.

The network command picks the interfaces

Starting the process doesn't enable OSPF on any interface. You choose them, with the network command:

network <ip-address> <wildcard-mask> area <area-id>

Router configuration mode. Every interface whose IP address matches the address and wildcard joins OSPF in that area.

In a wildcard mask, a 0 bit must match and a 1 bit is ignored. The network command selects interfaces; it does not advertise the typed range as a summary. A selected, operational interface contributes its connected network; it sends Hellos unless passive. On R1:

router ospf 1 router-id 1.1.1.1 network 192.168.1.0 0.0.0.255 area 0 network 10.0.12.0 0.0.0.3 area 0 network 10.0.13.0 0.0.0.3 area 0 passive-interface GigabitEthernet0/0

R1, starting in global configuration mode. Gi0/0 (192.168.1.1), Gi0/1 (10.0.12.1) and Gi0/2 (10.0.13.1) each match one line and join area 0. Gi0/0 is passive: advertise the LAN without Hellos.

The network command picks the interfaces
The network command picks the interfacesnetwork command; Matches interfaces with addresses…. network 10.0.12.1 0.0.0.0 area 0: exactly 10.0.12.1 (one interface) network 10.0.12.0 0.0.0.3 area 0: 10.0.12.0 to 10.0.12.3 network 192.168.1.0 0.0.0.255 area 0: 192.168.1.0 to 192.168.1.255 network 10.0.0.0 0.255.255.255 area 0: anything starting with 10. network 0.0.0.0 255.255.255.255 area 0: every interfacenetwork commandMatches interfaces with addresses…network 10.0.12.1 0.0.0.0area 0exactly 10.0.12.1 (one interface)network 10.0.12.0 0.0.0.3area 010.0.12.0 to 10.0.12.3network 192.168.1.00.0.0.255 area 0192.168.1.0 to 192.168.1.255network 10.0.0.00.255.255.255 area 0anything starting with 10.network 0.0.0.0255.255.255.255 area 0every interface

On small screens, swipe sideways or focus the diagram and use the arrow keys.

Each item on the left, with what it means on the right.

The other way is to enable OSPF on the interface itself, which some engineers prefer because it leaves no doubt about which interfaces are in:

interface GigabitEthernet0/1 ip ospf 1 area 0

Alternative on R1, starting in global configuration mode. The first line enters interface configuration; the second enables process 1 in area 0 on Gi0/1. You do not need both activation methods on the same interface.

Compare selected, passive and excluded interfaces
Enable the LAN; keep Hellos on router links
Enable the LAN; keep Hellos on router linksSolid grey lines are Ethernet connections. Dashed violet arrows represent OSPF control traffic on the R1–R2 link. R1’s Gi0/0 is enabled for OSPF but passive: its LAN is advertised without sending Hellos to user devices. Access switching is shown explicitly. PC-A, user address 192.168.1.10, connects through access switch SW1 to R1 Gi0/0 on subnet 192.168.1.0/24. R1 Gi0/1 connects to R2 on subnet 10.0.12.0/30. Gi0/0 advertises the LAN without Hellos; Gi0/1 advertises the link and exchanges Hellos.PC-A · userSW1 · switchR1 · routerR2 · router192.168.1.10Gi0/0Gi0/110.0.12.0/30User LAN · 192.168.1.0/24Router-to-router linkGi0/0 · enabled + passiveAdvertise LAN ✓Send Hellos ✗Gi0/1 · enabled + activeAdvertise link ✓Exchange Hellos ✓
Solid grey lines are Ethernet connections. Dashed violet arrows represent OSPF control traffic on the R1–R2 link. R1’s Gi0/0 is enabled for OSPF but passive: its LAN is advertised without sending Hellos to user devices. Access switching is shown explicitly.

Passive does not mean unadvertised. A passive OSPF interface contributes its network but forms no neighbours. An interface outside OSPF needs another route source if its network is to be learned.

Step 2: find neighbours

A Hello is an OSPF packet used to discover another router and check whether it is still reachable. A neighbour is another OSPF router heard on a directly attached link. An adjacency is the closer relationship used to synchronise databases. We need both routers to hear each other before that exchange can succeed.

Enabled, non-passive broadcast and point-to-point interfaces normally send Hellos every 10 seconds to 224.0.0.5 on Cisco IOS. Two routers on the same link confirm two-way communication once each sees its own router ID listed in the other's Hello, with compatible area, addressing and timer settings. They then synchronise databases if an adjacency is required. On a point-to-point link it is; on broadcast Ethernet the designated-router roles determine which pairs synchronise. Here is a simplified exchange between R1 and R2:

Before opening the exchange, keep four packet names in mind. Database description (DBD) packets list LSA headers, identifying records and their versions without carrying the full records. A link-state request (LSR) asks for a record that is missing locally or available in a newer version. A link-state update (LSU) carries the full records. A link-state acknowledgement (LSAck) acknowledges LSAs, not DBD packets.

Follow the complete R1–R2 packet exchange
R1RID 1.1.1.1
R2RID 2.2.2.2
  1. DownNothing heard yet. Both start sending hellos to 224.0.0.5 every 10 seconds.
  2. I'm 1.1.1.1, area 0, timers 10/40. I haven't heard any neighbours yet.Hello from R1 to R2.Hello
  3. InitR2 received R1's Hello, but that Hello did not list R2's own RID. This is R2's local view.
  4. HelloI'm 2.2.2.2, same area and timers, and I can hear 1.1.1.1.Hello from R2 to R1.
  5. I can hear 2.2.2.2 too.Hello from R1 to R2.Hello
  6. 2-WayEach router sees its own ID in the other's hello: they are neighbours.
  7. Initial DBD: propose a sequence number and exchange role.DBD from R1 to R2.DBD
  8. DBDMy RID is higher: I become DBD master for this exchange.DBD from R2 to R1.
  9. ExStart → ExchangeR2 controls DBD sequence numbers, not network-wide routing. DBDs list LSA headers.
  10. DBDHere is the list of LSAs I have.DBD from R2 to R1.
  11. And here is mine.DBD from R1 to R2.DBD
  12. LoadingRequest lists still need LSAs that are missing locally or newer at the peer. With an empty list, Exchange can finish directly at Full.
  13. Please send the full LSAs from 3.3.3.3 and 4.4.4.4.LSR from R1 to R2.LSR
  14. LSUHere they are.LSU from R2 to R1.
  15. Got them, thanks.LSAck from R1 to R2.LSAck
  16. FullDatabase synchronisation complete. Hellos continue; LSUs carry changes, refreshes and retransmitted LSAs.

Device key: R1, R2 (router)

The top line is the shared network link; port labels name its interfaces. Read message rows from top to bottom and follow each arrow from sender to receiver. Rows show protocol order, not additional cables or timing to scale.

Reading the addresses

IP labels identify interfaces or destinations. Subnet labels identify networks; /n is the prefix length. A router ID (RID) identifies a routing process and need not be a reachable interface.

An IP address alone is not a URL. A web URL includes a scheme such as https://; an address on a router or printer does not imply a website.

  • R1: RID 1.1.1.1 · 10.0.12.1
  • R2: RID 2.2.2.2 · 10.0.12.2
R1 and R2 on 10.0.12.0/30, from the first hello to a Full adjacency. Each envelope is drawn at the router that receives it.

Full means database synchronisation is complete. On broadcast Ethernet, OSPF elects a designated router (DR) and backup designated router (BDR) to organise database exchanges. The other routers are called DROthers; two DROthers can correctly remain at 2-Way. An MTU mismatch is one possible cause of ExStart or Exchange problems. The neighbours lesson covers each state and the troubleshooting checks; the DR/BDR lesson explains which neighbours form Full adjacencies.

The five OSPF packet types

OSPF runs directly on IP (protocol number 89), not on TCP or UDP, and uses five packet types:

The five OSPF packet types
The five OSPF packet typesType; Packet; Job; Used in step. 1: Hello Find neighbours, check settings match, keep neighbours alive 2 2: DBD (database description) List LSA headers, without the full records 2–3 3: LSR (link-state request) Ask for specific missing or newer LSAs 3 4: LSU (link-state update) Carry full LSAs, both on request and when flooding a change 3 5: LSAck (link-state acknowledgement) Explicitly acknowledge LSAs received 3TypePacketJobUsed in step1HelloFind neighbours, checksettings match, keepneighbours alive22DBD (databasedescription)List LSA headers, withoutthe full records2–33LSR (link-state request)Ask for specific missingor newer LSAs34LSU (link-state update)Carry full LSAs, both onrequest and when floodinga change35LSAck (link-stateacknowledgement)Explicitly acknowledgeLSAs received3

On small screens, swipe sideways or focus the diagram and use the arrow keys.

Read each row across: the left box names the item, the others describe it.

Step 3: share the map (the LSDB)

Comparing headers avoids copying records the neighbour already has in the same version. One LSU can carry several LSAs; an LSA is a record inside the packet, not a sixth packet type. Acknowledgements make this exchange reliable. An LSAck can acknowledge several LSAs at once, and receiving the same LSA instance back from a neighbour can also provide an implicit acknowledgement of a previously sent record.

Database synchronisation happens before a neighbour reaches Full. Routers compare LSA headers, request missing or newer records and acknowledge received LSAs. After convergence, the area-scoped topology records agree. Later updates are reliably flooded through the appropriate adjacencies. Inspect the router LSA section with show ip ospf database:

Example output
R1#show ip ospf database
            OSPF Router with ID (1.1.1.1) (Process ID 1)

                Router Link States (Area 0)

Link ID         ADV Router      Age         Seq#       Checksum Link count
1.1.1.1         1.1.1.1         412         0x80000004 0x00A1C3 3
2.2.2.2         2.2.2.2         409         0x80000003 0x0062F1 2
3.3.3.3         3.3.3.3         405         0x80000003 0x00B804 2
4.4.4.4         4.4.4.4         398         0x80000004 0x0024D9 3
Four routers, four router LSAs. Seq# rises each time a router originates a new version of its LSA; retransmitting the same version does not raise it. The output is trimmed: broadcast Ethernet segments also have network LSAs from DRs with a Full adjacency. Point-to-point OSPF links do not elect a DR.

Under normal OSPFv2 operation, each router refreshes its own LSAs at 30 minutes even without a topology change. MaxAge is one hour: records at that age are excluded from route calculation and flushed. These are LSA maintenance timers, not failure-detection timers.

Step 4: run SPF

Shortest path first (SPF) is the calculation that turns the stored topology into paths. Each router starts from its own position and adds the cost of outgoing interfaces along each possible path. A smaller total is preferred for these paths within the same area. Cost is a configured routing value, not a live measurement of delay or congestion.

See how stored link records become paths
Same topology records, different route calculations
Same topology records, different route calculationsAfter convergence in this single area, R1 and R3 hold the same area topology records. Each runs SPF from itself. The paths below go to LAN B, 192.168.4.0/24; they include R4’s LAN interface cost of 1. Both databases contain area-0 records from R1 through R4. R1 selects R1 to R2 to R4, cost 1 plus 1 plus 1 equals 3. R3 selects R3 to R4, cost 1 plus 1 equals 2.R1 · SPF rootLSDB: R1, R2, R3, R4Area-0 topology recordsR1 → R2 → R4Cost 1 + 1 + 1 = 3R3 · SPF rootLSDB: R1, R2, R3, R4Area-0 topology recordsR3 → R4Cost 1 + 1 = 2=
After convergence in this single area, R1 and R3 hold the same area topology records. Each runs SPF from itself. The paths below go to LAN B, 192.168.4.0/24; they include R4’s LAN interface cost of 1.

Assume cost 10 in both directions on R1–R3, and cost 1 on every other outgoing interface, including the LAN interfaces. From R3 to LAN A, the path R3 → R4 → R2 → R1 costs 4; its direct high-cost path costs 11.

The result for R1 is its SPF tree, summarised here:

DestinationBest path from R1Total costNext hop
10.0.24.0/30R1 → R2210.0.12.2 (R2)
10.0.34.0/30R1 → R2 → R4310.0.12.2 (R2)
192.168.4.0/24R1 → R2 → R4310.0.12.2 (R2)

10.0.34.0/30 (the R3–R4 link) is reached through R4 at cost 3 rather than through R3 at cost 11, because the R1–R3 link is expensive.

Step 5: fill the routing table

OSPF offers its best paths to the routing table. If selected over competing route sources, these intra-area routes appear as O, with the default administrative distance 110 and the total cost as the metric. From now on the router forwards packets using these routes; OSPF itself isn't involved in forwarding each packet.

An LSA in the database does not guarantee an installed OSPF route. It must describe usable reachability in the OSPF calculation. For the same destination prefix, a competing source with a lower administrative distance can win the routing-table selection. For example, a normal static route with distance 1 can hide the OSPF route. Forwarding then chooses the longest matching prefix among installed routes; it does not compare every route's OSPF cost.

Example output
R1#show ip route ospf
      10.0.0.0/8 is variably subnetted, 6 subnets, 2 masks
O        10.0.24.0/30 [110/2] via 10.0.12.2, 00:05:41, GigabitEthernet0/1
O        10.0.34.0/30 [110/3] via 10.0.12.2, 00:05:41, GigabitEthernet0/1
O     192.168.4.0/24 [110/3] via 10.0.12.2, 00:05:41, GigabitEthernet0/1
Exactly the SPF table above. 10.0.12.0/30, 10.0.13.0/30 and LAN A are connected routes on R1, so they don't appear as OSPF routes.

What happens when the R2–R4 link fails

The five steps aren't a one-off. When the R2–R4 link fails:

  1. When the interfaces report link-down, R2 and R4 detect the failure without waiting for the dead timer. With a silent failure, detection may wait for the configured dead interval (normally 40 seconds on these interfaces), unless another mechanism detects it sooner.
  2. Each sends a new version of its router LSA, without that link, in an LSU to its remaining neighbours (step 3).
  3. The changed topology records spread through the appropriate adjacencies in this single area; routers install and acknowledge the newer LSAs.
  4. The affected topology calculations use the updated map (step 4). This link failure changes the area's topology; not every kind of OSPF update requires a full SPF rebuild.
  5. R1 replaces its routes through R2 with routes through R3, at the higher cost (step 5).
OSPF recalculates and uses the surviving path
OSPF recalculates and uses the surviving pathAfter failure detection, LSA flooding and SPF calculation, R1 installs a path through R3. Both directions still need valid routes and policies. PC-A at 192.168.1.10 connects through access switch SW1 to router R1 on LAN A, 192.168.1.0/24. The web server at 192.168.4.10 connects through access switch SW2 to router R4 on LAN B, 192.168.4.0/24. R1 can route through R2 or R3 to R4. Solid grey lines are physical connections; switches forward within each LAN. The R2 to R4 link has failed.cost 1cost 10cost 1Link downR1R2R3R4SW1 · switchSW2 · switchPC-A · userWeb server192.168.1.10192.168.4.10LAN ALAN B192.168.1.0/24192.168.4.0/24The surviving data path goes through R3
New OSPF route on R1
Destination subnet
192.168.4.0/24
Next hop · router interface IP
10.0.13.2 · R3
Forwarding path
R1 → R3 → R4
OSPF cost
12
Diagram details · example interface map
Failed connection · interface IPv4
R2 Gi0/1: 10.0.24.1/30 ↔ R4 Gi0/1: 10.0.24.2/30. The X marks this failed link.
R1’s possible next hops
Gi0/1: 10.0.12.1/30 → R2 at 10.0.12.2; Gi0/2: 10.0.13.1/30 → R3 at 10.0.13.2. RIDs in the OSPF views: R1 1.1.1.1, R2 2.2.2.2, R3 3.3.3.3, R4 4.4.4.4.
LAN gateways
PC-A uses R1 Gi0/0 at 192.168.1.1; the server uses R4 Gi0/0 at 192.168.4.1. Both endpoint masks are /24 (255.255.255.0). Transit links are /30 (255.255.255.252).
Routing example
OSPF area 0; link costs are configured metrics, not measured speed. Dashed arrows carry link-state information; a solid coloured path carries the user’s request.
After failure detection, LSA flooding and SPF calculation, R1 installs a path through R3. Both directions still need valid routes and policies.

Timing matters: convergence includes detection, flooding, SPF scheduling and forwarding-table updates. There is no guaranteed recovery time from topology size alone.

Short lab: compare the route before and after failure

Use the four-router topology shown above, with cost 10 on R1–R3 and cost 1 on the other interfaces. The command outputs on this page are written examples, not tested router captures. Assume no competing preferred route for LAN B, operational host gateways and a return route. On R1, record the next hop and metric for LAN B before changing anything.

show ip ospf neighbor show ip ospf database router show ip route 192.168.4.0

R1, privileged EXEC. Compare directly attached neighbours, stored router records and the selected route. The initial LAN B next hop is 10.0.12.2 at cost 3.

In your isolated lab, shut the R2 interface towards R4. Run the same checks on R1 after convergence. R2 can remain R1's Full neighbour because the failed link is farther away. The LAN B route should instead use R3, 10.0.13.2, at cost 12. Restore the interface with no shutdown and confirm the original route returns.

Next, configure and verify single-area OSPF. That complete configuration lesson uses a separate three-router chain, not this four-router backup-path topology. Check enabled interfaces, neighbours, the LSDB and installed routes in that order.

Test yourself on the five steps

Predict · scenario 1

R1 has interfaces 10.1.5.1, 10.1.200.1 and 10.2.0.1. Which of them does network 10.1.0.0 0.0.255.255 area 0 enable?

Predict · scenario 2

During the database exchange, R1 sees an LSA listed in R2's DBD that it doesn't have. What does R1 send next?

Predict · scenario 3

R4 has no network statement or interface-level OSPF command enabling its LAN interface. With no redistribution or alternate route, what happens?

Predict · scenario 4

All routers in the area have the same LSDB. Which step makes R1 and R3 install different next hops for LAN B?

Predict · scenario 5

The R2–R4 link fails. Put R1's experience in the right order.

Further reading: Cisco: interpreting OSPF interface output and RFC 2328: OSPFv2.