Kestrel Exams kestrel logo KESTREL EXAMSPRACTICE TO PASS
Networking13 min read

BGP: How the Internet Routes Between Networks

OSPF finds cheap paths inside one network. Between networks, cheapness is not the main question: who is allowed to carry whose traffic is. BGP is the one protocol every network on the Internet speaks to answer that, and it is what holds the Internet together.

Practice this topic in the free app →All Networking guides

Chapter 5 · Inter-AS routing
Built from

Kurose and Ross, Computer Networking: A Top-Down Approach, section 5.4, cross-checked against RFC 4271 (A Border Gateway Protocol 4 (BGP-4)). The examples use AS numbers and addresses reserved for documentation (RFC 5398 and RFC 5737), so none of them belong to a real network. Every route choice below was computed in code.

How to use it

Learn the two attributes, AS-PATH and NEXT-HOP, by tracing one advertisement across three networks. Then memorize the selection order as a funnel: each step only breaks ties left by the step before. Most exam questions are one of those two skills.

Source note

The four-step selection order below is the textbook’s simplified version. The full decision process in RFC 4271 adds more tie-breakers between steps 2 and 3 (lowest ORIGIN, lowest MED, eBGP over iBGP), and router vendors add steps of their own, some even before local preference. Local preference, then AS-PATH length, then the others, is the order to learn.

The picture: signposts passed down a road#

Picture a chain of towns along a road. The town of Lakeside puts up a sign: “Lakeside: this way”. The next town, Millbrook, copies it and adds its own name: “Lakeside: via Millbrook, then Lakeside”. The town after that adds its name too. Soon every town has a sign listing the towns you pass through on the way to Lakeside.

Some towns have signs from two directions, and pick one. And some towns refuse to put up a sign at all, because they do not want strangers’ trucks driving through their streets.

That is BGP. Each sign is a route advertisement. The list of towns is the AS-PATH. The refusal is policy.

Vocabulary, before anything uses it#

TermWhat it means
BGPBorder Gateway Protocol, version 4: the inter-AS routing protocol of the whole Internet.
PrefixA block of addresses written in CIDR form, such as 203.0.113.0/24. BGP advertises prefixes, not single hosts.
BGP sessionA TCP connection between two routers, called peers, over which they swap BGP messages.
eBGPExternal BGP: a session between routers in two different ASes.
iBGPInternal BGP: a session between two routers in the same AS.
AttributeA piece of information attached to an advertised prefix. A route is a prefix plus its attributes.
AS-PATHThe list of ASes an advertisement has passed through, newest first.
NEXT-HOPThe IP address of the router interface where the AS-PATH begins: the doorway into the next AS.
Import policyAn AS’s rules for which advertised routes to accept, reject, or rank.
Local preferenceA ranking number the AS’s administrator gives a route. Higher wins.
Hot-potato routingChoosing the exit gateway that is cheapest to reach from inside your own AS.
Transit trafficTraffic that crosses a network when neither its source nor its destination is in that network.

What BGP does, and the two kinds of session#

BGP is the glue of the Internet. Without it, every AS would be an island. With it, a subnet anywhere can announce “I exist, and here is how to reach me”, and the rest of the Internet learns the way. BGP gives each router two jobs:

  1. Learn which prefixes can be reached through each neighboring AS.
  2. Pick the best route to each prefix, using policy and the routes’ attributes, and record it in the forwarding table.

Routers exchange BGP messages over semi-permanent TCP connections on port 179. TCP makes the messages reliable and in order, so BGP itself does not have to. The main message types are OPEN (start a session), UPDATE (advertise or withdraw routes), KEEPALIVE (“I am still here”) and NOTIFICATION (report an error and close).

eBGPiBGP
Runs betweenGateway routers in two different ASesRouters inside the same AS
Its jobLearn routes from, and offer routes to, a neighboring ASSpread the routes learned outside to every router inside
Physical link?Usually a direct link between the two gatewaysNot needed: it is a TCP connection that may cross several internal routers
Do not confuse these
iBGP is not an intra-AS routing protocol. OSPF finds paths inside the AS, including the path to each gateway. iBGP only tells the inside routers about outside prefixes. A router needs both: iBGP says “that prefix is reachable through gateway G”, and OSPF says how to reach G.

Advertising a route through three ASes#

When a router advertises a prefix, it attaches attributes. The two that matter most:

  • AS-PATH. Each AS that passes the advertisement to another AS adds its own number to the front. This also stops loops: if an AS sees its own number already in an AS-PATH, it rejects the advertisement.
  • NEXT-HOP. The address of the interface where the path begins, on the router in the neighboring AS.
Worked example

Three ASes, numbered 64501, 64502 and 64503. Router names start with their AS’s last digit. AS 64503 owns the prefix 203.0.113.0/24, which holds 256 addresses, 203.0.113.0 to 203.0.113.255.

AS 64502 is linked to both: router 2c faces 3a, and router 2a faces router 1c in AS 64501. AS 64501 also has its own direct link to AS 64503, from router 1b to router 3d.

StepSessionPrefixAS-PATHNEXT-HOP
1. 3a → 2ceBGP203.0.113.0/2464503192.0.2.1 (3a’s interface)
2. 2c → every router in AS 64502iBGP203.0.113.0/2464503 (unchanged)192.0.2.1 (unchanged)
3. 2a → 1ceBGP203.0.113.0/2464502 64503198.51.100.1 (2a’s interface)
4. 1c → every router in AS 64501iBGP203.0.113.0/2464502 64503198.51.100.1
5. 3d → 1b, then iBGP inside AS 64501eBGP203.0.113.0/2464503198.51.100.9 (3d’s interface)

Now a router inside AS 64501, say 1d, knows two routes to the same prefix. One has AS-PATH 64502 64503, length 2, through 1c. The other has AS-PATH 64503, length 1, through 1b. With no policy saying otherwise, 1d picks the length-1 route. It then asks OSPF for the path to 1b and writes “203.0.113.0/24: send toward 1b” in its forwarding table.

Import policy and choosing among routes#

A gateway first applies import policy. The administrator may reject a route outright, for example because it passes through an AS they do not trust or do not pay. The routes that survive are ranked. A router with several routes to the same prefix runs them through a funnel. Each step only breaks ties left by the step before:

  1. Highest local preference. This is pure policy, set by the administrator.
  2. Shortest AS-PATH. Fewer ASes crossed.
  3. Closest NEXT-HOP router, by intra-AS cost. This is hot-potato routing: get the packet out of your own network as cheaply as you can.
  4. Additional criteria, such as the lowest BGP router ID (a unique number each BGP router is configured with), to break any tie still left.
Worked example

A router R in AS 64501 has three routes to 203.0.113.0/24 (owned here by AS 64510). Its OSPF cost to each exit gateway is shown in the last column.

RouteLocal prefAS-PATHAS-PATH lengthR’s cost to its gateway
A, via gateway G110064502 6451027
B, via gateway G210064503 6451024
C, via gateway G310064505 64506 6451032

Step 1: all three have local preference 100, so all survive. Step 2: A and B have length 2, so C is out. Step 3: B’s gateway costs 4, A’s costs 7, so R picks route B.

Notice that G3 was the cheapest exit of all, at cost 2. It did not matter, because C was already gone at step 2. Hot potato only breaks ties among the survivors. And if the administrator gives C a local preference of 200, C wins at step 1, despite its longer path.

Do not confuse these
BGP does not find the shortest path. It finds the path policy likes best. Local preference beats AS-PATH length. AS-PATH length counts ASes, not routers or delay. Hot potato is selfish: it minimizes cost inside your AS, even if the total end-to-end path gets longer.

Policy: who carries whose traffic#

Networks connect as customers and providers. A customer pays a provider to carry its traffic to and from the rest of the Internet. Two rules follow from the money.

A customer network carries no transit traffic

A stub network is one whose traffic always starts or ends inside it. Many are multi-homed: connected to two providers, for backup or to share load. Such a network must not let traffic flow between its two providers through itself. It enforces this through what it advertises: it tells each provider only about its own prefixes.

Worked example

A dental chain’s network is a customer of two providers, ISP North and ISP South. It learns from North a route to some far-away prefix. If it re-advertised that route to South, South might send traffic for that prefix through the dental chain’s small links. So the chain advertises only its own prefix to each provider. Neither provider then has any route through the chain, and no transit traffic appears.

A provider carries traffic only for its customers

A common rule of thumb: traffic crossing a provider’s backbone should have its source or its destination (or both) in one of that provider’s customers. Say ISP North learns from ISP South a route to one of South’s customers. North does not pass that route on to a third provider, ISP East. If it did, traffic between East and South’s customer could cross North’s backbone, and nobody would be paying North for it. Large providers usually exchange traffic for free only for their own customers, through deals called peering.

Why intra-AS and inter-AS routing differ#

Intra-AS (e.g. OSPF)Inter-AS (BGP)
PolicyOne owner, so policy matters much less.Owners care who carries their traffic and whose they carry. Policy comes first.
ScaleOne network. Areas help if it is big.The whole Internet. Advertising prefixes, and aggregating them, keeps tables manageable.
PerformanceThe main goal: lowest-cost paths.Secondary: a cheaper path loses to a preferred one.
One sentence to carry into the exam

BGP lets each AS advertise its prefixes to its neighbors over eBGP and spread what it learns inside over iBGP, all on TCP port 179; each route is a prefix plus attributes such as AS-PATH and NEXT-HOP, and a router chooses among routes by local preference, then shortest AS-PATH, then the closest NEXT-HOP (hot potato), with policy deciding what is advertised at all.

Practice#

For selection questions, run the funnel in order and cross out routes at each step. Never skip ahead to the step you like.

  1. eBGP or iBGP? (a) A gateway in AS 64496 and a gateway in AS 64497, joined by a direct link. (b) Two routers in AS 64496 that are four hops apart.
  2. AS 64507 owns 192.0.2.0/24. The advertisement passes from AS 64507 to AS 64508, then to AS 64509, then to AS 64511. What AS-PATH does AS 64511 receive?
  3. AS 64508 receives an advertisement with AS-PATH 64509 64508 64507. What does it do, and why?
  4. A router has four routes to one prefix. P: local pref 150, AS-PATH length 3, gateway cost 1. Q: 150, length 2, cost 9. R: 150, length 2, cost 3. S: 100, length 1, cost 2. Which route does it choose?
  5. In question 4, the administrator changes S’s local preference to 300. Which route wins now?
  6. Why is hot-potato routing called selfish?
  7. A school district’s network is a customer of two providers. Should it re-advertise to one provider the routes it learns from the other? Why?
  8. What transport protocol and port does BGP use, and what does that save BGP from doing?

Solutions#

Best study method

Find the first line where your working split from the solution, and redo the problem from that line. Do not just read the answer and nod.

  1. (a) eBGP. (b) iBGP. Different ASes means external BGP. Same AS means internal BGP, and the session runs over TCP across the internal routers, so no direct link is needed.
  2. 64509 64508 64507. Each AS adds its number to the front as it passes the route on. AS 64507 starts it, 64508 adds itself, and 64509 adds itself before sending to 64511.
  3. It rejects the advertisement. Its own number, 64508, is already in the AS-PATH, so accepting it would create a routing loop.
  4. R. Step 1: highest local pref is 150, so S is out (its short path does not save it). Step 2: Q and R have length 2, so P is out. Step 3: R’s gateway costs 3, Q’s costs 9, so R wins.
  5. S. Its local preference, 300, is now the highest, so it is the only route left after step 1. Nothing later is checked.
  6. It minimizes cost inside your own AS only. The router dumps the packet out of the nearest exit, the “hot potato”. It ignores how far the packet then has to travel in other networks, so the end-to-end path can be longer.
  7. No. It is a stub customer, not a transit network. If it re-advertised, one provider could route traffic for the other through the district’s links. So it advertises only its own prefixes to each provider.
  8. TCP, port 179. TCP delivers the messages reliably and in order, so BGP does not need its own retransmission or reordering, unlike OSPF, which runs straight over IP.

Test-readiness checklist#

  • ☐  I can say what BGP lets a subnet do, and list each router’s two BGP jobs.
  • ☐  I can tell eBGP from iBGP, and say BGP runs over TCP port 179.
  • ☐  I can define a route as a prefix plus attributes, and explain AS-PATH and NEXT-HOP.
  • ☐  I can trace an advertisement through several ASes and write the AS-PATH at each step.
  • ☐  I can explain how AS-PATH prevents loops.
  • ☐  I can run the selection funnel: local preference, shortest AS-PATH, closest NEXT-HOP, then other tie-breakers.
  • ☐  I can explain why a stub customer does not carry transit traffic, and how it enforces that.
  • ☐  I can give the three reasons intra-AS and inter-AS routing differ: policy, scale and performance.
One last rule

In any BGP choice, policy beats distance. Check local preference first, every time, before you count a single AS.

Frequently asked questions#

What is BGP?

BGP, the Border Gateway Protocol, is the inter-AS routing protocol of the Internet. It lets each autonomous system advertise the address prefixes it can reach to its neighbors, and lets every router choose among the routes it hears using policy and path attributes.

What is the difference between eBGP and iBGP?

eBGP runs between gateway routers in two different autonomous systems and exchanges routes between them. iBGP runs between routers in the same autonomous system and spreads the routes learned from outside to every router inside. Both run over TCP port 179.

What are AS-PATH and NEXT-HOP?

AS-PATH is the list of autonomous systems a route advertisement has passed through, and each AS adds itself to the front. NEXT-HOP is the IP address of the router interface where that path begins, in the neighboring AS.

How does BGP choose a route?

In the simplified order: highest local preference, then shortest AS-PATH, then the closest NEXT-HOP router by intra-AS cost, known as hot-potato routing, then other tie-breakers. Each step only breaks ties left by the step before.

Suggest a change

Something here not clear? A topic you wish we covered? Tell us. We read every message, and a request is the fastest way to get a guide written.

Test yourself on this topic

The free networking practice app has 10 questions on this guide and 500 across the whole course, each with an explanation for every answer. Offline, no ads, no account.

Practise this topic →