BGP
BGP is how autonomous systems tell each other which prefixes they can reach. Two neighboring routers keep a session open over TCP port 179, and each one announces its routes through it. Every announcement carries the prefix and its AS path: the list of ASes it has passed through.
Before an AS passes a route on to another AS, it puts its own number at the front of the path.
Sessions between neighbors
OSPF routers find their neighbors by saying hello, but BGP routers don't. You write down each neighbor, with its address and its AS number, and the neighbor writes down yours. A session comes up only when both sides agree. It goes through these steps:
- Connect. One router opens a TCP connection to the other's address, on port 179.
- Open. Each router sends an OPEN message with its AS number, its router ID, and its hold time: how long it waits for news. If an AS number is not the one the other side expects, that side sends a NOTIFICATION with the reason, and closes the connection.
- Established. Each router answers with a KEEPALIVE, and the session is up.
- Updates. Each router sends UPDATE messages with its routes. After that, only changes are sent: a new route, or the withdrawal of one that is gone. KEEPALIVE messages keep coming. If nothing arrives for a whole hold time, the session goes down, and every route learned through it is removed.
A router shows each session's state by its name in the BGP standard. The states are Idle, Connect, Active, OpenSent, OpenConfirm, and Established. A session stuck in Active is trying to connect and getting no answer.
Your router
AS64500, 203.0.113.2
Hurricane Electric
AS6939, 203.0.113.1
routing: {
router_id: "203.0.113.2",
bgp: {
originated_prefixes: ["198.51.100.0/24"],
neighbors: {
"hurricane-electric": {
address: "203.0.113.1",
role: "provider",
},
},
},
},
- Idle
- Connect
- Active
- OpenSent
- OpenConfirm
- Established
Bring the session up with remote_as empty, then with a wrong number, then with the right one. Once it is up, try it without the policies, and with max_prefixes at 10.
Before you added the policies, the session was up. Yet no route came in, and none went out. In Circuit, a neighbor without an import_policy accepts nothing, and one without an export_policy announces nothing. So a session you forgot to filter carries nothing, instead of everything. Route policies covers what goes into them.
Announcements and the AS path
An announcement is a prefix with facts about it, called attributes. Two of them matter most here:
- The AS path: the ASes the announcement passed through, the nearest first. The last number is the AS where the prefix comes from, its The AS that first announced a prefix: the network the addresses belong to. It is the last AS in the AS path..
- The next hop: the address to send traffic to for this prefix. Between two ASes, it is usually the address of the router that sent the announcement.
Read the AS path from right to left, and you see how the announcement traveled. Read it from left to right, and you see the way the traffic goes.
Announce your prefix, and step through the rounds.
| AS | AS path to 198.51.100.0/24 | ASes on the way |
|---|---|---|
| You, AS64500 | ||
| Hurricane Electric, AS6939 | ||
| Cogent, AS174 | ||
| Arelion, AS1299 | ||
| Google, AS15169 | ||
| Netflix, AS2906 |
In this widget, each AS keeps the shortest path it hears. It passes the path on by the rules from Networks of networks. Real routers choose between paths with more than their length, as Choosing the best route shows. Real routers also differ in whether they send a route back to the neighbor it came from. When they do, the loop check you just watched stops it.
The AS path does three jobs at once:
- It stops loops. An AS that sees its own number in a path drops the route, as you did in round 2.
- It is a rough measure of distance. BGP prefers fewer ASes on the way, among other things.
- It says who is on the way. A policy can refuse a route because of an AS in its path. It can also refuse a route whose origin is not the AS that should announce that prefix.
eBGP and iBGP
A session between two different ASes is eBGP, external BGP. A session between two routers of the same AS is iBGP, internal BGP. A network with several edge routers uses iBGP, so that each one knows what the others learned from outside.
iBGP works a little differently. A router doesn't add its own AS number to the path, because the route hasn't left the AS. It also doesn't pass a route learned over iBGP to another iBGP neighbor. So every BGP router in an AS needs a session with every other one, or a router that reflects routes between them. In Circuit, a neighbor whose remote_as is your own asn is an iBGP session. BGP inside a network goes further.
BGP in Circuit
BGP is routing.bgp: the prefixes you announce, and your neighbors, each under a name you choose:
routing: {
router_id: "203.0.113.2",
static_routes: [{ prefix: "198.51.100.0/24", blackhole: true, description: "Originate" }],
bgp: {
originated_prefixes: ["198.51.100.0/24"],
neighbors: {
"hurricane-electric": {
address: "203.0.113.1",
remote_as: 6939,
description: "Hurricane Electric",
role: "provider",
import_policy: "transit-import",
export_policy: "transit-export",
max_prefixes: 1_200_000,
},
},
},
policies: {
sanity: presets.bgp_sanity,
"transit-import": [{ call: "sanity" }, { then: "accept" }],
"transit-export": [
{ description: "Our block", if: { prefix: "198.51.100.0/24" }, then: "accept" },
{ description: "Nothing else leaves", then: "reject" },
],
},
},| Field | What it does |
|---|---|
originated_prefixes | The prefixes this router announces. Each one needs a route in the table, which is what the blackhole route from Routing tables is for. |
address | The neighbor's address. |
remote_as | The neighbor's AS number. It must match the one in the neighbor's OPEN. |
role | What the neighbor is to you: provider, customer, peer, rs for a route server, or rs-client. With roles on both sides, each side refuses a route the other should not have passed on, such as a provider's route coming back through a customer. |
import_policy | The policy for the routes you learn from the neighbor. Nothing is accepted without one. |
export_policy | The policy for the routes you announce to the neighbor. Nothing is announced without one. |
max_prefixes | Close the session if the neighbor sends more routes than this. It is a fuse against a mistake on their side. |
presets.bgp_sanity is one of Circuit's ready-made values. It refuses routes that should never be on the internet, and Route policies opens it up. The input firewall must also let BGP in from your neighbors, so that their side can open the session too:
{ description: "BGP", if: { protocol: "tcp", dst_port: presets.ports.bgp, src: "203.0.113.1/32" }, then: "accept" },Not every platform runs BGP
RouterOS, Junos, and FortiOS run BGP. VRP and Raisecom ROS report routing.bgp as an error. Only RouterOS has BGP roles, so the others report role as an error. FortiOS has no RPKI, so it refuses the RPKI rule in presets.bgp_sanity. See Platforms.