BGP Explained: The Protocol That Holds the Internet Together
See how networks announce addresses to each other with BGP, how paths are chosen and why one bad announcement can spread widely.
Key takeaways
- BGP exchanges routes between autonomous systems, which are networks under one routing policy.
- Each announcement lists the networks it has passed through, which prevents loops and informs path choice.
- Networks choose routes mainly by their own policy, and only then by factors such as path length.
- Classic BGP largely trusts what neighbors say, so filtering and signed route origins are needed to limit mistakes and abuse.
The Border Gateway Protocol, or BGP, is how separately run networks tell each other which IP address ranges they can reach. A provider announces "you can reach these addresses through me", its neighbors pass that on, and so on, until routers across the internet know at least one way toward almost every public destination.
BGP does not look for the fastest path. It carries reachability together with information about the path, and each network applies its own business and engineering rules to choose among the options. That combination of reach and independent choice is what lets thousands of organizations cooperate without anyone being in charge of the whole.
The problem BGP solves
Inside one organization, routers can share detailed information and agree on the shortest paths using an interior routing protocol. Between organizations, that does not work. A network does not want to reveal its internals, and it will not accept a competitor's idea of the best route. It wants to say what it offers and decide for itself what to accept.
Each such network is an autonomous system (AS), identified by a number. Autonomous systems and AS numbers explains who gets one. BGP version 4 is specified in RFC 4271.
Sessions between neighbors
BGP routers do not discover each other automatically in the ordinary case. An administrator configures each side with the other's address and AS number. They then open a TCP connection on port 179 and exchange messages.
After the first full exchange, routers send only changes: new routes in UPDATE messages and withdrawals for those no longer reachable. A NOTIFICATION message reports an error and closes the session. When a session drops, all routes learned over it are removed, and the change ripples onward.
A session between different autonomous systems is external BGP (eBGP). Routers within the same AS also share BGP routes with each other using internal BGP (iBGP), so every border router learns what the others heard. Because iBGP does not pass routes freely from one internal peer to another, large networks use route reflectors or confederations to avoid connecting every router to every other.
What an announcement contains
An announcement names a prefix, such as 203.0.113.0/24, and attaches attributes. The most important:
- AS path: the list of autonomous systems the announcement has traveled through, most recent first.
- Next hop: where to send packets for this prefix.
- Origin: how the route first entered BGP.
- Local preference: a value used inside an AS to rank exits. Higher is preferred.
- MED: a hint to a neighbor about which of several entry points to use. Lower is preferred.
- Communities: tags that networks use to signal handling, such as "do not export beyond your customers".
The AS path does double duty. If a router sees its own AS number in a received path, it rejects the route, which stops loops. It also gives a rough measure of distance.
Those AS numbers come from a range reserved for documentation.
How a router picks the best route
When several routes to the same prefix arrive, BGP runs through a series of comparisons. In simplified form:
- Highest local preference.
- Shortest AS path.
- Preferred origin type.
- Lowest MED, normally compared only between routes from the same neighboring AS.
- External sessions preferred over internal.
- Closest exit according to the interior routing protocol.
- Tie-breakers such as router identifiers.
Vendors add steps of their own, including a locally significant "weight" on some platforms. The key point is that policy comes first. A network can prefer a longer path because it is cheaper, more reliable or contractually required.
Remember also that routers forward by longest prefix match. If one network announces a /22 and another announces a /24 inside it, traffic for the /24 follows the more specific route, whatever the attributes say.
A worked choice
Say your network has two ways to reach 198.51.100.0/24. One route arrives from a provider you pay, with a short AS path of two networks. The other arrives from a customer, with a path of four networks.
If you set local preference higher on customer routes than on provider routes, which is the usual arrangement, the router chooses the longer customer path. Path length is never consulted, because the first comparison already settled the matter.
Now suppose both routes came from two providers you rank equally. Local preference ties, so the shorter AS path wins. If the paths were also equally long, the router would move on to origin, then MED where it applies, and down the list.
Steering inbound traffic
You control what leaves your network far more easily than what arrives, because other networks make their own choices. Operators have a few levers. They can prepend their own AS number to make one path look longer. They can announce more specific prefixes through the link they prefer, since longest match overrides path attributes. They can attach communities that a provider has agreed to act on, such as lowering the preference it gives the route. And they can send MED hints to a neighbor that connects at several points.
Each lever is a request more than a command, with the exception of more specific prefixes, which work strongly but add entries to everyone's tables. Good practice is to change one thing, then observe from outside viewpoints before changing another.
What a looking glass shows
A looking glass is a web page or service that lets you query a router belonging to some network. Ask it about a prefix and you typically see the paths it knows, which one it selected and the attributes on each. Comparing several viewpoints reveals whether a prefix is visible everywhere, whether the origin is the one you expect and whether some region is taking an odd detour. It is read-only information, and it is often the quickest way to tell a routing problem from a problem inside a server or application.
Money shapes the routes
Network relationships usually fall into a few types:
| Relationship | What is exchanged | Typical preference |
|---|---|---|
| Customer | Provider gives access to everything it can reach | Most preferred |
| Peer | Each side offers its own and its customers' routes | Middle |
| Transit provider | You pay to reach the rest of the internet | Least preferred |
A network tends to prefer routes through customers, since that traffic earns money, then peers, then paid transit. It also should not carry traffic between two of its providers or peers, so it does not announce one's routes to another. Peering vs. transit covers the commercial side, and many peering sessions happen at internet exchange points.
Why a small mistake can travel far
Classic BGP has no built-in proof that a network has the right to announce a prefix, or that a path is genuine. Neighbors are expected to filter. When filters are missing, errors spread.
- A hijack is an announcement of address space by a network that is not entitled to it, by accident or intent. Traffic may be drawn to the wrong place.
- A route leak is the propagation of routes beyond where policy intended, for instance a customer passing one provider's routes to another. Traffic can flood a path that cannot carry it.
- A mistaken withdrawal can make a network, including its DNS servers, vanish from view.
BGP hijacks and route leaks covers these in detail, and BGP mistakes that have taken parts of the internet offline shows how they play out.
The defenses
Operators layer several protections:
- Prefix filters: accept from a customer only the ranges it is registered to use.
- Maximum prefix limits: shut or restrict a session if a neighbor suddenly sends far more routes than expected.
- Route validation: RPKI lets address holders publish signed statements of which AS may originate their prefixes, so others can reject mismatches. This checks the origin, not the whole path.
- Session protection: authentication on sessions and accepting packets only from directly connected neighbors.
- Operational discipline: staged changes, reviews and monitoring from outside viewpoints.
Adoption varies between networks, and no single measure covers every case.
Convergence takes time
When a path fails, withdrawals and alternatives propagate router by router. Some routers may briefly try paths that are already gone. Reaching a stable picture can take from seconds to minutes. Timers that limit how often updates are sent, and mechanisms that suppress unstable routes, trade speed for calm.
Very small prefixes are commonly filtered to protect table size. By convention, many networks will not accept anything longer than a /24 in IPv4 or a /48 in IPv6 from the wider internet.
Do you need to care?
If you have one internet connection at home or in a small office, you do not run BGP. Your provider handles it. It starts to matter when an organization connects to two or more providers with its own address space and wants traffic to survive the loss of either. That requires an AS number, suitable addresses, capable routers and someone who understands filtering, because a mistake affects more than your own network.
For everyone else, BGP is useful background. When a service is unreachable from some places but fine from others, routing between networks is one of the suspects, and public route viewers, often called looking glasses, let you see what various networks currently believe about a prefix.