Skip to main content
Version: 7.26

Neighbor Relationship

OSPF is a link-state protocol that considers the router interface an OSPF link. Whenever OSPF is started, it adds the state of all the links in the local link-state database.

Several steps must complete before the OSPF network becomes fully functional:

  • Neighbor discovery
  • Database synchronization
  • Routing calculation

Link-state routing protocols distribute and replicate a database describing the routing topology. The link-state protocol's flooding algorithm ensures that each router has an identical link-state database. The routing table is calculated based on this database.

After all the previous steps are completed, the link-state database on each neighbor contains the full routing domain topology (how many other routers are in the network, how many interfaces routers have, what networks the link between routers connects, the cost of each link, and so on).

Interfaces in OSPF​

An interface template decides on which interfaces OSPF sends Hello packets and discovers neighbors. A template matches interfaces by name or by network. Either of the following templates puts the same link into area 1 (an area named area1), the first by interface name and the second by the network of an address on the interface:

/routing/ospf/interface-template
add area=area1 interfaces=ether4
/routing/ospf/interface-template
add area=area1 networks=10.0.12.0/30

OSPF has a complete configuration of OSPFv2 and OSPFv3 on three routers.

Communication between OSPF routers​

OSPF operates over the IP network layer with protocol number 89. A destination IP address is set to the neighbor's IP address or to one of the OSPF multicast addresses, AllSPFRouters (224.0.0.5) or AllDRouters (224.0.0.6). These addresses are described later in this article. Every OSPF packet begins with a standard 24-byte header.

flowchart LR

  subgraph OSPFH["<b>OSPF Header</b>"]
    OSPFHF["<b>Version</b>: 1byte<br/><b>Packet Type</b>: 1byte<br/><b>Packet Length</b>: 2bytes<br/><b>Router ID</b>: 4bytes<br/><b>Area ID</b>: 4bytes<br/><b>Checksum</b>: 2bytes<br/><b>AuthType</b>: 2bytes<br/><b>Authentication</b>: 8bytes"]
  end

  IPH["IP Header"] --> OSPFH --> DATA["Data"]
FieldDescription
Packet typeSeveral types of OSPF packets exist: Hello packet, Database Description (DD) packet, Link-State Request packet, Link-State Update packet, and Link-State Acknowledgment packet. All of these packets except the Hello packet are used for link-state database synchronization.
Router IDThe Router ID of the router that sent the packet.
Area IDAllows the OSPF router to associate the packet with the proper OSPF area.
ChecksumAllows the receiving router to determine if a packet was damaged in transit.
Authentication fieldsThese fields allow the receiving router to verify that the packet's contents were not modified and that the packet came from the OSPF router whose Router ID appears in the packet.

Five OSPF packet types ensure proper LSA flooding over the OSPF network:

  • Hello packet - Discovers OSPF neighbors and builds adjacencies.
  • Database Description (DD) - Describes the content of the link-state database. Neighbors exchange DD packets while they build the adjacency (the ExStart and Exchange states).
  • Link-State Request (LSR) - Requests up-to-date pieces of the neighbor's database. Out-of-date parts of the routing database are determined after the DD exchange.
  • Link-State Update (LSU) - Carries a collection of specifically requested link-state records.
  • Link-State Acknowledgment (LSack) - Acknowledges other packet types to ensure reliable communication.

Neighbor discovery​

OSPF discovers potential neighbors by sending Hello packets periodically out of configured interfaces. By default, Hello packets are sent out at 10-second intervals. You can change this by setting hello-interval in OSPF interface settings. The router learns the existence of a neighboring router when it receives the neighbor's Hello in return with matching parameters.

The transmission and reception of Hello packets also allow a router to detect the failure of the neighbor. If Hello packets are not received within dead-interval (which by default is 40 seconds), the router starts to route packets around the failure. The "Hello" protocol ensures that the neighboring routers agree on the Hello interval and Dead interval parameters, preventing situations where Hello packets that are not received in time mistakenly bring the link down.

flowchart LR

  subgraph HDATA["<b>Hello Data</b>"]
  HDATAF["Netmask
    Hello Interval
    Options | Priority
    Dead Interval
    DR
    BDR
    1st neighbor ID"]
  end

  IPH["IP Header"] --> OSPFH["OSPF Header"] --> HDATA
FieldDescription
Network maskThe IP mask of the originating router's interface IP address.
Hello intervalThe period between Hello packets (default 10s).
OptionsOSPF options for neighbor information.
Router priorityAn 8-bit value used to aid in the election of the DR and BDR (not set in point-to-point links).
Router dead intervalThe time interval before the neighbor is considered down (by default, four times the Hello interval).
DRThe router ID of the current DR.
BDRThe router ID of the current BDR.
Neighbor router IDsA list of router IDs for all the originating router's neighbors.

On each type of network segment, the Hello protocol works a little differently. On point-to-point segments, only one neighbor is possible and no additional actions are required. However, if more than one neighbor can be on the segment, additional actions are taken to make OSPF more efficient.

Two routers do not become neighbors unless the following conditions are met:

  • Two-way communication between routers is possible. A router knows this when it sees its own Router ID in the neighbor list of the neighbor's Hello packet. Until then, the neighbor stays in the Init state.
  • The interface should belong to the same area.
  • The interface should belong to the same subnet and have the same network mask unless it has network-type configured as point-to-point.
  • Routers should have the same authentication options and exchange the same password (if any).
  • Hello and Dead intervals should be the same in Hello packets.
  • External routing and NSSA flags should be the same in Hello packets.
info

The Priority, DR, and BDR fields matter only on networks that elect a DR, broadcast and NBMA.

Discovery on broadcast subnets​

A node attached to the broadcast subnet can send a single packet, and that packet is received by all other attached nodes. This allows for auto-configuration and information replication. Another important capability in broadcast subnets is multicast. This capability allows sending a single packet which will be received by nodes configured to receive multicast packets. OSPF uses this capability to find OSPF neighbors and detect bidirectional connectivity.

Each OSPF router joins the IP multicast group AllSPFRouters (224.0.0.5). Then the router periodically multicasts its Hello packets to the IP address 224.0.0.5. All other routers that joined the same group will receive a multicasted Hello packet. In that way, OSPF routers maintain relationships with all other OSPF routers by sending a single packet instead of sending a separate packet to each neighbor on the segment.

This approach provides several advantages:

  • Automatic neighbor discovery by multicasting or broadcasting Hello packets.
  • Less bandwidth usage compared to other subnet types.
  • On the broadcast segment, n*(n-1)/2 neighbor relations exist, but those relations are maintained by sending only n Hellos.
  • If the broadcast has the multicast capability, OSPF operates without disturbing non-OSPF nodes on the broadcast segment.
  • If the multicast capability is not supported, all routers will receive broadcasted Hello packets even if a node is not an OSPF router.

Discovery on NBMA subnets​

Non-broadcast multiaccess (NBMA) segments are similar to broadcast. They support more than two routers. The only difference is that NBMA does not support a data-link broadcast capability. Due to this limitation, OSPF neighbors must be discovered initially through configuration. On RouterOS, static neighbor configuration is set in the /routing/ospf/static-neighbor menu. Set the Router Priority to 0 on the routers attached to the NBMA subnet that should not become the DR or BDR, and leave a priority other than 0 only on the routers that should. The default priority is 128 on NBMA interfaces too. With priority 0, a router never becomes DR or BDR.

Discovery on PTMP subnets​

Point-to-MultiPoint treats the network as a collection of point-to-point links.

By design, PTMP networks lack broadcast capabilities, so OSPF neighbors must be discovered initially through configuration, the same as for NBMA networks. All communication happens by sending unicast packets directly between neighbors. On RouterOS, the static neighbor configuration is set in the /routing/ospf/static-neighbor menu. Designated Routers and Backup Designated Routers are not elected on point-to-multipoint subnets.

For PTMP networks with broadcast support, a hybrid type named "ptmp-broadcast" can be used. This network type uses multicast Hellos to discover neighbors automatically and detect bidirectional communication between neighbors. After neighbor detection, "ptmp-broadcast" sends unicast packets directly to the discovered neighbors.

Master-slave relation​

Before database synchronization can begin, the two neighbors decide which of them controls the exchange of Database Description (DD) packets (the Master). Both start by claiming the master role. The router with the higher Router ID becomes the master, and the other router continues as the slave with the master's DD sequence number. The interface priority plays no part in this, and the master/slave relation does not affect the DR/BDR election (the DR does not have to be the Master).

Database synchronization​

Link-state database synchronization between OSPF routers is very important. Unsynchronized databases can lead to incorrectly calculated routing tables which could cause routing loops or black holes.

OSPF uses two types of database synchronization:

  • Initial database synchronization.
  • Reliable flooding.

When the connection between two neighbors first comes up, initial database synchronization occurs. OSPF uses explicit database download when neighbor connections first come up. This procedure is called Database exchange. Instead of sending the entire database, the OSPF router sends only its LSA headers in a sequence of OSPF Database Description (DD) packets. The router will send the next DD packet only when the previous packet is acknowledged. When an entire sequence of DD packets has been received, the router knows which LSAs it does not have and which LSAs are more recent. The router then sends Link-State Request (LSR) packets requesting desired LSAs, and the neighbor responds by flooding LSAs in Link-State Update (LSU) packets. After all the updates are received, neighbors are said to be fully adjacent.

Reliable flooding is another database synchronization method. This method is used when adjacencies are already established and the OSPF router must inform other routers about LSA changes. When the OSPF router receives such a Link State Update, it installs a new LSA in the link-state database, sends an acknowledgment packet back to the sender, repackages the LSA in a new LSU, and sends it out to all interfaces except the receiving interface.

OSPF determines if LSAs are up to date by comparing sequence numbers. Sequence numbers start with 0x80000001. The larger the number, the more recent the LSA is. The originating router increments the sequence number each time it originates a new version of the LSA. The age of an LSA grows while it is in the database. The originator refreshes each of its LSAs about every 30 minutes with a new sequence number, and an LSA that is not refreshed is removed when it reaches the maximum age of 60 minutes.

Databases are not always synchronized between all OSPF neighbors. OSPF decides whether databases must be synchronized depending on the network segment. For example, on point-to-point links, databases are always synchronized between routers. On Ethernet networks, databases are synchronized between certain neighbor pairs.

Synchronization on broadcast subnets​

On the broadcast segment, n*(n-1)/2 neighbor relations exist. A huge amount of Link State Updates and Acknowledgments would be sent over the subnet if the OSPF router tries to synchronize with each OSPF router on the subnet.

Six routers on a broadcast segment: R1 is the DR and R2 the BDR, every other router has an adjacency with both Six routers on a broadcast segment: R1 is the DR and R2 the BDR, every other router has an adjacency with both

This problem is solved by electing one Designated Router and one Backup Designated Router for each broadcast subnet. All other routers synchronize and form adjacencies only with those two elected routers. This approach reduces the number of adjacencies from n*(n-1)/2 to only 2n-3. Two routers that are neither DR nor BDR (dr-other) stay in the TwoWay neighbor state with each other and reach Full only with the DR and BDR.

The previous image illustrates adjacency formations on broadcast subnets. R1 is the Designated Router and R2 is the Backup Designated Router. For example, if R3 wants to flood Link State Update (LSU) to both R1 and R2, the router sends LSU to the IP multicast address AllDRouters (224.0.0.6) and only DR and BDR listen to this multicast address. Then the Designated Router sends LSU addressed to AllSPFRouters, updating the rest of the routers.

DR election​

DR and BDR routers are elected from data received in the Hello packet. When OSPF starts on a broadcast or NBMA interface, the interface stays in the waiting state for the dead interval and listens for an existing DR before it takes part in the election. Routers that start together elect the router with the highest priority as the DR and the next one as the BDR. With equal priorities, the higher Router ID wins. A router that starts alone on a subnet becomes the DR, and the next router becomes the BDR. The election is not preemptive: a router with a higher priority does not take over from a working DR or BDR. When the DR fails or leaves the election, the BDR becomes the DR, and the remaining router with the highest priority becomes the new BDR. The default priority is 128.

Being a Designated Router or Backup Designated Router consumes additional resources. A router with priority 0 does not take part in the election and never becomes DR or BDR. Set priority 0 on routers that should not take this role, for example slower routers on a segment with many neighbors.

Synchronization on NBMA subnets​

Database synchronization on NBMA networks is similar to synchronization on broadcast networks. DR and BDR are elected. Databases are initially exchanged only with DR and BDR routers, and flooding always goes through the DR. The only difference is that Link State Updates must be replicated and sent to each adjacent router separately.