BGP AS-Path

Learn how the BGP AS-Path attribute influences path selection, prevents routing loops, and helps control inbound traffic using AS-Path prepending. Simple examples and configurations included.

In our previous blogs, we started exploring how BGP (Border Gateway Protocol) decides the “best path” when multiple routes are available. We first looked at the Weight attribute, then the Local Preference, and also how Locally Originated routes are treated in the selection process.

If you missed those, here are the quick links to revisit:

Now, let’s move forward to the next important attribute in BGP path selection – AS-Path.

AS-Path is one of the most fundamental and powerful attributes in BGP.

Every time a route advertisement passes through an Autonomous System (AS), that AS number gets added to the AS-Path list.

In simple words, the AS-Path is like a travel diary of the route, showing which AS numbers the route has crossed before reaching your router.

For example:

AS-Path: 65001 65002 65003

This means the route originated in AS 65001, passed through AS 65002, and then AS 65003 before reaching your network.

The AS-Path attribute serves two major purposes in BGP:

  1. Path Selection:
    • When BGP compares multiple paths to the same destination, it will prefer the path with the shortest AS-Path.
    • Shorter path = fewer AS hops = considered “better” by BGP.
  2. Loop Prevention:
    • If a router sees its own AS number in the AS-Path of a received route, it rejects that route.
    • This ensures loop-free routing across the internet.

Imagine you have two possible paths to reach the destination network 10.10.10.0/24:

  • Path 1: 65010 65020 65030
  • Path 2: 65040 65030

Here, Path 2 has a shorter AS-Path (2 hops instead of 3), so BGP will prefer Path 2 as the best route.

AS-Path for Traffic Engineering

Unlike Weight and Local Preference, which are applied inbound (influencing how your own AS selects routes), the AS-Path can be manipulated outbound to influence inbound traffic.

This is done by a technique called AS-Path Prepending.

  • If you want to make a path less preferred by others, you artificially add your AS number multiple times to the AS-Path before advertising it.
  • This makes the path appear “longer” to external networks, so they will avoid it unless no other path is available.

Example of AS-Path Prepending

AS-Path without prepending: 65050

AS-Path with prepending:   65050 65050 65050

Here, the prepended path looks longer, so external peers will likely choose another path to reach you. This way, you can control incoming traffic flow.

image

conf t
interface g1/0
no shutdown
ip address 12.1.1.1 255.255.255.0

interface g2/0
no shutdown
ip address 13.1.1.1 255.255.255.0

router bgp 100
neighbor 12.1.1.2 remote-as 200
neighbor 13.1.1.3 remote-as 300

conf t
interface g1/0
no shutdown
ip address 12.1.1.2 255.255.255.0

router bgp 200
neighbor 12.1.1.1 remote-as 100

conf t
interface g2/0
no shutdown
ip address 13.1.1.3 255.255.255.0

router bgp 300
neighbor 13.1.1.1 remote-as 100

After completing the configuration, I verified the BGP status, and the sessions were successfully established with both routers

image

In the example above, routers R2 and R3 are each advertising the prefix 10.10.10.10/32 to router R1.

On router R1, we can see that the prefix 10.10.10.10/32 is being received from both R2 and R3. However, based on the BGP path selection process, R1 selects the best path through R3.

If you look closely, the AS-Path length is the same for the routes received from both R2 and R3.

image

In this case, the first three BGP attributes—Weight, Local Preference, and Locally Originated—are identical, and even the AS-Path length is the same for both routes. To influence the decision, we can manipulate the AS-Path so that R1 selects the path through R2 instead of R3, since the earlier attributes don’t provide any preference.

To achieve this, we need to configure a route-map using the AS-Path prepend method and apply it in the BGP outbound direction. By doing this, we can advertise the prefix from R2 with a shorter AS-Path (fewer AS numbers) and from R3 with a longer AS-Path (more AS numbers). As a result, R1 will prefer the path through R2, since BGP always selects the route with the shorter AS-Path when all other attributes are equal.

R2(config)#ip prefix-list AS_Path permit 10.10.10.10/32

R2(config)#route-map AS_Path_Prepend
R2(config-route-map)#match ip address AS_Path
R2(config-route-map)#set as-path prepend 200 200
R2(config-route-map)#exit

R2(config)#router bgp 200
R2(config-router)#neighbor 12.1.1.1 route-map AS_Path_Prepend out
R3(config)#ip prefix-list AS_Path permit 10.10.10.10/32

R3(config)#route-map AS_Path_Prepend
R3(config-route-map)#match ip address AS_Path
R3(config-route-map)#set as-path prepend 300 300 300
R3(config-route-map)#exit

R3(config)#router bgp 300
R3(config-router)#neighbor 13.1.1.1 route-map AS_Path_Prepend out

Now, when we check the prefixes on R1, we can see that the best path has shifted to R2 instead of R3. This change happened because R2 is now advertising the route with the shorter AS-Path, making it the preferred choice according to BGP path selection rules.

image

In this way, by using the AS-Path attribute, we can influence how inbound traffic flows into our network. By manipulating the AS-Path length, we make certain routes more or less attractive to external peers, effectively controlling which path incoming traffic will prefer.

  • AS-Path shows the journey of a route across ASes.
  • BGP prefers shorter AS-Paths when choosing the best route.
  • It prevents loops by rejecting routes containing the local AS.
  • It can be manipulated outbound (AS-Path Prepending) to influence inbound traffic.

In this blog, we explored the AS-Path attribute, one of the core building blocks of BGP path selection.

So far, we’ve covered four attributes in this series:

  1. Weight (local to the router)
  2. Local Preference (local to the AS)
  3. Locally Originated (routes originated within the router)
  4. AS-Path (inter-AS, used for global routing and loop prevention)

Leave a Comment