03: Routing Algorithms: Link-State and Distance-Vector
Course: Networking - Security Advanced · Updated: October 2026 Literature: K&R chapter 5 (Network Layer: Control Plane): introduction, routing algorithms Related lab: Lab: Routing algorithms and protocols
Learning objectives
After studying this article you can:
- explain the difference between the network layer's data plane (forwarding) and control plane (routing)
- model a network as a graph with link costs
- run Dijkstra's algorithm (link-state) by hand and build a forwarding table from the result
- run the Bellman-Ford / distance-vector algorithm by hand
- explain the count-to-infinity problem and the poisoned reverse fix
- compare link-state and distance-vector algorithms
1. Forwarding vs routing
| Forwarding (data plane) | Routing (control plane) | |
|---|---|---|
| Question | "This packet came in. Which output port should it go to?" | "What is the best path from source to destination?" |
| Scope | Local, inside one router | Network-wide |
| Time scale | Nanoseconds (in hardware) | Seconds |
| Result | Packet moved to an output link | Forwarding table filled in |
There are two ways to organise the control plane:
- Per-router control (traditional): every router runs a routing algorithm and the routers talk to each other. This is OSPF and BGP, covered in article 04.
- Logically centralised control (SDN): a controller computes the routes and installs them in the switches. This is covered in article 05.
2. The network as a graph
A network is a graph G = (N, E):
- N: the nodes (routers)
- E: the edges (links), each with a cost c(x,y)
The cost can stand for distance, delay, monetary cost or inverse bandwidth. Often it is just 1 per hop. The least-cost path is the path with the lowest total cost.
The example network from K&R, used in the rest of this article:
| Link | Cost | Link | Cost |
|---|---|---|---|
| u–v | 2 | x–w | 3 |
| u–x | 1 | x–y | 1 |
| u–w | 5 | w–y | 1 |
| v–x | 2 | w–z | 5 |
| v–w | 3 | y–z | 2 |
Tip: draw this graph on paper before you work through the example. Put u on the left, z on the right, v and w on top, and x and y at the bottom.
Routing algorithms can be classified as:
| Classification | Options |
|---|---|
| Information | Global (every node knows the whole topology, i.e. link-state) or decentralised (a node knows only its neighbours, i.e. distance-vector) |
| Dynamics | Static (configured by hand) or dynamic (adapt to changes) |
| Load sensitivity | Most Internet algorithms are load-insensitive |
3. Link-state routing: Dijkstra's algorithm
Every router floods information about its own links to all other routers. Each router then has the full map and computes the shortest paths to all destinations with Dijkstra's algorithm.
The algorithm
1 2 3 4 5 6 7 8 9 10 11 12 | |
Worked example (source u)
| Step | N' | D(v),p(v) | D(w),p(w) | D(x),p(x) | D(y),p(y) | D(z),p(z) |
|---|---|---|---|---|---|---|
| 0 | u | 2,u | 5,u | 1,u | ∞ | ∞ |
| 1 | ux | 2,u | 4,x | 2,x | ∞ | |
| 2 | uxv | 4,x | 2,x | ∞ | ||
| 3 | uxvy | 3,y | 4,y | |||
| 4 | uxvyw | 4,y | ||||
| 5 | uxvywz |
The resulting forwarding table in u, built by following the predecessors back to u's first hop:
| Destination | Link |
|---|---|
| v | (u,v) |
| x | (u,x) |
| y | (u,x) |
| w | (u,x) |
| z | (u,x) |
Complexity: O(n²) in the simple form, or O(n log n + E) with a priority queue (heap).
Oscillations: if link costs depend on the traffic load, routes can swing back and forth. This is one reason why Internet routing uses load-insensitive costs.
4. Distance-vector routing: Bellman-Ford
Each router knows only the cost to its neighbours. Routers repeatedly send their distance vector (their current estimates of the cost to every destination) to their neighbours.
The Bellman-Ford equation
1 | |
In words: the cheapest way from x to y goes through the neighbour v for which "the cost to get to v" plus "v's cost to y" is smallest.
Properties
- Iterative: routers keep updating until nothing changes
- Asynchronous: no global clock is needed
- Distributed: each node works only with information from its neighbours
- Self-terminating: a node only sends an update when its own vector changes
Changes in link cost
- Good news travels fast. If a link cost goes down, the improvement spreads in a few iterations.
- Bad news travels slowly: the count-to-infinity problem. If a link cost goes up a lot, or a link fails, routers can keep pointing at each other and increase their estimates by small steps:
1 2 3 4 5 | |
Poisoned reverse: if z routes to x via y, z tells y that its distance to x is ∞. Then y won't route back via z. This fixes loops between two nodes, but not larger loops. Real protocols (RIP) also use a maximum hop count (16 = infinity).
5. Link-state vs distance-vector
| Link-State (LS) | Distance-Vector (DV) | |
|---|---|---|
| Information | Full topology | Only neighbours' vectors |
| Message complexity | O(n·E) messages flooded | Messages exchanged only between neighbours |
| Speed of convergence | Fast, O(n²) computation | Variable. Can have routing loops and count-to-infinity. |
| Robustness | A broken router advertises a wrong link cost, but each router computes its own table | A broken router can advertise wrong paths, and the error spreads through the whole network |
| Examples | OSPF, IS-IS | RIP, Cisco EIGRP (an advanced DV). BGP is a path-vector protocol, a DV variant. |
6. Developments since 2018
- Segment Routing (SR-MPLS / SRv6): the source puts a list of "segments" (waypoints) in the packet header. Link-state protocols (OSPF/IS-IS) distribute the segment IDs. It is widely used by ISPs for traffic engineering.
- Fast Reroute / TI-LFA: routers compute backup paths in advance, so they can switch in under 50 ms when a link fails.
- Centralised path computation (PCE, SDN controllers, Google's B4/Espresso) works alongside distributed algorithms.
- Routing in data centres: large data centres use BGP even inside the data centre (RFC 7938) because of its simplicity and scale.
7. Summary
- Routing computes paths (control plane). Forwarding moves packets (data plane).
- Link-state (Dijkstra): every router knows the full map and computes the shortest paths.
- Distance-vector (Bellman-Ford): routers exchange estimates with their neighbours. It is simple, but suffers from count-to-infinity.
- Real protocols combine these ideas with extra mechanisms for scale, fast convergence and policy.
References
Videos
- Jim Kurose: Routing algorithms (chapter 5 video lectures): https://gaia.cs.umass.edu/kurose_ross/lectures.php
- Computerphile: "Dijkstra's Algorithm": https://www.youtube.com/@Computerphile
- Spanning Tree / Practical Networking: routing explained: https://www.youtube.com/@PracticalNetworking
- Abdul Bari: Dijkstra and Bellman-Ford (algorithms lectures): https://www.youtube.com/@abdul_bari
Articles and interactive material
- K&R interactive exercises: Dijkstra's link-state algorithm and Bellman-Ford distance-vector: https://gaia.cs.umass.edu/kurose_ross/interactive/
- VisuAlgo, interactive visualisation of shortest-path algorithms (Dijkstra, Bellman-Ford): https://visualgo.net/en/sssp
- Practical Networking, Routing overview: https://www.practicalnetworking.net/
- Peterson & Davie, Computer Networks: A Systems Approach (free online), chapter "Routing": https://book.systemsapproach.org/
Standards
- RFC 2453: RIP version 2
- RFC 2328: OSPF version 2
- RFC 8402: Segment Routing Architecture
- RFC 7938: Use of BGP for Routing in Large-Scale Data Centers
Tools
- NetworkX (Python):
nx.dijkstra_path(G, 'u', 'z'), to check your hand calculations: https://networkx.org/ - GNS3 / Containerlab / Cisco Packet Tracer: build virtual router labs: https://www.gns3.com/, https://containerlab.dev/
Cyber Security
Why it matters
Whoever controls the routing controls where the traffic goes. If an attacker can influence a routing algorithm's input (topology or costs), they can:
- redirect traffic through a node they control, for interception or man-in-the-middle attacks
- create a black hole, where traffic is dropped, which is a Denial of Service
- create routing loops that use up bandwidth and CPU
- hide malicious paths from monitoring
Distance-vector protocols are especially vulnerable, because each router believes its neighbours. One malicious or misconfigured router that advertises "I can reach everything at cost 0" can attract traffic from the whole network. Link-state protocols limit the damage somewhat, but an attacker who can inject link-state advertisements can still change the computed paths.
Inside an organisation, legacy protocols such as RIP often run with no authentication at all. An attacker on the LAN can then inject routes with tools such as Scapy or yersinia.
How to make it safe
- Authenticate routing messages: use HMAC-SHA authentication for OSPF (RFC 5709/7474), IS-IS (RFC 5310) and RIPv2 (RFC 4822). Don't use plain-text passwords or MD5.
- Use passive interfaces: don't send or accept routing updates on interfaces that face end users. Only router-to-router links should take part in routing.
- Filter routes: accept only prefixes you expect from each neighbour (prefix lists and route maps).
- Use a separate management plane: put routing and management traffic in a separate VRF or VLAN.
- Monitor the routing state: alert on unexpected route changes, new neighbours or a sudden drop in prefix counts.
- Protect the control plane: use Control Plane Policing (CoPP) so that floods of routing packets can't overload the router's CPU.
- Replace legacy protocols: replace RIP with OSPF/IS-IS with authentication, or with static routes in small networks.
Extra references
- NSA, Network Infrastructure Security Guide (2022, updated): https://media.defense.gov/2022/Jun/15/2003018261/-1/-1/0/CTR_NSA_NETWORK_INFRASTRUCTURE_SECURITY_GUIDE_20220615.PDF
- RFC 6862: Keying and Authentication for Routing Protocols (KARP) Overview
- RFC 7474: Security Extension for OSPFv2 When Using Manual Key Management
- MITRE ATT&CK, Network Device techniques (e.g. T1556.004 Network Device Authentication, T1601 Modify System Image): https://attack.mitre.org/
- CIS Benchmarks for Cisco IOS / Juniper JunOS: https://www.cisecurity.org/cis-benchmarks