BGP: How the Internet Actually Finds a Path
Every autonomous network on the planet speaks BGP to its neighbors. Here's how path-vector routing really works — and why policy beats distance.
What is BGP? The short answer
BGP is the routing protocol that glues the internet's more than 75,000 independent networks together. Each network announces the address blocks it owns, and every other network picks a path through those announcements. There is no central map and no master router — the internet works because every network keeps making these announcements and keeps listening to everyone else's.
Here is one announcement, followed end to end — before any theory. A small network, AS 64496, owns the address block 192.0.2.0/24. (Both are documentation examples, reserved for teaching: 192.0.2.0/24 is TEST-NET-1, and AS 64496 to 64511 are set aside for examples — so nothing here is a real network.) AS 64496 wants the whole internet to be able to reach those addresses, so it announces: "192.0.2.0/24 lives here."
Step 1 — the owner speaks. AS 64496 sends the announcement to its transit provider, AS 64497. The announcement carries a path — the list of networks it has crossed so far: just 64496.
Step 2 — the first transit stamps it. AS 64497 accepts the announcement, writes its own number on the front of the path — now 64497 64496 — and passes it to the next transit network, AS 64498. That stamp is the whole trick: every network that forwards an announcement adds itself, so the path is also the receipt.
Step 3 — the second transit stamps it again. AS 64498 does the same thing and hands the announcement to your network, AS 64499, with the path 64498 64497 64496.
Step 4 — you read it backwards. Your router now knows: to reach 192.0.2.0/24, hand the traffic to AS 64498 (the name on the left — your neighbour), it crosses AS 64497, and it ends at AS 64496 (the name on the right — always the owner). And if your own number ever appears in a path you receive, you drop that announcement — it has looped.
| Where you look | Prefix | AS_PATH you see | What it means |
|---|---|---|---|
| Inside the owner, AS 64496 | 192.0.2.0/24 | — (it starts here) | "These addresses are mine." |
| At transit AS 64497 | 192.0.2.0/24 | 64496 | One hop away, direct from the owner. |
| At transit AS 64498 | 192.0.2.0/24 | 64497 64496 | Reach it via AS 64497. |
| At your network, AS 64499 | 192.0.2.0/24 | 64498 64497 64496 | Send via AS 64498; the owner is on the right. |
That table is what a looking glass shows — a public window into one network's routing table. Every row in the real internet's tables grew exactly this way, one stamp at a time. The rest of this article explains the machinery underneath: why networks prefer one stamped path over another, and what can go wrong.
Start with the postal system
Imagine every country runs its own postal service, and there is no world post office. The US Postal Service wants to deliver your letter to Tokyo. It can't — so it hands the letter to a partner carrier, which hands it to another, until Japan Post delivers it.
Now the key question: how does USPS decide which partner to hand it to? Not by distance alone. It picks based on contracts, price, and politics. Maybe carrier A is cheaper, so USPS prefers A even when carrier B's route is physically shorter.
That is BGP. Each autonomous system (AS) is a country's postal service. BGP is the system of announcements ("I can deliver to these addresses") and preferences ("I prefer carrier A") that moves every packet across the planet. There is no central map. There are 75,000 postal services negotiating with each other, continuously.
The problem BGP solves
Inside one company, routers can run OSPF or EIGRP and agree on one map of the network — that works because one company controls all the routers and trusts its own math. The internet has no single map. It is roughly 75,000 independent networks — autonomous systems — run by ISPs, cloud providers, and large enterprises, and none of them trusts the others' routing math.
BGP, the Border Gateway Protocol, is the diplomacy layer. Each AS publishes reachability — "I can carry traffic to these prefixes" — and every other AS decides, by its own policy, which path to believe. It is the only protocol whose answer to "what's the best route?" is legitimately "it depends on who you ask."
A small ISP, two upstreams, one customer
Make it concrete. You run AS 64501, a small ISP. You buy transit from two big providers — ISP-A (AS 174) and ISP-B (AS 3356) — and you sell connectivity to a customer, AS 64512, which owns 198.51.100.0/24. This triangle — two upstreams, one customer — is where almost every BGP policy decision lives:
Two classic moves, both visible here. Outbound (your users reaching the internet): you set LOCAL_PREF 200 on routes learned from ISP-A, so everything leaves through A unless A fails — like telling your whole staff "always book carrier A unless their flights are cancelled." Inbound (the internet reaching your customer): you prepend your AS number three times on announcements sent to ISP-B, so the world prefers the shorter-looking path through A — like adding fake layovers to one flight option so travelers pick the other. Same triangle, two knobs, full control of both directions.
Two jobs: eBGP and iBGP
BGP looks like one protocol but plays two different roles.
| eBGP | iBGP | |
|---|---|---|
| Runs between | Different autonomous systems | Routers inside one AS |
| Transport | TCP port 179, single-hop by default | TCP port 179, often on loopbacks |
| Loop safety | AS_PATH check — drops routes already containing its own AS | Split-horizon — iBGP-learned routes are not re-advertised to other iBGP peers |
| Typical use | Peering with ISPs and other networks | Carrying internet routes across a provider backbone |
A note on the jargon in that table: TCP port 179 is just BGP's reserved door number — every BGP conversation knocks there. A loopback is a virtual address on a router that never goes down, so sessions built on loopbacks survive a single cable failure.
Think of it this way: eBGP is the negotiation between postal services. iBGP is the memo inside one postal service making sure every sorting office heard about the new international agreement.
Path vector, not shortest path
OSPF computes math; BGP tells stories. Each BGP route carries a path vector — the list of autonomous systems it has crossed, like the row of passport stamps showing every country a letter passed through — plus attributes you use to express policy. The big four:
- LOCAL_PREF — local to your AS. Highest wins. This is how you say "prefer ISP A over ISP B for everything."
- AS_PATH — transit history. Shortest usually wins — and long paths also look like you are prepending (a valid way to make a path less attractive).
- MED (Multi-Exit Discriminator) — used between neighboring ASes with multiple links. Lowest wins, and only compared between paths from the same AS. Think of it as whispering to your neighbor "if you have two doors into my building, use this one" — the whisper means nothing to anyone else.
- Communities — attached labels (like no-export, or custom tags) that let peers agree on meaning: "blackhole this," "local-only," "backup path." They are sticky notes with agreed-upon meanings.
Best-path selection, simplified
When BGP learns several ways to reach one prefix, it runs a best-path election — a fixed checklist, applied top to bottom, first match wins. The full list is vendor-long, but the first hits decide most elections:
- Highest weight (Cisco-local tiebreak; vendors without weight start here)
- Highest LOCAL_PREF
- Shortest AS_PATH (and locally originated routes first)
- Lowest origin type: IGP < EGP < incomplete
- Lowest MED, when the candidate paths came from the same neighboring AS
- Prefer eBGP-learned over iBGP-learned
- Lowest IGP metric to the next hop, then oldest route, then lowest router ID
The practical upshot: if traffic takes the "wrong" way, the cause is almost always a LOCAL_PREF or MED knob someone turned on purpose. And when two paths genuinely tie, routers can install both and load-share across them — equal-cost multipath. That last step is where forwarding quietly goes wrong: the per-flow hash spreading traffic over those links can polarize a Clos fabric onto a handful of paths, which the ECMP hash polarization visualizer makes painfully visible. To practice best-path elections hands-on, try the BGP toolkit.
Inside a real BGP UPDATE message
This part is genuinely intricate — here is the shape of it before the bytes. Every BGP message is just a sequence of labeled boxes: each box says "I am this many bytes long, and here is my value." Once you know the box labels, you can read any BGP message the way you would read a form. Here is an actual-format UPDATE announcing 203.0.113.0/24 (47 bytes total). Each color is one field — expand a field to see what every byte means:
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 00 2F 02 00 00 00 14 40 01 01 00 40 02 06 02 02 FC 01 FC 00 40 03 04 C0 00 02 01 18 CB 00 71
Marker — 16 bytes: FF×16
Sixteen bytes of all-ones — the fixed opening signature every BGP message starts with, like the "Dear Sir" of a formal letter. The receiver expects all 0xFF unless TCP-MD5 or TCP-AO authentication is negotiated, in which case the marker carries the authentication digest instead. Its real job: unambiguously framing messages on the TCP stream, so the receiver always knows where one message ends and the next begins.
Length — 2 bytes: 00 2F = 47
Total message length in bytes, header included — the "this envelope weighs 47 grams" stamp. Minimum 19 (header only, a KEEPALIVE), maximum 4096. Our message is 19 + 2 + 2 + 20 + 4 = 47 bytes.
Type — 1 byte: 02 = UPDATE
Message type: 1 = OPEN, 2 = UPDATE, 3 = NOTIFICATION, 4 = KEEPALIVE. UPDATE is the workhorse — it carries withdrawn routes, path attributes, and newly reachable prefixes.
Withdrawn Routes Length — 2 bytes: 00 00
Zero — this UPDATE withdraws nothing. ("Withdrawing" a route means announcing "I can no longer reach these addresses — stop sending them to me," like a store posting that it no longer carries a product.) When non-zero, it is followed by that many bytes of withdrawn prefixes, each encoded as a 1-byte prefix length plus the significant prefix bytes.
Total Path Attribute Length — 2 bytes: 00 14 = 20
Twenty bytes of path attributes follow. Every attribute is encoded the same way: 1 byte of flags, 1 byte type code, 1–2 bytes of length, then the value — the same labeled-box pattern all the way down.
ORIGIN — 40 01 01 00 (4 bytes)
Flags 0x40 = well-known + transitive (every BGP speaker must recognize this attribute and pass it on — it is mandatory luggage, not optional). Type 1 = ORIGIN. Length 1. Value 0 = IGP, meaning the prefix was injected from inside the origin AS (values: 0 = IGP, 1 = EGP, 2 = incomplete — and lower wins in the best-path election, because a route born inside a network is more trustworthy than one of murky provenance).
AS_PATH — 40 02 06 02 02 FC 01 FC 00 (9 bytes)
Flags 0x40 transitive, type 2, length 6. The value: segment type 02 = AS_SEQUENCE (an ordered list, as opposed to an unordered set), segment length 02 = two ASes, then FC 01 = 64513 and FC 00 = 64512 (hexadecimal for those AS numbers — FC01 in hex is 64513 in decimal). So AS_PATH is [ 64513, 64512 ] — the announcement crossed AS 64512, then AS 64513, to reach us. This is the loop-prevention field: if a router sees its own AS number here, it drops the route.
NEXT_HOP — 40 03 04 C0 00 02 01 (7 bytes)
Flags 0x40 transitive, type 3, length 4. Value C0 00 02 01 = 192.0.2.1 — the IP address of the router to forward traffic toward (the immediate next step, not the final destination). (192.0.2.0/24 is TEST-NET-1 documentation space, hence the tidy bytes — reserved addresses meant for examples exactly like this one.)
NLRI — 18 CB 00 71 (4 bytes)
Network Layer Reachability Information — the prefix being announced, i.e. the actual payload: "these addresses live over here now." First byte 0x18 = 24, the prefix length (hex 18 = decimal 24). Then the significant bytes: CB 00 71 = 203.0.113. Trailing zero octets are omitted, so a /24 needs only 3 bytes — no point writing zeros everyone can infer. The announcement: 203.0.113.0/24.
Loop prevention is built in
Because every route lists the ASes it crossed — the passport stamps — a router that sees its own AS number in AS_PATH drops the route. The loop is impossible by construction. Elegant, and it scales: no one would maintain a loop-free map of 75,000 networks by hand.
iBGP has no such check, so it uses a blunt rule instead: split-horizon — routes learned from one iBGP peer are never advertised to another iBGP peer. That means a full mesh (or route reflectors) between iBGP speakers. The classic day-one surprise: routers A, B, C in one AS; A peers with B, B peers with C — and routes from A never reach C. That is not a bug; add a direct A–C session or a route reflector.
BGP in four commands
IOS-style, at the level you need for your first lab. AS 65001 peering with AS 65002 over a direct link. Read each line as an instruction you type:
router bgp 65001 ! "I am now configuring BGP for my AS, number 65001"
bgp router-id 10.0.0.1 ! "Identify me by this address in BGP messages"
neighbor 203.0.113.2 remote-as 65002 ! "Talk BGP to the router at 203.0.113.2, which belongs to AS 65002"
neighbor 203.0.113.2 description ISP-A-primary ! "Label that neighbor so future-me knows what it is"
exit ! "Done with BGP config"
network 198.51.100.0 mask 255.255.255.0 ! "Announce my 198.51.100.0/24 block to the world"
Then verify before you believe anything:
show bgp summary ! session state should read Established
show ip bgp neighbor 203.0.113.2 advertised-routes
show ip bgp 198.51.100.0 ! which path won, and why
That verify-it-don't-trust-it habit scales: paste several devices' configs into the network config verifier and it checks the fleet in seconds — BGP neighbor AS mismatches, duplicate IPs, OSPF disagreements, unreachable subnets, and dead firewall rules — the exact class of typo that turns a routine change into a 2 a.m. incident.
Common gotchas
- Different loopbacks, no multihop: eBGP between loopbacks needs ebgp-multihop, because default TTL is 1.
- next-hop unreachable: iBGP passes the eBGP next hop through unchanged — use neighbor x.x.x.x next-hop-self or make sure the IGP carries the route.
- The network command is exact:
network 10.0.0.0 mask 255.255.0.0only advertises what is already in your table with that exact mask — it will not invent a route you do not have. - Using your ISP's MED against you: MED is compared between links to the same neighbor AS. Two ISPs' MEDs are not comparable — a classic accidental misroute, like comparing two restaurants' "spice level 3" as if the scales matched.
BGP explained simply
BGP is how networks tell each other "send traffic for these addresses to me." Each network makes its own announcements and chooses, by its own business rules, which of the announcements it hears to believe. Nobody is in charge, and the system still works — because every network follows the same simple habit: announce what you own, stamp what you forward, drop anything that has looped.
BGP explained for beginners: four ideas to hold on to
If the rest of this article feels dense, hold on to just these four ideas and the details will attach to them later:
- Autonomous system (AS) — one network under one owner, with one number: your internet provider, a cloud company, a university.
- Prefix — a block of addresses one network owns, written like 192.0.2.0/24.
- Announcement — the message that says "this prefix lives here — send its traffic my way."
- AS_PATH — the stamped list of networks an announcement crossed. The owner is on the right; each network adds itself on the left.
How does BGP choose a path?
When several announcements offer a way to the same prefix, a router works down a short checklist and stops at the first step that separates them. This is the short version — the fuller checklist is in "Best-path selection, simplified" above:
| Order | Test | Winner |
|---|---|---|
| 1 | LOCAL_PREF — your own preference setting | Highest wins. Your business rules beat everything. |
| 2 | AS_PATH length — how many networks it crosses | Shortest wins. |
| 3 | Origin — how the route was born | Routes born inside a network beat learned or murky ones. |
| 4 | MED — a neighbour's hint about which door to use | Lowest wins — and only compared between paths from the same neighbour. |
That is a simplification — real routers keep comparing past MED when everything ties — but those four steps decide most real-world choices, and step 1 explains the thing that surprises every beginner: the winning path is often not the shortest one, because a preference someone set on purpose outranks geography.
BGP vs OSPF vs static routing
Three ways a router can learn where to send traffic, side by side — the questions beginners actually ask:
| Question | BGP | OSPF | Static routing |
|---|---|---|---|
| Who runs it? | Every network owner, independently — no one is in charge overall | One organisation, inside its own network | An engineer, typing routes by hand |
| What does it connect? | Separate networks to each other — it is how the internet holds together | Routers inside a single network | One router to one known next step |
| How does it pick a path? | Policy first — business preference, then path length and the other tests above | Maths — the lowest total link cost wins | It doesn't pick — it goes exactly where it was told |
| How fast does it react? | Slowly on purpose — settling can take tens of seconds to minutes, because stability beats speed between networks | Fast — a failed link is usually routed around within seconds | Never on its own — someone has to edit the route |
| What breaks it? | A wrong or withdrawn announcement can misroute a prefix for the whole internet | A design or area mistake — but the damage stays inside your own network | Silence — the route keeps pointing at a dead path until a human fixes it |
The one-line summary: static routing is a note pinned to one router, OSPF is one company's internal map, and BGP is the negotiation between companies. Most networks run all three at once, each where it belongs.
Your next steps
Spin up a three-AS topology in GNS3 or EVE-NG: two ISPs, your AS in the middle. Get both eBGP sessions Established, then practice the two moves every network engineer does in production: steer outbound traffic with LOCAL_PREF, and steer inbound traffic with AS_PATH prepending. When what you predict matches show ip bgp, you understand BGP. For guided practice without a lab, the BGP toolkit walks through prefix lookups, RPKI validation, and hijack detection on the live internet. But RPKI validation is not foolproof: an overly broad ROA maxLength let the August 2026 Softaculous/Hetzner hijack pass RPKI as Valid — read what went wrong and how to tighten your ROAs.
- BGP is a path-vector protocol: routers choose routes by policy, not by shortest hop.
- eBGP runs between autonomous systems over TCP/179; iBGP runs inside one.
- Best-path selection (simplified): Local Preference, then AS-path length, then MED.
- The AS_PATH attribute prevents loops automatically; iBGP uses split-horizon instead.