Sub topic 13
How Routes Are Learned: Distance Vector, Link State, and BGP
This chapter assumes you can read a routing table, know how longest prefix match picks a route, and understand the difference between the control plane and the data plane. It covers how routes get into the table without an administrator typing each one.
Why static routes stop scaling
A static route is a route an administrator enters by hand. It works well for a small stub network with one exit. It fails when the network grows or changes, for two reasons:
- Every router needs an entry for every prefix. Adding a subnet means editing many devices.
- Static routes do not react to failures. If the next hop dies, the entry stays in the table and packets are sent into a dead end.
A dynamic routing protocol is software on each router that exchanges reachability information with neighbors, computes routes, and updates the table when the topology changes. These protocols are part of the control plane. They do not forward user packets. They decide which entries the data plane will use.
Dynamic protocols fall into three families, which differ in what routers tell each other:
| Family | What a router advertises | Typical examples | Typical scope |
|---|---|---|---|
| Distance vector | Its own table: prefix and cost | RIP, EIGRP | Small networks |
| Link state | The state of its own links | OSPF, IS-IS | Inside one organization |
| Path vector | Prefix plus the list of networks to cross | BGP | Between organizations |
Interior and exterior protocols
An autonomous system (AS) is a set of networks under one administrative control that presents a single routing policy to the internet. Each AS has an AS number (ASN).
- An interior gateway protocol (IGP) runs inside one AS. Its goal is to find the best path quickly. RIP, OSPF, and IS-IS are IGPs.
- An exterior gateway protocol runs between ASes. Its goal is to apply business policy and scale to the whole internet. BGP is the only one in use.
Distance vector
In a distance vector protocol, each router tells its neighbors what it can reach and how far away it is. The router does not know the whole topology. It only knows "I can reach this prefix through that neighbor at this cost."
The Bellman-Ford rule: for each prefix, the cost through neighbor N is the cost to N plus the cost N advertises. Keep the neighbor with the lowest total.
RIP uses hop count as its cost. A route with 16 hops is treated as unreachable, so RIP cannot be used in a network with a diameter larger than 15 hops.
The following program runs the update rule over a small line of routers A - B - C, where C owns the prefix 10.3.0.0/16.
INFINITY = 16
# links[router] = list of neighbors; every link costs 1
links = {"A": ["B"], "B": ["A", "C"], "C": ["B"]}
# table[router][prefix] = (cost, next_hop)
table = {r: {} for r in links}
table["C"]["10.3.0.0/16"] = (0, "connected")
def exchange_round():
changed = False
for router, neighbors in links.items():
for neighbor in neighbors:
# The neighbor advertises its table to this router.
for prefix, (cost, _) in table[neighbor].items():
new_cost = min(cost + 1, INFINITY)
if new_cost >= INFINITY:
continue
old = table[router].get(prefix)
if old is None or new_cost < old[0]:
table[router][prefix] = (new_cost, neighbor)
changed = True
return changed
rounds = 0
while exchange_round():
rounds += 1
print("rounds with changes:", rounds)
for router in sorted(table):
print(router, table[router])After the loop, A holds (2, 'B') for the prefix and B holds (1, 'C'). Information moves one hop per exchange, so news travels slowly across a large network. This is the convergence time: the time until all routers agree on a consistent view after a change.
Failure modes of distance vector
Routers trust what neighbors say, and they cannot verify it. This produces the count-to-infinity problem. If C's link fails, B may still hear from A that A can reach the prefix at cost 2. B then installs a route through A at cost 3. A then hears B's new cost and moves to 4, and the two routers raise each other's cost step by step until they reach the maximum. Packets loop between them for that whole time.
Common mitigations:
- Split horizon: do not advertise a route back out the interface you learned it from.
- Poison reverse: advertise it back, but with an infinite cost.
- Hold-down timers: ignore changes to a failed route for a period.
- A small maximum metric, such as 16 in RIP, to limit how long a loop can last.
These reduce the problem but do not remove it in every topology. This is the main reason distance vector is limited to small networks.
Link state
In a link-state protocol, each router describes only its own links, such as neighbors, interfaces, and link costs. It floods that description to every router in the area. Every router then holds the same database of the whole topology and runs its own shortest-path computation on it.
- Routers discover neighbors with Hello packets.
- Each router builds a link-state advertisement (LSA in OSPF, LSP in IS-IS) listing its links.
- Advertisements are flooded reliably to all routers in the area.
- Each router builds the same link-state database.
- Each router runs Dijkstra's shortest path first (SPF) algorithm with itself as the root.
- The resulting best next hops are installed in the routing table.
OSPF cost is derived from interface bandwidth, so faster links are preferred by default. When a link fails, the router next to it floods a new advertisement. Every router reruns SPF and converges in a fraction of a second in a well-tuned network. Because every router has the full map, loops from stale information are far less common than in distance vector. Short-lived loops can still occur while routers hold different versions of the database.
Costs of link state
- Memory and CPU grow with the size of the database and the number of SPF runs.
- Flooding adds control traffic after every change.
- Large networks are split into areas. OSPF uses area 0 as the backbone, and other areas attach to it. Routers summarize routes at area borders, which limits how far detail spreads.
BGP: path vector between networks
The Border Gateway Protocol (BGP) carries the internet's global routing table. It runs over TCP port 179. Neighbors are configured explicitly, rather than discovered. Two kinds of sessions exist:
- eBGP: between routers in different ASes.
- iBGP: between routers in the same AS, used to spread externally learned routes internally.
BGP is a path vector protocol. Each advertisement includes the prefix and the AS_PATH, the ordered list of ASes the advertisement has crossed. When a router sends a route to an eBGP neighbor, it prepends its own ASN.
AS_PATH gives BGP two properties:
- Loop prevention. A router that sees its own ASN in a received AS_PATH discards the route.
- A policy hook. Operators can filter on or prefer routes by path.
Policy over shortest path
BGP does not look for the fastest path. It applies the operator's policy, which usually reflects contracts: customers, providers, and peers. Among several routes to the same prefix, a router compares attributes in a fixed order. A simplified order is:
- Highest LOCAL_PREF, a value an operator sets inside an AS to prefer one exit.
- Shortest AS_PATH.
- Lowest MED, a hint from a neighboring AS.
- eBGP routes over iBGP routes.
- Lowest IGP cost to the BGP next hop.
- Tie-breakers such as router ID.
Real implementations have more steps, and vendors add some. Because LOCALPREF is first, an operator can send traffic through a cheaper link even when a shorter ASPATH exists elsewhere.
BGP and the IGP work together
An iBGP-learned route carries a next hop that is outside the AS. The router must resolve that next hop using the IGP to find how to reach it. So the IGP supplies internal reachability, and BGP supplies external prefixes. BGP does not replace the IGP.
Failure modes of BGP
- Convergence is slow, from seconds to minutes, because of timers and the size of the table.
- BGP trusts what neighbors announce. A network that announces a prefix it does not own, by mistake or on purpose, can attract traffic meant for others. This is a route leak or a prefix hijack. Mitigations include prefix filters, and RPKI origin validation, which checks that an AS is authorized to originate a prefix.
- Longest prefix match still applies after selection. A more specific announcement such as a /24 beats a covering /16 from the legitimate owner, which is why hijackers often announce more specific prefixes.
Putting routes from different sources together
A router may learn the same prefix from several sources, such as a static route, OSPF, and BGP. Metrics from different protocols cannot be compared, because a RIP hop count and an OSPF cost mean different things. The router first prefers the source with the lowest administrative distance, a trust ranking assigned to each source. The chapter on metrics and administrative distance covers that ranking. Within one protocol, the protocol's own metric decides.
Longest prefix match comes first. A /24 learned from RIP beats a /16 learned from OSPF, whatever their distances, because the prefix length is compared before source preference.
Comparing the three approaches
| Property | Distance vector | Link state | Path vector (BGP) |
|---|---|---|---|
| Knowledge of topology | Neighbors' summaries only | Full map of the area | Paths per prefix, not a map |
| Algorithm | Bellman-Ford | Dijkstra SPF | Policy-based best-path rules |
| Convergence | Slow | Fast | Slow |
| Loop risk | Higher | Low | Low (AS_PATH check) |
| Main goal | Simplicity | Fast, accurate paths | Policy and scale |
Choosing a protocol
- A small office with one or two exits: static routes, often with a default route.
- A campus or data center under one administration: a link-state IGP such as OSPF or IS-IS.
- Connecting to an ISP or running more than one upstream: BGP.
- Legacy or tiny networks: RIP is still seen, but there is little reason to choose it now.
Inspecting learned routes on Linux
Routing daemons such as FRRouting install their routes into the kernel. The kernel marks each route with the protocol that installed it.
# Show routes with the protocol that installed each one
ip route show
# Example lines you might see:
# 10.2.0.0/16 via 192.0.2.2 dev eth0 proto ospf metric 20
# 203.0.113.0/24 via 192.0.2.9 dev eth1 proto bgp metric 20
# Show only routes learned through OSPF
ip route show proto ospfA route marked proto static was added by hand. A route marked proto kernel is a connected network.
Exercise
Router R receives two advertisements for 198.51.100.0/24. One arrives from OSPF with cost 30. The other arrives from BGP with an ASPATH of three ASes. Router R also has a BGP route for 198.51.0.0/16 with an ASPATH of one AS. A packet arrives for 198.51.100.7. Which route forwards it, and which rule decides between the two /24 routes?
Show answer
The /24 route is used, because longest prefix match compares prefix length before anything else, so the /16 is not considered. Between the two /24 routes, the metrics are not comparable, since OSPF cost and BGP AS_PATH length mean different things. Router R prefers the source with the lower administrative distance. With typical defaults, OSPF (110) beats external BGP (20) only if its distance is lower, and here eBGP at 20 is lower than OSPF at 110, so the BGP route is installed. The exact values depend on vendor configuration.
Exercise
In a distance vector network, router B loses its link to C, which owns a prefix. A still advertises a route to that prefix with cost 2. Describe what goes wrong and name two techniques that reduce the problem.
Show answer
B may accept A's advertisement and install a route through A at cost 3. A then learns B's cost and raises its own to 4, and the two keep increasing each other's cost until the maximum metric, which is count to infinity. Packets loop between A and B in the meantime. Split horizon (A does not advertise the route back to B, since it learned it from B's side) and hold-down timers both reduce this, as do poison reverse and a low maximum metric.