Sub topic 22
BGP and Path-Vector Routing Between Autonomous Systems
This chapter assumes you know distance-vector and link-state routing, the difference between forwarding and routing, and CIDR prefixes with route aggregation. It covers the routing protocol that connects the networks of the Internet.
Why a third kind of routing is needed
RIP and OSPF work inside one organisation. They assume every router wants the cheapest path and that all routers share one metric. Between organisations neither assumption holds. A provider may refuse to carry traffic for a competitor, even when that path is shorter. Costs also cannot be compared across networks, because one network's cost 10 means nothing in another.
An autonomous system (AS) is a set of networks and routers under one administrative control with a single routing policy. Each AS has a number, such as AS 65001. Routing inside an AS uses an interior gateway protocol (IGP) such as RIP or OSPF. Routing between ASes uses an exterior gateway protocol. On the Internet, that protocol is BGP-4 (Border Gateway Protocol).
The path-vector idea
A path vector is like a distance vector, except that each route carries the full list of ASes it passes through instead of a numeric distance. A BGP route to a prefix includes:
- the destination prefix in CIDR form, such as
203.0.113.0/24 - the AS_PATH, the ordered list of ASes the advertisement has crossed
- the NEXT_HOP, which is the address of the router to send packets to
- other attributes, such as LOCAL_PREF
When a router passes a route to a neighbouring AS, it prepends its own AS number to the AS_PATH.
The path list gives BGP two things that distance vector lacks:
- Loop detection by inspection. If a router sees its own AS number in a received AS_PATH, it discards the route. Count-to-infinity cannot occur this way, so BGP needs neither split horizon nor poison reverse.
- A basis for policy. An AS can reject or prefer routes that traverse particular ASes.
eBGP, iBGP and the session
- eBGP runs between border routers in different ASes.
- iBGP runs between routers in the same AS. It spreads externally learned routes to the interior routers. It does not prepend the AS number, so a route learned over iBGP is not re-advertised to other iBGP peers. This is why iBGP needs a full mesh or route reflectors.
BGP peers form a session over TCP port 179. They keep it open instead of re-flooding periodically. The four message types are:
| Message | Purpose |
|---|---|
| OPEN | Starts the session and exchanges AS number and capabilities |
| UPDATE | Announces new routes or withdraws old ones |
| KEEPALIVE | Confirms the peer is alive and acknowledges OPEN |
| NOTIFICATION | Reports an error and closes the session |
After the initial table exchange, only changes are sent.
Route selection
A router often hears about the same prefix from several neighbours. It picks one best route using a fixed order of tie-breakers. The first rules are:
- Highest LOCAL_PREF, which is set by local policy.
- Shortest AS_PATH.
- Lowest origin type, then lowest MED, then eBGP over iBGP.
- Lowest IGP cost to the NEXT_HOP, then lowest router ID.
Policy comes before path length. An AS that prefers a cheap customer link will choose it even when the AS_PATH is longer.
The following program models loop rejection and selection.
MY_AS = 65001
routes = {}
def receive(prefix, as_path, local_pref=100):
if MY_AS in as_path:
return False
routes.setdefault(prefix, []).append((local_pref, as_path))
return True
def best(prefix):
return min(routes[prefix], key=lambda r: (-r[0], len(r[1])))
p = "203.0.113.0/24"
print(receive(p, [65002, 65010]))
print(receive(p, [65003, 65004, 65010]))
print(receive(p, [65005, 65001, 65010]))
print(best(p))
print(receive(p, [65006, 65007, 65010], local_pref=200))
print(best(p))The output is True, True, False, then (100, [65002, 65010]), then True, then (200, [65006, 65007, 65010]). The third route contains 65001, so it is dropped. The shortest path wins until a higher LOCAL_PREF overrides it.
BGP and aggregation
BGP carries CIDR prefixes, so it uses route aggregation. An AS that owns 192.168.0.0/24 and 192.168.1.0/24 can announce one 192.168.0.0/23. Without this, the global routing table would grow far larger. Routers still forward by longest-prefix match, so a more specific announcement overrides an aggregate.
Comparison with the other protocols
| Property | RIP | OSPF | BGP |
|---|---|---|---|
| Family | Distance vector | Link state | Path vector |
| Scope | Inside an AS | Inside an AS | Between ASes |
| Metric | Hop count, with 16 as infinity | Link cost | Policy, then AS_PATH length |
| Transport | UDP | Directly over IP | TCP port 179 |
| Loop handling | Split horizon, poison reverse | Full topology map | AS number in path |
Failure modes and edge cases
- Slow convergence. Withdrawals can cause routers to try several alternative paths in turn, so convergence takes minutes.
- Trust. BGP accepts what neighbours announce. An AS that announces a prefix it does not own, or a more specific prefix, can attract traffic. This is a prefix hijack.
- Shortest AS path is not shortest distance. One AS hop may span a continent, and the metric ignores that.
Exercises
Exercise
A router in AS 100 receives an UPDATE for a prefix with AS_PATH 300 200 100 400. What does it do, and why?
Show answer
It discards the route. Its own AS number, 100, appears in the AS_PATH, so accepting it would create a routing loop.
Exercise
A router has two routes to one prefix. Route X has LOCALPREF 100 and ASPATH length 2. Route Y has LOCALPREF 150 and ASPATH length 5. Which is chosen, and which rule decides it?
Show answer
Route Y. Highest LOCALPREF is checked before ASPATH length, so the longer path wins.
Exercise
Why does BGP not need split horizon or poison reverse to avoid count-to-infinity?
Show answer
Each route carries the full AS_PATH, so a router can see its own AS number and reject a looping route. Distance vector only has a distance number, so it cannot detect this.