Project: an edge router
In this project, you build Circuit's edge router example. It joins your network to the internet, and announces your own address space to a transit and at an internet exchange.
Start the project
Install Circuit, and start a project with circuit new, which asks for your AS number. Then copy the example's three files into the project:
circuit.ts, with your AS, your prefixes, and your communitiessites/pop-1/edge-01.ts, with the routerlib/checks.ts, with a check of your own
The names in this example
The transit is Hurricane Electric, AS6939. The exchange is DE-CIX Frankfurt, whose route servers are AS6695. Replace the addresses, AS numbers, and port names with the ones your providers give you.
Your AS, your prefixes, and your communities
// The network, AS64500: its own address space, its BGP communities, and the checks Circuit runs on it.
import { importsFiltered } from "./lib/checks";
export const as64500 = circuit({
asn: 64500,
prefixes: {
ours: { v4: "198.51.100.0/24", v6: "2001:db8:100::/48" },
},
communities: {
groups: { transit: { value: 1 }, exchange: { value: 2 } },
learned_from_group: { code: 1 },
learned_from_as: { code: 3 },
},
sites: {
"pop-1": { description: "The point of presence: one router with an IP transit and an internet exchange." },
},
checks: {
exports_end_in_reject: true,
imports_end_in_accept_or_reject: true,
untrusted_vlans: { filtering_devices: ["edge-01"] },
imports_filtered: importsFiltered,
},
});asnis your AS. Every BGP session of every device uses it, unless a device writes another.prefixes.oursis your address space: one IPv4 block and one IPv6 block, each with a name. The router uses them by name, such asas64500.prefixes.ours.v4.communitiesis your BGP community scheme, as in Route policies. The router tags each route it learns with the group it came from,transitorexchange, and with the AS it came from.checksturns on two of Circuit's checks for BGP, and one for the VLANs that come from outside. It also adds one of your own, fromlib/checks.ts.
The router
// The edge router: BGP with an IP transit and an internet exchange, filtered on the way in and on the way out.
// Hurricane Electric and DE-CIX stand for your own transit and exchange. The addresses are documentation ranges, so use the ones your providers give you.
import { as64500 } from "../../circuit";
export default as64500.device({
name: "edge-01",
site: "pop-1",
role: "Edge router",
platform: "routeros",
model: "CCR2216-1G-12XS-2XQ",
connection: { host: "10.0.0.1", user: "operator" },
system: {
timezone: "Asia/Jakarta",
ntp: { servers: ["pool.ntp.org"] },
users: { operator: { role: "admin", password: secret("EDGE_01_PASSWORD") } },
},
management: { allow: ["10.0.0.0/24"], ssh: {} },
interfaces: {
ethernet: {
"1g-1": { description: "Management", addresses: ["10.0.0.1/24"] },
"25g-1": { description: "Hurricane Electric", access_vlan: "transit", ingress_acl: "edge" },
"25g-2": { description: "DE-CIX", access_vlan: "exchange", ingress_acl: "edge" },
},
vlan_interface: {
transit: { vlan: "transit", addresses: ["203.0.113.2/30"] },
exchange: { vlan: "exchange", addresses: ["192.0.2.10/24", "2001:db8:ffff::10/64"] },
},
},
switching: {
vlans: {
transit: { id: 300, description: "IP transit from Hurricane Electric, AS6939", untrusted: true },
exchange: { id: 200, description: "DE-CIX peering LAN", untrusted: true },
},
// In the switch chip, before a packet reaches the router's CPU: nothing from outside may claim our addresses or private ones.
acls: {
edge: [
{ description: "Our own space", if: { vlan: ["transit", "exchange"], src: as64500.prefixes.ours.v4 }, then: "drop" },
{ description: "Private ranges", if: { vlan: ["transit", "exchange"], src: presets.rfc1918 }, then: "drop" },
],
},
},
routing: {
router_id: "203.0.113.2",
// A route for each block we announce, so it is in the table even when nothing inside it is up.
static_routes: [
{ prefix: as64500.prefixes.ours.v4, blackhole: true, description: "Originate" },
{ prefix: as64500.prefixes.ours.v6, blackhole: true, description: "Originate" },
],
bgp: {
originated_prefixes: [as64500.prefixes.ours.v4, as64500.prefixes.ours.v6],
neighbors: {
"hurricane-electric": {
address: "203.0.113.1",
remote_as: 6939,
description: "Hurricane Electric",
role: "provider",
max_prefixes: 1_200_000,
import_policy: "transit-import",
export_policy: "export",
communities: { groups: ["transit"] },
},
"de-cix-rs-v4": {
address: "192.0.2.1",
remote_as: 6695,
description: "DE-CIX route server",
role: "rs",
max_prefixes: 100_000,
import_policy: "exchange-import",
export_policy: "export",
communities: { groups: ["exchange"] },
},
"de-cix-rs-v6": {
address: "2001:db8:ffff::1",
remote_as: 6695,
description: "DE-CIX route server",
role: "rs",
max_prefixes: 50_000,
import_policy: "exchange-import",
export_policy: "export",
communities: { groups: ["exchange"] },
},
},
},
prefix_sets: {
ours: { prefixes: as64500.prefixes.ours },
"bogons-v4": { prefixes: presets.bogons.ipv4 },
"bogons-v6": { prefixes: presets.bogons.ipv6 },
},
policies: {
sanity: presets.bgp_sanity,
// A route from the exchange beats the same route from the transit: the higher local preference wins.
"transit-import": [
{ call: "sanity" },
{ description: "Bogons", if: { prefix_set: "bogons-v4" }, then: "reject" },
{ description: "Bogons", if: { prefix_set: "bogons-v6" }, then: "reject" },
{ if: { prefix_set: "ours" }, then: "reject" },
{ communities: "import" },
{ set: { local_pref: 100 }, then: "accept" },
],
"exchange-import": [
{ call: "sanity" },
{ description: "Bogons", if: { prefix_set: "bogons-v4" }, then: "reject" },
{ description: "Bogons", if: { prefix_set: "bogons-v6" }, then: "reject" },
{ if: { prefix_set: "ours" }, then: "reject" },
{ communities: "import" },
{ set: { local_pref: 200 }, then: "accept" },
],
export: [
{ communities: "export" },
{ description: "Our own space", if: { prefix_set: "ours" }, then: "accept" },
{ description: "Nothing else leaves", then: "reject" },
],
},
},
firewall: {
address_sets: {
management: ["10.0.0.0/24"],
"bgp-neighbors": bgpNeighbors(),
},
filter: {
input: {
default_action: "drop",
rules: [
{ description: "Replies", if: { connection_state: ["established", "related"] }, then: "accept" },
{ description: "Ping", if: { protocol: "icmp" }, then: "accept" },
{ description: "Management", if: { src_address_set: "management" }, then: "accept" },
{
description: "BGP",
if: { src_address_set: "bgp-neighbors", protocol: "tcp", dst_port: presets.ports.bgp },
then: "accept",
},
],
},
forward: {
rules: presets.bad_tcp_flags,
},
},
},
});The transit and the exchange
25g-1 and 25g-2 are access ports in the VLANs transit and exchange, and the router's address on each is on a VLAN interface. Both VLANs are untrusted: true, because traffic from outside arrives there. The check untrusted_vlans makes sure that only edge-01 gives them an address. Each port runs the ACL edge in the switch chip.
Your prefixes and the BGP sessions
There are three sessions, as in BGP: one with the transit, and one per address family with the exchange's route server. role tells each side what the other is, and max_prefixes is the fuse from Route policies. communities: { groups: [...] } says which group of the scheme each neighbor is in.
A blackhole route for each of your blocks keeps the block in the routing table at all times, and originated_prefixes announces it.
The policies
- Each import first calls
sanity, which ispresets.bgp_sanity. It refuses routes that should never be on the internet, such as prefixes that are too long, and RPKI invalid ones. - Then the import refuses the special-purpose blocks in
presets.bogons, such as the private ranges, and your own space coming back. It tags the route with{ communities: "import" }. It sets a local preference of 100 for the transit and 200 for the exchange, so a route learned at the exchange wins, as in Choosing the best route. - The export accepts your own space and rejects everything else, so you never pass one provider's routes to another.
Protecting the router
The input chain drops by default. It accepts replies, ping, the management network, and BGP from bgp-neighbors only. bgpNeighbors() fills that address set with every neighbor's address. In the forward chain, presets.bad_tcp_flags drops packets with flags that no real connection sends.
The ACL edge drops a packet from outside whose source is your own block or a private range. No such sender can be out there, so the packet is forged. The switch chip drops it before it costs the router's CPU anything.
Your own check
// This network's own checks, beside Circuit's built-in ones. circuit.ts turns them on in `checks`.
import type { Check } from "circuit";
/** Every BGP neighbor has an import policy. Without one, the router accepts no route from it. */
export const importsFiltered: Check = (network) =>
network.devices.flatMap((device) =>
Object.entries(device.routing?.bgp?.neighbors ?? {})
.filter(([, neighbor]) => !neighbor.import_policy)
.map(([name]) => ({
device: device.name,
path: `routing.bgp.neighbors.${name}`,
message: "has no import_policy, so the router accepts no route from it. Write one.",
}))
);Circuit can't know that each of your sessions should have an import policy. So the example adds a check of its own, and circuit.ts turns it on as imports_filtered.
Make the decisions yourself
0 of 4 decided
neighbors: {
"hurricane-electric": {
address: "203.0.113.1",
remote_as: 6939,
role: "provider",
import_policy: "transit-import",
export_policy: "export",
max_prefixes: 1_200_000,
},
// de-cix-rs-v4 and de-cix-rs-v6 alike, with import_policy: "exchange-import"
},
policies: {
sanity: presets.bgp_sanity,
"transit-import": [
{ call: "sanity" },
{ description: "Bogons", if: { prefix_set: "bogons-v4" }, then: "reject" },
{ description: "Bogons", if: { prefix_set: "bogons-v6" }, then: "reject" },
{ if: { prefix_set: "ours" }, then: "reject" },
{ communities: "import" },
{ set: { local_pref: 100 }, then: "accept" },
],
"exchange-import": [
{ call: "sanity" },
{ description: "Bogons", if: { prefix_set: "bogons-v4" }, then: "reject" },
{ description: "Bogons", if: { prefix_set: "bogons-v6" }, then: "reject" },
{ if: { prefix_set: "ours" }, then: "reject" },
{ communities: "import" },
{ set: { local_pref: 200 }, then: "accept" },
],
export: [
{ communities: "export" },
{ description: "Our own space", if: { prefix_set: "ours" }, then: "accept" },
{ description: "Nothing else leaves", then: "reject" },
],
},
firewall: {
address_sets: { management: ["10.0.0.0/24"], "bgp-neighbors": bgpNeighbors() },
filter: {
input: {
default_action: "drop",
rules: [
// Replies, ping, and the management network come first.
{ description: "BGP", if: { src_address_set: "bgp-neighbors", protocol: "tcp", dst_port: presets.ports.bgp }, then: "accept" },
],
},
},
},
$ circuit validate
✔ 1 device(s), no errors.
Where you haven't picked yet, the file shows the example's choice.
Validate, diff, and apply
- Set the router's password:
circuit secrets set EDGE_01_PASSWORD. - Run
circuit validate. On the example, it prints✔ 1 device(s), no errors. - Run
circuit diff edge-01. It logs in to the router, reads what the router runs now, and shows the plan. It changes nothing. - Run
circuit apply edge-01. It shows the plan again and asks. Read every line, and say yes only when the plan is what you want. - Commit the files.
Once the router runs the example, a later change shows as a short plan. Here, the limit for the exchange's IPv4 route server goes from 100,000 to 150,000:
edge-01: 1 command(s) in 1 step(s)
/routing bgp connection
~ {"remote.address":"192.0.2.1/32"} input.limit-process-routes-ipv4=150000~ marks a change to something the router already has. A change inside a policy looks bigger, because Circuit replaces the policies whole, as Making the plan explains. When the exchange's local preference goes from 200 to 150, the plan lists every filter rule on the router twice: once with -, and once with +.
Before your routes travel far, your transit and the exchange's route servers must know that the routes are yours. They filter what you send them by the records of an internet routing registry, and many check RPKI too. So first publish a route object and a ROA for each block. Hijacks and leaks shows how.
Where to go from here
The example is one router with one transit. To grow it, add:
- A second transit, with prepending from Choosing the best route to steer the traffic coming in.
- A LAG to each provider, from Staying up, so that one cable can fail.
- A second edge router that shares routes with this one over iBGP, with OSPF from Routers that talk: OSPF to find the way between them.