net/go.book
All Parts Marketing Resources PDF

Routing Between Networks: Longest Prefix, OSPF, and BGP

"Good, Fast, Cheap: Pick any two (you can't have all three)." — RFC 1925, The Twelve Networking Truths (1996)

A switch forwards frames inside one network. A router forwards packets between networks, and it does so with no map of the whole internet. Each router knows only a table of destinations and next hops, and every packet is decided one hop at a time. The internet is what emerges when thousands of those local decisions agree well enough to get a packet across the planet.

Routing is where networking stops being a wiring problem and becomes a policy problem. The shortest path is rarely the whole answer: a network may prefer a cheaper provider, a longer path with better latency, or a route that keeps traffic inside a region. This chapter builds the vocabulary to read those decisions: the longest-prefix rule every router uses, the protocols that populate the table inside an organization, and BGP, the protocol that stitches organizations together.

Longest Prefix Wins

Every router has a routing table, also called the RIB (Routing Information Base). Each row says: to reach this network, send the packet to this next hop. When a packet arrives, the router compares the destination IP against every row and picks the most specific match — the longest prefix.

Destination          Next hop        Interface
0.0.0.0/0            203.0.113.1     wan0      (default route)
10.0.0.0/8           10.0.0.1        lan0
10.1.0.0/16          10.1.0.1        lan1
10.1.2.0/24          10.1.2.1        lan2

A packet to 10.1.2.55 matches all four rows, so the /24 wins: it is the most specific statement the router has. A packet to 10.9.9.9 matches only /8 and the default; /8 wins. A packet to 8.8.8.8 matches only the default route, which is how most homes reach the internet: "if you know nothing more specific, send it upstream."

That is the entire forwarding decision. The router compiles the RIB into a FIB (Forwarding Information Base), a hardware-friendly structure — often a TCAM or a compressed trie — that can answer "longest match" at line rate without scanning a million rows. A more specific route always beats a less specific one, no matter what metric the routing protocol attached to it. Prefix length is king.

Metrics, Administrative Distance, and Trust

When two protocols offer a route to the same prefix, a router needs a tiebreaker before it even compares metrics. Cisco devices call this administrative distance: connected routes win over static routes, static routes win over OSPF, OSPF wins over RIP, and so on. The values are vendor-specific, but the idea is universal: a router trusts some sources more than others. Metrics only compare routes from the same protocol.

This is why a static route can override a dynamic one, and why a floating static route — a static route with a deliberately worse administrative distance — is a common backup path. It is also why "the route exists but traffic goes the other way" is usually not a bug: it is policy expressed as preference.

Static, Default, and Dynamic

A static route is typed by an administrator and never changes on its own. It is perfect for a small network, a stub site, or a deliberate override. It is terrible for anything that changes: when a link fails, a static route keeps pointing at a dead next hop until a human notices.

A default route (0.0.0.0/0) is a special static route meaning "everything else." Home routers use it to send all non-local traffic to the ISP.

Dynamic routing protocols let routers exchange reachability information and converge automatically after a failure. They differ in what they optimize and how far they scale. Two families matter:

  • Interior Gateway Protocols run inside one organization: OSPF, IS-IS, EIGRP.
  • Exterior Gateway Protocols run between organizations: BGP.

OSPF: A Shared Map of the Area

OSPF (RFC 2328 for IPv4, RFC 5340 for IPv6) is a link-state protocol. Every router floods information about its own links — who it is connected to, and at what cost — and every router builds the same map of the area. Each then runs Dijkstra's algorithm on that map to compute the shortest path to every destination.

Cost is derived from bandwidth: cost = reference bandwidth / interface bandwidth. With the common 100 Mbps reference, a 100 Mbps link costs 1 and a 10 Mbps link costs 10; with a 1 Gbps reference, a 1 Gbps link costs 1 and a 100 Mbps link costs 10. The reference is an implementation choice, so check it before comparing costs across vendors.

OSPF scales by splitting a large network into areas. Area 0 is the backbone; other areas connect to it through area border routers. Routers inside an area only need the map of that area, not the whole organization, which keeps the database and the computation manageable. On a multi-access segment, routers elect a designated router and a backup to reduce the number of adjacencies and the amount of flooding.

BGP: The Protocol That Holds the Internet Together

BGP (RFC 4271) is different in kind. It runs between autonomous systems — networks owned by an ISP, a cloud provider, a university, a company — and its job is not to find the shortest path. Its job is to exchange reachability and let each network apply policy.

An autonomous system is identified by an AS number. BGP speakers establish a TCP session on port 179, exchange routes, and attach an AS path to each announcement: the list of autonomous systems a route has traversed. When a router sees its own AS in the path, it rejects the route — that is the loop-prevention mechanism.

Route to 203.0.113.0/24
AS path: 64500 64510 64520

BGP chooses among routes using a long list of attributes: weight, local preference, AS path length, origin, MED, and so on. The first attributes are local policy, not distance. A network can prefer a route through a longer AS path because it is cheaper, or because a provider is preferred for business reasons. BGP is policy-first, shortest-path second, and that is exactly why it can hold a global network together without a central authority.

Two refinements are worth knowing. BGP communities are tags attached to routes that let networks signal policy to each other — "no export," "prepend to this peer," "this is a customer route." RPKI (Resource Public Key Infrastructure) adds signed ROAs that say which AS is authorized to originate a prefix; routers that perform route origin validation can reject hijacks. Neither is universal, which is why route leaks still happen.

A BGP mistake can be global
A misconfigured router that announces a prefix it does not own can attract traffic from much of the internet — a route leak or hijack. The famous 2008 Pakistan Telecom outage, the 2021 Facebook withdrawal, and countless smaller incidents all came from BGP announcements, not from broken cables. The defenses are route filtering, RPKI origin validation, and maximum-prefix limits. BGP trusts announcements by default; that trust is the vulnerability.

Where MPLS, VXLAN, and Anycast Fit

Three ideas show up constantly in modern networks and are worth naming even before you meet them in detail:

  • MPLS attaches a short label to each packet and forwards by label instead of by IP lookup. The label stack makes traffic engineering and VPNs practical for carriers; segment routing keeps the same idea but encodes the path in the packet's label stack or IPv6 header.
  • VXLAN wraps a Layer 2 frame inside a UDP packet (destination port 4789 by default), with a 24-bit VNI that identifies the tenant network. It is how large data centers build virtual Layer 2 networks across a Layer 3 underlay without the 4,094-VLAN limit.
  • Anycast announces the same IP prefix from many locations. BGP routes each user to the nearest one. It is how root DNS servers, CDNs, and DDoS scrubbing centers work.

Routing is not one algorithm. It is a stack of local decisions: longest prefix inside a router, a shared map inside an organization, and policy between organizations.

Frequently Asked Questions

If a route has a shorter AS path, does BGP always prefer it? No. Local preference and other policy attributes are evaluated first. A provider may deliberately prefer a longer path that is cheaper or has better service. AS path length is a tiebreaker, not the rule.

Why can't OSPF run the whole internet? Because every router would need the entire topology, and every change would flood everywhere. OSPF is designed for one administrative domain. BGP exists precisely to connect domains without sharing their internal maps.

What is the difference between a routed network and a switched network? A switch forwards by MAC inside one broadcast domain. A router forwards by IP between subnets and decrements the TTL. Most networks are a mix: switched access, routed core.

Does a default route always mean "the internet"? No. It means "when no more specific route matches, send it here." On a home router that is the ISP. On a server it might be a firewall or a VPN gateway. The meaning depends on what is on the other side.

Further Reading

Fun Facts and Notes

  • The IPv4 routing table of a full internet feed contains roughly a million prefixes. A router does not scan it linearly; it uses specialized hardware and tries (prefix trees) to find the longest match at line rate.
  • BGP has run on TCP port 179 since 1989. The protocol has survived four versions and an internet that grew from a research network to a global utility.
  • Anycast is why 8.8.8.8 can be fast on several continents at once: it is not one server, it is one address announced from many.
  • The TTL field was originally measured in seconds, but in practice every router decrements it by one. It survives as a hop counter and as the trick traceroute uses.
Discuss this chapter Star the book
Comment on GitHub
Rate