Routing

Forwarding vs routing, longest-prefix match, distance-vector and link-state protocols (RIP, OSPF), BGP between networks, and anycast.

Intermediate 3 min readLesson 5 of 15

Routing decides the path packets take across networks. It has two halves:

  • Forwarding (data plane) — for each arriving packet, look up the destination in a table and send it out the right interface. This happens millions of times per second, often in hardware.
  • Routing (control plane) — the algorithms and protocols that build those tables, and update them when links fail.

The forwarding table and longest-prefix match

A router's table maps destination prefixes to next hops. When several prefixes match, the most specific (longest) wins.

PrefixNext hop
0.0.0.0/0 (default route)ISP gateway
10.0.0.0/8Router A
10.1.0.0/16Router B
10.1.2.0/24Router C

Destination 10.1.2.9 matches all four; the /24 is the longest, so it goes to Router C. Destination 10.9.9.9 matches only /8 and the default → Router A.

Shell
ip route            # Linux routing table
route print         # Windows
netstat -rn         # macOS

Two families of routing algorithms

Distance vector

Each router tells its neighbours its distance to every destination ("I can reach X in 3 hops"). Routers update with Bellman–Ford: dist(X) = min over neighbours (cost to neighbour + neighbour's distance).

  • Simple, little memory
  • Slow convergence; the count-to-infinity problem
  • Example: RIP
Link state

Each router floods a description of its own links to every router, so all routers have the full map, then each runs Dijkstra to compute shortest paths.

  • Fast convergence, no loops once converged
  • More memory and CPU
  • Examples: OSPF, IS-IS
251731ABCDE
Link-state: every router knows this graph and runs Dijkstra. A → E shortest path: A-B-C-D-E, cost 7.

See Shortest paths for the algorithms themselves.

Interior vs exterior routing

The Internet is made of ~75,000 autonomous systems (ASes) — networks under one administration.

Interior (IGP)Exterior (EGP)
ScopeWithin one ASBetween ASes
GoalShortest/fastest pathPolicy: business relationships, cost, control
ProtocolsOSPF, IS-IS, (RIP)BGP

BGP: the glue of the Internet

BGP (Border Gateway Protocol) is a path-vector protocol. Each AS announces the prefixes it can reach along with the AS path (the list of ASes the route goes through). Including the path prevents loops: an AS rejects routes that already contain its own number.

AS 64500 (owns 198.51.100.0/24)AS 3356 (transit)AS 7922 (your ISP)198.51.100.0/24, path [64500]198.51.100.0/24, path [3356 64500]choose the best route by policy, then path length

Route selection considers local preference (prefer routes via customers, who pay you, over peers, over providers), then the shortest AS path, then other tie-breakers. BGP is slow to converge (minutes) and trusts its neighbours, which is why misconfigurations can cause global outages. RPKI cryptographically validates which AS may announce a prefix.

Anycast

With anycast, the same IP prefix is announced from many locations, and BGP naturally routes each user to the nearest one (in network terms). It's used by DNS root servers, public resolvers (1.1.1.1, 8.8.8.8) and CDNs. It also absorbs DDoS attacks, since attack traffic is spread across every site.

Traceroute: watching routing happen

traceroute sends packets with TTL = 1, 2, 3… Each router decrements the TTL, and when it hits 0 the router returns an ICMP "time exceeded", revealing itself.

$ traceroute example.com
 1  router.home (192.168.1.1)       1.2 ms
 2  10.20.0.1 (ISP)                 8.9 ms
 3  ae-12.core1.fra.isp.net        14.1 ms
 4  * * *                           (router not answering ICMP)
 5  93.184.216.34                  92.4 ms

A big jump in latency between two hops usually means a long-distance link (an ocean crossing, for instance).

Routing in the cloud and in containers

Cloud VPCs have route tables per subnet: a route 0.0.0.0/0 → internet gateway makes a subnet public; → NAT gateway gives private subnets outbound-only access. Kubernetes networking plugins (Calico, Cilium) program routes or BGP so every pod gets a routable IP.

Key takeaways
  • Forwarding looks up the longest matching prefix; routing builds the tables.
  • Distance vector (Bellman–Ford, RIP) shares distances; link state (Dijkstra, OSPF) shares the topology.
  • IGPs optimise paths within an AS; BGP connects ASes based on policy.
  • BGP's AS paths prevent loops; RPKI adds origin validation.
  • Anycast announces one prefix from many places to reach the nearest site.