Lesson 11 intermediate

Routing Fundamentals

How a router decides where a packet goes: the routing table, longest prefix match, connected, static and default routes, administrative distance and metrics, static versus dynamic routing, distance vector versus link state, and how to read and change routes on Linux.

Routing is the job of moving a packet from one network to another, hop by hop, until it reaches the destination’s network. No router knows the whole path. Each one only answers: “for this destination, which neighbor do I hand the packet to next?” The answer comes from its routing table.

What is in a routing table

Every entry says: “to reach this prefix, send to this next hop out of this interface“, plus some numbers to choose between options.

$ ip route
default via 192.168.1.1 dev enp3s0 proto dhcp metric 100
10.8.0.0/24 dev wg0 proto kernel scope link src 10.8.0.2
10.20.0.0/16 via 10.8.0.1 dev wg0
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 linkdown
192.168.1.0/24 dev enp3s0 proto kernel scope link src 192.168.1.42 metric 100
RouteKindMeaning
192.168.1.0/24 dev enp3s0ConnectedMy own LAN; deliver directly with ARP
10.20.0.0/16 via 10.8.0.1StaticOffice networks are behind the VPN peer
default via 192.168.1.1Default (0.0.0.0/0)Everything else goes to the home router

Windows shows the same with route print or Get-NetRoute.

Longest prefix match

When several routes match a destination, the router picks the most specific one: the longest prefix wins, regardless of the order of the table.

For destination 10.20.5.9 on the table above:

  • 0.0.0.0/0 matches (everything does) — 0 bits.
  • 10.20.0.0/16 matches — 16 bits. Winner.

Ask the kernel which route it would use:

$ ip route get 10.20.5.9
10.20.5.9 via 10.8.0.1 dev wg0 src 10.8.0.2 uid 1000
$ ip route get 1.1.1.1
1.1.1.1 via 192.168.1.1 dev enp3s0 src 192.168.1.42 uid 1000

ip route get is the single most useful routing command: it removes all guessing.

Choosing between equal prefixes

If two sources offer the same prefix, the router uses two tie-breakers:

  1. Administrative distance (trust in the source, lower is better). Typical Cisco values: connected 0, static 1, eBGP 20, EIGRP 90, OSPF 110, IS-IS 115, RIP 120, iBGP 200.
  2. Metric (cost within one protocol, lower is better): hop count for RIP, a bandwidth-based cost for OSPF, path attributes for BGP. On Linux, the metric field plays this role between interfaces, which is why a wired link (metric 100) beats Wi-Fi (metric 600).

If two routes tie on everything, many routers use both: ECMP (equal-cost multipath) spreads flows across them.

Static routing

You write routes by hand.

$ sudo ip route add 10.30.0.0/16 via 192.168.1.254
$ sudo ip route replace default via 192.168.1.1 dev enp3s0
$ sudo ip route del 10.30.0.0/16

These disappear at reboot; make them permanent in netplan, NetworkManager (nmcli connection modify … +ipv4.routes "10.30.0.0/16 192.168.1.254") or systemd-networkd.

Static routingDynamic routing
Configured by handRouters learn routes from each other
No protocol overhead, fully predictableUses some CPU, memory and bandwidth
Does not react to failures (unless tracked)Reroutes around failures automatically
Fine for small or stub networks, default routesNeeded for anything with redundancy or many networks
Easy to make mistakes at scaleScales to the whole internet

A stub network (a branch with one way out) only needs a default route. A floating static route with a higher administrative distance makes a cheap backup: it only appears if the dynamic route vanishes.

Dynamic routing: two families

Distance vector

Each router tells its neighbors “here are the networks I can reach and how far away they are”. Neighbors add their own cost and pass it on. Every router knows the distance and direction (vector) to each network, but not the topology. This is the Bellman-Ford approach.

  • Simple and light.
  • Slow to converge after failures, and prone to routing loops and the “count to infinity” problem.
  • Fixes: split horizon (do not advertise a route back where you learned it), route poisoning (advertise a dead route with an infinite metric), hold-down timers, and a maximum hop count (RIP gives up at 16).
  • Example: RIP. EIGRP is an advanced distance vector protocol that fixed most of these problems.

Link state

Each router floods a description of its own links to every router in the area. Every router builds the same full map of the network (the link-state database) and runs Dijkstra’s shortest path first algorithm to compute the best path to every destination.

  • Fast convergence, no loops by design, every router sees the whole picture.
  • Needs more memory and CPU, and careful design (areas or levels) on big networks.
  • Examples: OSPF and IS-IS.
Distance vectorLink state
SharesIts routing table, with neighborsIts links, with everyone in the area
KnowsDistance and directionThe full topology
AlgorithmBellman-FordDijkstra (SPF)
ConvergenceSlowFast
ExamplesRIP (EIGRP is an advanced variant)OSPF, IS-IS

BGP, the protocol between internet providers, is a third kind: path vector. It shares the full list of networks (autonomous systems) a route crosses and chooses paths by policy rather than shortest distance. Lesson 12 covers all of these protocols.

Interior and exterior

  • IGP (interior gateway protocol): routing inside one organization: OSPF, IS-IS, EIGRP, RIP.
  • EGP (exterior gateway protocol): routing between organizations: BGP.

Watch routing happen

traceroute and mtr show the routers along a path. ip route get tells you the first hop. On a Linux router running FRRouting, vtysh -c 'show ip route' adds the source protocol of each route:

$ sudo vtysh -c 'show ip route'
Codes: K - kernel route, C - connected, S - static, R - RIP,
       O - OSPF, B - BGP, > - selected route, * - FIB route

S>* 0.0.0.0/0 [1/0] via 203.0.113.1, eth0, 2d01h
C>* 10.0.12.0/30 is directly connected, eth1, 2d01h
O>* 10.0.34.0/30 [110/20] via 10.0.12.2, eth1, 01:12:09
B>* 198.51.100.0/24 [20/0] via 203.0.113.1, eth0, 2d00h

The numbers in brackets are [administrative distance/metric]: [110/20] is an OSPF route with cost 20.

Linux as a router

A Linux machine only forwards packets between interfaces if you allow it:

$ sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 0
$ sudo sysctl -w net.ipv4.ip_forward=1
net.ipv4.ip_forward = 1

Make it permanent in /etc/sysctl.d/. Docker and Kubernetes turn this on for you, which is how containers reach the outside world.

Exercise

A router has routes for 10.0.0.0/8, 10.1.0.0/16 and 10.1.2.0/24. Which one does it use for 10.1.2.77?

  1. 10.0.0.0/8, because it was added first
  2. 10.1.0.0/16, because it is in the middle
  3. 10.1.2.0/24, the longest matching prefix
  4. All three, with ECMP
Show answer

Answer: C. The longest prefix match wins. Order of entries does not matter, and ECMP only applies to equal prefixes with equal cost.

Exercise

A router learns 172.16.0.0/16 from both OSPF and RIP. With default administrative distances, which route goes into the table?

  1. The OSPF route, because 110 is lower than 120
  2. The RIP route, because RIP is older
  3. Whichever arrived first
  4. The one with the lower hop count
Show answer

Answer: A. For the same prefix, the source with the lower administrative distance wins. Metrics are only compared within one protocol.

Exercise

Which statement describes link-state routing?

  1. Routers share their full routing tables only with direct neighbors
  2. Routers pick paths by business policy
  3. It counts hops and gives up at 16
  4. Every router floods its links, builds a full map and runs Dijkstra
Show answer

Answer: D. OSPF and IS-IS flood link-state information so every router has the same topology. Sharing tables with neighbors is distance vector; policy-based is BGP.

Key takeaways

  • A routing table maps prefixes to next hops; the default route catches everything else.
  • The longest matching prefix wins; then administrative distance; then metric.
  • ip route get <address> tells you exactly which route the kernel will use.
  • Static routes are simple but do not react to failures; dynamic protocols reroute automatically.
  • Distance vector shares tables with neighbors; link state shares links with everyone; BGP is path vector.

FAQ

What is the difference between routing and forwarding?

Routing is building the table (by hand or with protocols). Forwarding is the per-packet act of looking up the destination and sending it out. Routers do routing in software and forwarding in hardware, as fast as possible.

Why does my laptop have two default routes?

One for each connected interface, such as Ethernet and Wi-Fi. The lower metric wins, which is why plugging in a cable usually takes over from Wi-Fi.

What is policy-based routing?

Choosing a route by more than the destination: source address, port or a firewall mark. Linux does it with ip rule and multiple routing tables, for example to send one application through a VPN.

What is a route “flap”?

A route that keeps appearing and disappearing, usually because of an unstable link. Each change makes routers recompute, so protocols dampen flapping routes and summarization hides them from the rest of the network.

↑ ↓ navigate Enter open Esc close