Route policies
A route policy is a list of rules that every route passes through, from the top. On import, it decides which routes you accept from a neighbor, and how you mark them. On export, it decides which routes you announce to that neighbor.
The rule for both is short: never accept everything, and never announce everything.
How a policy is read
In a device file, routing.policies holds named lists of rules. Each BGP neighbor says, by name, which list is its import_policy and which is its export_policy. A rule is one of three kinds.
A call runs another policy at that point, for every route: { call: "sanity" }. A route that the called policy accepts or rejects stops there, and any other route goes on to the next rule. A call holds nothing else. To call a policy for some routes only, put the condition inside that policy.
A communities rule, such as { communities: "import" }, stands for the rules that your community scheme needs at that point. Communities explains it.
Every other rule has up to three parts, used in this order:
if: which routes the rule applies to, such as aprefix, aprefix_set, aprefix_length, anrpkistate, or alarge_community. A rule withoutifapplies to every route.set: what to change on the route, such aslocal_preffrom Choosing the best route.then:acceptorreject, which ends the policy for that route.
A rule without then changes the route and moves on to the next rule. The first decision wins, and a route that no rule accepts is rejected.
What to keep out
A neighbor can send you routes it shouldn't. It may do so by mistake or on purpose. Four kinds should never reach your routing table:
- Addresses that are not on the internet: private ranges such as
10.0.0.0/8, and Addresses that can never be a real destination on the internet. For example, 127.0.0.0/8 means the device itself, and 169.254.0.0/16 is used only on one link.. Circuit's ready-made values list them aspresets.rfc1918andpresets.bogons. - Prefixes too long or too short. Most networks accept nothing longer than a /24 for IPv4, or a /48 for IPv6. So a longer prefix wouldn't travel far, and it only makes every table bigger. Nothing shorter than a /8 or a /16 is a real network either.
- Your own prefixes, coming back. You already know where your own space is. A neighbor that announces it means that someone else claims it. It may be a mistake or an attempt to steal its traffic.
- Routes that RPKI marks invalid. With Resource Public Key Infrastructure: the holder of a prefix publishes a signed record, a ROA, that says which AS may announce it. Routers check each route against those records., a validator compares each route with the published records. A route is
validif a record allows it,invalidif one contradicts it, andnot-foundif there is none. Your validators go inrouting.rpki.
presets.bgp_sanity holds the rules for the second and the fourth. The first and the third need a prefix_set each, a named list of prefixes. A route matches a set when its prefix is one of the set's own, or inside one:
routing: {
prefix_sets: {
// Every IPv4 block RFC 6890 keeps off the internet: the martians and the private ranges.
bogons: { prefixes: presets.bogons.ipv4 },
ours: { prefixes: ["198.51.100.0/24"] },
},
policies: {
sanity: presets.bgp_sanity,
"transit-import": [
{ call: "sanity" },
{ description: "Martians and private ranges", if: { prefix_set: "bogons" }, then: "reject" },
{ description: "Our own space, coming back", if: { prefix_set: "ours" }, then: "reject" },
{ communities: "import" },
{ set: { local_pref: 100 }, then: "accept" },
],
},
},presets.bogons.ipv4 also holds the documentation ranges, such as 203.0.113.0/24. The widget uses them for real networks, so its bogons set leaves them out.
The policy transit-import, read from the top
- 1
Call sanity
- IPv4 shorter than /8: reject
- IPv4 longer than /24: reject
- IPv6 shorter than /16: reject
- IPv6 longer than /48: reject
- RPKI invalid: reject
- 2
Martians and private ranges
- 3
Our own space, coming back
- 4
Tag where it came from
Changes the route and moves on
- 5
Local preference 100
Changes the route and moves on
- 6
Accept
Routes from AS64510, a careless transit
No route yet.
Feed the routes in one at a time. Then turn a rule off and see what gets in.
prefix_sets: {
bogons: { prefixes: presets.bogons.ipv4 },
ours: { prefixes: ["198.51.100.0/24"] },
},
policies: {
sanity: presets.bgp_sanity,
"transit-import": [
{ call: "sanity" },
{ description: "Martians and private ranges", if: { prefix_set: "bogons" }, then: "reject" },
{ description: "Our own space, coming back", if: { prefix_set: "ours" }, then: "reject" },
{ communities: "import" },
{ set: { local_pref: 100 } },
{ then: "accept" },
],
},
Look at 203.0.113.128/25. RPKI marks it invalid, but the length check rejected it, because that check comes first inside sanity. The first decision wins, so a later rule never sees the route.
With sanity off, the default route, the /25, the /64, and 192.0.2.0/24 all get in. With the last rule off, nothing gets in at all, not even the good routes. That is why every import ends in a rule with then: "accept", after every rule that rejects.
What to announce
An export accepts your own prefixes, and your customers' if you sell them transit. Then it rejects everything else. The edge router example writes it like this, where the prefix set ours holds your own blocks:
export: [
{ communities: "export" },
{ description: "Our own space", if: { prefix_set: "ours" }, then: "accept" },
{ description: "Nothing else leaves", then: "reject" },
],If you announce more than that, you cause a Routes learned from one provider or peer, announced to another, so traffic between two other networks starts to flow through yours.. A small network suddenly becomes the way between two large ones, and its links can't carry that.
You buy transit from Hurricane Electric and from AS64510, a transit that doesn't filter what its customers announce.
What AS64510 learns from you
- 198.51.100.0/24, your own prefix
Send the traffic with each choice. Then announce everything, and try the limit.
as64510: {
address: "203.0.113.5",
remote_as: 64510,
role: "provider",
import_policy: "as64510-import",
export_policy: "as64510-export",
max_prefixes: 1_200_000,
},
"as64510-export": [
{ description: "Announce 198.51.100.0/24", if: { prefix: "198.51.100.0/24" }, then: "accept" },
{ description: "Nothing else leaves", then: "reject" },
],
$ circuit validate
✔ 1 device(s), no errors.
You never sent that traffic anywhere yourself. The routes you announced pulled it in, because AS64510 believed them and preferred them. Most transits give their customers' routes a higher local preference, so a leak from a customer wins over the real path.
Three things guard against a leak, and the example uses all of them:
- The export ends in a reject. The example turns on
exports_end_in_reject: trueinchecks. Socircuit validatefails on an export that doesn't end in a reject, as the widget's terminal showed.diffandapplystop too, until you fix it. rolesays what each neighbor is to you:provider,customer,peer, or an exchange's route server,rs. It turns on RFC 9234, so both sides reject a leaked route by themselves. Of the platforms Circuit supports, only RouterOS has BGP roles.max_prefixesis a fuse. When a neighbor sends more prefixes than this, the session closes, and it stays closed until you clear it. On RouterOS,circuit bgpwith--clear-limitopens it again. Set the limit a little higher than what each neighbor should send. Your providers set one for you on their side, as the limit in the widget showed.
Communities: tags on routes
A A tag on a route. Your router can add it when a route comes in and match on it later, or a neighbor can add it to ask you to do something with the route. is a tag that a route carries. The example uses large communities. Each one is three numbers: your AS, a function, and a parameter, such as 64500:1:1.
The example's circuit.ts says what its communities mean, in communities, as BGP communities explains. Then { communities: "import" } in the import does two things to each route:
- It removes every community of yours that the neighbor put on the route, so nobody outside can fake them.
- It adds where the route came from. That is
64500:1:1for the transit group, and64500:3:6939for Hurricane Electric, AS6939.
A later rule can then match a large_community. For example, a customer may buy only the routes you learn at exchanges. Its export accepts the routes tagged as learned from an exchange. What an ISP sells shows that product.