Skip to content
BoKSA

03: Routing Algorithms: Link-State and Distance-Vector

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:

  1. 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.
  2. 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

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
Initialisation:
  N' = {u}                        # set of nodes whose least-cost path is final
  for all nodes v:
      if v is a neighbour of u: D(v) = c(u,v)
      else: D(v) = ∞

Loop until N' contains all nodes:
  find w not in N' with the smallest D(w)
  add w to N'
  for each neighbour v of w not in N':
      D(v) = min( D(v), D(w) + c(w,v) )
      # remember predecessor p(v) if the path via w is cheaper

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
d_x(y) = min over all neighbours v of { c(x,v) + d_v(y) }

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
  • 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
Links: x–y = 4, y–z = 1, x–z = 50.    Then c(x,y) changes from 4 to 60.
y thinks: "z can reach x at cost 5"  → y now routes via z (cost 1+5 = 6)
z thinks: "y can reach x at cost 6"  → z routes via y (cost 1+6 = 7)
y → 8, z → 9, ... this continues for 44 iterations, until z
finally prefers its direct link to x (cost 50). Meanwhile: a routing loop!

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).

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

Articles and interactive material

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


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

  1. 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.
  2. 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.
  3. Filter routes: accept only prefixes you expect from each neighbour (prefix lists and route maps).
  4. Use a separate management plane: put routing and management traffic in a separate VRF or VLAN.
  5. Monitor the routing state: alert on unexpected route changes, new neighbours or a sudden drop in prefix counts.
  6. Protect the control plane: use Control Plane Policing (CoPP) so that floods of routing packets can't overload the router's CPU.
  7. Replace legacy protocols: replace RIP with OSPF/IS-IS with authentication, or with static routes in small networks.

Extra references