BGP MASTERCLASS · CHAPTER 6

Filtering and routing policies: don't be a transit AS

Prove with traceroute that your dual-homed AS is carrying transit traffic, stop it with an AS-path access-list, and decide where your traffic comes in and goes out.

14 min readVideos: Day 2, parts 5 and 6Level: intermediate

From point A to point B

Point A: your AS has two providers, the sessions are up and everything “works”. But you don't know what you advertise, to whom, or where your traffic comes in and goes out. Point B: you prove what is happening with received-routes, advertised-routes and traceroute, you cut transit with an AS-path access-list, and you decide where traffic leaves and enters with local preference and AS_PATH prepend.

As Jesús puts it, with BGP you become the creator of your network's routing. To do that, you have to think like a router.

What you will see
  1. Ticket 6, and why a dual-homed customer must not be a transit AS.
  2. Troubleshooting with soft-reconfiguration inbound, received-routes and advertised-routes.
  3. The four ways to filter: distribute-list, prefix-list, route-map and AS-path access-list.
  4. Regular expressions for the AS_PATH and the ^$ filter with filter-list.
  5. Inbound and outbound policies: local preference for outgoing traffic and AS_PATH prepend for incoming traffic.

Ticket 6: three requests in one

The ticket says: “R4 can no longer be a transit area, R4 must have INBOUND traffic preferring R3 and OUTBOUND traffic preferring R3. Do not use WEIGHT.” It looks like one ticket, but it is three tasks:

  1. Not being a transit AS. R4 must advertise only the prefixes of its own AS.
  2. Incoming traffic through R3. Everyone else must prefer R3 to reach 40.40.40.0.
  3. Outgoing traffic through R3. R4 must prefer R3 to reach the Internet, without weight, which is Cisco-specific.

The ticket is after symmetry: R3 is the best link. Remember that R4 lives in sub-AS 64100 of a confederation and is seen from outside as AS 1.2; that's why its process is router bgp 64100.

A transit AS lets third-party traffic through. R4's company doesn't sell Internet access: it buys service from two providers to advertise its prefixes and get its users online. If one provider can reach the other through R4, the company pays for links that carry someone else's traffic: “come through my house and take whatever you want”.

Troubleshooting with received-routes

Before filtering, Jesús checks what each router sees. Something odd shows up on R1: it learns 20.20.20.0 directly from R2, but not through R3. Is there a hidden filter?

! R3: keep the unmodified updates from both neighbors
R3(config)# router bgp 1.1
R3(config-router)# neighbor 172.16.2.1 soft-reconfiguration inbound
R3(config-router)# neighbor 172.16.4.4 soft-reconfiguration inbound
! Adj-RIB-Out: what R3 sends to R1
R3# show ip bgp neighbor 172.16.2.1 advertised-routes
! Adj-RIB-In: what R3 receives from R4, and which path is best
R3# show ip bgp neighbor 172.16.4.4 received-routes
R3# show ip bgp
! R1: what it receives from R3
R1# show ip bgp neighbor 172.16.2.3 received-routes

With soft-reconfiguration inbound the router stores, unmodified, what each neighbor sends, and received-routes shows that Adj-RIB-In. advertised-routes shows the Adj-RIB-Out. This way you compare both sides of the same peering.

The clue was on R3: it receives 20.20.20.0 from R4, but its best path is a different one. And BGP only advertises its best path. If R3's best path already goes through AS 100, R1 has nothing new to learn from R3. It wasn't a filter: it was the best path.

Method. When a prefix “doesn't arrive”, follow it neighbor by neighbor: advertised-routes on the sender, received-routes on the receiver, and show ip bgp to see the best path.

The proof: R2 goes through your AS

Now look at 30.30.30.0, R3's prefix, from R2. R2 learns it through 172.16.1.1 (R1) and through 172.16.3.4 (R4). The details show origin IGP (advertised with network), local preference 100, valid, external and best through R1.

R2# show ip bgp 30.30.30.0/24
R2# traceroute 30.30.30.30 source 20.20.20.20 ttl 1 5
! the serial link to R1 goes down
R2(config)# interface serial 0/1
R2(config-if)# shutdown
R2# show ip bgp
! now the path goes through R4 (AS 1.2) and R3
R2# traceroute 30.30.30.30 source 20.20.20.20 ttl 1 5

The first traceroute goes through R1 to R3. Jesús shuts the R2–R1 serial link and the best path moves to 172.16.3.4: the second traceroute goes through R4 and then R3. The customers on 20.20.20.0 are now using links that R4's company pays for to reach another AS.

A dual-homed AS doing transit, without and with the ^$ filter Four routers in a square: R1 in AS 100 and R3 in AS 1.1 on top, R2 in AS 200 and R4 in AS 1.2 below. The serial link between R1 and R2 is down. Left panel, no filter: R4 advertises to R2 every prefix it learns, including 30.30.30.0/24 from R3, so traffic from R2 to 30.30.30.0/24 goes through R4 and then R3. AS 1.2 is acting as a transit AS. Right panel, with ip as-path access-list 100 permit ^$ and neighbor filter-list 100 out: R4 only advertises its own prefix 40.40.40.0/24, so R2 has no path to 30.30.30.0/24 through AS 1.2. NO FILTER · AS 1.2 IS TRANSIT R1 · AS 100 11 · 22 · 33.0/24 R3 · AS 1.1 30.30.30.0/24 R2 · AS 200 20.20.20.0/24 R4 · AS 1.2 advertises all it learns 40 · 30 · 11 · 22 · 33 serial down ✕ to 30.30.30.0/24 R2 reaches R3 through R4: your links carry someone else’s traffic FILTER-LIST 100 OUT · ^$ R1 · AS 100 11 · 22 · 33.0/24 R3 · AS 1.1 30.30.30.0/24 R2 · AS 200 20.20.20.0/24 R4 · AS 1.2 advertises only 40.40.40.0/24 serial down ✕ only 40.40.40.0/24 R2 has no path to 30.30.30.0/24 through AS 1.2: no transit

Without a filter, R4 advertises everything it learns to R2, and R2 reaches 30.30.30.0 through AS 1.2. With the ^$ filter, R4 only advertises 40.40.40.0. Diagram: The House of Routing.

Four ways to filter in BGP

A BGP filtering policy can be built in four ways:

ToolWhat it selectsWhen to use it
distribute-listPrefixes, with a traditional ACLSimple per-neighbor filters
prefix-listPrefixes and length ranges (ge, le)Better than an ACL: you are more precise with prefixes
route-mapWhatever you want: prefix-list, AS-path, communities…The standard: it's modular and it's where you set attributes with set
ip as-path access-listThe AS_PATH, with regular expressionsFiltering by autonomous system, not by prefix

The AS-path access-list is enormously powerful: if a provider advertises 10,000 prefixes and you want to block them all, you select its AS number and you're done.

Regular expressions for the AS_PATH

To filter on the AS_PATH you need regular expressions, which also serve you in Linux, in programming and in network automation. These are the characters on the slide:

  • . any character; * zero or more repetitions; + one or more; ? zero or one.
  • ^ start of the string; $ end of the string.
  • [ ] a single character from those inside, for example [13579].
  • _ a delimiter: space, comma, brace, parenthesis, start or end of the string.

And these are the examples from the class, written for a router using asdot notation:

ExpressionMatches
^$Empty AS_PATH: only the NLRI originated by your own AS
_1\.2$NLRI originated by AS 1.2 (the last AS in the path)
_1\.1_NLRI that pass through AS 1.1
_[13579]+$NLRI whose origin AS has only odd digits (1, 13, 357…)

Two details. In asdot the dot is a regex wildcard: you escape it with \., and that form only works if the router uses bgp asnotation dot (in asplain, AS 1.2 is 65538). And with + you require at least one digit; with * zero digits would also do, and the expression would match any AS_PATH. Test each regex with show ip bgp regexp before applying it.

ip as-path access-list 100 permit ^$

The ticket's solution is the first expression: R4 advertises only what originates in its AS and doesn't re-announce what it learns from others.

! only routes originated in this AS (empty AS_PATH)
R4(config)# ip as-path access-list 100 permit ^$
R4(config)# router bgp 64100
R4(config-router)# neighbor 172.16.4.3 filter-list 100 out
R4(config-router)# neighbor 172.16.3.2 filter-list 100 out
R4(config-router)# do clear ip bgp * soft
! check on R2
R2# show ip bgp summary
R2# show ip bgp

The AS-path access-list is applied with filter-list, per neighbor and per direction: here out toward R3 (172.16.4.3) and R2 (172.16.3.2). It ends with an implicit deny: whatever doesn't match ^$ isn't advertised.

On R2, with the serial link to R1 down, neighbor 172.16.1.1 is Idle and show ip bgp only has 40.40.40.0: R2 can no longer reach R1 through AS 1.2. When the serial link comes back, R2 sees 40.40.40.0 through 172.16.3.4, which wins with fewer AS in the AS_PATH, and through 172.16.1.1. That second path is what you want from a dual-homed design: if the R2–R4 link fails, traffic toward your AS still has a way back.

Jesús then shuts the R2–R4 link and the first ping to 40.40.40.40 fails. It isn't a black hole: BGP is still tearing down the session, and seconds later the traceroute goes through R1, R3 and R4. BGP is slow; on the Internet it can take from 30 seconds to three minutes because of its timers. Since you don't control the BGP cloud, control precisely what you advertise.

Side effect in the confederation. With the filter, R2 doesn't receive 50.50.50.0 or 60.60.60.0 either. Those routes reach R4 with the confederation sub-AS in the AS_PATH (in parentheses in show ip bgp), and ^$ doesn't let them through. In ticket 12, Jesús tries to open the filter to sub-AS 64300 and finally removes the filter-list to keep going. In production you would adjust the AS-path access-list to your sub-AS.

Inbound and outbound policies

Now the routing part of the ticket. You can have policies in each direction, and the rule looks backwards:

  • An inbound policy (on what you receive) changes how your traffic leaves. Tools: local preference and weight.
  • An outbound policy (on what you advertise) changes how traffic comes in to you. Tools: AS_PATH prepend and MED, which is rarely used.

There is a deeper difference. Local preference lives inside your AS: you set it and your routers honor it, so you control what goes out. Prepend is a suggestion: it makes your AS_PATH longer, but the other AS decide with their own policies. With it you influence what comes in.

AS_PATH prepend for inbound traffic and local preference for outbound traffic Two panels with the same diamond: R1 in AS 100 on top, R2 in AS 200 on the left, R3 in AS 1.1 on the right and R4 in AS 1.2 at the bottom. Left panel, inbound traffic: R4 applies an outbound route-map toward R2 that prepends 1.2 three times, so 40.40.40.0/24 reaches R2 with AS_PATH 1.2 1.2 1.2 1.2 and reaches R3 with AS_PATH 1.2. R1 sees five AS through R2 and two AS through R3, chooses the shortest AS_PATH, and traffic toward 40.40.40.0/24 enters through R3. Right panel, outbound traffic: R4 applies an inbound route-map to routes from R3 that sets local preference 500; routes from R2 keep the default 100. The highest local preference wins, so R4 sends its outbound traffic through R3. You control what leaves; you influence what comes in. INBOUND TRAFFIC · OUTBOUND POLICY R1 · AS 100 Internet side R2 · AS 200 R3 · AS 1.1 R4 · AS 1.2 40.40.40.0/24 1.2 1.2 1.2 1.2 1.2 route-map RM out to R2: prepend 1.2 1.2 1.2 R1: via R2 = 5 AS · via R3 = 2 AS traffic to 40.40.40.0/24 enters via R3 OUTBOUND TRAFFIC · INBOUND POLICY R1 · AS 100 Internet side R2 · AS 200 R3 · AS 1.1 R4 · AS 1.2 40.40.40.0/24 LocPrf 100 LocPrf 500 route-map RM-IN in from R3: local-preference 500 highest local preference wins R4 traffic to the Internet leaves via R3

The traffic direction is at the top of each panel. Prepending toward R2 makes R1 prefer R3 to reach 40.40.40.0. Local preference 500 on what comes from R3 makes R4 leave through R3. Diagram: The House of Routing.

AS_PATH prepend: incoming traffic

To make traffic come in through R3 you create an outbound policy, but not toward R3: toward R2. There you repeat your own AS so that path looks longer. Jesús calls it poisoning the circuit you don't want others to use.

R4(config)# route-map RM permit 10
R4(config-route-map)# set as-path prepend 1.2 1.2 1.2
R4(config-route-map)# exit
R4(config)# route-map RM permit 20
R4(config-route-map)# exit
R4(config)# router bgp 64100
R4(config-router)# neighbor 172.16.3.2 route-map RM out
R4(config-router)# do clear ip bgp * soft
! R2 sees the longer path, R1 prefers R3
R2# show ip bgp
R1# show ip route
R1# traceroute 40.40.40.40 source 11.11.11.11 ttl 1 5
R2# traceroute 40.40.40.40 source 20.20.20.20 ttl 1 5

Sequence 10 has no match, so it applies to every prefix and adds 1.2 three times. Sequence 20 is an empty permit: a safety net in case you add a match to sequence 10 tomorrow, so the implicit deny doesn't block everything else. And the name you apply to the neighbor must be identical to the route-map's name.

On R2, 40.40.40.0 through 172.16.3.4 now shows 23456 23456 23456 23456: the original AS plus the three prepends, seen as the transition AS 23456 because R2 doesn't understand 4-byte ASNs. For R1, the AS_PATH through R2 has five AS and through R3 it has two (1.1 1.2). With equal weight, local preference and locally originated, the shortest AS_PATH wins. The traceroute from 11.11.11.11 arrives through R3, and R2's goes around through R1, R3 and R4. Note: the AS_PATH counts autonomous systems, not router hops.

Local preference: outgoing traffic

To make traffic leave through R3 you create an inbound policy with local preference, an attribute carried in each prefix's NLRI: the default is 100 and the highest wins. The policy acts before the local BGP table, where the best path runs.

On R4, 11.11.11.0 was already leaving through 172.16.4.3, purely by best path decision. To make the effect visible, Jesús first applies local-preference 500 to what comes from R2 (172.16.3.2): every route through R2 becomes best, and the traceroute to 30.30.30.30 goes around through R2, R1 and R3. Then he moves the route-map to the right neighbor:

R4(config)# route-map RM-IN permit 10
R4(config-route-map)# set local-preference 500
R4(config-route-map)# exit
R4(config)# route-map RM-IN permit 20
R4(config-route-map)# exit
R4(config)# router bgp 64100
! first test: applied to R2, then moved to R3
R4(config-router)# no neighbor 172.16.3.2 route-map RM-IN in
R4(config-router)# neighbor 172.16.4.3 route-map RM-IN in
R4(config-router)# do clear ip bgp * soft
R4# show ip bgp
R4# traceroute 11.11.11.11 source 40.40.40.40

With RM-IN inbound on 172.16.4.3, everything R4 learns from R3 is worth 500 and traffic leaves through R3; the value travels with the route to the other routers in the confederation. With a prefix-list inside the route-map you could give, for example, 700 only to 30.30.30.0: policies per prefix, per neighbor and per direction.

Ticket 6 solved: no transit, incoming traffic through R3 with prepend, and outgoing traffic through R3 with local preference, without touching weight.

Class videos

These are the two parts of the 2nd BGP Marathon that go with this chapter. Jesús recorded them live on March 6, 2021, with Cisco IOSv routers in GNS3. The audio is in Spanish, with subtitles in Spanish, English, French and Portuguese.

Recorded live; Spanish audio with Spanish, English, French and Portuguese subtitles. When you play it, the video loads from YouTube. Day 2 playlist on YouTube

Test what you learned

1. R3 receives 20.20.20.0 from R4, but R1 doesn't receive it from R3. There are no filters. What explanation do you check first?

Answer. R3's best path. BGP only advertises its best path. If R3's best path to 20.20.20.0 is the one that comes through R1, there's nothing new to advertise to R1.

2. What does R4 advertise to a neighbor with ip as-path access-list 100 permit ^$ and neighbor … filter-list 100 out?

Answer. Only routes with an empty AS_PATH, that is, those originated in its own AS. That's how it stops being a transit AS.

3. You want traffic toward your prefix to come in through R3. What kind of policy do you create, and toward which neighbor?

Answer. An outbound policy with AS_PATH prepend toward the other neighbor, R2.

4. You apply set local-preference 500 inbound on neighbor 172.16.4.3. Which traffic changes: incoming or outgoing?

Answer. Outgoing. The routes learned from R3 win the best path inside your AS, so your routers leave through R3. To influence incoming traffic you use outbound policies, such as prepend.

5. On a router with bgp asnotation dot you want to filter routes originated by AS 1.2. Why isn't _1.2$ enough?

Answer. Because in a regex the dot is a wildcard: _1.2$ would also match an AS such as 132. Escape the dot: _1\.2$.

Key takeaways

  • A dual-homed customer advertises only its own prefixes: if it advertises what it learns, it becomes a transit AS and pays for other people's traffic.
  • To find a missing prefix: advertised-routes on the sender, received-routes on the receiver, and the best path. BGP only advertises its best path.
  • You can filter with distribute-list, prefix-list, route-map or AS-path access-list. The route-map is the standard; the AS-path access-list filters by AS.
  • ip as-path access-list 100 permit ^$ with filter-list 100 out advertises only what originates in your AS.
  • In asdot, escape the dot (_1\.2$) and test each regex with show ip bgp regexp before applying it.
  • An inbound policy (local preference) changes how traffic leaves; an outbound policy (AS_PATH prepend) changes how it comes in. You control what goes out; you influence what comes in.

Official resources

Source and credits. Chapter written from the BGP Masterclass that Jesús Espinoza recorded live in 2021. Text drafted with AI assistance and reviewed; original diagrams by The House of Routing. Technical details and configuration were checked against official Cisco documentation and IETF RFCs.

Navigate