Dynamic Routing (OSPF & BGP)
OSPF, BGP, and how the internet finds paths
Why Dynamic Routing?
- No automatic failover: if a link goes down, the static route still points to the dead next-hop until an admin changes it
- Doesn't scale: 100 routers x 100 networks = 10,000 manual route entries to manage
- Error-prone: one typo can create a routing loop or black hole
- No load balancing: can't dynamically distribute traffic across equal-cost paths
- Automatic convergence: routers detect failures and recalculate paths in seconds (or milliseconds)
- Scales to millions of routes: BGP handles the full internet routing table (~1M prefixes)
- Loop prevention: built into the protocol algorithms (split horizon, SPF, AS path)
- ECMP: equal-cost multipath — automatically load-balance across parallel links
IGP vs EGP
Routes within a single Autonomous System (AS).
- OSPF (Open Shortest Path First) — link-state
- IS-IS (Intermediate System to Intermediate System) — link-state
- EIGRP (Enhanced Interior Gateway Routing Protocol) — advanced distance-vector (Cisco proprietary)
- RIP (Routing Information Protocol) — distance-vector (legacy, rarely used)
Optimized for fast convergence within a single administrative domain.
Routes between Autonomous Systems.
- BGP (Border Gateway Protocol) — the only EGP in use today
Optimized for policy control, scalability, and stability across organizational boundaries. Slower convergence by design — prioritizes stability over speed.
An AS (Autonomous System) is a network or group of networks under a single administrative entity, identified by an ASN (AS Number). Examples: Google (AS15169), Cloudflare (AS13335), AWS (AS16509).
Routing Algorithm Categories
| Category | How It Works | Protocols | Pros | Cons |
|---|---|---|---|---|
| Distance-Vector | Each router shares its entire routing table with direct neighbors. Routes include a distance (metric) and direction (next-hop). "Routing by rumor." | RIP, EIGRP | Simple, low memory | Slow convergence, count-to-infinity problem, limited scalability |
| Link-State | Each router floods its link states (direct connections) to all routers. Every router builds a complete topology map, then independently runs Dijkstra's SPF algorithm. | OSPF, IS-IS | Fast convergence, loop-free, complete visibility | Higher memory/CPU (stores full topology), complex implementation |
| Path-Vector | Like distance-vector, but includes the full AS path to each destination. If a router sees its own AS in the path, it rejects the route (loop prevention). | BGP | Scales to the internet, policy-rich, loop-free across ASes | Slow convergence (by design), complex configuration |
OSPF (Open Shortest Path First)
The most widely deployed IGP. A link-state protocol where every router in an area has a complete, identical view of the network topology.
Core Concept: Link-State Database
How OSPF Builds the Topology
- Discover neighbors: Routers send Hello packets on each OSPF-enabled interface (multicast
224.0.0.5). Routers that share a link and agree on parameters (area ID, hello/dead timers, authentication) become neighbors. - Form adjacencies: Neighbors exchange their complete Link-State Databases (LSDBs) using Database Description (DBD) packets, then fetch missing/newer LSAs with Link-State Request/Update.
- Flood LSAs: Each router generates LSAs describing its own links and floods them to all routers in the area. Every router ends up with an identical LSDB.
- Run SPF: Each router independently runs Dijkstra's Shortest Path First algorithm on the LSDB to build a shortest-path tree rooted at itself.
- Install routes: The SPF tree determines the best next-hop for each destination. These are installed in the routing table (FIB).
OSPF Areas
Large OSPF deployments are divided into areas to limit the scope of LSA flooding and reduce the size of the LSDB. Every OSPF network must have Area 0 (the backbone), and all other areas must connect to it.
Area Types
| Area Type | Description | LSAs Allowed |
|---|---|---|
| Backbone (Area 0) | All inter-area traffic must transit Area 0. All ABRs connect to it. | All types |
| Standard area | Normal non-backbone area. Receives all LSA types. | Types 1, 2, 3, 4, 5 |
| Stub area | No external routes (Type 5 LSAs). ABR injects a default route instead. Reduces LSDB size for areas with a single exit. | Types 1, 2, 3 (+ default) |
| Totally stubby | No external AND no inter-area summary routes. Only intra-area routes + default. Maximum LSDB reduction. | Types 1, 2 (+ default) |
| NSSA (Not-So-Stubby Area) | Like stub, but allows importing external routes into the area as Type 7 LSAs. Useful when a stub area has a local ASBR. | Types 1, 2, 3, 7 |
LSA Types
| LSA Type | Name | Generated By | Scope | Describes |
|---|---|---|---|---|
| 1 | Router LSA | Every router | Intra-area | All of a router's links and their costs within an area |
| 2 | Network LSA | DR (Designated Router) | Intra-area | All routers connected to a multi-access segment (e.g., Ethernet) |
| 3 | Summary LSA | ABR (Area Border Router) | Inter-area | Routes to networks in other areas (summary of Type 1/2 info) |
| 4 | ASBR Summary LSA | ABR | Inter-area | How to reach an ASBR in another area |
| 5 | AS External LSA | ASBR (AS Boundary Router) | AS-wide | Routes redistributed from other protocols (BGP, static, etc.) |
| 7 | NSSA External LSA | ASBR in NSSA | NSSA only | Like Type 5, but confined to NSSA. ABR converts to Type 5 at the border. |
SPF Algorithm (Dijkstra)
How Dijkstra Builds the Shortest Path Tree
Given the complete LSDB, each router runs SPF independently:
- Start with the root node (self). Set cost to self = 0, cost to all others = infinity.
- Examine all links from the current node. For each neighbor, calculate total cost (current cost + link cost). If this is less than the neighbor's current known cost, update it.
- Mark current node as "visited." Move to the unvisited node with the lowest cost.
- Repeat steps 2-3 until all reachable nodes are visited.
- The resulting tree gives the shortest path (lowest total cost) from this router to every other router in the area.
Hello Protocol & DR/BDR
OSPF Hello Parameters
| Parameter | Default (broadcast) | Description |
|---|---|---|
| Hello interval | 10 seconds | How often Hello packets are sent |
| Dead interval | 40 seconds (4x Hello) | If no Hello received within this time, neighbor is declared dead |
| Router ID | Highest loopback IP or highest interface IP | Unique 32-bit identifier for each OSPF router |
DR/BDR Election (Multi-Access Networks)
On broadcast/multi-access segments (Ethernet), full-mesh adjacencies between N routers would create N*(N-1)/2 adjacencies. Instead, OSPF elects:
- DR (Designated Router): All routers form adjacencies with the DR (not with each other). The DR represents the segment with a Network LSA (Type 2). Elected by highest OSPF priority (default 1), then highest Router ID.
- BDR (Backup Designated Router): Takes over if the DR fails. Second-highest priority.
- DROther: All other routers. They only form full adjacencies with DR and BDR, not with each other.
Updates are sent to 224.0.0.6 (DR/BDR listen) and the DR floods to 224.0.0.5 (all OSPF routers).
OSPF Cost Metric
OSPF cost is inversely proportional to bandwidth:
Cost = Reference Bandwidth / Interface Bandwidth
Default reference bandwidth = 100 Mbps (10^8 bps)| Link Type | Bandwidth | Default Cost | With 10G Reference |
|---|---|---|---|
| 10 Gbps | 10,000 Mbps | 1 (capped at 1) | 1 |
| 1 Gbps | 1,000 Mbps | 1 (capped at 1) | 10 |
| 100 Mbps | 100 Mbps | 1 | 100 |
| 10 Mbps | 10 Mbps | 10 | 1,000 |
| T1 (1.544 Mbps) | 1.544 Mbps | 64 | 6,477 |
Warning
Common pitfall: With the default 100 Mbps reference, both 1 Gbps and 10 Gbps links have cost = 1. OSPF can't distinguish between them. Always increase the reference bandwidth in modern networks: auto-cost reference-bandwidth 10000 (on all routers in the area).
BGP (Border Gateway Protocol)
BGP is the routing protocol of the internet. It connects Autonomous Systems (ISPs, cloud providers, enterprises) and carries the full global routing table. Version 4 (BGP-4, RFC 4271) is the current standard.
Path-Vector Protocol
BGP doesn't know the complete topology. Instead, it tracks the sequence of ASes a route has traversed. If a router sees its own ASN in the AS_PATH, it rejects the route — this prevents inter-domain loops.
AS_PATH Example
# Google DNS (8.8.8.0/24) as seen from different vantage points:
# From AS 64500 (your network):
8.8.8.0/24 AS_PATH: 64500 3356 15169
^ ^
| Google's AS
Level 3 (transit)
# From a different vantage point:
8.8.8.0/24 AS_PATH: 64501 1299 15169
^ ^
| Google's AS
Telia (different transit)
# Shorter AS_PATH is generally preferredAS Numbers
| Range | Type | Description |
|---|---|---|
| 1 — 64,495 | 16-bit public | Assigned by RIRs (ARIN, RIPE, APNIC, etc.) for use on the global internet |
| 64,496 — 64,511 | 16-bit documentation | Reserved for documentation/examples |
| 64,512 — 65,534 | 16-bit private | For internal/private use. Like RFC 1918 for ASNs. Used in K8s Calico BGP peering. |
| 65,535 | Reserved | Not assignable |
| 131,072 — 4,199,999,999 | 32-bit (extended) | RFC 6793. Written as plain integers. 16-bit ASN space was running out (similar to IPv4 exhaustion). |
Tip
Calico BGP: In K8s with Calico, each node typically uses a private ASN (64512-65534) and peers with a ToR switch or route reflector. calicoctl node status shows BGP peering state. MetalLB can also announce LoadBalancer IPs via BGP.
eBGP vs iBGP
- Peering between different AS numbers
- Typically over directly connected links
- TTL = 1 by default (must be neighbors)
- AS_PATH is modified (prepends local ASN)
- Next-hop is updated to the eBGP peer's address
- AD = 20 (highly trusted)
- Learned routes are advertised to all peers
- Peering within the same AS
- Can peer with any router in the AS (uses loopback IPs, IGP provides reachability)
- TTL = 255 (multihop by default)
- AS_PATH is NOT modified
- Next-hop is NOT changed (must resolve via IGP)
- AD = 200 (less trusted than OSPF)
- iBGP split-horizon: routes learned from an iBGP peer are NOT advertised to other iBGP peers (requires full mesh or route reflectors)
BGP Peering
BGP Session Establishment
BGP uses TCP port 179 for reliable transport. Peers are explicitly configured (no autodiscovery like OSPF Hello).
# FRRouting (common on Linux) BGP config example
router bgp 64512
bgp router-id 10.0.0.1
neighbor 10.0.0.2 remote-as 64513 # eBGP peer (different AS)
neighbor 10.0.0.3 remote-as 64512 # iBGP peer (same AS)
!
address-family ipv4 unicast
network 192.168.1.0/24 # Originate this prefix
neighbor 10.0.0.2 route-map IMPORT-POLICY in
neighbor 10.0.0.2 route-map EXPORT-POLICY out
exit-address-familyBGP Route Selection
When BGP receives multiple routes to the same prefix, it applies this decision process in order. The first criterion that breaks the tie wins.
| Step | Criterion | Prefer | Who Controls It |
|---|---|---|---|
| 1 | Weight (Cisco-specific) | Highest | Local router config (not propagated) |
| 2 | Local Preference | Highest | iBGP attribute. Set by inbound policy. Used to prefer one ISP over another for outbound traffic. |
| 3 | Locally originated | Yes | Routes originated by this router via network or redistribute |
| 4 | AS_PATH length | Shortest | Determined by internet topology. Can be manipulated with AS_PATH prepending. |
| 5 | Origin | IGP > EGP > Incomplete | How the route was introduced into BGP (rarely a tiebreaker) |
| 6 | MED (Multi-Exit Discriminator) | Lowest | Set by the neighboring AS to indicate preferred entry point. Only compared between routes from the same neighboring AS. |
| 7 | eBGP > iBGP | eBGP | Prefer routes learned from external peers |
| 8 | IGP metric to next-hop | Lowest | Closest exit point ("hot potato routing") |
| 9 | Router ID | Lowest | Final tiebreaker |
Note
Most common tiebreakers in practice: Local Preference (step 2) for outbound traffic engineering, AS_PATH length (step 4) as the natural metric, and MED (step 6) for inbound traffic engineering. Most other steps rarely matter.
BGP and the Internet
The Global Routing Table
- The full BGP table contains approximately 1,000,000+ IPv4 prefixes and 200,000+ IPv6 prefixes (as of 2026)
- A "full table" router (transit provider, IXP participant) must store all of these
- Edge routers often only take a default route from their upstream provider (no need for full table if you only have one exit)
- Route reflectors and confederations solve the iBGP full-mesh scaling problem (N routers would need N*(N-1)/2 iBGP sessions)
BGP Hijacking
BGP has no built-in authentication of route origins. Any AS can announce any prefix. If the announcement is more specific (longer prefix) or propagates faster, traffic will follow the hijacked route.
Notable BGP Hijack Incidents
| Year | Incident | What Happened |
|---|---|---|
| 2008 | YouTube / Pakistan Telecom | Pakistan Telecom announced 208.65.153.0/24 (YouTube's prefix) to comply with a government censorship order, but the announcement leaked to the global internet via PCCW. YouTube was unreachable worldwide for ~2 hours. The hijacked /24 was more specific than YouTube's /22, winning via longest prefix match. |
| 2018 | AWS Route 53 / MyEtherWallet | An attacker hijacked an AWS-owned prefix (205.251.192.0/24) used by Route 53 DNS servers. DNS queries for MyEtherWallet.com were redirected to a phishing site. Approximately $150K in cryptocurrency was stolen. Attack originated from an ISP in Columbus, Ohio (eNet/AS10297). |
| 2019 | Google traffic via China Telecom | China Telecom (AS4134) leaked internal routes, causing Google, Cloudflare, and AWS traffic to route through China for ~2 hours. Likely accidental, but demonstrated the fragility of BGP trust. |
| 2024 | Orange Espana | Attacker gained access to Orange Spain's RIPE account via stolen credentials, modified their RPKI ROAs, and caused BGP route validation failures that disrupted the carrier's traffic. |
Defenses: RPKI + ROV
- RPKI (Resource Public Key Infrastructure): Cryptographically binds IP prefixes to authorized ASNs using ROAs (Route Origin Authorizations). Prefix owners create ROAs signed by RIR certificates.
- ROV (Route Origin Validation): Routers validate received BGP announcements against RPKI data. Routes that fail validation (the announcing AS doesn't match the ROA) are marked as "Invalid" and can be rejected.
- BGPsec: Extends RPKI to validate the entire AS_PATH (not just origin). Cryptographic signatures at each hop. Not widely deployed due to performance and incremental deployment challenges.
- IRR (Internet Routing Registry): Older, non-cryptographic approach. Operators register their prefixes and routing policies. Used for generating prefix filters. Less secure than RPKI (no crypto verification).
Warning
Reality check: As of 2026, RPKI adoption is growing but not universal. Major networks (Cloudflare, Google, AWS, most Tier-1 ISPs) validate routes, but many smaller networks don't. "Invalid" routes from non-RPKI-validating networks can still propagate. The internet's routing security remains a work in progress.
Protocol Comparison Summary
| Feature | OSPF | IS-IS | BGP | RIP (legacy) |
|---|---|---|---|---|
| Type | Link-state IGP | Link-state IGP | Path-vector EGP | Distance-vector IGP |
| Algorithm | Dijkstra SPF | Dijkstra SPF | Best path selection | Bellman-Ford |
| Transport | IP protocol 89 | Directly over L2 (CLNP) | TCP port 179 | UDP port 520 |
| Metric | Cost (bandwidth-based) | Cost (configurable) | Multiple attributes | Hop count (max 15) |
| Convergence | Fast (sub-second with BFD) | Fast (sub-second with BFD) | Slow (minutes) by design | Slow (minutes) |
| Scalability | Thousands of routes per area | Larger than OSPF (flatter, less overhead) | Millions of routes (internet-scale) | Very limited |
| Hierarchy | Areas (backbone + non-backbone) | Levels (L1 / L2) | ASes (flat within, hierarchical between) | None |
| AD (Cisco) | 110 | 115 | 20 (eBGP) / 200 (iBGP) | 120 |
| Common use | Enterprise, campus, DC underlay | ISP backbone, large DCs (Facebook uses IS-IS) | Internet peering, DC overlay, K8s (Calico) | Almost never (legacy only) |
Tip
Data center context: Modern DC designs often use BGP as the IGP in a Clos/leaf-spine topology. Each switch runs BGP with a unique private ASN. This is called "BGP-on-the-host" or "eBGP underlay" — simpler than OSPF for large, uniform DC fabrics. Facebook, Microsoft, and many hyperscalers use this approach. In K8s, Calico leverages this by having nodes peer directly with ToR switches via BGP.