How BGP Works: AS Paths, Path Attributes, and Why the Internet Is Not Shortest-Path
A packet from a laptop in Mumbai to a server in Frankfurt does not ask the Internet for the shortest path. It follows a route that some network accepted, preferred for business reasons, and installed in a forwarding table. The protocol that carries those inter-domain choices is BGP, the Border Gateway Protocol. Version 4, specified in RFC 4271, is what autonomous systems use to tell each other which prefixes they can reach and which sequence of networks a packet would cross.
That is a different job from the protocols already on the path after a route exists. TCP delivers a byte stream once an address is chosen. UDP does not even do that. DNS turns a name into an address. None of them decides which network is allowed to announce that address, or which neighboring network should be used to reach it.
What an autonomous system actually is
An autonomous system (AS) is a set of routers under one routing policy, identified by an autonomous system number. The number is 16 or 32 bits. A university, a transit provider, a content network, and a small enterprise that owns its own addresses can each be an AS. Inside the AS, an interior gateway protocol such as OSPF or IS-IS usually computes paths from link metrics. Between autonomous systems, that shared metric does not exist. Network A does not trust network B's link costs, and network B may not want A's traffic at all.
BGP is the protocol that crosses that boundary. A BGP session is a TCP connection to port 179 between two routers that have been configured as neighbors. TCP is a deliberate choice: BGP needs reliable, ordered delivery of routing updates, and it does not implement its own retransmission. The session stays up and exchanges incremental updates. It is not a request per packet.
Neighbors in different autonomous systems form an external BGP session (eBGP). Neighbors in the same AS form an internal session (iBGP). The message format is the same. The rules for what may be re-advertised, and for how the next hop is rewritten, are not.
Path vector, not shortest path
Link-state protocols flood every adjacency and run a shortest-path computation. Classic distance-vector protocols advertise a cost to a prefix and can count to infinity when a route loops. BGP is a path-vector protocol. An advertisement carries the list of autonomous systems the route has already traversed, in the AS_PATH attribute.
Loop detection is structural. If a router sees its own AS number in the path, it discards the route. That is why a routing loop across providers does not require a count-to-infinity timer. It also means the path is a policy object, not a distance. An operator can prepend their own AS number several times. The path still reaches the same place. It simply looks longer to everyone downstream, so those networks are less likely to prefer it. AS-path length is a count of autonomous systems, not of routers or fiber miles. A two-AS path can be the slow one.
What an update actually contains
BGP speakers exchange four message types: OPEN, UPDATE, KEEPALIVE, and NOTIFICATION. OPEN negotiates the session, including the local AS and a hold time. KEEPALIVE resets that hold timer. NOTIFICATION closes the session on an error. The routing state moves in UPDATE.
An UPDATE can withdraw prefixes and advertise others in the same message. An advertisement has network layer reachability information (the prefixes) and a set of path attributes. A few of those attributes are mandatory. Without them the route is not valid.
ORIGIN where the prefix was injected (IGP, EGP, incomplete)
AS_PATH autonomous systems already crossed
NEXT_HOP router that should receive packets for this prefix
LOCAL_PREF preference inside the receiving AS (not sent to eBGP peers)
MED multi-exit discriminator, a hint to one neighboring AS
COMMUNITY optional tag used for policy (RFC 1997)
ORIGIN, AS_PATH, and NEXT_HOP are well-known mandatory attributes. LOCAL_PREF is well-known discretionary: every BGP speaker understands it, but it is carried on iBGP, not on eBGP. MED is optional and non-transitive. Communities are optional and transitive, so they can travel with the route if later networks pass them on.
The next hop is the detail that surprises people who only know connected routes. On an eBGP session the advertising router usually sets NEXT_HOP to its own address on that session. On iBGP the next hop is left unchanged by default. A route learned from a customer at the edge can be re-advertised inside the AS with the customer's router still named as the next hop. Interior routers then need a route to that address, either from the IGP or because the edge router was configured to rewrite the next hop to itself.
How one prefix becomes one best path
BGP does not install every advertisement. For each prefix it runs a decision process and selects one best path to advertise onward. RFC 4271, section 9.1, splits that into a preference calculation and a tie break. Vendor knobs sit in front of the standard order. Cisco's weight is the usual example: it is local to the router, highest weight wins, and it is never sent to a peer.
After local vendor preferences, the comparison that operators actually design for looks like this:
1. Highest LOCAL_PREF
2. Locally originated route over a learned one
3. Shortest AS_PATH (an AS_SET counts as one)
4. Lowest ORIGIN (IGP, then EGP, then incomplete)
5. Lowest MED, normally only among routes from the same neighbor AS
6. eBGP-learned over iBGP-learned
7. Lowest IGP cost to the NEXT_HOP
8. Tie-break: oldest path on some routers, then lowest BGP identifier,
then lowest peer address
Shortest AS path is step three, not step one. A route with local preference 200 and an AS path of length four beats a route with local preference 100 and an AS path of length two. That is the mechanism behind the usual transit policy. Customer routes get a higher local preference than peer routes, and peer routes get a higher local preference than provider routes. The result is valley-free routing in the sense described by Gao and Rexford: a network does not provide free transit between two providers, and it does not send customer traffic onto a peer if a customer route exists. Nothing in the BGP state machine enforces that. Local preference does.
The IGP-cost step is hot-potato routing. Once policy has selected a class of route, the router prefers the exit closest to itself in the interior protocol. Two exits to the same provider can therefore be used by different cities even though the AS path is identical. Cold-potato behavior, where the network carries the packet farther in order to hand it off at a preferred interconnect, has to be built on purpose with MED, communities, or local preference. It is not the default.
Why iBGP is not a full copy of eBGP
An iBGP speaker does not re-advertise a route learned from one iBGP peer to another iBGP peer. That split-horizon rule prevents an update from looping forever inside the AS, because iBGP does not add its own AS number to AS_PATH on each hop. The cost is obvious. Every BGP router that needs the full external view must either peer with every other BGP router, or learn those routes through something that is allowed to reflect them.
A route reflector, defined in RFC 4456, is that something. Clients peer with the reflector instead of with each other. The reflector re-advertises client routes to other clients and to non-clients. Confederations split one AS into private sub-AS numbers for the same reason. Both are scaling tools. Neither changes the fact that the forwarding next hop may still be the original edge router, and that every reflector client must be able to resolve it.
Withdrawals, timers, and slow convergence
When a prefix is no longer reachable, the speaker sends a withdrawal. Peers delete that path and may select an alternate they had kept, or withdraw the prefix further downstream if the withdrawn path was their best. There is no global recomputation. Convergence is a wave of updates and withdrawals limited by minimum route advertisement intervals. The classic eBGP MRAI is 30 seconds, which exists to stop a flapping prefix from being re-advertised continuously. Route flap damping can suppress a noisy prefix for longer. Many networks leave damping off, because a legitimate change after a link failure can look like a flap and then stay suppressed.
This is why the Internet routes around failure instantly is the wrong model. A session that drops withdraws the routes learned on it, but only after the hold timer expires if the failure is silent. Alternate paths propagate subject to policy and to advertisement timers. A more-specific prefix can also keep traffic on a dead path until that more-specific is withdrawn, because forwarding uses longest-prefix match on the selected routes, not a global shortest path.
What BGP does not do
BGP selects and distributes routes. It does not move the packet. The selected route is installed in the routing table and then in the forwarding table, where the lookup is longest-prefix match. Two covering prefixes can have different best paths. A /24 advertised by a customer wins over a /16 from a provider for addresses inside that /24, regardless of which announcement has the shorter AS path.
BGP also does not authenticate the Internet by itself. A session can be protected with TCP-AO or, in older deployments, a TCP MD5 signature, and operators filter prefixes by IRR data, RPKI origin validation, and max-prefix limits. Those checks sit beside the decision process. An UPDATE that survives the filters is still ranked by local preference and AS path, not by whether the origin is the geographically closest network.
The useful mental model is small. BGP is a policy database replicated over TCP sessions between autonomous systems. AS_PATH stops loops and acts as a coarse length. Local preference, communities, and MED express contracts. The forwarding plane only sees the route that survived that ranking.