With two or three routers, static routes are enough. But as offices and links multiply, static routing becomes a table nobody dares touch, and when a link fails, traffic doesn't reroute by itself. OSPF solves this: routers learn about each other's networks and recalculate routes when something breaks. Here is how it works and how to set it up on Cisco and MikroTik.
How OSPF works
- Adjacency. Routers on shared links find each other with Hello packets (to 224.0.0.5) and form adjacencies.
- Exchanging the map. Neighbors exchange descriptions of their links and networks. In the end every router in an area has the same “map” of the network.
- Calculation. From the map, each router computes the shortest paths (Dijkstra's algorithm) and puts the best ones into its routing table.
- Failure. If a neighbor disappears (by default, no Hello for 40 seconds), the map is updated and routes are recalculated.
“Shortest” means lowest cost. By default cost = 100 Mbit/s ÷ interface speed, so 1 and 10 Gbit/s links both get cost 1. Raise the reference bandwidth to the same value on all routers, for example 10 Gbit/s; otherwise OSPF can't tell a fast link from a slow one.
Key concepts
- Router ID is a router's unique number in IPv4 address format. Set it explicitly, usually to the loopback address: if the router picks it itself, it may change after a reboot.
- An area is a group of routers sharing one map. Area 0 is the backbone, and other areas attach to it. A small network needs only area 0.
- A passive interface advertises its network but sends no Hellos. Use it on every user-facing port: there is no point looking for neighbors in a network of PCs, and a rogue router there can't inject its routes.
Cisco example
interface Loopback0 ip address 10.255.255.1 255.255.255.255 ! router ospf 1 router-id 10.255.255.1 auto-cost reference-bandwidth 10000 passive-interface default no passive-interface GigabitEthernet0/0 network 10.255.255.1 0.0.0.0 area 0 network 10.0.0.0 0.0.0.3 area 0 network 192.168.10.0 0.0.0.255 area 0 ! interface GigabitEthernet0/0 ip ospf authentication message-digest ip ospf message-digest-key 1 md5 LongSecretKey
The network commands take a wildcard mask; how to work it out is explained in the article on subnet masks. passive-interface default makes every interface passive, and then you enable only those with neighbors. Newer IOS versions can use HMAC-SHA through a key chain (key chain and ip ospf authentication key-chain) instead of MD5.
Check with show ip ospf neighbor (the state should be FULL) and show ip route ospf.
MikroTik example (RouterOS 7)
/interface bridge add name=loopback /ip address add address=10.255.255.2/32 interface=loopback /routing ospf instance add name=main router-id=10.255.255.2 /routing ospf area add name=backbone area-id=0.0.0.0 instance=main /routing ospf interface-template add area=backbone networks=10.0.0.0/30 auth=md5 auth-id=1 auth-key=LongSecretKey add area=backbone networks=192.168.20.0/24 passive add area=backbone networks=10.255.255.2/32 passive
RouterOS 7 configures OSPF differently from version 6: interface templates replace /routing ospf network. Scripts from old v6 guides don't work on v7. Check with /routing ospf neighbor print (state Full) and /ip route print where ospf.
Why the adjacency doesn't come up
If neighbors don't reach Full, the cause is almost always a parameter mismatch between the two ends of the link:
- different areas on the same link;
- different Hello and Dead timers: they are rarely set by hand, but if they are, they must match on both sides;
- different subnet masks on the link's interfaces;
- authentication enabled on one side only, or different keys;
- different MTU: neighbors get stuck in ExStart or Exchange. A common case between Cisco and MikroTik or on tunnels;
- a passive interface where a neighbor should be;
- the same Router ID on two routers, for example after copying a configuration;
- the firewall: OSPF is neither TCP nor UDP but its own IP protocol, number 89. On a MikroTik with a strict input chain it must be allowed explicitly.
When you don't need OSPF
If an office has one router and two ISPs, OSPF won't help: the ISPs won't exchange routes with you. For that there are recursive routes with reachability checks, covered in the article on dual WAN, the MikroTik dual WAN generator and Cisco backup link (IP SLA). OSPF is for when you have several routers of your own and several paths between them: branches, a ring between buildings, core and distribution in a large office.
The other tools for configuring and checking equipment are gathered on the Cisco and MikroTik pages, and the subnet calculator helps you draw up an addressing plan.