The internet has no central routing table and nobody in charge of one. It is many thousands of independent networks, each with a number, telling their neighbours which IP addresses they can reach. The protocol they use is BGP, the Border Gateway Protocol, and the numbers are autonomous system numbers, or ASNs.
This post builds BGP up from those numbers. Every example uses address blocks and ASNs reserved for documentation, so nothing here belongs to a real network.
Autonomous systems and their numbers
An autonomous system (AS) is a group of IP prefixes run by one or more operators under a single, clearly defined routing policy. That is the definition in RFC 1930. An ISP is an AS. So is a university or a company that manages its own connections to more than one ISP. From outside it looks like one unit with one consistent view of what it can reach.
Each AS is identified by an ASN. IANA holds the whole number space and allocates blocks of it to five regional internet registries: AFRINIC, APNIC, ARIN, LACNIC and the RIPE NCC. The registries assign individual numbers to network operators. APNIC's region also has seven national internet registries that do this work locally. India's is IRINN, a division of the National Internet Exchange of India (NIXI), which APNIC recognised in 2012.
ASNs were first 16 bits long, giving 65,536 values. RFC 6793 extended them to 32 bits, about 4.29 billion values, so that the internet would not run out. The registry reserves a few ranges worth recognising:
| Range | Purpose |
|---|---|
| 0 and 65535 | Reserved, never assigned |
| 23456 | AS_TRANS, a stand-in used during the 16-bit to 32-bit transition |
| 64496 to 64511 | Documentation and sample code (16-bit) |
| 64512 to 65534 | Private use (16-bit) |
| 65536 to 65551 | Documentation and sample code (32-bit) |
| 4200000000 to 4294967294 | Private use (32-bit) |
Private ASNs work like private IP addresses: anyone may use them internally, but they must be stripped from the path before a route reaches the public internet. AS_TRANS exists because an old router that only understands 16-bit numbers cannot carry AS65536 in its path. It sees 23456 in that position, and the true number travels alongside in a separate attribute called AS4_PATH.
BGP sessions and the AS path
Two routers that speak BGP to each other hold a session. It runs over TCP on port 179, so BGP needs no retransmission or ordering logic of its own. A session between routers in different ASes is external BGP (eBGP). A session between routers inside the same AS is internal BGP (iBGP).
There are only four message types in the base protocol, each between 19 and 4,096 bytes long:
| Message | What it does |
|---|---|
| OPEN | First message after the TCP handshake. Carries the BGP version, the sender's AS number, a proposed hold time and a router identifier. |
| UPDATE | Announces prefixes with their path attributes, withdraws prefixes that are no longer reachable, or both. |
| KEEPALIVE | Confirms an OPEN, then proves the peer is alive. The suggested interval is one third of the hold time. |
| NOTIFICATION | Reports an error. The session is closed straight after. |
After the OPEN exchange, each side sends the full set of routes its policy allows. From then on only changes are sent; there is no periodic refresh. If nothing at all arrives within the hold time (the suggested default is 90 seconds), the session is declared dead and every route learned over it is removed.

iBGP has two rules of its own. A router does not add its AS number to the path when it passes a route to an internal peer, and it does not pass a route learned from one internal peer on to another. The second rule prevents loops inside the AS, at the cost that its BGP routers must all be connected to each other, directly or through a route reflector.
Every route in an UPDATE carries a list called AS_PATH. When an AS originates a prefix, the path contains only its own number. Each time the route crosses an eBGP session, the sending AS puts its number at the front. In the diagram above, AS64500 originates 203.0.113.0/24, and by the time the route reaches AS64510 it looks like this:
Prefix 203.0.113.0/24
AS path 65536 64496 64500
Next hop 192.0.2.1
Origin IGP
Read the path from right to left. The rightmost number, 64500, is the origin AS, the network that claims to own the prefix. The leftmost, 65536, is the neighbour that handed the route over. Packets travel the other way: left to right through the list.
The path does two jobs. It is a rough measure of distance, and it prevents loops. A router that receives a route with its own ASN already in the path discards it, because accepting it would send traffic in a circle.
How a router picks one route
A well-connected router often hears several routes to the same prefix. BGP's decision process, defined in RFC 4271, removes candidates in a fixed order until one is left:
- Highest
LOCAL_PREF, a number the AS sets by its own policy. It is carried on iBGP only and never sent to another AS. - Shortest AS path, counted in AS numbers.
- Lowest
ORIGINcode (IGP, then EGP, then INCOMPLETE). - Lowest
MULTI_EXIT_DISC(MED), compared only between routes from the same neighbouring AS. A neighbour sets it to say which of several links it would like used. - A route learned over eBGP beats one learned over iBGP.
- Lowest interior cost to the route's next hop.
- Lowest BGP Identifier of the advertising router, then lowest peer address.
Router software adds a few steps of its own, but this is the core order. Here is a worked case. Router R1 in AS64510 has four routes to 203.0.113.0/24. The operator's policy gives routes from peers a LOCAL_PREF of 200 and routes from its paid provider 100.
| Route | Learned from | LOCAL_PREF | AS path | Origin | MED |
|---|---|---|---|---|---|
| A | provider AS65536, eBGP | 100 | 65536 64496 64500 | IGP | none |
| B | peer AS64499, eBGP | 200 | 64499 64497 64500 64500 64500 | IGP | none |
| C | peer AS64498, eBGP | 200 | 64498 64500 | IGP | 50 |
| D | peer AS64498, via iBGP from router R2 | 200 | 64498 64500 | IGP | 10 |
Step 1 removes A, because policy comes before everything else. Step 2 removes B, whose path has five entries against two. Step 3 is a tie. Step 4 compares C and D, which come from the same neighbour, and D has the lower MED. D wins, so R1 forwards this traffic across its own network to R2's link. Step 5 never runs. Had the MEDs been equal, it would have chosen C.
Customers, providers, peers and shaping traffic
Policy mostly follows money. A neighbouring AS is one of three things to you: a customer, a provider or a peer.
- A customer pays a provider for transit: the provider carries the customer's traffic to and from the rest of the internet.
- Two peers exchange traffic for each other's own networks and customers, and nothing more.
From this comes the export rule that RFC 9234 writes down and that textbooks call valley-free routing. Routes you originate or learn from a customer may be announced to everyone. Routes you learn from a peer or a provider may be announced only to your customers. A path therefore climbs through providers, crosses at most one peer link and then descends through customers. It never goes down and back up, because a customer would then be carrying traffic between two of its providers for free.
Peering needs a physical meeting place. An internet exchange point (IXP) is a neutral site where many networks plug into a shared switch and exchange local traffic directly. An IXP usually also runs a route server: a network opens one BGP session with it and exchanges routes with many members at once.

An AS chooses its outbound paths with LOCAL_PREF. Influencing how traffic comes in is harder, because that is other networks' decision. There are three main tools:
- AS path prepending. An AS may add its own number several times, as AS64500 did towards AS64497 in route B. The path looks longer, so distant networks tend to prefer the other entrance. It is only a hint: any network whose
LOCAL_PREFfavours that path will still use it. - Communities. A community is a 32-bit tag attached to a route. By convention the first 16 bits hold an ASN and the rest a value whose meaning that AS defines. Two well-known values are
NO_EXPORT(do not announce outside this AS) andNO_ADVERTISE(do not announce to any peer). - More-specific prefixes. Forwarding uses longest prefix match, so a /24 beats the /22 that contains it wherever both are visible, whatever their BGP attributes say. In practice, IPv4 prefixes longer than /24 are generally not accepted between networks.
What goes wrong: hijacks and leaks
The base protocol gives a router no way to check that an AS is entitled to announce a prefix.
A hijack is an AS originating a prefix that is not its own. The textbook case is 24 February 2008. According to the RIPE NCC's analysis, Pakistan Telecom (AS17557) announced 208.65.153.0/24, part of a /22 that YouTube announced, and its provider passed the route on. Because the /24 was more specific, routers around the world preferred it for about two hours and a quarter. YouTube responded by announcing two /25s, which were more specific again, before the provider withdrew the false route.
A route leak is different: the origin is genuine, but the route is passed on beyond its intended scope, breaking the valley-free rule. On 24 June 2019, Cloudflare reported, a small ISP's route optimiser generated more-specific versions of many prefixes, a customer of that ISP passed them to its other provider, Verizon, and Verizon announced them onwards. Traffic for Cloudflare, Amazon and others was drawn towards networks that could not carry it. A different kind of failure, a network withdrawing its own routes, is covered in the 2021 Facebook outage.
The defences are layered:
- Prefix filtering. A provider accepts from each customer only the prefixes that customer is registered to announce. RFC 8212 goes further: an eBGP session with no policy configured should accept and announce nothing.
- Maximum-prefix limits. If a neighbour suddenly sends far more routes than expected, the session is shut down.
- RPKI route origin validation. The holder of a prefix publishes a signed record naming the AS allowed to originate it and the longest prefix length permitted. A router then marks each route Valid, Invalid or NotFound and can drop the Invalid ones. This checks the origin only, not the rest of the path.
- Roles and the Only to Customer attribute. RFC 9234 lets two routers agree their relationship in the OPEN message and marks routes that must only travel downwards, so that a leak can be detected.
Seeing it for yourself
- Route collectors. The RIPE NCC's Routing Information Service and the University of Oregon's RouteViews project run collectors, many at exchange points, that peer with volunteer networks and archive what they hear. RIPEstat lets you search that data by prefix or ASN, RIS Live streams updates as they happen, and RouteViews offers a web looking glass.
- Looking glasses. Many operators run a public page where you can ask one of their routers what route it holds for a prefix. Look for the same fields as the example above: prefix, AS path, next hop, origin.
- whois. Running
whoisagainst an ASN from an AS path returns the registry's record of who holds it. The IANA registry lists which regional registry's whois server is responsible for each block of numbers.
The computer science underneath
Path vector routing. BGP is neither of the two classic families. A distance vector protocol shares only a cost per destination, which is why it suffers from the count-to-infinity problem described in distance vector versus link state. BGP shares the whole list of ASes, so a loop is visible the moment it would form. See how BGP routes the internet for the attributes one by one.
Policy over metrics. Inside one network, a router minimises a cost, as covered in how routers choose paths. Between networks there is no shared cost, so BGP's first step is a local preference and shortest path is only a tie-breaker. Nothing in the process measures bandwidth, latency or load.
Longest prefix match. BGP chooses the best route per prefix. The forwarding table then chooses the most specific prefix per packet, as explained in longest prefix match and the FIB. Hijacks with more-specific prefixes work in the gap between those two steps.
Anycast. One prefix can be announced from many places, and each network follows its best path to one of them. That is all anycast is.
What an interviewer could ask from this
What is an autonomous system, and why does it need a number? A set of IP prefixes under one routing policy. The number identifies it in AS paths, which is how other networks apply policy and detect loops.
How does BGP prevent routing loops? Each AS adds its ASN to the path when it sends a route to another AS. A router discards any route whose path already contains its own ASN. Inside an AS, routes learned over iBGP are not passed to other iBGP peers.
Which comes first, local preference or AS path length? Local preference. A longer path with a higher local preference wins, which is why prepending cannot force another network's choice.
What is the difference between a hijack and a route leak? In a hijack the wrong AS originates the prefix. In a leak the origin is correct but a route is re-announced where policy says it should not go, such as from one provider to another.
What does RPKI origin validation protect against, and what does it not? It catches a route whose origin AS or prefix length is not authorised by the prefix holder. It does not validate the rest of the AS path.
Takeaways
- The internet is a graph of autonomous systems, and BGP is how they tell each other what they can reach.
- The AS path is both the loop check and a tie-breaker. Read it right to left: origin first, nearest neighbour last.
- Route selection is policy first. Local preference outranks path length, and nothing in the process measures speed.
- Valley-free export rules keep traffic flowing along paid relationships. A leak is a broken rule and a hijack is a false origin.
- Filtering, max-prefix limits and RPKI exist because the base protocol trusts whatever a neighbour announces.












