Difficulty: Intermediate
Explain routing basics. Compare static and dynamic routing, and distance vector with link state protocols (RIP, OSPF, BGP).
A router's job is simple to state and hard to do at scale: for each incoming packet, pick the best next hop toward the destination. To do that it consults its routing table, which maps destination prefixes to next hops and interfaces. When several entries match, the longest prefix wins. The interesting question is how the table gets filled.
Static routing means the administrator types the routes by hand, for example ip route 10.20.0.0 255.255.0.0 192.168.1.2. It is simple, predictable, has no protocol overhead and is secure, which makes it fine for small networks and stub sites, and for default routes (0.0.0.0/0) toward the ISP. But it does not adapt: if a link fails, traffic keeps going to the dead next hop unless you add floating static routes with a higher administrative distance. Dynamic routing uses routing protocols so routers discover each other, exchange information and automatically recompute paths when topology changes. It scales, but consumes CPU, memory and bandwidth and has to be configured and secured.
Dynamic protocols are grouped by scope. Interior Gateway Protocols (RIP, OSPF, EIGRP, IS-IS) run inside one autonomous system, an organisation's network. The Exterior Gateway Protocol, BGP, runs between autonomous systems and is the glue of the internet.
Distance vector protocols, like RIP, are the neighbour gossip approach. Each router periodically tells its directly connected neighbours its whole routing table: I can reach network X in 3 hops. It only knows distances and directions, not the whole map, which is why it is called routing by rumour. RIP uses hop count as the metric with a maximum of 15 (16 means unreachable), and uses the Bellman-Ford algorithm. Its weakness is slow convergence and the count-to-infinity problem, where routers keep incrementing a metric for a dead route as they pass it back and forth. Mitigations include split horizon (do not advertise a route back out the interface it was learned from), route poisoning, and hold-down timers.
Link state protocols, like OSPF, work differently. Each router floods information about its own directly connected links (link state advertisements) to all routers in the area. Every router builds an identical map, the link state database, and independently runs Dijkstra's shortest path first algorithm to compute the best tree. OSPF uses cost, based on bandwidth, as the metric. It converges much faster and scales better with areas, but needs more memory and CPU. Large networks are divided into areas around a backbone area 0 to limit flooding.
BGP is technically a path vector protocol: it advertises the full AS path to each prefix, which prevents loops (if you see your own AS number in the path, reject it) and allows policy-based decisions such as prefer this provider or avoid that country, rather than purely shortest path. A famous real-world reminder of BGP's importance: misconfigured BGP announcements have taken large websites offline for hours. When several protocols offer a route to the same prefix, the router uses administrative distance to choose: directly connected 0, static 1, OSPF 110, RIP 120.
$ ip route
default via 192.168.1.1 dev eth0 proto dhcp metric 100
10.20.0.0/16 via 192.168.1.2 dev eth0 metric 200
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.23
A packet to 10.20.5.7 matches the /16 (more specific than default) and goes via 192.168.1.2. Anything unmatched uses the default route.
Static Routing, Dynamic Routing, Distance Vector, Link State, OSPF