BGP & internet routing concepts
Explain how autonomous systems exchange routes with BGP and why route leaks cause outages.
By Invicly Tech, , in Computer Networking. About 13 minutes to read.
In short
The internet is a network of autonomous systems, each run by one organisation with its own routing policy. BGP is the protocol they use to tell each other which prefixes they can reach, along with the AS path to get there. It chooses routes by policy rather than pure shortest path, and mistakes can take large parts of the internet offline.
Explained simply
The internet is not one network. It is tens of thousands of separate networks, run by ISPs, cloud providers, universities and companies, all connected to each other. Each one is called an autonomous system (AS) and has a number, its ASN.
BGP (Border Gateway Protocol) is how these networks tell each other what they can reach. A network announces "I own these addresses" to its neighbours, they pass the message on, and each network that passes it adds its own number to a list called the AS path. That list shows the route the message travelled.
BGP does not just pick the shortest path. Each network applies its own rules, called policy, often based on money: a route through a paying customer is preferred over one through a provider you must pay. That is why BGP is said to route by policy.
BGP mostly works on trust. If a network announces addresses it does not own, by accident or on purpose, others may believe it and send traffic the wrong way. Such mistakes have taken big websites offline. Newer tools such as RPKI help check who may announce what.
Press Play on a diagram to walk through it one step at a time.
- AS 64500 (origin) to AS 64501 (ISP): UPDATE 198.51.100.0/24, path 645001UPDATE 198.51.100.0/24, path 64500
- AS 64501 (ISP) to AS 64502 (peer): UPDATE 198.51.100.0/24, path 64501 645002UPDATE 198.51.100.0/24, path 64501 64500
- AS 64502 (peer) to AS 64501 (ISP): Traffic to 198.51.100.0/24 flows back along the path3Traffic to 198.51.100.0/24 flows back along the path
- Between different ASes
- Prepends its ASN to the AS path
- TTL 1 by default: direct neighbours
- Cisco distance 20
- Inside one AS
- AS path unchanged
- Needs a full mesh or route reflectors
- Cisco distance 200
- 1UPDATE arrives
203.0.113.0/24 from origin AS 64510.
- 2Find covering ROAs
Signed prefix, max length, ASN.
- 3No ROA covers it
NotFound: usually accepted.
- 4ROA matches
Same ASN, within max length: Valid.
- 5ROA exists, no match
Invalid: dropped by many networks.
In depth
Autonomous systems and ASNs
ASNs were originally 16 bits (0 to 65535); 32-bit ASNs (RFC 6793) extend the range to over 4 billion. Regional internet registries assign them: ARIN, RIPE NCC, APNIC, LACNIC and AFRINIC. Some ranges are reserved. Private ASNs, like private IP addresses, are 64512 to 65534 and 4200000000 to 4294967294; they are used inside organisations and must not leak onto the internet. 64496 to 64511 and 65536 to 65551 are for documentation.
Networks connect in two main business relationships. In transit, a customer pays a provider to carry its traffic to the whole internet. In peering, two networks exchange traffic between their own customers for free, often at an internet exchange point (IXP), a building where many networks plug into a shared switch.
These relationships decide which routes a network passes on, and to whom, which is the core of BGP policy.
- Private: 64512 to 65534 (16-bit), 4200000000 to 4294967294 (32-bit)
- Transit: customer pays provider
- Peering: settlement-free exchange, often at an IXP
Sessions, messages and states
BGP neighbours (peers) are configured by hand; there is no automatic discovery. A session is a TCP connection to port 179, which gives BGP reliable, ordered delivery for free. There are four message types in the base standard: OPEN (start a session, exchange ASN, hold time and capabilities), UPDATE (announce or withdraw routes), KEEPALIVE (I am still here) and NOTIFICATION (an error; the session closes). ROUTE-REFRESH (RFC 2918) asks a peer to resend its routes.
The session moves through Idle, Connect, Active, OpenSent, OpenConfirm and Established. A session stuck in Active is actively trying, and failing, to make the TCP connection: often a wrong address, a firewall, or the other side not configured yet.
If no message arrives within the hold time, the session is torn down. RFC 4271 suggests a hold time of 90 seconds; Cisco defaults to 180 seconds with keepalives every 60. Unlike OSPF, BGP sends full updates only once and afterwards only changes.
- Router AS 64500 to Router AS 64501: TCP SYN to port 1791TCP SYN to port 179
- Router AS 64501 to Router AS 64500: TCP SYN-ACK, then ACK2TCP SYN-ACK, then ACK
- Router AS 64500 to Router AS 64501: OPEN: AS 64500, hold 180, capabilities3OPEN: AS 64500, hold 180, capabilities
- Router AS 64501 to Router AS 64500: OPEN: AS 64501, hold 1804OPEN: AS 64501, hold 180
- Router AS 64500 to Router AS 64501: KEEPALIVE: OPEN accepted5KEEPALIVE: OPEN accepted
- Router AS 64501 to Router AS 64500: KEEPALIVE: Established6KEEPALIVE: Established
- Router AS 64500 to Router AS 64501: UPDATE: 198.51.100.0/24, path 645007UPDATE: 198.51.100.0/24, path 64500
eBGP, iBGP and why iBGP needs a full mesh
eBGP runs between routers in different ASes. By default the peers must be directly connected (packets are sent with TTL 1), and Cisco gives eBGP routes administrative distance 20. Each eBGP hop prepends its own ASN to the AS path.
iBGP runs between routers in the same AS, to share what the border routers learned. It does not change the AS path, and Cisco gives it distance 200. To prevent loops, a route learned from one iBGP peer is never passed to another iBGP peer. So every BGP router in an AS must peer with every other one: a full mesh of n(n - 1) / 2 sessions, which is 45 for 10 routers.
Large networks avoid this with route reflectors (RFC 4456), which are allowed to pass iBGP routes on to their clients, or with confederations. One more trap: iBGP keeps the external next hop unchanged, so inside routers need a route to it, or the border router sets next-hop-self.
How BGP picks the best path
When several paths to the same prefix exist, BGP compares them attribute by attribute and stops at the first difference. On Cisco the order is: highest weight (Cisco-only, local to one router), highest local preference (default 100, shared across the AS), a route this router originated, shortest AS path, lowest origin type (IGP before EGP before incomplete), lowest MED, eBGP over iBGP, lowest IGP cost to the next hop, then tie-breakers such as the oldest route and lowest router ID.
Operators steer outbound traffic with local preference: set it higher on routes from customers than from peers, and higher from peers than from providers. Steering inbound traffic is harder, because other networks decide. Tools include AS path prepending (repeating your own ASN to look longer), MED towards a neighbour with several links, and communities (tags such as NO_EXPORT that ask neighbours to treat a route a certain way).
- 1Highest weight
Cisco-only, this router.
- 2Highest local pref
Default 100, whole AS.
- 3Locally originated
Own network or aggregate.
- 4Shortest AS path
Fewest ASNs in the list.
- 5Lowest origin, then MED
IGP before EGP before incomplete.
- 6eBGP over iBGP
Then lowest IGP cost to next hop.
Hijacks, leaks and RPKI
A prefix hijack is an announcement of address space by an AS that does not hold it. Announcing a more specific prefix is especially powerful, because longest-prefix match beats any AS path: in 2008, Pakistan Telecom announced a /24 inside YouTube's /22, and much of the internet sent YouTube traffic to Pakistan. A route leak (RFC 7908) is a route passed on beyond its intended scope, such as a customer re-announcing one provider's routes to another. In June 2019 a leak through a small ISP and Verizon caused wide outages for Cloudflare and others.
RPKI (Resource Public Key Infrastructure) lets an address holder sign a ROA (Route Origin Authorization): this prefix, up to this max length, may be originated by this ASN. Routers doing route origin validation mark each route Valid, Invalid or NotFound (RFC 6811), and many large networks drop Invalid. RPKI checks only the origin AS, not the whole path, so filters on customer routes are still needed.
- Hijack: wrong origin, often a more specific prefix
- Leak: right origin, wrong propagation
- ROV states: Valid, Invalid, NotFound
- Most networks filter IPv4 prefixes longer than /24
Learn it in 4 short steps
About 40m each. Track them with a timer in your plan.
- 1Define autonomous systems and ASNs~40m
- 2Compare eBGP and iBGP~40m
- 3Explain AS path and route selection~40m
- 4Discuss hijacks, leaks and RPKI~40m
Key points
- eBGP runs between ASes; iBGP distributes routes inside one AS.
- Shorter AS paths are preferred, after local preference.
- BGP runs over TCP port 179.
- RPKI lets networks reject announcements from unauthorised origins.
Key terms
Tap a card to flip it. 0/5 known
Examples
whois -h whois.radb.net 1.1.1.0/24 | grep -i origin # origin AS
traceroute -A 1.1.1.1 # AS numbers per hopReading a BGP table and picking the best path
The router has two paths to 203.0.113.0/24. The first, via 192.0.2.2, has a longer AS path (three ASNs) but local preference 200. The second, via 192.0.2.6, is shorter (two ASNs) with the default 100. Step 1: weights are equal (0). Step 2: local preference is compared next, and 200 beats 100, so the first path wins even though it is longer; it is marked with *>. If the operator removed the local preference policy, step 4 (AS path length) would choose the second path. The i at the end means origin IGP.
R1# show ip bgp 203.0.113.0/24
Network Next Hop Metric LocPrf Weight Path
*> 203.0.113.0/24 192.0.2.2 200 0 64502 64503 64510 i
* 192.0.2.6 100 0 64501 64510 iAn eBGP session in FRRouting with filters both ways
AS 64500 wants to announce 198.51.100.0/24 to its provider AS 64501. Step 1: a blackhole static route puts the prefix in the routing table, because BGP's network statement only announces prefixes that exist there. Step 2: a prefix list and an outbound route map allow only that prefix out, so a typo cannot leak other routes. Step 3: an inbound route map is required too; FRR follows RFC 8212, which says eBGP sessions exchange nothing until policy is applied. Here IN accepts everything, which is fine for a lab but should filter in production.
ip prefix-list MINE seq 5 permit 198.51.100.0/24
ip route 198.51.100.0/24 blackhole
route-map OUT permit 10
match ip address prefix-list MINE
route-map IN permit 10
!
router bgp 64500
bgp router-id 192.0.2.1
neighbor 192.0.2.2 remote-as 64501
address-family ipv4 unicast
network 198.51.100.0/24
neighbor 192.0.2.2 route-map IN in
neighbor 192.0.2.2 route-map OUT out
exit-address-familyQuick reference
- BGP runs over TCP port 179; messages: OPEN, UPDATE, KEEPALIVE, NOTIFICATION (+ ROUTE-REFRESH)
- States: Idle, Connect, Active, OpenSent, OpenConfirm, Established; stuck in Active = TCP failing
- Private ASNs: 64512 to 65534 and 4200000000 to 4294967294
- Best path starts: weight, local pref, locally originated, AS path, origin, MED, eBGP over iBGP
- iBGP routes are not passed to other iBGP peers: full mesh or route reflectors
- Steer outbound with local preference; inbound with prepending, MED or communities
- RPKI ROV results: Valid, Invalid, NotFound; it validates the origin AS, not the full path
- FRR: show bgp summary, show ip bgp PREFIX; Cisco: show ip bgp summary
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
A route to 203.0.113.0/24 arrives with AS path 64501 64502 64502 64502 64510. Which AS originated it, how long is the path for best-path comparison, and what do the repeated ASNs suggest?
- Exercise 2medium
Three paths to the same prefix: A (local pref 100, AS path length 2, eBGP), B (local pref 150, length 4, iBGP), C (local pref 150, length 3, iBGP). Weights are equal. Which wins, and at which step?
- Exercise 3hard
Your company holds 198.51.100.0/22, originated by AS 64500, and announces only the /22. Write the ROA you should create, and explain why setting max length to /24 would be risky.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Why is BGP called the protocol that runs the internet?
- It is the only protocol used between independent networks to exchange reachability. Every ISP, cloud and large company that connects to more than one network speaks it.
- Do I need BGP for my company network?
- Usually not if you have a single ISP: a default route is enough. You need BGP when you connect to two or more providers with your own address space and ASN, or in some cloud and data centre designs.
- What happened in the 2021 Facebook outage?
- On 4 October 2021, a maintenance command disconnected Facebook's backbone, and its DNS servers then withdrew their BGP routes, so the rest of the internet could no longer reach Facebook's DNS or services for several hours.
- What is the difference between a route leak and a hijack?
- In a hijack, someone announces address space they do not hold as if they were its origin. In a leak, a valid route is passed on to neighbours who should never have received it, often by mistake.