Cisco BGP as-path regex

If you have any experience with processing large amounts of text there is a good chance you will have devised some cunning regular expressions to pluck out the relevant data and discard whatever else doesn’t match. Occasionally regex pops up in the IOS/ ASA world and a recent post on CSC has led to this post about pattern matching in as-path lists.

We start with a simple topology:

BGP topology

Each router advertises a loopback network giving R1 the following BGP table:

     Network          Next Hop            Metric LocPrf Weight Path
 *>  2.2.2.0/24       192.168.1.2              0             0 2222 i
 *>  3.3.3.0/24       192.168.1.2                            0 2222 3333 i
 *>  4.4.4.0/24       192.168.1.2                            0 2222 4444 i
 *>  5.5.5.0/24       192.168.1.2                            0 2222 4444 44445 i

On R1 we will implement an as-path access list to play around with our regex statements.

!
ip as-path access-list 100 permit .*
!
route-map FOO permit 10
 match as-path 100
!
router bgp 1111
 bgp log-neighbor-changes
 neighbor 192.168.1.2 remote-as 2222
 !
 address-family ipv4
  neighbor 192.168.1.2 activate
  neighbor 192.168.1.2 route-map FOO in
 exit-address-family
!

Our first regex statement is simple, .* , match everything. Confirm that we still have all the prefixes present:

     Network          Next Hop            Metric LocPrf Weight Path
 *>  2.2.2.0/24       192.168.1.2              0             0 2222 i
 *>  3.3.3.0/24       192.168.1.2                            0 2222 3333 i
 *>  4.4.4.0/24       192.168.1.2                            0 2222 4444 i
 *>  5.5.5.0/24       192.168.1.2                            0 2222 4444 54444 i

Next lets permit only prefixes from AS3333, we will use the following statement:

!
ip as-path access-list 100 permit 3333
!

Confirm the BGP table state:

     Network          Next Hop            Metric LocPrf Weight Path
 *>  3.3.3.0/24       192.168.1.2                            0 2222 3333 i

Lets try the same with AS4444 :

!
ip as-path access-list 100 permit 4444
!

BGP table looks good:

     Network          Next Hop            Metric LocPrf Weight Path
 *>  4.4.4.0/24       192.168.1.2                            0 2222 4444 i
 *>  5.5.5.0/24       192.168.1.2                            0 2222 4444 54444 i

Taking this a step further we now want to just receive the prefixes originating from AS4444. These prefixes will have the AS number 4444 at the rightmost position, so we now use the dollar symbol ($) at the end of the regex statement to indicate the end of the string should follow 4444. This gives us:

!
ip as-path access-list 100 permit 4444$
!

Which gives us:

     Network          Next Hop            Metric LocPrf Weight Path
 *>  4.4.4.0/24       192.168.1.2                            0 2222 4444 i
 *>  5.5.5.0/24       192.168.1.2                            0 2222 4444 54444 i

hmmmm, we still the prefix from AS54444 as this also matches. Time to introduce a cisco specific regex symbol, the underscore. In this instance this will match the whitespace, ie ‘_4444’ will match ‘ 4444’ but not ‘ 54444’ :

!
ip as-path access-list 100 permit _4444$
!

BGP table now shows us just the prefix orginating from AS4444:

      Network          Next Hop            Metric LocPrf Weight Path
 *>  4.4.4.0/24       192.168.1.2                            0 2222 4444 i

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: