Skip to main content
Version: 7.26

OSPF

OSPF (Open Shortest Path First) is a link-state Interior Gateway Protocol (IGP). RouterOS runs OSPF version 2 for IPv4 and OSPF version 3 for IPv6. Each version is a separate instance with its own areas, neighbors and link-state database.

An OSPF configuration in RouterOS has three parts:

  • An instance in /routing/ospf/instance sets the version, the Router ID, redistribution, the default route and route filters.
  • Areas in /routing/ospf/area belong to an instance and set the area ID and area type.
  • Interface templates in /routing/ospf/interface-template select interfaces by name or by network and put them into an area, with the network type, cost, timers and authentication.

From the templates, RouterOS creates the dynamic entries in /routing/ospf/interface. Neighbors appear in /routing/ospf/neighbor and the link-state database in /routing/ospf/lsa.

The protocol itself is explained on the following pages:

PageTopic
Understanding OSPFTerms and supported standards.
Neighbor RelationshipPackets, neighbor discovery on each network type, DR and BDR, database synchronization.
Routing Table CalculationCost, SPF calculation, forwarding address.
Areas and Virtual LinksArea types, LSA types, external routes, summarization, virtual links.
TroubleshootingNeighbors that do not come up, missing routes.

Configure OSPF on three routers​

The example connects two sites through an area border router (ABR):

  • R1 is in area 1. It connects to R2 over Ethernet (broadcast network type) and has a LAN, 192.168.1.0/24.
  • R2 is the ABR. It connects to R1 in area 1 and to R3 in the backbone area over a point-to-point link.
  • R3 is in the backbone area and has a LAN, 192.168.3.0/24.

The ports used between the routers are not bridge ports, and R2 runs the default firewall configuration. Each router has a loopback address (a /32 on the lo interface) that is used as its Router ID. The loopback address does not depend on the state of a physical interface.

flowchart LR
  LAN1["192.168.1.0/24"] --- R1
  R1 -- "ether4 10.0.12.1/30<br/>area 1, broadcast<br/>ether2 10.0.12.2/30" --- R2
  R2 -- "ether3 10.0.23.1/30<br/>backbone, ptp<br/>ether10 10.0.23.2/30" --- R3
  R3 --- LAN3["192.168.3.0/24"]

R1

/ip/address
add address=10.255.255.1/32 interface=lo
add address=10.0.12.1/30 interface=ether4
add address=192.168.1.1/24 interface=bridge
/routing/ospf/instance
add name=ospf-v2 version=2 router-id=10.255.255.1
/routing/ospf/area
add name=area1 area-id=0.0.0.1 instance=ospf-v2
/routing/ospf/interface-template
add area=area1 interfaces=ether4
add area=area1 interfaces=lo,bridge passive

R2

/ip/address
add address=10.255.255.2/32 interface=lo
add address=10.0.12.2/30 interface=ether2
add address=10.0.23.1/30 interface=ether3
/routing/ospf/instance
add name=ospf-v2 version=2 router-id=10.255.255.2
/routing/ospf/area
add name=backbone area-id=0.0.0.0 instance=ospf-v2
add name=area1 area-id=0.0.0.1 instance=ospf-v2
/routing/ospf/interface-template
add area=area1 interfaces=ether2
add area=backbone interfaces=ether3 type=ptp
add area=backbone interfaces=lo passive
/ip/firewall/filter
add chain=input protocol=ospf in-interface=ether2 action=accept \
comment="accept OSPF" \
place-before=[find comment="defconf: drop all not coming from LAN"]
add chain=input protocol=ospf in-interface=ether3 action=accept \
comment="accept OSPF" \
place-before=[find comment="defconf: drop all not coming from LAN"]

R3

/ip/address
add address=10.255.255.3/32 interface=lo
add address=10.0.23.2/30 interface=ether10
add address=192.168.3.1/24 interface=bridge
/routing/ospf/instance
add name=ospf-v2 version=2 router-id=10.255.255.3
/routing/ospf/area
add name=backbone area-id=0.0.0.0 instance=ospf-v2
/routing/ospf/interface-template
add area=backbone interfaces=ether10 type=ptp
add area=backbone interfaces=lo,bridge passive

Notes on the configuration:

  • The templates select interfaces by name. A template can also select them by network, for example networks=10.0.12.0/30, see Neighbor Relationship.
  • A passive interface is advertised, but OSPF sends no Hello packets on it and forms no adjacency. Make LAN interfaces passive unless another router is on the LAN.
  • The link between R2 and R3 has only two routers, so type=ptp skips the DR and BDR election. Set the same network type at both ends.
  • On a router with the default configuration, the input chain drops everything that does not come from the LAN interface list. OSPF packets on other interfaces are dropped too, and no adjacency forms. The R2 configuration accepts OSPF before that rule. Add the same rule on every router whose firewall drops traffic on the OSPF interfaces.

Check OSPF​

On R2, both neighbors are in the Full state. On the broadcast link to R1, R2 is the designated router (dr=10.0.12.2), and R1 is the backup designated router:

[admin@R2] > /routing/ospf/neighbor/print
Flags: D - DYNAMIC
0 D instance=ospf-v2 area=area1 interface=ether2 address=10.0.12.1 priority=128
router-id=10.255.255.1 dr=10.0.12.2 bdr=10.0.12.1 state="Full"
state-changes=6 adjacency=15s timeout=40s

1 D instance=ospf-v2 area=backbone interface=ether3 address=10.0.23.2
router-id=10.255.255.3 state="Full" state-changes=6 adjacency=50s
timeout=30s

The interface list shows the state of each OSPF interface (dr, bdr, dr-other, ptp, passive) and the values the templates set:

[admin@R2] > /routing/ospf/interface/print
Flags: D - DYNAMIC
0 D interface=lo address=10.255.255.2%lo area=backbone instance-id=0
state=passive type=broadcast cost=1 priority=128 passive
retransmit-interval=5s transmit-delay=1s hello-interval=10s
dead-interval=40s

1 D interface=ether2 address=10.0.12.2%ether2 area=area1 instance-id=0
state=dr type=broadcast bdr=10.0.12.1 cost=1 priority=128
retransmit-interval=5s transmit-delay=1s hello-interval=10s
dead-interval=40s

2 D interface=ether3 address=10.0.23.1%ether3 area=backbone instance-id=0
state=ptp type=ptp cost=1 priority=128 retransmit-interval=5s
transmit-delay=1s hello-interval=10s dead-interval=40s

The link-state database of area 1 on R2 holds the router LSAs of R1 and R2, the network LSA that R2 originates as the DR, and the inter-area-prefix LSAs (type 3) through which R2, as the ABR, advertises the backbone networks into area 1:

[admin@R2] > /routing/ospf/lsa/print where area=area1
Flags: S - SELF-ORIGINATED; D - DYNAMIC
0 D instance=ospf-v2 area=area1 type="router" originator=10.255.255.1
id=10.255.255.1 sequence=0x80000004 age=15 checksum=0x680B body=
options=E
type=network id=10.0.12.2 data=10.0.12.1 metric=1
type=stub id=10.255.255.1 data=255.255.255.255 metric=1
type=stub id=192.168.1.0 data=255.255.255.0 metric=1

1 SD instance=ospf-v2 area=area1 type="router" originator=10.255.255.2
id=10.255.255.2 sequence=0x80000004 age=8 checksum=0xBB4B body=
options=E bits=B
type=network id=10.0.12.2 data=10.0.12.2 metric=1

2 SD instance=ospf-v2 area=area1 type="network" originator=10.255.255.2
id=10.0.12.2 sequence=0x80000001 age=9 checksum=0xC859 body=
netmask=255.255.255.252
router-id=10.255.255.1
router-id=10.255.255.2

3 SD instance=ospf-v2 area=area1 type="inter-area-prefix"
originator=10.255.255.2 id=10.0.23.0 sequence=0x80000001 age=48
checksum=0x7CB5 body=
netmask=255.255.255.252
metric=1

4 SD instance=ospf-v2 area=area1 type="inter-area-prefix"
originator=10.255.255.2 id=10.255.255.2 sequence=0x80000001 age=48
checksum=0x78CB body=
netmask=255.255.255.255
metric=1

5 SD instance=ospf-v2 area=area1 type="inter-area-prefix"
originator=10.255.255.2 id=10.255.255.3 sequence=0x80000001 age=40
checksum=0x78C9 body=
netmask=255.255.255.255
metric=2

6 SD instance=ospf-v2 area=area1 type="inter-area-prefix"
originator=10.255.255.2 id=192.168.3.0 sequence=0x80000001 age=40
checksum=0x469C body=
netmask=255.255.255.0
metric=2

R1 has routes to R3's loopback and LAN through R2:

[admin@R1] > /ip/route/print where ospf
Flags: D - DYNAMIC; A - ACTIVE; o - OSPF
Columns: DST-ADDRESS, GATEWAY, ROUTING-TABLE, DISTANCE
DST-ADDRESS GATEWAY ROUTING-TABLE DISTANCE
DAo 10.0.23.0/30 10.0.12.2%ether4 main 110
DAo 10.255.255.2/32 10.0.12.2%ether4 main 110
DAo 10.255.255.3/32 10.0.12.2%ether4 main 110
DAo 192.168.3.0/24 10.0.12.2%ether4 main 110
[admin@R1] > /ping 192.168.3.1 src-address=192.168.1.1 count=3
SEQ HOST SIZE TTL TIME STATUS
0 192.168.3.1 56 63 1ms208us
1 192.168.3.1 56 63 507us
2 192.168.3.1 56 63 515us
sent=3 received=3 packet-loss=0% min-rtt=507us avg-rtt=743us
max-rtt=1ms208us

If a neighbor does not reach the Full state, see Troubleshooting.

OSPFv3 for IPv6​

OSPFv3 runs as a separate instance with version=3 and its own areas and templates. It uses the same dotted-decimal Router ID. Neighbors talk over link-local addresses, so the links between the routers need no global IPv6 addresses. Add the following configuration to the previous example.

R1

/ipv6/address
add address=fd00:255::1/128 interface=lo advertise=no
add address=fd00:1::1/64 interface=bridge
/routing/ospf/instance
add name=ospf-v3 version=3 router-id=10.255.255.1
/routing/ospf/area
add name=area1-v3 area-id=0.0.0.1 instance=ospf-v3
/routing/ospf/interface-template
add area=area1-v3 interfaces=ether4
add area=area1-v3 interfaces=lo,bridge passive

R2

/ipv6/address
add address=fd00:255::2/128 interface=lo advertise=no
/routing/ospf/instance
add name=ospf-v3 version=3 router-id=10.255.255.2
/routing/ospf/area
add name=backbone-v3 area-id=0.0.0.0 instance=ospf-v3
add name=area1-v3 area-id=0.0.0.1 instance=ospf-v3
/routing/ospf/interface-template
add area=area1-v3 interfaces=ether2
add area=backbone-v3 interfaces=ether3 type=ptp
add area=backbone-v3 interfaces=lo passive

R3

/ipv6/address
add address=fd00:255::3/128 interface=lo advertise=no
add address=fd00:3::1/64 interface=bridge
/routing/ospf/instance
add name=ospf-v3 version=3 router-id=10.255.255.3
/routing/ospf/area
add name=backbone-v3 area-id=0.0.0.0 instance=ospf-v3
/routing/ospf/interface-template
add area=backbone-v3 interfaces=ether10 type=ptp
add area=backbone-v3 interfaces=lo,bridge passive

On a router with the default configuration, the IPv6 input chain drops OSPF on interfaces outside the LAN interface list, as the IPv4 chain does. Accept it on R2:

/ipv6/firewall/filter
add chain=input protocol=ospf in-interface=ether2 action=accept \
comment="accept OSPF" \
place-before=[find comment="defconf: drop everything else not coming from LAN"]
add chain=input protocol=ospf in-interface=ether3 action=accept \
comment="accept OSPF" \
place-before=[find comment="defconf: drop everything else not coming from LAN"]

The neighbors appear with link-local addresses:

[admin@R2] > /routing/ospf/neighbor/print where instance=ospf-v3
Flags: D - DYNAMIC
0 D instance=ospf-v3 area=area1-v3 interface=ether2 address=fe80::ff:fe00:104
priority=128 router-id=10.255.255.1 dr=10.255.255.2 bdr=10.255.255.1
state="Full" state-changes=6 adjacency=16s timeout=40s

1 D instance=ospf-v3 area=backbone-v3 interface=ether3
address=fe80::ff:fe00:310 router-id=10.255.255.3 state="Full"
state-changes=6 adjacency=51s timeout=40s

The IPv6 routes use the link-local address of the neighbor as the gateway:

[admin@R1] > /ipv6/route/print where ospf
Flags: D - DYNAMIC; A - ACTIVE; o - OSPF
Columns: DST-ADDRESS, GATEWAY, ROUTING-TABLE, DISTANCE
DST-ADDRESS GATEWAY ROUTING-TABLE DISTANCE
DAo fd00:3::/64 fe80::ff:fe00:202%ether4 main 110
DAo fd00:255::2/128 fe80::ff:fe00:202%ether4 main 110
DAo fd00:255::3/128 fe80::ff:fe00:202%ether4 main 110
[admin@R1] > /ping fd00:3::1 src-address=fd00:1::1 count=3
SEQ HOST SIZE TTL TIME STATUS
0 fd00:3::1 56 63 595us echo reply
1 fd00:3::1 56 63 623us echo reply
2 fd00:3::1 56 63 601us echo reply
sent=3 received=3 packet-loss=0% min-rtt=595us avg-rtt=606us
max-rtt=623us

The following sections change interface templates with selectors such as [find interfaces=ether4]. With the OSPFv3 configuration in place, such a selector also matches the OSPFv3 template, so the change applies to both instances. To change one instance only, add its area to the selector, for example [find interfaces=ether4 and area=area1].

OSPF chooses the path with the lowest total cost. The cost of an interface is 1 by default, on every link speed, so without changes OSPF counts hops. The cost applies to traffic that leaves the interface, so set it at both ends of a link to change the path in both directions.

To make a link less preferred, for example a backup link, raise its cost at both ends. OSPF then uses another path when one exists, and the backup link only when that path fails. In the example network, the link between R2 and R3 is the only path, so the change only raises the metric of the routes through it. On R2:

/routing/ospf/interface-template
set [find interfaces=ether3] cost=100

On R3:

/routing/ospf/interface-template
set [find interfaces=ether10] cost=100

The metric of an OSPF route is in the ospf.metric field:

/routing/route/print detail where dst-address=192.168.3.0/24

Authenticate OSPF packets​

Authentication protects the adjacency against routers that send OSPF packets without the key. Set the same auth type, auth-key and auth-id on both ends of the link. On R1:

/routing/ospf/interface-template
set [find interfaces=ether4] auth=sha256 auth-key=Ex4mple-key auth-id=1

On R2:

/routing/ospf/interface-template
set [find interfaces=ether2] auth=sha256 auth-key=Ex4mple-key auth-id=1

Until both ends use the same settings, the routers drop each other's packets, and the adjacency goes down when the dead interval expires. The interface print shows auth and auth-id, not the key.

OSPF declares a neighbor down when no Hello arrives for the dead interval, 40 seconds by default. BFD detects a failed link in about a second. BFD runs only on interfaces that a /routing/bfd/configuration entry allows. On R2:

/routing/bfd/configuration
add interfaces=ether3
/routing/ospf/interface-template
set [find interfaces=ether3 and area=backbone] use-bfd=yes
/ip/firewall/filter
add chain=input protocol=udp dst-port=3784 action=accept \
comment="accept BFD" \
place-before=[find comment="defconf: drop all not coming from LAN"]

On R3:

/routing/bfd/configuration
add interfaces=ether10
/routing/ospf/interface-template
set [find interfaces=ether10 and area=backbone] use-bfd=yes

BFD control packets use UDP port 3784, so R2, which runs the default firewall, needs the accept rule. The area=backbone selector enables BFD for the OSPFv2 instance only. Check the session:

/routing/bfd/session/print

When the BFD session goes down, OSPF drops the neighbor without waiting for the dead interval and logs bfd down.

Redistribute routes and originate a default route​

A router that advertises routes from outside OSPF is an autonomous system boundary router (ASBR). redistribute advertises active routes of the listed types as external routes. They get metric 1 and external type 1. For example, to advertise R3's static routes:

/routing/ospf/instance
set [find name=ospf-v2] redistribute=static

A static default route is a static route too, so this also advertises R3's default route. Control the default route with originate-default:

  • never keeps the default route out, also when a redistributed protocol has one.
  • always advertises 0.0.0.0/0, also when the router has no default route.
  • if-installed advertises 0.0.0.0/0 while the router has an active default route.

To advertise R3's static routes and its default route to the rest of the network:

/routing/ospf/instance
set [find name=ospf-v2] redistribute=static \
originate-default=if-installed

To change the metric or the external type, use a routing filter in out-filter-chain. The following filter advertises the external routes as type 2 with metric 50:

/routing/filter/rule
add chain=ospf-out \
rule="set ospf-ext-type type2; set ospf-ext-metric 50; accept"
/routing/ospf/instance
set [find name=ospf-v2] out-filter-chain=ospf-out

The filter also applies to the default route from originate-default: it changes its attributes, and a reject removes it. Areas and Virtual Links explains external route types and the order in which redistribution and filters are applied.

Summarize routes between areas​

An ABR advertises every prefix of an area into the backbone. An area range replaces them with one summary. On R2, to advertise the area 1 networks from 192.168.0.0/23 as one route:

/routing/ospf/area/range
add area=area1 prefix=192.168.0.0/23 advertise=yes cost=10

The backbone then gets 192.168.0.0/23 instead of 192.168.1.0/24. While at least one area 1 prefix is in the range, R2 installs a blackhole route for it, so traffic to an unused part of the range is dropped on R2. With advertise=no, nothing from the range is advertised out of the area.

The CLI reference lists all OSPF parameters: /routing/ospf.