Skip to main content
Version: 7.26

Route Selection and Filtering

Routing filters accept, reject and change the routes that a routing protocol receives or sends. Filter rules are grouped in named chains in the /routing/filter/rule menu. A protocol uses a chain by its name, for example input.filter and output.filter-chain of a BGP connection, or in-filter-chain and out-filter-chain of an OSPF instance. Select rules in /routing/filter/select-rule choose which of the candidate routes BGP sends to a peer.

The examples on this page use eBGP. The router is in AS 65001. It receives routes from the BGP connection peer1 (AS 65002) and peer2 (AS 65004), and sends routes to peer3 (AS 65003).

Route filtering​

Rule syntax and chain processing​

A filter rule is a script-like statement with matchers and actions:

if ( [matchers] ) { [actions] } else { [actions] }

A rule without if runs its actions on every route. The following rule matches subnets of 192.0.2.0/24 that are longer than /24 and increases their distance by 1. Every other route gets its distance decreased by 1:

/routing/filter/rule
add chain=myChain \
rule="if (dst in 192.0.2.0/24 && dst-len>24) {set distance +1; accept} else {set distance -1; accept}"

RouterOS runs the rules of a chain in order for each route:

  • accept and reject end the chain for the route.
  • A rule that neither accepts nor rejects passes the route, with the changes it made, to the next rule. The changes of several rules add up.
  • A route that reaches the end of the chain is rejected. A chain without an accept rejects every route.
  • jump runs another chain. At the end of that chain, or at return, the calling chain continues with its next rule. An accept or reject in the called chain ends the processing.
  • In an input chain, a rejected route stays in the routing table with the F (filtered) flag. It is not a candidate for the best route, but it uses memory. To drop BGP prefixes before they are stored, use the input.accept-* parameters of the BGP connection, for example input.accept-nlri.
  • In an output chain, a rejected route is not sent to the peer.

A changed input or output chain applies to the routes the router already has. The BGP session does not need a reset.

For example, accept only routes in 192.0.2.0/24 from peer1, no longer than /24, and give the routes that carry community 65002:100 a higher local preference:

/routing/filter/rule
add chain=peer1-in rule="if (dst-len > 24) { reject }"
add chain=peer1-in \
rule="if (bgp-communities includes 65002:100) { set bgp-local-pref 200 }"
add chain=peer1-in rule="if (dst in 192.0.2.0/24) { accept }"
/routing/bgp/connection/set [find name=peer1] input.filter=peer1-in

The longer prefixes and the routes outside 192.0.2.0/24 are filtered:

[admin@MikroTik] > /routing/route/print where bgp.session=peer1-1
Flags: F - FILTERED, A - ACTIVE; b - BGP
Columns: DST-ADDRESS, GATEWAY, AFI, ROUTING-TABLE, DISTANCE, SCOPE,
TARGET-SCOPE, IMMEDIATE-GW
DST-ADDRESS GATEWAY AFI ROUT DI SCOPE TA IMMEDIATE-GW
Ab 192.0.2.0/24 172.16.1.2 ip main 20 40 10 172.16.1.2%ether2
Fb 192.0.2.0/25 172.16.1.2 ip main 20 40 10 172.16.1.2%ether2
Fb 192.0.2.5/32 172.16.1.2 ip main 20 40 10 172.16.1.2%ether2
Fb 192.0.2.128/26 172.16.1.2 ip main 20 40 10 172.16.1.2%ether2
Fb 198.51.100.0/24 172.16.1.2 ip main 20 40 10 172.16.1.2%ether2
Fb 203.0.113.0/24 172.16.1.2 ip main 20 40 10 172.16.1.2%ether2

The accepted route has the new local preference:

[admin@MikroTik] > /routing/route/print detail where dst-address=192.0.2.0/24
Flags: A - ACTIVE; b - BGP
Ab afi=ip contribution=active dst-address=192.0.2.0/24 routing-table=main
gateway=172.16.1.2 immediate-gw=172.16.1.2%ether2 distance=20 scope=40
target-scope=10 belongs-to="bgp-IP-172.16.1.2" bgp.session=peer1-1
.as-path="65002" .communities=graceful-shutdown,65002:100,65002:200
.ext-communities=rt:65002:10,soo:65002:20,rt:1.1.1.1:222,
raw:8006000000000000 .large-communities=7010:5:5,65551:1:100
.local-pref=200 .origin=igp debug.fwp-ptr=0x203C2480

b afi=ip contribution=candidate dst-address=192.0.2.0/24 routing-table=main
gateway=172.16.4.4 immediate-gw=172.16.4.4%ether3 distance=20 scope=40
target-scope=10 belongs-to="bgp-IP-172.16.4.4" bgp.session=peer2-1
.as-path="65004" .origin=igp debug.fwp-ptr=0x203C2420

Read-only properties​

Read-only properties can only be matched. A numeric property is compared with a number or with another numeric property, for example if (bgp-path-len < dst-len). A matcher with only one possible operation needs no operator, for example if (protocol connected). A flag property needs no operator or value, for example if (ospf-dn) { reject }.

Numeric properties:

PropertyDescription
dst-lenDestination prefix length.
bgp-path-lenNumber of AS numbers in the BGP AS path. It is 0 for a route without an AS path, for example a connected route in a BGP output chain.
bgp-input-local-asLocal AS number of the BGP session that received the route.
bgp-input-remote-asAS number of the peer the route was received from.
bgp-output-local-asLocal AS number of the BGP session that sends the route.
bgp-output-remote-asAS number of the peer the route is sent to.
ospf-metricOSPF metric of the route.
ospf-tagOSPF tag of the route.
rip-metricRIP metric of the route.
rip-tagRIP tag of the route.

Flag properties:

PropertyDescription
activeThe route is active.
bgp-atomic-aggregateThe route carries the BGP ATOMIC_AGGREGATE attribute.
bgp-communities-emptyThe route has no BGP communities.
bgp-ext-communities-emptyThe route has no BGP extended communities.
bgp-large-communities-emptyThe route has no BGP large communities.
bgp-networkThe route is originated by this router from the BGP output.network address list.
ospf-dnThe OSPF route has the DN bit set.

Prefix properties:

PropertyDescription
dstDestination prefix.
ospf-fwdOSPF forwarding address.
bgp-input-local-addrLocal address of the BGP session that received the route.
bgp-input-remote-addrAddress of the peer the route was received from.
bgp-output-local-addrLocal address of the BGP session that sends the route.
bgp-output-remote-addrAddress of the peer the route is sent to.

Other properties:

PropertyValuesDescription
afiipv4, ipv6, l2vpn, l2vpn-cisco, vpnv4, vpnv6Address family of the route. Separate several families with commas: if (afi ipv4,ipv6). The routing table shows IPv4 routes as ip, the filter uses ipv4.
bgp-as-pathregexpMatches the AS path with a regular expression that works on AS numbers. See AS path regexp matching later on this page.
bgp-as-path-slow-legacyregexpDeprecated. Matches the AS path as a text string, so the pattern 500 matches AS 65002. This matcher is slow. Use it only for a short time, during the move of a configuration to bgp-as-path.
chainchain nameRuns another chain as a matcher: if (chain other-chain). The matcher is true when that chain accepts the route. The changes that chain makes to the route stay.
origininstance nameMatches routes from a specific OSPF or RIP instance: if (origin ospf-instance-1).
ospf-typeext1, ext2, inter, intra, nssa1, nssa2Type of the OSPF route: external route (type 5 LSA) with a type 1 or type 2 metric, inter-area route (type 3 LSA), intra-area route (type 1 or 2 LSA), or NSSA route (type 7 LSA) with a type 1 or type 2 metric.
protocolbgp, bgp-mpls-vpn, connected, dhcp, fantasy, isis, modem, ospf, rip, slaac, static, vpnProtocol the route comes from.
rpkiinvalid, unknown, valid, unverifiedRPKI prefix origin validation state of the route.
rpki-aspainvalid, unknown, valid, unverifiedASPA validation state of the AS path.
rtabtable nameRouting table of the route.
vrfVRF nameVRF of the route.

Writable properties​

Writable properties can be matched and changed with set. A numeric value can take the value of another numeric property (set distance bgp-med), and a + or - before the value changes the current value by that amount: set bgp-local-pref +1, set distance +bgp-path-len. Set a flag with yes or no: set blackhole yes.

Numeric properties:

PropertyDescription
distanceRoute distance.
scopeRoute scope.
scope-targetTarget scope of the route. The routing table shows it as target-scope.
bgp-weightBGP weight.
bgp-medBGP MED of the route on this router. The router sends it to iBGP peers, not to eBGP peers.
bgp-out-medBGP MED sent to the peer. Set it in the output chain of an eBGP connection. iBGP peers do not get it.
bgp-local-prefBGP LOCAL_PREF.
bgp-igp-metricBGP IGP metric.
bgp-path-peer-prependNumber of times the AS number of the peer is repeated at the start of the AS path. In the input chain, if (bgp-path-peer-prepend > 4) { reject } rejects routes that the peer prepended more than 4 times, and set bgp-path-peer-prepend 1 removes the extra prepends of the peer. In the output chain, set bgp-path-peer-prepend 3 repeats the AS number of the peer the route was received from: the path sent has it 3 times. On the output of a route that this router originates, it has no effect.
bgp-path-prependNumber of times the local AS number appears in the AS path sent to the peer. Set it in the BGP output chain.
ospf-ext-metricMetric of an external OSPF route.
ospf-ext-tagTag of an external OSPF route.
rip-ext-metricMetric of an external RIP route.
rip-ext-tagTag of an external RIP route.

Flag properties:

PropertyDescription
ospf-ext-dnDN bit of an external OSPF route.
blackholeMakes the route a blackhole route.
suppress-hw-offloadWhether to suppress L3 hardware offloading of the route.
use-te-nexthop

Other properties:

PropertyValuesDescription
gwIP address or interface nameGateway of the route, an IPv4 or IPv6 address or an interface name. On BGP output, the gateway can be changed when the router is a route reflector, when nexthop-choice is propagate, or for a non-eBGP session without nexthop-choice=force-self.
gw-llIPv6 link-local addressIPv6 link-local next hop. Give it with the interface: set gw-ll fe80::1%ether2. The same BGP output conditions as for gw apply.
gw-interfaceinterface nameInterface part of the gateway, for a next hop such as 192.0.2.1%ether1.
gw-checknone, arp, icmp, bfdHow the router checks the gateway.
pref-srcIPv4 or IPv6 addressPreferred source address.
bgp-originigp, egp, incompleteBGP ORIGIN attribute.
ospf-ext-fwdIPv4 or IPv6 addressForwarding address of an external OSPF route.
ospf-ext-typetype1, type2Metric type of an external OSPF route.
commentstringComment of the route. The routing table shows it as a ;;; line.
bgp-communitiescommunities or a community-list nameBGP Communities attribute (RFC 1997). Each community is 32 bits.
bgp-ext-communitiesextended communities or a community-ext-list nameBGP Extended Communities attribute (RFC 4360). RouterOS parses route target (prefix rt:) and site of origin (prefix soo:) values, for example set bgp-ext-communities rt:65002:10. Write other types as a 64-bit hex value: set bgp-ext-communities 0x8006000000000000. A hex value of a type RouterOS parses is shown in that type, for example 0x0002fde800000064 as rt:65000:100.
bgp-large-communitieslarge communities or a community-large-list nameBGP Large Communities attribute (RFC 8092). It holds 32-bit AS numbers, which do not fit in a standard community (65551:100 is rejected as a standard community). Each community is 12 bytes in three parts: global-admin:local-part-1:local-part-2.

Commands​

CommandDescription
acceptAccept the route and stop processing the chain.
rejectReject the route and stop processing the chain. In an input chain, the router keeps the route as filtered (F flag), and it is not a candidate for the best route. In an output chain, the route is not sent.
returnReturn to the calling chain.
jump <chain>Run the rules of another chain.
set <property> <value>Set a writable property.
unset <property>Remove the value of pref-src, bgp-med, bgp-out-med or bgp-local-pref.
append <property> <value>Add to bgp-communities, bgp-ext-communities, bgp-large-communities or comment.
delete <property> <value>Delete communities from bgp-communities, bgp-ext-communities or bgp-large-communities. The keywords are listed in Delete BGP communities.
filter <property> <value>The opposite of delete: keep only the given communities and delete the rest. Works on the same properties as delete.
rpki-verify <group>Check the routes of the chain against the RPKI group for prefix origin validation. Put it before the rpki matchers of the chain: rpki-verify group1, then if (rpki invalid) { reject }. Without rpki-verify in the chain, rpki unknown and rpki unverified do not match. When the group has no RPKI data, the state is unverified.
rpki-aspa-verify <group>Check the routes of the chain against the RPKI group for ASPA validation.

Operators​

Logical operators​

OperatorDescriptionExample
&&Logical AND.if (dst in 192.168.0.0/16 && dst-len in 16-32) { reject }
||Logical OR.if (dst in 192.168.0.0/16 || dst-len in 16-32) { reject }
notLogical NOT.if (not bgp-network) { reject }

Numeric operators​

OperatorDescription
inTrue when the value is in the range. Write the range as 16-32 or 16..32.
==True when the values are equal.
!=True when the values are not equal.
>True when the left value is greater than the right value.
<True when the left value is less than the right value.
>=True when the left value is greater than or equal to the right value.
<=True when the left value is less than or equal to the right value.

Prefix operators​

OperatorDescription
inTrue when the prefix is the given network or one of its subnets: dst in 192.0.2.0/24 matches 192.0.2.0/24 and 192.0.2.0/25.
==True when the prefix is equal to the given value.
!=True when the prefix is not equal to the given value.

in also takes the name of a firewall address list (/ip/firewall/address-list): dst in list_name. With an address list, in matches the exact prefixes of the list, and also the /32 routes inside a list entry, because address lists match host addresses. A list entry 192.0.2.0/24 matches the routes 192.0.2.0/24 and 192.0.2.5/32, but not 192.0.2.0/25. To leave out the /32 routes, add a prefix length: if (dst in list_name && dst-len < 32) { accept }.

Community operators​

The operators work on bgp-communities, bgp-ext-communities and bgp-large-communities. In the examples, a route with communities 65002:100 and 65002:200, and no other communities, matches.

OperatorTrue whenExample that matches
equalThe route has exactly the given communities.equal 65002:100,65002:200
equal-listThe route has exactly the communities of the list.
anyThe route has at least one of the given communities.any 65002:100,65002:300
any-listThe route has at least one community of the list.
includesThe route has all the given communities, and possibly more.includes 65002:100
includes-listThe route has all the communities of the list.
subsetEvery community of the route is one of the given communities.subset 65002:100,65002:200,65002:300
subset-listEvery community of the route is in the list.
any-regexpAt least one community of the route matches the regexp. The regexp syntax is in Community regexp matching.any-regexp ^65002:2
subset-regexpEvery community of the route matches the regexp.subset-regexp ^65002:
note

When the route has no communities of that type, the "any" operators (any, any-list, any-regexp) are false, because the route has no community to match. The "subset" operators (subset, subset-list, subset-regexp) are true, because an empty set is a subset of any set. To check for an empty attribute, use bgp-communities-empty, bgp-ext-communities-empty or bgp-large-communities-empty.

String operators​

OperatorDescriptionExample
findTrue when the value contains the substring.if (comment find c12)
regexpTrue when the value matches the regexp.if (comment regexp ^abc[0-9]+)

Delete BGP communities​

The delete command removes communities from a route. Besides community values and list names, it accepts keywords for each type of community:

  • bgp-communities:
    • wk - Remove the well-known communities.
    • other - Remove the communities that are not well-known.
    • regexp "<pattern>" - Remove the communities that match the regexp.
    • <community-list name> - Remove the communities of the list, including the communities that match regexp entries of the list.
  • bgp-ext-communities:
    • rt - Remove the route targets.
    • soo - Remove the site of origin values.
    • other - Remove the extended communities that are not route targets or site of origin values.
    • regexp "<pattern>" - Remove the extended communities that match the regexp.
    • <community-ext-list name> - Remove the extended communities of the list, including the ones that match regexp entries of the list.
  • bgp-large-communities:
    • all - Remove all large communities.
    • regexp "<pattern>" - Remove the large communities that match the regexp.
    • <community-large-list name> - Remove the large communities of the list, including the ones that match regexp entries of the list.

You can give several values separated by commas. For example, delete all site of origin values, the other types of extended communities, and the route targets from a community-ext-list:

/routing/filter/community-ext-list
add list=myRTList communities="rt:1.1.1.1:222"
/routing/filter/rule
add chain=myChain rule="delete bgp-ext-communities soo,other,myRTList;"

Delete all large communities of the administrative domain 65551, whatever the other two parts are:

/routing/filter/rule
add chain=myChain \
rule="delete bgp-large-communities regexp \"^65551:[0-9]+:[0-9]+\$\""

AS path regexp matching​

An AS path is a sequence of autonomous system numbers (ASNs). For example, the AS path 123 456 789 means that the route originated in AS 789 and passed AS 456 before it reached AS 123. To match AS numbers or sets of AS numbers in the AS path, use a regular expression (regexp) with the bgp-as-path matcher.

A regexp can work on the AS path in two ways:

  • On the whole AS path converted to a string. The regexp matches characters. Cisco routers and the deprecated bgp-as-path-slow-legacy matcher work this way.
  • On each AS number of the path as a number. The regexp matches AS numbers. Juniper routers and the bgp-as-path matcher work this way.

Matching per AS number is faster and uses fewer resources than string matching. Patterns written for string matching cannot be copied as they are: they can give syntax errors or match other paths.

Take this rule:

/routing/filter/rule
add chain=myChain rule="if (bgp-as-path .1234.) {accept}"

The pattern matches the AS number 1234 anywhere in the middle of the AS path, for example 1 1234 2. With string matching, . matches any character, including the space between AS numbers, so the same pattern also matches paths that do not contain AS 1234, such as 112345.

The string pattern 1234[5-9] matches the text 12345 through 12349 anywhere in the path, for example 12345 3434 and 11 9123467 22. Matched per AS number, the same pattern matches the AS number 1234 followed by an AS number from 5 to 9, for example 1234 7 111 or 111 1234 5 222. It does not match 12345 3434.

warning

Do not copy regexp patterns from string-matching configurations, such as Cisco configurations. They can match other paths than intended, which can lead to dangerous configurations.

^$ matches a BGP route with an empty AS path, such as a route an iBGP peer originated. A route without an AS path, such as a connected route in a BGP output chain, does not match any regexp, not even ^$. To match both, use bgp-path-len == 0. In an output chain, the AS path does not include the local AS number.

Test a regexp​

The /routing/filter/test-as-path-regexp command checks a regexp against an AS path before you use it in a filter. It also takes number lists from /routing/filter/num-list. Create a list:

/routing/filter/num-list/add list=test range=100-1500

Test a regexp that uses the list:

[admin@MikroTik] > /routing/filter/test-as-path-regexp regexp="[[:test:]] 5678\$" as-path="1234,5678"
match

In a command, write $ inside a quoted string as \$. The command prints match or no match.

Supported operators​

OperatorDescriptionExampleMatchesDoes not match
^Start of the path.^1234: the path starts with AS 1234.1234 56785678 1234
$End of the path.1234$: the route originated in AS 1234.5678 12341234 5678
*Zero or more occurrences of the AS number.^1234*$: an empty path, or a path of AS 1234 only.1234, 1234 1234 1234, empty path1234 5678
+One or more occurrences of the AS number.1234+: AS 1234 appears at least once.1234, 3 1234 612345 678
?Zero or one occurrence of the AS number.^1234? 5678: the path starts with 5678, or with one 1234 and then 5678.5678, 1234 56781234 1234 5678, 12345 5678
.Any one AS number.^.$: a path of length one.12345, 456781234 5678
|One of the alternatives.^(1234|5678): the path starts with AS 1234 or 5678.1234, 5678, 1234 567891011
[ ], [^ ]One AS number from the set, or with ^ after the bracket, one AS number not in the set. A set can hold AS numbers, ranges and number lists: [[:list_name:]].^[1234 5678 1-100]: the path starts with 1234, 5678 or an AS number from 1 to 100.1234, 99, 5678101
()Group of terms.^(1234$|5678): the path is only AS 1234, or starts with AS 5678.1234, 5678 99991234 5678

Repetition ranges such as {2} are not supported. Write the AS number several times instead: ^1234 1234$.

Community and number lists​

Number lists in the /routing/filter/num-list menu hold numbers and ranges. AS path regexps use them as [[:list_name:]], and numeric matchers as in list_name, for example if (bgp-input-remote-as in list_name).

Community lists hold BGP communities for matching, appending and setting. Each type of community has its own menu: community-list, community-ext-list and community-large-list.

For example, match a route that carries exactly the communities of a list, and remove all its communities:

/routing/filter/community-list
add communities=65002:100 list=myCommunityList

/routing/filter/rule
add chain=myChain \
rule="if (bgp-communities equal-list myCommunityList) {delete bgp-communities wk,other; accept;}"

Each entry of a community list holds either community values (the communities parameter) or a regexp (the regexp parameter), not both. A list can mix both kinds of entries:

/routing/filter/community-list/add list=myList communities=65002:100
/routing/filter/community-list/add list=myList regexp="^65002:.*"

Community regexp matching​

Community regexp matching works on the text form of each community, unlike AS path regexp matching, which works on AS numbers. The regexp is applied to each community of the attribute separately, in the form that /routing/route/print detail shows:

Community typeText formExamples
BGP communities (RFC 1997)asn:value, both fields in decimal. Well-known communities are shown by name, and some printed names differ from the names you type in a rule or list (llgr-stale is shown as LLGR-stale, no-peer as nopeer, route-filter-4 as filter-4). A regexp must use the printed name, because matching is case-sensitive. Printed names: internet, graceful-shutdown, accept-own, accept-own-nh, filter-4, filter-6, filter-xlate-4, filter-xlate-6, LLGR-stale, no-LLGR, blackhole, no-export, no-advertise, local-as, nopeer.65551:100, no-export
BGP extended communities (RFC 4360)The prefix rt: (route target) or soo: (site of origin), then the value in asn:value, ipv4:value or asn4:value form. A two-octet AS number in the four-octet field is shown with an L suffix. Other known types are shown as encap:, es-label:, es-import: or mobility: values. All other extended communities are shown as raw:<hex>.rt:65551:100, soo:1.2.3.4:5, rt:65000L:10, raw:8006000000000000
BGP large communities (RFC 8092)global-admin:local-part-1:local-part-2, each part a decimal 32-bit number. All three parts are always shown, including zero values.65551:0:100

The regexp uses the same engine as the comment regexp matcher of routing filters. Its syntax is like POSIX ERE and supports ^, $, ., *, +, ?, |, () and the character classes [ ] and [^ ]. Repetition bounds {n,m} are not supported. A backslash escapes one literal character, so \d matches the letter d. Use [0-9] for digits. Matching is case-sensitive and not anchored: use ^ and $ to match the whole community. The scripting ~ operator uses a different regexp implementation, so a pattern does not always work in both.

For example, reject the routes that carry a large community of the administrative domain 7010:

/routing/filter/community-large-list/add list=lcom7010 \
regexp="^7010:[0-9]+:[0-9]+\$"

/routing/filter/rule
add chain=bgp-in \
rule="if (bgp-large-communities any-list lcom7010) {reject;}"

Regexp entries of community lists work for matching (any-list, includes-list, subset-list, equal-list) and with delete and filter, which remove the communities that match a regexp entry of the list. append and set write only the entries with community values, because a regexp is a pattern, not a value. set with a list that has no community values (an empty list or a list of regexps only) removes all communities of that type from the route. append with such a list changes nothing.

Route selection​

When BGP sends routes to a peer, it sends the best route for each prefix by default. This also holds when BGP additional paths (output.add-path) are enabled. The router has two candidate routes for 192.0.2.0/24. The route from peer1 is active, because the input filter of the earlier example gave it a higher local preference:

[admin@MikroTik] > /routing/route/print where dst-address=192.0.2.0/24
Flags: A - ACTIVE; b - BGP
Columns: DST-ADDRESS, GATEWAY, AFI, ROUTING-TABLE, DISTANCE, SCOPE,
TARGET-SCOPE, IMMEDIATE-GW
DST-ADDRESS GATEWAY AFI ROUT DISTANCE SCOPE TA IMMEDIATE-GW
Ab 192.0.2.0/24 172.16.1.2 ip main 20 40 10 172.16.1.2%ether2
b 192.0.2.0/24 172.16.4.4 ip main 20 40 10 172.16.4.4%ether3

Select rules in the /routing/filter/select-rule menu choose other routes. Set the select chain as output.filter-select of the BGP connection. A select rule with do-where selects the routes that the routing filter chain of do-where accepts. When the select chain selects no route for a prefix, BGP does not send that prefix to the peer.

The following select chain does the same as the default selection, and chooses the active route:

/routing/filter/rule
add chain=get_active rule="if (active) {accept}"

/routing/filter/select-rule
add chain=my_select_chain do-where=get_active

To send peer3 the routes learned from AS 65004 instead of the active routes:

/routing/filter/rule
add chain=from-as65004 rule="if (bgp-input-remote-as == 65004) {accept}"
/routing/filter/select-rule
add chain=prefer-as65004 do-where=from-as65004
/routing/bgp/connection/set [find name=peer3] \
output.filter-select=prefer-as65004

peer3 gets the routes through AS 65004. The prefixes that have no route from AS 65004 are not sent to peer3. The session name is the connection name with a number, here peer3-1:

[admin@MikroTik] > /routing/bgp/advertisements/print detail where peer=peer3-1
peer=peer3-1 dst=192.0.2.0/24 afi=ip nexthop=172.16.0.1 origin=0
as-path=sequence 65001 65004

peer=peer3-1 dst=203.0.113.0/24 afi=ip nexthop=172.16.0.1 origin=0
as-path=sequence 65001 65004

The output chain (output.filter-chain) works on the routes the select chain selected. When it rejects the selected route, BGP does not send another candidate route for that prefix.

With BGP additional paths, a select rule with do-take sets how many candidate routes BGP sends for each prefix. On a RouterOS peer, enable input.add-path to accept the additional paths. For example, send up to 2 paths for each prefix to peer3:

/routing/filter/select-rule
add chain=select-for-addpath do-take=2
/routing/bgp/connection/set [find name=peer3] output.add-path=ip \
output.filter-select=select-for-addpath

The router has three candidate routes for 203.0.113.0/24, from peer1, peer2 and an iBGP peer, and sends two of them:

[admin@MikroTik] > /routing/bgp/advertisements/print where peer=peer3-1 && dst=203.0.113.0/24
Columns: DST, PEER, AFI, NEXTHOP
DST PEER AFI NEXTHOP
203.0.113.0/24 peer3-1 ip 172.16.0.1
203.0.113.0/24 peer3-1 ip 172.16.0.1

Routing filter wizard​

The /routing/filter/filter-wizard command builds a filter rule from parameters, as an alternative to writing the rule syntax. It shows the rule it creates, adds the rule at the end of the chain and gives it the comment added by filter-wizard:

[admin@MikroTik] > /routing/filter/filter-wizard action=accept chain=vpn-in afi=vpnv4 set-bgp-ext-communities=rt:2:2
Filter rule 'if (afi vpnv4) { set bgp-ext-communities rt:2:2; accept; }' added

[admin@MikroTik] > /routing/filter/rule/print where chain=vpn-in
4 ;;; added by filter-wizard
chain=vpn-in
rule="if (afi vpnv4) { set bgp-ext-communities rt:2:2; accept; }"

The wizard parameters do not always use the rule names: afi=ip becomes afi ipv4, and dst=192.0.2.0/24 becomes dst == 192.0.2.0/24, an exact match, not dst in.

When the wizard cannot add a rule, it prints the error and logs it with the route and error topics:

[admin@MikroTik] > /routing/filter/filter-wizard action=accept chain=vpn-in afi=vpnv4 match-chain=vpn-in
Error adding 'if (chain vpn-in && afi vpnv4) { accept; }'match with 'vpn-in' creates chain loop (6)

[admin@MikroTik] > /log/print where topics~"error" && message~"vpn-in"
2026-12-01 08:53:54 route,error Error adding 'if (chain vpn-in && afi vpnv4) { accept; }'match with 'vpn-in' creates chain loop (6)