BGP IPv4 - Regular Expression

BGP IPv4 - Regular Expression

BGP IPv4 Regular Expressions (regex) serve as powerful tools for route filtering in Border Gateway Protocol configurations. They enable network administrators to define specific patterns, allowing for precise control over the selection and advertisement of routes. Regular Expressions, employed in BGP route maps, facilitate the matching and manipulation of route attributes, offering a flexible and granular approach to shaping routing decisions. This capability is crucial for optimizing network performance, managing traffic, and implementing fine-grained routing policies tailored to the unique requirements of an organization's network infrastructure.

Lab:

BGP Topology

Disclaimer

This Configuration Guide is designed to assist members to enhance their skills in particular technology area. While every effort has been made to ensure that all material is as complete and accurate as possible, the enclosed material is presented on an “as is” basis. Neither the authors nor Forum assume any liability or responsibility to any person or entity with respect to loss or damages incurred from the information contained in this guide. This configuration guide was developed by Forum. Any similarities between material presented in this configuration guide and any other material is completely coincidental.

Cisco IOS Software, Linux Software (I86BI_LINUX-ADVENTERPRISEK9-M), Version 15.2(4)M1, DEVELOPMENT TEST SOFTWARE

Task 1: Configure IPv4 BGP Process

Step 1: In the configuration mode of router configure IPv4 BGP Process by following command:

R1:
router bgp 100			
neighbor 22.0.1.2 remote-as 200  	
neighbor 22.0.1.2 ebgp-multihop 5	
neighbor 22.0.1.2 update-source loopback 1	                                          
address-family ipv4				
network 11.0.1.0 mask 255.255.255.0		
network 11.0.2.0 mask 255.255.255.0
network 11.0.3.0 mask 255.255.255.0
exit

R2:
router bgp 200
neighbor 11.0.1.1 remote-as 100
neighbor 11.0.1.1 ebgp-multihop 5	
neighbor 11.0.1.1 update-source loopback 1
neighbor 33.0.1.3 remote-as 300
neighbor 33.0.1.3 ebgp-multihop 5	
neighbor 33.0.1.3 update-source loopback 1
address-family ipv4
network 22.0.1.0 mask 255.255.255.0
network 22.0.2.0 mask 255.255.255.0
network 22.0.3.0 mask 255.255.255.0
exit

R3:
router bgp 300
neighbor 22.0.1.2 remote-as 200
neighbor 22.0.1.2 ebgp-multihop 5
neighbor 22.0.1.2 update-source loopback 1
neighbor 44.0.1.4 remote-as 400
neighbor 44.0.1.4 ebgp-multihop 5
neighbor 44.0.1.4 update-source loopback 1
address-family ipv4
network 33.0.1.0 mask 255.255.255.0
network 33.0.2.0 mask 255.255.255.0
network 33.0.3.0 mask 255.255.255.0
exit

R4:
router bgp 400
neighbor 33.0.1.3 remote-as 300
neighbor 33.0.1.3 ebgp-multihop 5
neighbor 33.0.1.3 update-source loopback 1
address-family ipv4
network 44.0.1.0 mask 255.255.255.0
network 44.0.2.0 mask 255.255.255.0
network 44.0.3.0 mask 255.255.255.0
exit

Step 2: Verify IPv4 BGP routes and its details by following command

R1#show ip bgp
BGP table version is 13, local router ID is 11.0.3.1
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter,
              x best-external, a additional-path, c RIB-compressed,
Origin codes: i - IGP, e - EGP, ? - incomplete
RPKI validation codes: V valid, I invalid, N Not found

     Network          Next Hop      Metric      LocPrf       Weight      Path
 *>  11.0.1.0/24      0.0.0.0         0                      32768        i
 *>  11.0.2.0/24      0.0.0.0         0                      32768        i
 *>  11.0.3.0/24      0.0.0.0         0                      32768        i
 *>  22.0.1.0/24      22.0.1.2        0                        0         200 i
 *>  22.0.2.0/24      22.0.1.2        0                        0         200 i
 *>  22.0.3.0/24      22.0.1.2        0                        0         200 i
 *>  33.0.1.0/24      22.0.1.2                                 0         200 300 i
 *>  33.0.2.0/24      22.0.1.2                                 0         200 300 i
 *>  33.0.3.0/24      22.0.1.2                                 0         200 300 i
 *>  44.0.1.0/24      22.0.1.2                                 0         200 300 400 i
 *>  44.0.2.0/24      22.0.1.2                                 0         200 300 400 i
 *>  44.0.3.0/24      22.0.1.2                                 0         200 300 400 i

Task 2: Configure IPv4 BGP Regular Expression

Step 1: In the configuration mode of router configure IPv4 BGP Regular Expression

BGP Regular Expression is used for matching AS-Path information using string of various special characters. Regular expressions can be configured using ‘ip as−path access−list’ and ‘show ip bgp regexp regular-expression’ command with Border Gateway Protocol (BGP)

CHARACTER USAGE

  1. Single character is matched in an AS path at any place.
R1#ip as-path access-list 1 permit 2
R1#show ip bgp regexp 2 
//matches and displays all AS-path which are matching with single character ‘2’
BGP table version is 13, local router ID is 11.0.3.1
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter,
              x best-external, a additional-path, c RIB-compressed,
Origin codes: i - IGP, e - EGP, ? - incomplete
RPKI validation codes: V valid, I invalid, N Not found

     Network          Next Hop      Metric 	 LocPrf 	Weight  	Path
 *>  22.0.1.0/24      22.0.1.2        0             	  0 	    200 i
 *>  22.0.2.0/24      22.0.1.2        0             	  0 	    200 i
 *>  22.0.3.0/24      22.0.1.2        0             	  0 	    200 i
 *>  33.0.1.0/24      22.0.1.2                            0  	    200 300 i
 *>  33.0.2.0/24      22.0.1.2                            0	        200 300 i
 *>  33.0.3.0/24      22.0.1.2                         	  0	        200 300 i
 *>  44.0.1.0/24      22.0.1.2                         	  0	        200 300 400 i
 *>  44.0.2.0/24      22.0.1.2                         	  0	        200 300 400 i
 *>  44.0.3.0/24      22.0.1.2                         	  0	        200 300 400 i

R1#ip as-path access-list 1 permit 3
R1#show ip bgp regexp 3
//matches and displays all AS-path which are matching with single character ‘3’
BGP table version is 13, local router ID is 11.0.3.1
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter,
              x best-external, a additional-path, c RIB-compressed,
Origin codes: i - IGP, e - EGP, ? - incomplete
RPKI validation codes: V valid, I invalid, N Not found

     Network             Next Hop        Metric 	LocPrf 	Weight 	Path
 *>  33.0.1.0/24      22.0.1.2                               	0 	200 300 i
 *>  33.0.2.0/24      22.0.1.2                               	0 	200 300 i
 *>  33.0.3.0/24      22.0.1.2                               	0 	200 300 i
 *>  44.0.1.0/24      22.0.1.2                               	0 	200 300 400 i
 *>  44.0.2.0/24      22.0.1.2                               	0 	200 300 400 i
 *>  44.0.3.0/24      22.0.1.2                               	0 	200 300 400 i

R1#ip as-path access-list 1 permit 00
R1#show ip bgp regexp 00
//matches and displays all AS-path which are matching with ‘00’
BGP table version is 13, local router ID is 11.0.3.1
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter,
              x best-external, a additional-path, c RIB-compressed,
Origin codes: i - IGP, e - EGP, ? - incomplete
RPKI validation codes: V valid, I invalid, N Not found

     Network          Next Hop      Metric 	   LocPrf   	Weight  	Path
 *>  22.0.1.0/24      22.0.1.2        0                    	  0 	    200 i
 *>  22.0.2.0/24      22.0.1.2        0             	      0 	    200 i
 *>  22.0.3.0/24      22.0.1.2        0             	      0 	    200 i
 *>  33.0.1.0/24      22.0.1.2                                0 	    200 300 i
 *>  33.0.2.0/24      22.0.1.2                                0	        200 300 i
 *>  33.0.3.0/24      22.0.1.2                                0 	    200 300 i
 *>  44.0.1.0/24      22.0.1.2                                0 	    200 300 400 i
 *>  44.0.2.0/24      22.0.1.2                                0 	    200 300 400 i
 *>  44.0.3.0/24      22.0.1.2                                0 	    200 300 400 i

R1#ip as-path access-list 1 permit 20|30
R1#show ip bgp regexp 20|30
//matches and displays all AS-path which are matching with ‘20 or 30’
BGP table version is 13, local router ID is 11.0.3.1
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter,
              x best-external, a additional-path, c RIB-compressed,
Origin codes: i - IGP, e - EGP, ? - incomplete
RPKI validation codes: V valid, I invalid, N Not found

     Network          Next Hop      Metric  	LocPrf  	Weight  	Path
 *>  22.0.1.0/24      22.0.1.2        0             	      0 	    200 i
 *>  22.0.2.0/24      22.0.1.2        0             	      0 	    200 i
 *>  22.0.3.0/24      22.0.1.         0             	      0 	    200 i
 *>  33.0.1.0/24      22.0.1.2                                0 	    200 300 i
 *>  33.0.2.0/24      22.0.1.2                                0 	    200 300 i
 *>  33.0.3.0/24      22.0.1.2                                0 	    200 300 i
 *>  44.0.1.0/24      22.0.1.2                                0 	    200 300 400 i
 *>  44.0.2.0/24      22.0.1.2                                0 	    200 300 400 i
 *>  44.0.3.0/24      22.0.1.2                                0 	    200 300 400 i
  1. A range of characters matches any single character in the range. Example: [1234] or [1-4] Dot matches any single character
R1#ip as-path access-list 1 permit [2-4].[01]
R1#show ip bgp regexp [2-4].[01]
//matches and displays all AS-path which contains combination of 20, 21, 30, 31, 40, 41.
BGP table version is 13, local router ID is 11.0.3.1
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter,
              x best-external, a additional-path, c RIB-compressed,
Origin codes: i - IGP, e - EGP, ? - incomplete
RPKI validation codes: V valid, I invalid, N Not found

     Network           Next Hop      Metric 	LocPrf 	    Weight  	Path
 *>  22.0.1.0/24      22.0.1.2         0             	      0 	    200 i
 *>  22.0.2.0/24      22.0.1.2         0             	      0 	    200 i
 *>  22.0.3.0/24      22.0.1.2         0             	      0 	    200 i
 *>  33.0.1.0/24      22.0.1.2                                0 	    200 300 i
 *>  33.0.2.0/24      22.0.1.2                                0 	    200 300 i
 *>  33.0.3.0/24      22.0.1.2                                0 	    200 300 i
 *>  44.0.1.0/24      22.0.1.2                                0 	    200 300 400 i
 *>  44.0.2.0/24      22.0.1.2                                0 	    200 300 400 i
 *>  44.0.3.0/24      22.0.1.2                                0 	    200 300 400 i

R1#ip as-path access-list 1 permit [3-4].[0]
R1#show ip bgp regexp [3-4].[0]
//matches and displays all AS-path which contains combination of 30 & 40
BGP table version is 13, local router ID is 11.0.3.1
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter,
              x best-external, a additional-path, c RIB-compressed,
Origin codes: i - IGP, e - EGP, ? - incomplete
RPKI validation codes: V valid, I invalid, N Not found

     Network          Next Hop     Metric 	LocPrf 	Weight 	Path
 *>  33.0.1.0/24      22.0.1.2            		      0 	200 300 i
 *>  33.0.2.0/24      22.0.1.2            		      0 	200 300 i
 *>  33.0.3.0/24      22.0.1.2            		      0 	200 300 i
 *>  44.0.1.0/24      22.0.1.2            		      0 	200 300 400 i
 *>  44.0.2.0/24      22.0.1.2            		      0 	200 300 400 i
 *>  44.0.3.0/24      22.0.1.2            		      0 	200 300 400 i
  1. Regular expression using special characters

R1#ip as-path access-list 1 permit ^2 R1#show ip bgp regexp ^2 //matches and displays all AS-path which are beginning with 2 BGP table version is 13, local router ID is 11.0.3.1 Status codes: s suppressed, d damped, h history, * valid, > best, i - internal, r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter, x best-external, a additional-path, c RIB-compressed, Origin codes: i - IGP, e - EGP, ? - incomplete RPKI validation codes: V valid, I invalid, N Not found

 Network             Next Hop    Metric 	LocPrf 	Weight 	Path

*> 22.0.1.0/24 22.0.1.2 0 0 200 i *> 22.0.2.0/24 22.0.1.2 0 0 200 i *> 22.0.3.0/24 22.0.1.2 0 0 200 i *> 33.0.1.0/24 22.0.1.2 0 200 300 i *> 33.0.2.0/24 22.0.1.2 0 200 300 i *> 33.0.3.0/24 22.0.1.2 0 200 300 i *> 44.0.1.0/24 22.0.1.2 0 200 300 400 i *> 44.0.2.0/24 22.0.1.2 0 200 300 400 i *> 44.0.3.0/24 22.0.1.2 0 200 300 400 i

R1#ip as-path access-list 1 permit ^200 R1#show ip bgp regexp ^200 //matches and displays all AS-Path which are beginning with 200 BGP table version is 13, local router ID is 11.0.3.1 Status codes: s suppressed, d damped, h history, * valid, > best, i - internal, r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter, x best-external, a additional-path, c RIB-compressed, Origin codes: i - IGP, e - EGP, ? - incomplete RPKI validation codes: V valid, I invalid, N Not found

 Network             Next Hop     Metric 	LocPrf 	Weight 	Path

*> 22.0.1.0/24 22.0.1.2 0 0 200 i *> 22.0.2.0/24 22.0.1.2 0 0 200 i *> 22.0.3.0/24 22.0.1.2 0 0 200 i *> 33.0.1.0/24 22.0.1.2 0 200 300 i *> 33.0.2.0/24 22.0.1.2 0 200 300 i *> 33.0.3.0/24 22.0.1.2 0 200 300 i *> 44.0.1.0/24 22.0.1.2 0 200 300 400 i *> 44.0.2.0/24 22.0.1.2 0 200 300 400 i *> 44.0.3.0/24 22.0.1.2 0 200 300 400 i

R1#ip as-path access-list 1 permit ^300 R1#show ip bgp regexp ^300

// there are no AS-Path beginning with 300, hence are not listed.

R1#ip as-path access-list 1 permit 300$ R1#show ip bgp regexp 300$ //matches and displays all AS-Path which are Ending with 300 BGP table version is 13, local router ID is 11.0.3.1 Status codes: s suppressed, d damped, h history, * valid, > best, i - internal, r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter, x best-external, a additional-path, c RIB-compressed, Origin codes: i - IGP, e - EGP, ? - incomplete RPKI validation codes: V valid, I invalid, N Not found

 Network          Next Hop     Metric  	 LocPrf 	Weight 	  Path

*> 33.0.1.0/24 22.0.1.2 0 200 300 i *> 33.0.2.0/24 22.0.1.2 0 200 300 i *> 33.0.3.0/24 22.0.1.2 0 200 300 i

R1#ip as-path access-list 1 permit 200$ R1#show ip bgp regexp 200$ //matches and displays all AS-Path which are Ending with 200 BGP table version is 13, local router ID is 11.0.3.1 Status codes: s suppressed, d damped, h history, * valid, > best, i - internal, r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter, x best-external, a additional-path, c RIB-compressed, Origin codes: i - IGP, e - EGP, ? - incomplete RPKI validation codes: V valid, I invalid, N Not found

 Network          Next Hop     Metric 	 LocPrf 	Weight  	Path

*> 22.0.1.0/24 22.0.1.2 0 0 200 i *> 22.0.2.0/24 22.0.1.2 0 0 200 i *> 22.0.3.0/24 22.0.1.2 0 0 200 i

R1#ip as-path access-list 1 permit 400$ R1#show ip bgp regexp 400$ //matches and displays all AS-Path which are Ending with 400 BGP table version is 13, local router ID is 11.0.3.1 Status codes: s suppressed, d damped, h history, * valid, > best, i - internal, r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter, x best-external, a additional-path, c RIB-compressed, Origin codes: i - IGP, e - EGP, ? - incomplete RPKI validation codes: V valid, I invalid, N Not found

 Network             Next Hop     Metric 	LocPrf 	Weight 	Path

*> 44.0.1.0/24 22.0.1.2 0 200 300 400 i *> 44.0.2.0/24 22.0.1.2 0 200 300 400 i *> 44.0.3.0/24 22.0.1.2 0 200 300 400 i