11. BGP: Routing Between Networks
How tens of thousands of independent networks tell each other which addresses they can reach, choose between routes by business policy rather than distance, and occasionally get it spectacularly wrong
Imagine that the country's parcels aren't carried by one postal service but by hundreds of independent courier firms. Each firm runs its own vans and depots and knows its own patch perfectly, but knows nothing about the inside of anyone else's. To get a parcel to Ashby, a firm only needs to know which other firm can take it there.
So the firms send each other short notices. Ashby Parcels tells its neighbours: "We deliver to Ashby. Route: Ashby Parcels." Barton Freight, which is happy to carry parcels for Ashby, adds its own name and passes the notice on: "We deliver to Ashby. Route: Barton Freight, Ashby Parcels." A firm that finds its own name already on a notice throws it away, because taking that route would send the parcel round in a circle.
And when a firm hears two notices for Ashby, it doesn't just pick the shorter list. It picks the one that makes business sense: a route through a firm that pays it comes first, then a route through a partner that swaps parcels for free, and only then a route through a firm it has to pay. Nobody publishes their prices or their depot maps. Each firm simply announces what it is willing to carry, and for whom.
That's the Border Gateway Protocol, BGP, the protocol that holds the Internet together. The firms are autonomous systems: independently run networks such as ISPs, hosting companies, universities and content providers. The notices are BGP UPDATE messages, and the list of names is the AS path.
In Routing Basics, our ISP's core router R3 had routes marked
"eBGP" for the hosting company's 203.0.113.0/24 and 203.0.113.128/25, and we took them as
given. The last two articles showed how routers fill in their tables inside one network.
This one shows how networks fill in each other's, and ends with R3 holding exactly the routes
Routing Basics showed.
Why a Second Kind of Routing Protocol
An autonomous system (AS) is a network, or a group of networks, run by one organisation with one routing policy. Our ISP is an AS, the hosting company is another, and the transit provider our ISP buys Internet access from is a third. Around 77,000 of them were visible in the global routing table in 2025.
Routing protocols come in two kinds, depending on which side of an AS's border they work:
Interior gateway protocols (IGPs)
Run inside one AS: RIP, OSPF and IS-IS. Every router trusts every other router, they all share one goal, the shortest path, and the topology is no secret. They find the best way across the network.
Exterior gateway protocols (EGPs)
Run between ASes. Today that means BGP and nothing else. Neighbours are separate businesses that don't fully trust each other, each with its own commercial interests, and none willing to show the others its internal map. BGP finds a way across the Internet that each network is willing to use.
Why not one giant OSPF?
The last article gave three reasons, and there's a fourth:
- Scale. Link-state routing needs every router to hold the whole map and rerun Dijkstra on it when anything changes. The Internet has around a million IPv4 prefixes and 220,000 IPv6 ones, from tens of thousands of networks, changing every second.
- Privacy. Flooding our ISP's LSAs to the world would publish its internal topology to its competitors.
- Policy. The best route between networks often isn't the shortest. Our ISP will carry its customers' traffic to anyone, but it won't carry a competitor's traffic for free. Shortest path has no way to say that.
- Trust. In OSPF, every router believes every LSA. Between networks, every route a neighbour sends you is a claim, and you need to decide whether to believe it, which BGP lets you do one neighbour at a time.
BGP's answer is to route between ASes, not routers. An AS announces the prefixes it can reach, and the internals of each AS stay hidden. A BGP route says "these addresses can be reached through this sequence of networks", never "through these routers and links".
Policy first, distance second
An IGP asks "what is the shortest path?" BGP asks "which of the paths my neighbours have offered me do I prefer, and which of them am I willing to offer to whom?" Path length is just one of the tie-breakers. The first questions are about money and relationships.
Where BGP came from
The early Internet's exterior protocol was itself called EGP (RFC 827 in 1982, RFC 904 in 1984). It assumed a tree-shaped Internet hanging off one backbone, and by the late 1980s, with the NSFNET and regional networks connected in meshes, that no longer held. In January 1989, at the 12th IETF meeting in Austin, Texas, Kirk Lougheed of Cisco and Yakov Rekhter of IBM sketched a replacement over lunch on two napkins, the "two-napkin protocol". It became BGP, RFC 1105, in June 1989.
BGP version 4, which added support for CIDR, was published in 1994 and has been the Internet's routing protocol ever since. The current specification is RFC 4271 (January 2006), extended by dozens of later RFCs for IPv6, 32-bit AS numbers, communities and security.
AS numbers
Every AS that speaks BGP to the outside world needs an AS number (ASN). They were
originally 16 bits, 0 to 65535. When those started to run out, RFC 4893 (2007), now
RFC 6793 (2012), extended them to 32 bits, about 4.3 billion. A 32-bit ASN is normally
written as a plain number ("asplain"), such as 65536; you'll occasionally see the older
"asdot" form, 1.0, meaning 1 × 65536 + 0.
ASNs, like IP addresses, are handed out by IANA in blocks to the five Regional Internet Registries: AFRINIC (Africa), APNIC (Asia-Pacific), ARIN (North America), LACNIC (Latin America and the Caribbean) and the RIPE NCC (Europe, the Middle East and Central Asia). An organisation gets its ASN from its RIR, usually along with its address space. Some ranges are set aside:
| Range | Purpose | RFC |
|---|---|---|
| 0 | Reserved, must never appear in a path | 7607 |
| 23456 | AS_TRANS, a stand-in for 32-bit ASNs | 6793 |
| 64496– |
Documentation, like this article | 5398 |
| 64512– |
Private use, never on the Internet | 6996 |
| 4200000000– |
Private use (32-bit) | 6996 |
| 65535, 4294967295 | Reserved | 7300 |
Every ASN in this article comes from the documentation ranges, so none of them belong to a real network.
The Networks in This Article
Our ISP from the last three articles is now an AS, AS64501, with two routers: R2 at the edge of our street and R3 at the core. Since Link-State Routing, R5 and R6 have been retired, and R2 still sends everything it doesn't know to R3 with a static default route. So R3 is the ISP's only BGP router. It talks BGP to four neighbours, in four other ASes:
- AS64496, a tier-1 backbone. It buys transit from nobody. No direct link to our ISP: it's our transit provider's provider.
- AS64497, the transit provider at
192.0.2.9on R3's eth2. Our provider: we pay it to reach the rest of the Internet. - AS64502, a similar-sized ISP in the next county, on a new link, eth4
(
192.0.2.32/30: R3 is.33, the peer.34). A peer: we swap our customers' traffic for free. It buys transit from both AS64496 and AS64497. - AS65536, the hosting company: R4 at
192.0.2.6on eth1, and DC-2 at192.0.2.13on eth3. Our customer: it pays us. It's a younger company, so it got a 32-bit ASN.
The peering link is the one new thing since Routing Basics:
it is the free exchange with "a neighbouring ISP" that
Link-State Routing mentioned. Its IPv6 side is
2001:db8:1200:f4::/64, R3 ::1 and the peer ::2. The other networks' IPv4 space is left
out of this article, because the three IPv4 documentation ranges are already spoken for; we'll
follow their IPv6 prefixes instead. R3's BGP router ID is 0.0.0.3, the same numbering it
uses in OSPF; R4's is 203.0.113.1 and DC-2's is 203.0.113.129.
The words "tier-1" and "transit" describe business, not technology. A tier-1 network reaches the whole Internet through customers and settlement-free peers alone, without paying anyone for transit. Everyone else buys transit from somebody.
Path Vector: Carry the Whole Path
Distance-vector routers tell their neighbours "I can reach this network at this distance". BGP routers say "I can reach this prefix, and here is the AS_PATH, the list of ASes a packet will cross to get there". Each AS that passes a route on to another AS adds its own number to the front of the list. That's a path-vector protocol: a distance vector in which the distance has been replaced by the path itself.
Follow the hosting company's /24 as it spreads. The path is read from left to right, so the
first number is the neighbour you'd hand traffic to, and the last is the origin, the AS
that owns the prefix:
-
AS65536 originates it
R4 announces
203.0.113.0/24to R3 with AS_PATH65536. (Inside AS65536 the path is empty; R4 adds its own ASN as the route leaves.) -
Our ISP passes it on
R3 announces it to the transit provider and the peer as
64501 65536. -
The transit provider passes it on
AS64497 announces it to the tier-1 as
64497 64501 65536. -
And so on, across the Internet
Every AS that hears the route knows not just how far away the prefix is, but exactly which networks the traffic will cross.
Loop prevention for free
Carrying the path makes loop detection trivial: an AS rejects any route whose AS_PATH
already contains its own number. If a route for the /24 ever came back to our ISP, say as
64502 64496 64497 64501 65536, R3 would spot 64501 in it and discard the route. The
count to infinity can't happen, because a
router is never fooled into thinking that its own stale advertisement, coming back to it, is
a real route. That's the "path-vector cousin" that
Loop-Free Distance Vector promised.
Distance vector (RIP)
Routes carry a number: "the street, cost 2". A router can't tell whether that 2 runs back through itself, so after a failure two routers can count upwards together while packets loop. Every router uses the same metric.
Path vector (BGP)
Routes carry the AS path: "203.0.113.0/24 via 64497 64501 65536". A router that sees its own AS in the path drops the route, so inter-AS loops are impossible. Each AS chooses by its own policy, with path length only a tie-breaker.
Two limits are worth knowing now. The AS path lists ASes, not routers, so it says nothing about loops inside an AS; iBGP has its own rules for that, below. And path vector doesn't make withdrawals instant: when a prefix disappears, routers may briefly switch to other, longer paths that are also about to disappear. That's called path exploration, and we'll meet it in BGP in Operation.
BGP Sessions
OSPF routers discover each other with multicast Hellos (see Hellos and Neighbours). BGP routers don't discover anything: every neighbour is configured by hand, with its address and its AS number. That is a deliberate choice. A BGP session usually connects two different organisations, so it exists because two engineers agreed it should, often after signing a contract.
Two BGP routers that talk to each other are peers or neighbours. (The word "peer" also describes a business relationship, as in "our peer ISP"; context usually makes it clear.) A session between routers in different ASes is external BGP, eBGP; between routers in the same AS it's internal BGP, iBGP. Every one of R3's sessions is eBGP.
BGP runs over TCP
Every other routing protocol in this series sends its own packets: RIP over UDP, OSPF directly over IP. BGP opens a TCP connection to port 179 and lets TCP handle everything a routing protocol would otherwise have to build itself: retransmission, ordering, flow control. That makes BGP itself simple. It sends each route once, and from then on only the changes, with no periodic refresh like RIP's 30-second table dumps.
Either side may open the connection. If both do at once, RFC 4271 keeps the one opened by the
router with the higher BGP identifier and closes the other. eBGP peers are normally directly
connected, like R3 and R4 on 192.0.2.4/30, and by default eBGP packets are sent with a TTL
of 1, so they can't reach a router more than one hop away.
Four messages
Every BGP message starts with the same 19-byte header: a 16-byte marker of all ones, a 2-byte length of the whole message (19 to 4,096 bytes; RFC 8654 allows up to 65,535 when both sides support it), and a 1-byte type.
| Type | Message | Purpose |
|---|---|---|
| 1 | OPEN | The first message on a new connection: who I am, my AS, my timers, what I support |
| 2 | UPDATE | New routes, and routes being withdrawn |
| 3 | NOTIFICATION | Something is wrong; the session is closed straight after |
| 4 | KEEPALIVE | "I'm still here": just the 19-byte header, nothing else |
| 5 | ROUTE-REFRESH | Please send me all your routes again (RFC 2918) |
The marker once had a job in early BGP's authentication; today it is always sixteen ff
bytes, kept for compatibility. A KEEPALIVE is the whole header and nothing more:
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff 00 13 04
└──────────────── marker (16 bytes) ──────────┘ len type
= 19 = KEEPALIVE
OPEN: introducing yourself
Once the TCP connection is up, each side sends an OPEN. Here is the one R4 sends to R3, 45 bytes in all:
R4's OPEN message
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff marker
00 2d length = 45
01 type = OPEN
04 version 4
5b a0 My AS = 23456 (AS_TRANS)
00 5a hold time = 90 seconds
cb 00 71 01 BGP identifier = 203.0.113.1
10 optional parameters: 16 bytes
02 06 01 04 00 01 00 01 capability 1: IPv4 unicast
02 06 41 04 00 01 00 00 capability 65: my AS is 65536
The My AS field is only 2 bytes, too small for 65536. So R4 writes
23456, the reserved stand-in AS_TRANS, and gives its real
ASN in the four-octet AS capability (code 65, 0x41). An old
router that has never heard of 32-bit ASNs sees a neighbour in AS23456 and carries on
working; a modern one like R3 reads the capability and knows it's talking to AS65536.
Capabilities (RFC 5492) are how BGP has grown without breaking: each side lists what it supports, and features are only used if both do. Capability 1 is multiprotocol BGP (RFC 4760), which lets a session carry address families other than IPv4 unicast, IPv6 above all. Other common ones are route refresh (2) and graceful restart (64).
R3 checks the OPEN: is the peer really in the AS it was configured with? If R3 had been told to expect AS65537, it would send a NOTIFICATION, "OPEN message error: bad peer AS", and close the connection. If all is well, it replies with a KEEPALIVE to say "accepted".
Hold time and keepalives
Each side proposes a hold time in its OPEN, and both use the smaller of the two. If that much time passes without any message from the peer, the session is declared dead. To stop that from happening on a quiet session, each side sends a KEEPALIVE at regular intervals, typically a third of the hold time.
RFC 4271 suggests a hold time of 90 seconds, with keepalives every 30. Cisco IOS and FRR default to 180 seconds and 60. R3 offers 180, R4 offers 90, so the session uses 90, and both routers send a KEEPALIVE at least every 30 seconds. A hold time must be either 0 (meaning "no keepalives at all") or at least 3 seconds. Ninety seconds is a long time to keep sending traffic to a dead neighbour, so many networks lower it, or run BFD (see Convergence in Practice) alongside to detect failures in milliseconds.
NOTIFICATION: something went wrong
A NOTIFICATION carries an error code, a subcode and some data, and then the sender closes the TCP connection. The codes are: 1, message header error; 2, OPEN message error; 3, UPDATE message error; 4, hold timer expired; 5, finite state machine error; and 6, Cease, which covers deliberate shutdowns. Cease's subcodes (RFC 4486) include 1, maximum number of prefixes reached, which we'll meet as a safety net later, and 2, administrative shutdown.
The session's state machine
A BGP session moves through six states, defined in RFC 4271's finite state machine:
-
Idle
Nothing happening. The router refuses all connections until it is started, and after an error it waits here for a while before trying again.
-
Connect
Trying to open the TCP connection to port 179, and listening for one from the peer.
-
Active
The attempt failed, so the router is waiting for the ConnectRetry timer (RFC 4271 suggests 120 seconds) while still listening. The name is misleading: a session stuck in Active is not working, and seeing "Active" in a router's neighbour list is a classic sign of a wrong address, a firewall, or a peer that hasn't configured its side yet.
-
OpenSent
TCP is up, our OPEN has been sent, and we're waiting for theirs.
-
OpenConfirm
Their OPEN was acceptable; we've sent a KEEPALIVE and are waiting for theirs.
-
Established
The session is up. Now, and only now, the routers exchange UPDATEs: first everything each one has to offer the other, then just the changes, for as long as the session lasts.
When a session drops, every route learned over it is withdrawn at once. That is why a single BGP session going down can cause hundreds of thousands of route changes in a second.
Inside an UPDATE Message
An UPDATE does two jobs at once: it withdraws routes that are no longer reachable, and it announces new ones. All the announced prefixes in one UPDATE share the same set of path attributes: the AS_PATH, the next hop, and the rest. The layout:
┌────────────────────────────┐
│ header: marker, length, 2 │ 19 bytes
├────────────────────────────┤
│ withdrawn routes length │ 2 bytes
│ withdrawn routes │ prefixes no longer reachable
├────────────────────────────┤
│ total path attribute length│ 2 bytes
│ path attributes │ flags · type · length · value, repeated
├────────────────────────────┤
│ NLRI │ the prefixes being announced
└────────────────────────────┘
NLRI, network layer reachability information, is BGP's name for "the prefixes".
Prefixes are packed tightly: one byte giving the prefix length in bits, followed by only as
many bytes as that length needs. 203.0.113.0/24 is 18 cb 00 71: 0x18 is 24, and 24 bits
fit in the three bytes cb 00 71. The zero last byte is never sent.
Each path attribute starts with a flags byte, a type code and a length. The four flag bits that matter, from the top:
- Optional (
0x80): set if routers may ignore this attribute; clear for well-known attributes that every BGP router must understand. - Transitive (
0x40): if a router doesn't understand an optional attribute, should it still pass it on? Well-known attributes always set this bit. - Partial (
0x20): set by a router that passed on an optional transitive attribute it didn't understand. - Extended length (
0x10): the length field is 2 bytes instead of 1.
R4's announcement, byte by byte
Here is the UPDATE that R4 sends R3 once their session is established: 54 bytes announcing
203.0.113.0/24 with four attributes.
UPDATE from R4 (AS65536) to R3
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff marker
00 36 length = 54
02 type = UPDATE
00 00 withdrawn routes length = 0
00 1b path attribute length = 27
40 01 01 00 ORIGIN = IGP
40 02 06 02 01 00 01 00 00 AS_PATH = 65536
40 03 04 c0 00 02 06 NEXT_HOP = 192.0.2.6
80 04 04 00 00 00 00 MED = 0
18 cb 00 71 NLRI: 203.0.113.0/24
| Bytes | Meaning |
|---|---|
40 01 01 00 |
Flags 0x40 (well-known, transitive), type 1 ORIGIN, length 1, value 0 = IGP |
40 02 06 … |
Type 2 AS_PATH, 6 bytes: one segment of type 2 (AS_SEQUENCE) holding 1 ASN, 00 01 00 00 = 65536 |
40 03 04 … |
Type 3 NEXT_HOP, 4 bytes: c0 00 02 06 = 192.0.2.6 |
80 04 04 … |
Flags 0x80 (optional, non-transitive), type 4 MULTI_EXIT_DISC, value 0 |
18 cb 00 71 |
The NLRI: /24, then 203.0.113 |
The lengths add up: 4 + 9 + 7 + 7 = 27 bytes of attributes (0x1b), and 19 + 2 + 2 + 27 + 4 =
54 bytes in all (0x36). Every ASN in the path takes 4 bytes, because both routers announced
the four-octet AS capability. (When a 32-bit-capable router talks to an old 16-bit one, it
sends a 2-byte AS_PATH with AS_TRANS in place of big ASNs, plus the real path in a separate
AS4_PATH attribute that the old router passes along untouched.)
DC-2's UPDATE for the same /24 is identical except for NEXT_HOP = 192.0.2.13 and
MED = 100. Its other UPDATE announces the /25, with one extra attribute:
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff 00 3e 02 00 00 00 22
40 01 01 00
40 02 06 02 01 00 01 00 00
40 03 04 c0 00 02 0d NEXT_HOP = 192.0.2.13
80 04 04 00 00 00 00 MED = 0
c0 08 04 ff ff ff 01 COMMUNITIES = NO_EXPORT
19 cb 00 71 80 NLRI: 203.0.113.128/25
A /25 needs four bytes of prefix, cb 00 71 80. The COMMUNITIES attribute (flags
0xc0, optional and transitive; type 8) carries the well-known community NO_EXPORT,
0xffffff01. It means "use this route, but don't pass it outside your AS". We'll see why the
hosting company asks for that in the worked example.
Withdrawing a route
If R4 lost its /24, it would send a 27-byte UPDATE with no attributes and no NLRI, only a
withdrawal:
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff 00 1b 02
00 04 18 cb 00 71 withdrawn: 203.0.113.0/24
00 00 no path attributes, no NLRI
There is also an implicit withdrawal: announcing a prefix again, with new attributes, replaces whatever the sender said about it before. BGP never needs to send "route X changed"; it just sends route X again.
What changes when R3 passes it on
When R3 announces the /24 to the transit provider, it sends a 51-byte UPDATE:
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff 00 33 02 00 00 00 18
40 01 01 00 ORIGIN = IGP, unchanged
40 02 0a 02 02 00 00 fb f5 00 01 00 00
AS_PATH = 64501 65536
40 03 04 c0 00 02 0a NEXT_HOP = 192.0.2.10
18 cb 00 71 NLRI: 203.0.113.0/24
Three things changed, each following a rule. R3 put its own ASN, fb f5 = 64501, in front of
the path. It set the next hop to its own address on the link, 192.0.2.10, because the
transit provider can't reach R4's 192.0.2.6. And the MED is gone: RFC 4271 says a MED
received from one neighbouring AS must never be passed on to another.
IPv6 and other address families
The NEXT_HOP and NLRI fields above can only hold IPv4. IPv6 routes, like the hosting company's
2001:db8:113::/48, travel in two newer attributes from RFC 4760: MP_REACH_NLRI (type
14), which holds the address family, the next hop and the prefixes together, and
MP_UNREACH_NLRI (type 15) for withdrawals. R4 sends the /48 over a second session
between the routers' IPv6 addresses, 2001:db8:1200:f2::2 and ::1, which is why R3's IPv6
route in Routing Basics has the global next hop
2001:db8:1200:f2::2.
Path Attributes
These are the attributes you'll meet most often. "Well-known" ones must be understood by every BGP router; "optional" ones may be ignored, and are passed on only if they're also transitive.
| Type | Attribute | Kind and meaning |
|---|---|---|
| 1 | ORIGIN | Well-known. How the origin learned the prefix: IGP (0), EGP (1) or INCOMPLETE (2) |
| 2 | AS_PATH | Well-known. The ASes the route has crossed |
| 3 | NEXT_HOP | Well-known. The address to send traffic to |
| 4 | MULTI_ |
Optional, non-transitive. "Of my links to you, prefer this one" |
| 5 | LOCAL_ |
Well-known, but only sent within an AS (iBGP). Higher is preferred |
| 6, 7 | ATOMIC_ |
Marks for routes created by aggregation |
| 8 | COMMUNITIES | Optional, transitive. 32-bit tags (RFC 1997) |
| 9, 10 | ORIGINATOR_ |
Route-reflector loop prevention (RFC 4456) |
| 14, 15 | MP_ |
IPv6 and other families (RFC 4760) |
| 17 | AS4_PATH | The 32-bit path, carried across old routers (RFC 6793) |
| 32 | LARGE_ |
96-bit tags for 32-bit ASNs (RFC 8092) |
| 35 | OTC | "Only to customer", a route-leak guard (RFC 9234) |
A few need more explanation:
- ORIGIN is a historical leftover. A prefix configured into BGP with a
networkstatement is IGP (iin router output), one redistributed from another routing source is INCOMPLETE (?), and EGP (e) means it came from the long-dead EGP protocol. It survives as a weak tie-breaker. - AS_PATH is a list of segments. Normally there's one AS_SEQUENCE, the ordered path. An AS_SET is an unordered set, used when a router merges several routes into one aggregate and wants to remember every AS involved; it counts as length 1. RFC 6472 (2011) recommends against AS_SETs, and they're rare.
- NEXT_HOP is an IP address, not an AS. Over eBGP it is normally the sending router's own address on the shared link.
- LOCAL_PREF never crosses an AS border. It's how an AS tells all of its own routers which exit it prefers, and it is the most important knob in BGP. Default: 100.
Choosing the Best Path
For each prefix, a BGP router may hear many routes: one from each neighbour that offers it. It keeps them all in three logical tables, named in RFC 4271:
- The Adj-RIB-In, one per neighbour: every route that neighbour has sent, after the router's import policy has filtered and adjusted it.
- The Loc-RIB: the single best route for each prefix, chosen by the decision process. The Loc-RIB's routes are offered to the router's main routing table, where they compete with OSPF, static and connected routes by administrative distance.
- The Adj-RIB-Out, one per neighbour: what this router announces to that neighbour, after its export policy.
Only the best route is passed on, so a BGP router tells its neighbours about one path per prefix, not every path it knows. (An extension called ADD-PATH, RFC 7911, relaxes this.)
The decision process
To choose, a router compares its candidate routes in a fixed order of tests. Each test eliminates the routes that don't do best on it; the first test that leaves a single route decides. Before any of that, a route whose NEXT_HOP the router can't reach is ignored. This is the order Cisco documents, which most of the industry follows, with the steps that aren't in RFC 4271 marked:
-
Highest weight (Cisco only)
A number local to one router, never sent to anyone. Cisco gives routes the router originates itself 32768 and learned routes 0. FRR copies it; Juniper and RFC 4271 have no such thing.
-
Highest LOCAL_PREF
The AS-wide policy knob. Default 100. This is where "prefer customers" happens.
-
Locally originated (Cisco)
Prefer a route this router injected itself, with a
networkstatement, redistribution or aggregation. -
Shortest AS_PATH
Fewest ASes. An AS_SET counts as one. The first test that resembles "shortest path", and it counts networks, not hops or miles.
-
Lowest ORIGIN
IGP beats EGP beats INCOMPLETE.
-
Lowest MED, between routes from the same neighbouring AS
MEDs from different ASes are not compared by default, because each AS chooses its own scale. A route without a MED counts as 0. Cisco's
bgp always-compare-medcompares across ASes, andbgp bestpath med missing-as-worsttreats a missing MED as the worst. -
eBGP over iBGP
A route learned directly from another AS beats one relayed by a router in our own AS.
-
Lowest IGP cost to the NEXT_HOP
Leave the AS by the nearest exit, as measured by OSPF or IS-IS. This is "hot-potato routing", below.
-
Oldest eBGP route (Cisco; Junos similar)
If both routes are external, Cisco keeps the one that arrived first, and Junos the one it's already using, to avoid flapping between equals. Not in RFC 4271, and switched off by Cisco's
bgp bestpath compare-routerid. -
Lowest router ID, then lowest neighbour address
The final, arbitrary but deterministic tie-breaks.
In pseudocode:
function best_path(candidates): # every usable route for one prefix
keep = [r for r in candidates if reachable(r.next_hop)]
keep = highest(keep, r.weight) # Cisco and FRR only
keep = highest(keep, r.local_pref)
keep = prefer(keep, r.locally_originated) # Cisco
keep = lowest(keep, len(r.as_path)) # an AS_SET counts as 1
keep = lowest(keep, r.origin) # IGP < EGP < INCOMPLETE
keep = med_filter(keep) # within each neighbour AS only
keep = prefer(keep, r.is_ebgp)
keep = lowest(keep, igp_cost(r.next_hop))
if all(r.is_ebgp for r in keep):
keep = oldest(keep) # Cisco and Junos, not RFC 4271
keep = lowest(keep, r.peer_router_id)
keep = lowest(keep, r.peer_address)
return keep[0]
# each step keeps only the routes that tie for the best value; once one is left, it wins
Look at the order
LOCAL_PREF is tested before AS_PATH length. A route through a customer with a path of five ASes beats a route through a provider with a path of one, if the customer route has the higher LOCAL_PREF. In BGP, policy always outranks distance; length only settles ties among routes the AS likes equally.
Worked decision: R3 and the hosting company's /24
R3 has two routes for 203.0.113.0/24, one from each of the hosting company's routers. Our
ISP's import policy gives every route from a customer LOCAL_PREF 200 (more on that
below). Run them through the tests:
| Test | via R4, 192.0.2.6 |
via DC-2, 192.0.2.13 |
|---|---|---|
| Weight | 0 | 0, tie |
| LOCAL_PREF | 200 | 200, tie |
| Locally originated | no | no, tie |
| AS_PATH length | 1 (65536) |
1 (65536), tie |
| ORIGIN | IGP | IGP, tie |
| MED (both from AS65536) | 0 | 100: eliminated |
The hosting company is using the MED exactly as intended: "of the two links into our
network, please use the main data centre's". Both routes come from the same neighbouring AS,
so R3 compares their MEDs, and R4's route wins. R3 installs 203.0.113.0/24 via 192.0.2.6,
the route from Routing Basics, and the later tests are never reached.
Change the facts and other tests decide. If the hosting company sent no MEDs, both would count
as 0, both are eBGP, and both next hops are on directly connected links, so the IGP cost is
the same. A Cisco R3 would then keep whichever route arrived first: if R4's session had
flapped recently, that's DC-2's. A router following RFC 4271 to the letter skips "oldest" and
picks the lowest router ID, 203.0.113.1, which is R4's.
Vendor knobs can change the answer too. An engineer at our ISP could give DC-2 a Cisco weight of 100, and DC-2 would win at the very first step, before LOCAL_PREF is even looked at, but only on this one router. That's what weight is for, and why it's dangerous in a network with more than one BGP router: the others don't know about it.
MED across neighbours
The default rule, "compare MEDs only between routes from the same neighbouring AS", surprises people, so here's a case where the knob matters. Suppose a router at some other network has two routes for the same prefix, with equal LOCAL_PREF and equal path lengths:
- Route A from AS64505: path
64505 64511, MED 50, learned first, from a router with ID10.5.5.5. - Route B from AS64506: path
64506 64511, MED 10, learned later, from a router with ID10.6.6.6.
By default the MEDs aren't compared, because they come from different ASes. Both routes are
eBGP, with equal IGP costs, so a Cisco router keeps the older one, A; with "oldest"
disabled, the lower router ID also gives A. Turn on bgp always-compare-med and the test
compares 10 with 50, and B wins. Comparing across ASes only makes sense if the networks
involved agree on what the numbers mean.
Scenario
Candidate routes
Pick a route to edit it, or add a copy of the selected one (up to six). A blank MED means no MED, which counts as 0; a blank LOCAL_PREF is the default, 100. Write an AS_SET in braces, such as 64497 {64510 64511}: it counts as 1.
Decision process
Red ✕: eliminated by that test. Faded: already out, or a test never reached because one route was already left. The highlighted row decided.
Inside an AS: iBGP
Our ISP is small enough to have a single BGP router, so it doesn't need anything in this section. Bigger networks have many border routers, and all of them need to know every external route, so that each can choose the best exit. That's the job of internal BGP: the same protocol, between routers in the same AS.
The full-mesh rule
When a route crosses iBGP, the AS_PATH doesn't change, because it hasn't left the AS. So the AS_PATH can't detect loops inside an AS. BGP's fix is blunt: a route learned from an iBGP peer is never passed on to another iBGP peer. Every router must therefore hear every external route directly from the border router that learned it, which means every BGP router in the AS needs a session with every other one, a full mesh:
Five routers need 10 sessions; a hundred need 4,950, and adding the hundred-and-first means configuring a hundred more. Two extensions fix this:
- Route reflectors (RFC 4456). A designated router, the reflector, is allowed to pass iBGP routes between its clients, so each client needs only a session to the reflector (in practice to two, for redundancy). To stop loops between reflectors, reflected routes carry two extra attributes: ORIGINATOR_ID, the router ID of the router that brought the route into the AS, and CLUSTER_LIST, the reflectors it has passed through. A router that finds its own ID in either drops the route. Almost every large network uses reflectors.
- Confederations (RFC 5065). Split the AS into sub-ASes, usually with private ASNs, that run eBGP-like sessions between each other, while the outside world still sees one AS. Rarer than reflectors, and mostly found in networks that were merged.
Next hops and next-hop-self
iBGP doesn't change the NEXT_HOP either. A route that a border router learned from
192.0.2.9 arrives at a router deep inside the AS still saying "next hop 192.0.2.9", an
address on a link that interior router may know nothing about. There are two fixes: carry the
border links in the IGP, or have each border router set the next hop to its own loopback
address when it passes routes into iBGP. That's called next-hop-self, and it's what most
networks do. The IGP's job is then simply to reach every router's loopback, and BGP rides on
top of it.
iBGP sessions normally run between loopback addresses, so a session survives as long as any path between the two routers does. The IGP finds that path; BGP doesn't care how.
Hot-potato routing
Step 8 of the decision process, "lowest IGP cost to the next hop", is where BGP and the IGP
meet. Take the tier-1, AS64496. The transit provider connects to it in two cities, so the
tier-1's routers hear the hosting company's routes at two border routers, B1 and B2, which
pass them to the rest of the tier-1 over iBGP with next-hop-self, giving their loopback
addresses as next hops: B1 is 2001:db8: and B2 is
2001:db8:. A core router C, somewhere in between, now has two routes
for 2001:db8:113::/48:
| Test | via B1 | via B2 |
|---|---|---|
| LOCAL_PREF | 200 (customer) | 200, tie |
| AS_PATH | 64497 64501 65536 | same, tie |
| ORIGIN, MED | IGP, none | same, tie |
| eBGP or iBGP | iBGP | iBGP, tie |
| IGP cost to next hop | 10 | 30: eliminated |
The route via B1 wins because C's IGP, running Dijkstra, says B1 is closer. Traffic is handed to the next network as soon as possible, like a hot potato, which minimises the distance it travels on this network's links. Every router in the tier-1 makes this choice for itself, so traffic from different parts of the tier-1 leaves by different exits.
The opposite, cold-potato routing, carries traffic as far as possible on your own network, and is what a neighbour asks for when it sends you MEDs. Hot potato is also why Internet paths are so often asymmetric: each network hands traffic off at its own nearest exit, and in the two directions those are usually different places (see Asymmetric routing).
Policy: The Business of Routing
Every BGP router has an import policy for each neighbour (which routes to accept, and with what LOCAL_PREF) and an export policy (which routes to announce). These policies are written by people, and between ASes they mirror money.
Customers, providers and peers
Almost every link between two ASes is one of two kinds:
- Customer–provider. The customer pays the provider to carry its traffic to and from the whole Internet. Our ISP is AS64497's customer, and the hosting company is ours.
- Peer–peer. Two networks exchange traffic between their own customers for free, because both benefit. Our ISP and AS64502 peer: traffic between our street and their customers crosses the eth4 link instead of going up through transit providers that both of them would have to pay. A peer does not give you access to the rest of the Internet.
Those relationships decide both questions a policy answers.
Which route do I prefer? A route through a customer earns money, a route through a peer is free, and a route through a provider costs money. So almost every network sets LOCAL_PREF by relationship. Our ISP, like many, uses:
| Learned from | LOCAL_PREF | Why |
|---|---|---|
| A customer | 200 | They pay us to carry it |
| A peer | 100 | Free |
| A provider | 50 | We pay for it |
Which routes do I announce to whom? You announce your customers' routes, and your own, to everyone: that's what your customers pay you for. But you announce routes learned from peers and providers only to your customers. If our ISP announced the tier-1's routes to its peer, the peer could send its traffic for the tier-1 through our ISP, and we would end up paying our transit provider to carry it for free.
| Learned from | Announce to customers? | Announce to peers and providers? |
|---|---|---|
| Our own prefixes | Yes | Yes |
| A customer | Yes | Yes |
| A peer | Yes | No |
| A provider | Yes | No |
These are known as the Gao–Rexford conditions, after Lixin Gao and Jennifer Rexford's paper Stable Internet Routing Without Global Coordination (SIGMETRICS 2000; IEEE/ACM Transactions on Networking, 2001). They showed that if every AS prefers customer routes and follows these export rules, and nobody is, through some chain of relationships, their own provider, BGP is guaranteed to settle on a stable set of routes. That isn't true of BGP in general: with arbitrary policies, networks can disagree forever, each switching routes in response to the others.
Valley-free paths
The export rules give every Internet path a particular shape. Draw providers above their customers, and a valid path climbs from the origin through customer-to-provider links, crosses at most one peering link at the top, and then only descends. Plot each AS a route passes through, in the order the announcement travels (left to right below), and the path can look like a mountain, but never a valley:
The left path is how the hosting company's /24 reaches AS64502 the long way round, through
the tier-1. The right one breaks the rules: AS64502 has taken a route from one of its
providers and given it to the other. That's a route leak, and we'll see what it does in
Hijacks and Leaks.
Prepending: making a path look longer
LOCAL_PREF controls how your own traffic leaves your network. Controlling how other networks' traffic arrives is much harder, because their LOCAL_PREF is theirs to set. The main tool is AS-path prepending: adding extra copies of your own ASN to routes you announce to one neighbour, so that the path through that neighbour looks longer and loses the AS_PATH test.
Take the peer ISP, AS64502, which buys transit from both the tier-1 and AS64497. Normally the
tier-1 hears its prefix 2001:db8:2200::/40 two ways, both from customers with LOCAL_PREF 200:
directly, as 64502, and through AS64497, as 64497 64502. The direct path is shorter and
wins. Now suppose AS64502 wants less traffic on its expensive tier-1 link, so it prepends twice
on announcements to the tier-1:
-
Round 1: AS64502 announces
To the tier-1:
64502 64502 64502. To AS64497:64502. To our ISP:64502. Each of them installs what it heard, for now. -
Round 2: the providers tell each other
AS64497 passes its customer route to the tier-1 as
64497 64502, two ASes long. The tier-1 compares: both LOCAL_PREF 200, and two beats three. It switches to the route through AS64497. (The tier-1 also passed its first route to AS64497 as64496 64502 64502 64502, but AS64497 prefers its customer route with LOCAL_PREF 200 to anything from a provider.) -
Round 3: settling
The tier-1's best route now comes from AS64497, so it withdraws the copy it had sent AS64497, and the copy it sends down to AS64502 is dropped there as a loop. Nothing else changes: traffic from the tier-1 to the peer ISP now arrives through AS64497.
Prepending only works against routes the other network likes equally. If AS64502 prepended
towards AS64497 instead, nothing would change: AS64497 hears 64502 64502 64502 from its
customer with LOCAL_PREF 200 and 64496 64502 from its provider with 50, and LOCAL_PREF is
tested before path length. That's the most common prepending disappointment.
Communities: tags that carry intent
A community (RFC 1997, 1996) is a 32-bit tag attached to a route, written as two 16-bit
halves, ASN:value. A route can carry any number of them, and they're passed from AS to AS.
By convention the first half is the ASN that defines what the tag means. Communities are how
networks talk to each other about routes without changing the protocol:
- Well-known communities mean the same everywhere. NO_EXPORT (
65535:65281): don't announce this route outside your AS. NO_ADVERTISE (65535:65282): don't announce it to anyone. BLACKHOLE (65535:666, RFC 7999): please drop all traffic to this prefix, the remote version of the null route R3 used for198.51.100.99/32. - Provider action communities. Our ISP publishes a list of communities its customers can
attach, for instance
64501:80for "set LOCAL_PREF to 80 inside AS64501, use this as a backup", or64501:3for "prepend 64501 three times when announcing to peers". The customer controls the ISP's policy for its own routes, without a phone call. - Informational communities. Many networks tag every route as it enters with where it came from (which city, which kind of neighbour), so that their export policies can match tags instead of long lists of prefixes.
A 16-bit half can't hold a 32-bit ASN like the hosting company's 65536. Large communities
(RFC 8092, 2017) fix that with three 32-bit numbers, ASN:function:parameter, such as
65536:1:2.
MED: a polite request
A MULTI_EXIT_DISC (MED) is the inbound-traffic tool for a network with several links to the same neighbour: "if you're choosing between my links, prefer the one with the lower MED". It's how the hosting company steered R3 towards R4. A MED crosses only one AS border, is compared only after LOCAL_PREF and AS_PATH, and is honoured only if the receiver chooses to; many providers honour customers' MEDs and ignore their peers'.
What R3's policy looks like
In the Cisco-style syntax R3 uses, abridged, its policy is a handful of lines per neighbour:
router bgp 64501
bgp router-id 0.0.0.3
neighbor 192.0.2.6 remote-as 65536 ! R4, customer
neighbor 192.0.2.13 remote-as 65536 ! DC-2, customer
neighbor 192.0.2.9 remote-as 64497 ! transit provider
neighbor 192.0.2.34 remote-as 64502 ! peer
address-family ipv4 unicast
network 198.51.100.0 mask 255.255.255.0
neighbor 192.0.2.6 route-map FROM-CUSTOMER in
neighbor 192.0.2.13 route-map FROM-CUSTOMER in
neighbor 192.0.2.9 route-map DENY-ALL in
neighbor 192.0.2.9 route-map TO-UPSTREAM out
neighbor 192.0.2.34 route-map FROM-PEER in
neighbor 192.0.2.34 route-map TO-UPSTREAM out
!
route-map FROM-CUSTOMER permit 10
match ip address prefix-list HOSTING-PREFIXES
set local-preference 200
route-map FROM-PEER permit 10
match ip address prefix-list PEER-PREFIXES
set local-preference 100
route-map TO-UPSTREAM permit 10
match ip address prefix-list OURS-AND-CUSTOMERS
Two details matter later. Every import policy only permits the prefixes that neighbour is
known to hold; anything else, a route-map denies by default. And R3 takes no routes at all
from the transit provider: our ISP buys a "default only" service and configured that default
statically, which is why R3's table in Routing Basics has a static
0.0.0.0/0 via 192.0.2.9 and no Internet routes.
Worked Example: One /24 Reaches the Internet
Now let's watch the hosting company's routes spread, as each AS applies the rules above. To keep it readable we'll move in rounds: in each round, every AS whose best route changed announces it to the neighbours its export policy allows, and every receiver reruns its decision process.
The hosting company announces three things to R3:
203.0.113.0/24from R4, with MED 0: the main data centre.203.0.113.0/24from DC-2, with MED 100: a backup way in.203.0.113.128/25from DC-2, with MED 0 and NO_EXPORT: "send traffic for the top half of our range over the DC-2 link, but don't tell anyone else". The hosting company splits its incoming traffic across its two links, and keeps the trick between itself and its provider; everyone else only needs the/24. (A/25would be filtered by most of the Internet anyway: the common convention is to accept nothing longer than/24in IPv4 and/48in IPv6.)
Round by round
Round 1 AS65536 → AS64501 65536 (R4, MED 0) 65536 (DC-2, MED 100)
AS65536 → AS64501 /25: 65536 (DC-2, NO_EXPORT)
AS64501 best /24: 65536 via R4, LOCAL_PREF 200 (MED decides)
AS64501 best /25: 65536 via DC-2, LOCAL_PREF 200 (the only route)
Round 2 AS64501 → AS64497 64501 65536
AS64501 → AS64502 64501 65536
(the /25 is NO_EXPORT: AS64501 sends it to nobody)
AS64497 best: 64501 65536, LOCAL_PREF 200 (customer)
AS64502 best: 64501 65536, LOCAL_PREF 100 (peer)
Round 3 AS64497 → AS64496 64497 64501 65536
AS64497 → AS64502 64497 64501 65536
AS64496 best: 64497 64501 65536, LOCAL_PREF 200 (customer)
AS64502 keeps 64501 65536 (100 beats 50)
(AS64502's best came from a peer: it tells only customers, and has none)
Round 4 AS64496 → AS64502 64496 64497 64501 65536
AS64502 keeps 64501 65536 (100 beats 50)
No AS's best route changed: converged.
Notice what didn't happen. Nobody sent the route back to the AS it came from; and if anyone had, the loop check would have discarded it. The tier-1 didn't send it back to AS64497, since that's where its best route came from. And AS64502, whose favourite route came from a peer, didn't offer it to its providers: if it had, it would be offering the tier-1 free transit to our customer through itself.
What every AS ended up with
AS Adj-RIB-In: neighbour → AS_PATH, LOCAL_PREF Loc-RIB (best)
AS64501 R4 → 65536, 200, MED 0 via R4
DC-2 → 65536, 200, MED 100
AS64497 AS64501 → 64501 65536, 200 64501 65536
AS64496 AS64497 → 64497 64501 65536, 200 64497 64501 65536
AS64502 AS64501 → 64501 65536, 100 64501 65536
AS64497 → 64497 64501 65536, 50
AS64496 → 64496 64497 64501 65536, 50
AS64502's decision is the textbook case of policy beating everything else. All three routes pass the weight test, then LOCAL_PREF eliminates both provider routes at once (50 against 100), and the peer route wins before path length is even considered. It happens to be the shortest as well, but if our ISP had prepended towards its peer, the peer route would still win.
Back to Routing Basics
On R3, the BGP table now looks like this (Cisco-style output, with the header trimmed):
R3: show ip bgp
R3# show ip bgp
BGP table version is 9, local router ID is 0.0.0.3
Network Next Hop Metric LocPrf Weight Path
*> 198.51.100.0 192.0.2.1 20 32768 i
*> 203.0.113.0 192.0.2.6 0 200 0 65536 i
* 192.0.2.13 100 200 0 65536 i
*> 203.0.113.128/25 192.0.2.13 0 200 0 65536 i
* means valid and > best. Cisco leaves out the prefix length
when it matches the old classful mask (/24 for both of these), which is why
only the /25 shows one. The Metric column is the MED. Our
own street, 198.51.100.0/24, is locally originated with a
network statement, from the OSPF route R3 already has, so it shows OSPF's
next hop and cost of 20 and the local weight of 32768. Its LocPrf is blank because
nothing set one, so it counts as the default, 100.
And the detail for one prefix:
R3# show ip bgp 203.0.113.0/24
BGP routing table entry for 203.0.113.0/24, version 7
Paths: (2 available, best #1, table default)
Advertised to update-groups:
1
65536
192.0.2.6 from 192.0.2.6 (203.0.113.1)
Origin IGP, metric 0, localpref 200, valid, external, best
65536
192.0.2.13 from 192.0.2.13 (203.0.113.129)
Origin IGP, metric 100, localpref 200, valid, external
R3# show ip bgp 203.0.113.128/25
BGP routing table entry for 203.0.113.128/25, version 8
Paths: (1 available, best #1, table default, not advertised to EBGP peer)
Not advertised to any peer
65536
192.0.2.13 from 192.0.2.13 (203.0.113.129)
Origin IGP, metric 0, localpref 200, valid, external, best
Community: no-export
Now follow these routes into R3's routing table, using the RIB rules from Routing Basics:
-
203.0.113.0/24 via 192.0.2.6, eBGP, AD 20, metric 0
BGP offers only its best path, R4's. Its administrative distance as an eBGP route is 20, and the metric Routing Basics showed, 0, is R4's MED. The DC-2 path stays in the BGP table as a backup and never reaches the routing table while R4's route exists.
-
203.0.113.0/24 via 192.0.2.13, static, AD 250
The floating static isn't from BGP at all. It's our ISP's belt and braces: if every BGP route for the
/24vanished, say after a bad configuration change on the hosting company's side, traffic would still be sent down the DC-2 link. With AD 250 it loses to the eBGP route (AD 20) whenever that exists. -
203.0.113.128/25 via 192.0.2.13, eBGP, AD 20
The only route for that prefix. Longest-prefix match does the rest: traffic for
203.0.113.200takes the/25to DC-2, and traffic for203.0.113.80, our web server, takes the/24to R4. -
0.0.0.0/0 via 192.0.2.9, static
Not from BGP: R3 accepts nothing from the transit provider, so everything else follows the static default.
Those are exactly the BGP and static routes of the IPv4 table in Routing Basics; the connected
routes, the OSPF route and the .99/32 blackhole come from elsewhere. The rest of the Internet only ever sees
the /24: the transit provider, the tier-1 and the peer all send traffic for any address in
it to our ISP, and only at R3 does the /25 split it between the two links.
The IPv6 side is simpler, because the hosting company announces its /48 only from R4:
2001:db8:113::/48, from R4, AS_PATH65536, LOCAL_PREF 200. Installed as eBGP via2001:db8:, exactly as in Routing Basics.1200:f2::2 2001:db8:1200::/40, locally originated: the ISP's own block, announced to the transit provider and the peer.2001:db8:2200::/40, from the peer, AS_PATH64502, LOCAL_PREF 100. New since Routing Basics: installed via2001:db8:.1200:f4::2
The /40 is R3's announcement of the ISP's whole IPv6 block, and the blackhole route for it in
Routing Basics has a second job: a Cisco-style network statement only announces a prefix
that is already in the routing table, so the discard route both keeps unassigned addresses from
looping and gives BGP something to announce. The peer's /40 is the one route R3 has gained
since Routing Basics; without it, that traffic would follow ::/0 to the transit provider and
cost money.
Scenario
Route origin validation
The hosting company’s ROAs: 203.0.113.0/24 max /24, 203.0.113.128/25 max /25 and 2001:db8:113::/48, all for AS65536. No other prefix here has a ROA, so its routes are “not found”. Ticked networks drop invalid routes.
Grey arrows run from provider to customer; the dashed amber line is the peering link. Green: an UPDATE accepted. Red ✕: dropped, by loop check, import deny, peer prefix filter or ROV. Dashed blue: a withdrawal. Amber boxes changed their best route this round. Tap an AS for its details.
Adj-RIB-In and Loc-RIB
| AS | From | AS_PATH | LP | ROV | Loc-RIB |
|---|
Round by round
UPDATE bytes
Hijacks and Leaks
BGP was designed in 1989 for a far smaller Internet, run by people who mostly knew each other. It has no built-in way to check that the AS announcing a prefix is entitled to it, or that a path really exists. Every route is a claim, and a router believes it unless its own filters say otherwise. Two kinds of mistake follow from that, and, more rarely, deliberate abuse.
Hijacks: announcing someone else's addresses
A prefix hijack is an AS originating a prefix it doesn't hold. Most are accidents: a typo, or a route meant to stay internal escaping into BGP. Two things decide how far the damage spreads:
- Same prefix. If the bogus announcement is for exactly the same prefix, every AS that hears both runs its normal decision process, and the bogus route wins wherever it has the higher LOCAL_PREF or the shorter path. Some of the Internet goes one way, some the other.
- More-specific prefix. If the bogus announcement is more specific, it doesn't compete with the real route at all. It's a different prefix, so every router that accepts it installs it alongside the real one, and longest-prefix match sends the traffic to the bogus one. That's why more-specific hijacks are the most damaging kind.
Suppose an engineer at AS64502 means to block the low half of the hosting company's range for
AS64502's own customers, and the route 203.0.113.0/25 leaks into BGP, originated by AS64502.
Without any defences, the tier-1, the transit provider and our own ISP would all accept it,
and every packet any of them had for 203.0.113.0 to .127, including our web server at
.80, would go to AS64502 and disappear. Even R3, with a perfectly good route to its own
customer, would obey the more-specific route from its peer. The diagram shows what actually
happens, with the defences described below: R3's prefix filter on the peering
session drops the route (and so would ROV), the tier-1 drops it by ROV, and only the transit
provider, which checks neither, is fooled.
Three incidents
- AS7007, 25 April 1997. A router at MAI Network Services, AS7007, received a large part of
the Internet's routing table, by most accounts from a downstream customer, broke the routes
up into
/24s, and announced them as its own. More specific than the real routes, they won wherever they were accepted, and its upstream, Sprint, accepted them and passed them on (contemporary reports say other large backbones carried them too). Large parts of the Internet sent their traffic to AS7007 for hours, and the bogus routes lingered even after the router was disconnected. It was the incident that made prefix filtering on customer sessions normal practice. - YouTube and Pakistan Telecom, 24 February 2008. Ordered to block YouTube inside the
country, Pakistan Telecom (AS17557) announced
208.65.153.0/24, a more-specific of YouTube's208.65.152.0/22, intending to send it nowhere. Its provider PCCW Global (AS3491) passed it to the world at 18:47 UTC, and YouTube was unreachable across much of the Internet. YouTube started announcing the same/24at 20:07, which won some traffic back on path length, and then two/25s at 20:18, which won back everything that accepted them by being even more specific. PCCW withdrew the bogus route at 21:01. - Facebook, 4 October 2021. Not a hijack, but a vivid lesson in how much depends on BGP. During routine maintenance, a command meant to assess backbone capacity disconnected all of Facebook's data centres from each other. Facebook's DNS servers are designed to stop announcing their prefixes over BGP if they can't reach the data centres, to steer users elsewhere; with every data centre unreachable, they all withdrew, at around 15:40 UTC. Facebook's names stopped resolving everywhere, and Facebook, Instagram and WhatsApp vanished from the Internet for around six hours. The same outage cut off the internal tools and building access the engineers needed to fix it.
Leaks: passing on routes you shouldn't
A route leak is a real route, with a real path, announced to a neighbour in breach of the
export rules: a valley. RFC 7908 (2016) classifies them; the classic one is a customer with
two providers passing routes from one to the other. Our topology has exactly that customer.
Suppose AS64502's export filter breaks and it announces everything to everyone. Follow the
tier-1's prefix, 2001:db8:4400::/40:
-
Round 1
The tier-1 announces it to its customers. AS64497 and AS64502 both install
64496, from a provider, LOCAL_PREF 50. -
Round 2: the leak
AS64502 passes its provider's route to its other provider, AS64497, as
64502 64496, and to its peer, our ISP. AS64497 now has the same prefix from a customer, LOCAL_PREF 200, against its provider's 50, and switches: it now sends its traffic for the tier-1 through the peer ISP. Our ISP is saved by its own import filter: the peering session only accepts AS64502's own prefix, so R3 drops the route. Without that filter, R3 would have installed it, more specific than its static default, and sent our traffic for the tier-1 across the peering link too. (AS64497 also told AS64502 about its old route,64497 64496, but AS64502 keeps the shorter direct one.) -
Round 3
AS64497's best now comes from AS64502, so it withdraws what it had told AS64502, and the copy it passes up to the tier-1 is dropped there as a loop. Settled: AS64497, and with it every one of its customers that follows its routes, now sends traffic for the tier-1 through AS64502's links, which were never sized for it.
Leaks of this kind, from a small network into a big one, have taken down large parts of the Internet more than once. They're worse than they look on paper because they often involve thousands of prefixes at once, and the leaked routes win precisely because the receiver's policy, "prefer customers", is working as designed.
Defences
No single mechanism fixes BGP's trust problem. The defences are layers, each catching some mistakes, and each protecting only the networks that deploy it.
- Filter what customers and peers may announce. The most effective defence is the oldest:
a provider should accept from a customer only the prefixes that customer holds. The lists
are usually generated from the Internet Routing Registries (IRRs), databases where
networks publish their routes and their customers in route and as-set objects. IRR data
is only as good as what people put in it, and some registries check very little, but it
catches most accidents. In our two examples, AS64497 filtering its customer AS64502 would
have stopped both the
/25and the leak at the first hop. - Max-prefix limits. Each session has a limit on how many prefixes the neighbour may send. If a peer that normally sends 20 suddenly sends 20,000, something has gone wrong, and the router closes the session with a NOTIFICATION (Cease, subcode 1). Crude, but it would have limited AS7007.
- RPKI and route origin validation. The Resource Public Key Infrastructure (RFC 6480)
lets the holder of an address block sign a Route Origin Authorization (ROA): "AS65536
may originate
203.0.113.0/24, with prefixes up to length 24". The signatures chain back to the five RIRs. Routers fetch the validated ROAs from a local validator, and route origin validation (ROV, RFC 6811, 2013) classifies each route as valid (a ROA covers it and matches its origin AS and length), invalid (ROAs cover it, but none match), or not found (no ROA covers it). Networks that validate drop invalids. In the diagram, the tier-1 and our ISP validate and are protected; the transit provider doesn't, and is hijacked. Even without its prefix filter, ROV alone would have protected our ISP from its peer's mistake. Note the second ROA: without it, the hosting company's own legitimate/25would be invalid too. RFC 9319 recommends exactly that style, a ROA for each prefix you actually announce, rather than a single ROA with a generous maximum length that a hijacker could exploit. By 2025, more than half of the routes in the global table were covered by ROAs, and many of the largest networks drop invalids. - ROV checks only the origin. A deliberate attacker can announce the right prefix with the
real origin AS on the end of a forged path,
64502 65536, and pass ROV. Two newer mechanisms address paths. BGPsec (RFC 8205, 2017) has every AS sign the path as it passes the route on, but it requires every router on the path to take part and costs a great deal of computation; it has seen essentially no deployment. ASPA (Autonomous System Provider Authorization) is lighter: each AS publishes, in the RPKI, the list of its providers, and a router can then check that a path has a valid mountain shape, which catches most leaks and many forged paths. At the time of writing ASPA is still an Internet-Draft, with early implementations in some routing software. - Roles and OTC. RFC 9234 (2022) lets two BGP routers declare the relationship in their OPEN messages, and adds the Only to Customer attribute, which marks a route that must only travel down. A route that arrives from a customer with OTC set, or from a peer that didn't set the OTC itself, has already travelled down and is now heading back up: it's a leak, and is dropped.
- GTSM, protecting the session itself. An eBGP neighbour is one hop away, so the Generalized TTL Security Mechanism (RFC 5082) has both routers send BGP packets with a TTL of 255 and reject any that arrive with less. A packet from further away has lost at least one TTL on the way, so an attacker elsewhere on the Internet can't even reach the BGP process to try to disrupt the session. TCP authentication (TCP-AO, or the older MD5 option) adds a shared key.
- MANRS. The Mutually Agreed Norms for Routing Security, launched by the Internet Society in 2014 and run by the Global Cyber Alliance since 2024, is a voluntary programme whose members commit to the basics: filter customers' announcements, block spoofed source addresses, keep contact details up to date, and publish routing data in the IRR and RPKI.
BGP in Operation
Convergence and path exploration
BGP converges slowly by IGP standards. When a prefix is announced, the news crosses the
Internet in seconds. Withdrawals are worse. When AS65536's /24 disappears, a router that has
a backup path switches to it and announces that, not knowing that it too is about to be
withdrawn, and the next AS switches to its backup, and so on. Routers explore ever-longer
paths that are all already dead before agreeing that the prefix is gone. Labovitz, Ahuja, Bose
and Jahanian measured this in Delayed Internet Routing Convergence (SIGCOMM 2000), finding
withdrawals that took minutes to settle.
A timer makes it more orderly but also slower: the Minimum Route Advertisement Interval (MRAI) spaces out announcements for the same prefix to the same neighbour, with RFC 4271 suggesting 30 seconds for eBGP and 5 for iBGP. It trades speed for fewer messages.
Route flap damping
A link that keeps going up and down makes its routes flap, and every flap ripples across the Internet. Route flap damping (RFC 2439, 1998) punishes unstable routes: each withdrawal adds a penalty, 1,000 by default, that decays exponentially with a half-life of 15 minutes:
When the penalty passes the suppress threshold (2,000 by Cisco's defaults), the route is ignored until the penalty decays below the reuse threshold, 750, which for a route that just reached 2,000 takes about 21 minutes. Damping fell out of favour in the 2000s, because path exploration makes a single withdrawal look like several flaps, so stable prefixes were being suppressed for an hour after one outage. RFC 7196 (2014) made it usable again with much higher thresholds, recommending a suppress threshold of at least 6,000.
The size of the table
A router in the default-free zone holds more than a million IPv4 prefixes and around 220,000 IPv6 ones, and every one of them is a BGP route, often several times over, once per neighbour. The table keeps growing: networks announce more-specific prefixes for traffic engineering, and IPv4 addresses sold on the transfer market are announced as small blocks of their own.
Growth has teeth when the forwarding hardware is fixed-size. Some widely deployed routers, including Cisco's Catalyst 6500 and 7600 families with their default settings, had room in TCAM for 512K IPv4 routes. On 12 August 2014, the global table briefly passed that mark when Verizon (AS701) announced thousands of extra more-specific routes, and the overflowing routers fell back to slow software forwarding or failed outright. Outages hit networks around the world on what became known as 512K Day. Operators reconfigured their TCAM, often to 768K, so when the table passed 768K around the end of August 2019 they were ready, and little broke.
Watching it all
You don't need a router to see BGP. Route collectors run by RIPE NCC (RIS) and the
University of Oregon (RouteViews) peer with hundreds of networks and archive every UPDATE
they receive, which is how incidents like the ones above can be reconstructed minute by
minute. Many networks also run public looking glasses, web pages where you can run
show ip bgp on their routers yourself. Look up a prefix from two places, and you'll see the
two networks' different AS paths to it, and their different policies.
Where to Go Next
- UDP: the start of the Transport section. Routing gets packets to the right machine; transport gets them to the right program.
- TCP: the reliable byte stream BGP itself runs on, and how a connection like R3's session to R4 on port 179 is opened, kept and closed.
- Routing Basics: what happens to the routes once BGP has chosen them: administrative distance, the RIB and the FIB, and longest-prefix match.
- Link-State Routing: the IGP underneath, whose costs drive BGP's hot-potato step.
- Distance-Vector Routing: BGP's ancestor, and the count to infinity that carrying the whole path prevents.
- DNS: the other system that had to keep working for Facebook to be reachable.
Key Takeaways
- The Internet is tens of thousands of autonomous systems, each with its own policy. IGPs (OSPF, IS-IS, RIP) route inside one; BGP, the only EGP in use, routes between them. One giant link-state network would fail on scale, privacy, policy and trust.
- ASNs were 16-bit and are now 32-bit (RFC 6793), assigned by IANA through the five RIRs. AS_TRANS, 23456, stands in for 32-bit ASNs where only 16 bits fit. 64496–64511 and 65536–65551 are for documentation; 64512–65534 are private.
- BGP is path vector: each route carries its AS_PATH, each AS prepends its own number, and an AS drops any route already containing its number, so loops between ASes are impossible.
- Sessions are configured by hand and run over TCP port 179. OPEN, UPDATE, NOTIFICATION and KEEPALIVE share a 19-byte header. Sessions go Idle, Connect, Active, OpenSent, OpenConfirm, Established. The hold time is the smaller of the two offered (RFC 4271 suggests 90 s, with keepalives every 30; Cisco IOS and FRR default to 180 and 60).
- R4's 54-byte UPDATE for 203.0.113.0/24 carries ORIGIN IGP, AS_PATH 65536 in 4-byte form, NEXT_HOP 192.0.2.6 and MED 0, then the NLRI 18 cb 00 71. R3 passes it on as AS_PATH 64501 65536 with itself as next hop and the MED removed.
- Best path, in order: weight (Cisco), LOCAL_PREF, locally originated, shortest AS_PATH, lowest ORIGIN, lowest MED (same neighbour AS only by default), eBGP over iBGP, lowest IGP cost to the next hop (hot potato), oldest eBGP path (Cisco; Junos similar), lowest router ID, lowest neighbour address. Policy is tested before path length.
- Inside an AS, iBGP needs a full mesh, n(n−1)/2 sessions, unless route reflectors or confederations are used; next-hop-self makes border routers' routes usable inside.
- Relationships drive policy: LOCAL_PREF customer 200 > peer 100 > provider 50, and routes from peers and providers are exported only to customers (Gao–Rexford), making paths valley-free. Prepending, MEDs and communities (RFC 1997, large communities RFC 8092) influence inbound traffic, but can't override another network's LOCAL_PREF.
- In the worked example the /24 converged in four rounds, and R3's table ended up exactly as in Routing Basics: the /24 via R4 (MED 0 beat DC-2's 100; eBGP, AD 20), the NO_EXPORT /25 via DC-2, a floating static backup, and a static default.
- Hijacks (AS7007 in 1997, YouTube in 2008) and leaks exploit BGP's trust; more-specific hijacks win by longest-prefix match. Defences are layered and partial: IRR-based filters, max-prefix limits, RPKI route origin validation (RFC 6811), RFC 9234 roles, GTSM, MANRS, and for paths, BGPsec (undeployed) and ASPA (still a draft).
That completes the Routing section. A packet can now find its way from our laptop to any address on the Internet: across the house by switching, across the ISP by OSPF, and across the world by BGP, with longest-prefix match at every hop. But getting a packet to the right machine isn't the end of the story. The web server runs dozens of programs, and the packet has to reach the right one. That's the job of the transport layer, and even BGP leans on it: every session in this article ran over TCP. We start with the simplest transport protocol of all: UDP.