Why BGP Is Called a Path Vector Protocol
Distance vector protocols such as RIP tell neighbors "I can reach this prefix at cost N". That is all they carry, so a router cannot tell whether a route it hears about already passed through itself. Counting to infinity is the price. BGP, defined in RFC 4271, takes a different approach: along with each prefix it carries the full list of autonomous systems the announcement has traversed. That list is the AS_PATH attribute, and it is what makes BGP a path vector protocol.
This post covers one thing: how the AS_PATH is built, how it prevents inter-AS loops, where it does not help (inside an AS), and how it doubles as a policy tool. If you want the session setup first, read BGP peering, the TCP session and FSM states. For the full decision process, see the UPDATE message and best path selection.
How the AS_PATH Is Built
AS_PATH is a well-known mandatory attribute inside an UPDATE message. It is made of segments, and each segment is either an AS_SEQUENCE (an ordered list) or an AS_SET (an unordered set, used mainly when aggregating routes). The rules from RFC 4271 section 5.1.2 boil down to this:
When a router originates a prefix and sends it to an eBGP peer, it puts its own AS number in the path.
When a router passes a route it learned to an eBGP peer, it prepends its own AS to the front of the path.
When a router sends a route to an iBGP peer, it does not modify the AS_PATH.
Take three ASes in a line: AS 65001 originates 192.0.2.0/24 (a documentation prefix), AS 65002 is in the middle, AS 65003 is at the far end. AS 65003 sees the route with AS_PATH 65002 65001. The leftmost AS is the neighbor that sent it. The rightmost is the origin.
# Illustrative output, Cisco-style syntax. Exact formatting varies by vendor and release.
R3# show ip bgp 192.0.2.0/24
BGP routing table entry for 192.0.2.0/24
Paths: (1 available, best #1)
65002 65001
10.0.23.2 from 10.0.23.2 (2.2.2.2)
Origin IGP, localpref 100, valid, external, bestNote that 4-byte AS numbers (RFC 6793) work the same way. Only the encoding on the wire differs.
The Loop Prevention Rule
The rule is simple. When a router receives a route and finds its own AS number anywhere in the AS_PATH, it treats the route as an AS loop and excludes it from the decision process (RFC 4271 section 9.1.2). In practice this check fires on routes from eBGP peers, because that is where the AS_PATH changes. The announcement has already been through this AS, so accepting it again could only create a forwarding loop.
Walk through it. Suppose the topology has a triangle: AS 65001, AS 65002 and AS 65003 all peer with each other.
AS 65001 originates 192.0.2.0/24 and sends it to both neighbors with path
65001.AS 65002 receives it, and then advertises it to AS 65003 as
65002 65001.AS 65003 now has two candidates:
65001directly, and65002 65001via AS 65002. It prefers the shorter one, as we will see below.AS 65003 would normally advertise its best route back to AS 65002 as
65003 65001. That is fine, no loop. But if AS 65002 ever re-advertised a path containing 65003 back to AS 65003, AS 65003 would drop it on sight because its own AS appears in the path.
The key point is that no timers, hop counters or "poison reverse" tricks are needed. The check is stateless: look for your own AS number in the list.
Path Length as the Metric
The same attribute also gives BGP its coarse "distance". In RFC 4271, once local preference has been compared (some vendors add earlier steps, such as Cisco's weight), the router prefers the route with the shortest AS_PATH. An AS_SEQUENCE counts one per AS number. An AS_SET counts as one in total, no matter how many AS numbers it holds. This is a hop count at AS granularity, not at router granularity, which is why BGP is a policy protocol rather than a shortest-path protocol. A path through two ASes can cross far more routers than a path through three.
What AS_PATH Does Not Protect: Inside an AS
Because a router does not prepend its AS when speaking to iBGP peers, the AS_PATH cannot detect loops inside one AS. Every iBGP peer shares the same AS number, so a looped route would look perfectly normal. BGP closes that gap with a different rule: a route learned from an iBGP peer is not re-advertised to other iBGP peers. This is why the classic design needs a full mesh of iBGP sessions.
Route reflectors (RFC 4456) relax the full-mesh requirement, and so they have to bring their own loop prevention. They add two attributes: ORIGINATOR_ID (the router ID of the route's originator inside the AS) and CLUSTER_LIST (the cluster IDs the route has passed through). A router that sees its own router ID as the originator, or its own cluster ID in the list, discards the route.
# Illustrative: a route reflector with one client (Cisco-style)
router bgp 65010
neighbor 10.0.0.2 remote-as 65010
neighbor 10.0.0.2 route-reflector-clientPrepending: Using the Rule for Policy
Since path length influences best path selection, operators lengthen the path on purpose to make a route less attractive to neighbors. This is AS path prepending. You repeat your own AS in the outbound path on the link you want to be the backup.
# Illustrative route-map, Cisco-style. Prefix and AS numbers are examples.
route-map BACKUP-OUT permit 10
set as-path prepend 65001 65001 65001
router bgp 65001
neighbor 198.51.100.1 remote-as 65099
neighbor 198.51.100.1 route-map BACKUP-OUT outRemember that prepending is only a hint. Remote networks that set a higher local preference for your route will ignore the longer path, because local preference is compared before AS_PATH length.
When You Need to Bypass the Check
Sometimes the loop check gets in the way. A common case is the same enterprise AS number used at multiple sites that connect through a provider's network. A route from site A crosses the provider and arrives at site B with the enterprise's own AS in the path, so site B drops it. Vendors offer knobs to deal with this, with different names and semantics:
allowas-in on the receiving router accepts routes whose AS_PATH contains the local AS, usually up to a configured number of occurrences.
as-override on the provider edge replaces the customer AS in the path with the provider's own AS before sending it on.
# Illustrative only. Check your platform's documentation for exact behavior.
router bgp 65100
neighbor 203.0.113.1 remote-as 64999
neighbor 203.0.113.1 allowas-in 1These features turn off a safety mechanism, so use them with a clear reason, and be careful that you do not create the very loop the check was designed to stop.
Lab Idea: See It Yourself
You can verify all of this in a three-router lab with any virtual router image you have access to. Give each router its own AS in a triangle, originate one prefix on the first, and run through these steps:
Display the BGP table on each router and read the AS_PATH for the prefix.
Add a prepend on one link and watch the best path change.
Capture an UPDATE in Wireshark and find the AS_PATH attribute and its segment type.
Use the same AS number on two non-adjacent routers, and observe the drop. Then try allowas-in.
Whatever outputs you see will depend on your platform, so treat the snippets in this post as shapes to expect rather than exact text.
Key Takeaways
BGP carries the list of ASes a route has crossed in AS_PATH. That makes it a path vector protocol.
eBGP loop prevention is a stateless check: if your own AS is in the path, reject the route.
iBGP does not prepend, so it relies on split horizon, or on ORIGINATOR_ID and CLUSTER_LIST with route reflectors.
AS_PATH length is a coarse tie-breaker, and prepending is the common way to influence it.
Knobs like allowas-in and as-override bypass the check, so use them deliberately.
Comments (0)
Please sign in to comment.