Routing tables
A routing table is a router's list of routes. Each route is a prefix, a block of addresses, and where to send traffic for it. For every packet, the router finds the routes whose prefix holds the destination address. It uses the one with the longest prefix.
You met the routing table in Routers and the gateway.
What a route says
Here are two routes from the table of a small office router:
| Prefix | Sends to | Where the route comes from |
|---|---|---|
10.0.10.0/24 | out of the office VLAN, to the device | The router has the address 10.0.10.1/24 on that VLAN |
0.0.0.0/0 | the router at 203.0.113.1 | Someone wrote it by hand |
The address a route sends to is its The next router on a packet's way to its destination: the router that this router hands the packet to.. It is usually on one of the router's own networks, so the router can hand the packet straight to it. The next hop doesn't know the whole way either. It has its own table and makes its own choice, so a packet travels hop by hop.
Routes come from three places:
- Connected routes come from the router's own addresses. When a VLAN interface has the address 10.0.10.1/24, the router knows that all of 10.0.10.0/24 is on that VLAN. So it adds a route for it, and you never write one.
- Static routes are written by hand, such as "send 10.20.0.0/16 to 10.0.10.254".
- Learned routes come from other routers, through a A way for routers to tell each other which addresses they can reach, so nobody has to write every route by hand.. OSPF and BGP, later in this level, are two of them.
The default route
The prefix 0.0.0.0/0 has a length of 0. It compares no bits, so it holds every IPv4 address. A route for it is the default route: the router uses it when nothing more specific matches. In a small network, it points at the internet provider.
Your laptop's default gateway is the same idea. Its routing table has a connected route for its own network, and a default route through the router.
The longest match wins
For each packet, a router does this:
- It reads the packet's destination address.
- It finds every route whose prefix holds the address. A /24 prefix holds an address when the first 24 bits of both are the same. The other bits are not compared.
- Of those routes, it picks the one with the longest prefix.
- It sends the packet where that route says. If no route holds the address, it drops the packet.
A longer prefix is a smaller block, so it says more exactly where the address is. 10.0.10.0/24 is 256 addresses on one VLAN, and 0.0.0.0/0 is every address. When both hold 10.0.10.25, the more exact route wins.
After a few lookups, add the route 10.0.0.0/8 to the blackhole, and look up 10.0.11.7 again.
| Prefix | Sends to | Kind | Holds the address? | Remove |
|---|---|---|---|---|
| 203.0.113.0/30 | out of 25g-1, straight to the device | connected | ||
| 10.0.10.0/24 | out of office, straight to the device | connected | ||
| 10.0.20.0/24 | out of guests, straight to the device | connected | ||
| 10.20.0.0/16 | router 10.0.10.254 | static | ||
| 0.0.0.0/0 | router 203.0.113.1 | static |
Pick an address above, or type one.
// The connected routes come from these addresses.
interfaces: {
ethernet: {
"25g-1": { description: "Internet provider", addresses: ["203.0.113.2/30"] },
},
vlan_interface: {
office: { vlan: "office", addresses: ["10.0.10.1/24"] },
guests: { vlan: "guests", addresses: ["10.0.20.1/24"] },
},
},
// The static routes are written by hand.
routing: {
static_routes: [
{ prefix: "0.0.0.0/0", next_hop: "203.0.113.1", description: "Internet provider" },
{ prefix: "10.20.0.0/16", next_hop: "10.0.10.254", description: "Lab, behind the lab router" },
],
},
The widget lists the longest prefix first, but the order of a routing table decides nothing. Only the length does. Firewall rules work the other way: the first rule that matches decides.
The same prefix twice
When two routes have exactly the same prefix, the length can't decide. The router then compares their A number that ranks two routes to exactly the same prefix. Lower wins. Each vendor gives each source of routes, such as static routes or OSPF, its own default., and the lower one wins. A connected route always beats a static one.
A backup internet line is often a second default route with a higher distance. The router uses it only when the first route is gone, for example because its link went down.
Blackhole routes
A blackhole route sends traffic nowhere: the router drops whatever matches it. That stops a loop that is easy to build by accident.
Say your internet provider gives you the block 198.51.100.0/24, and so far you use only 198.51.100.0/26 for your office. The provider's routers send the whole /24 to you. Then a packet arrives for 198.51.100.200, an address you don't use yet. Your connected route for the /26 doesn't hold it, but the default route does. So your router sends the packet back to the provider, which sends it to you again.
Provider's table
| Prefix | Sends to |
|---|---|
| 198.51.100.0/24 | your router, 203.0.113.2 |
| 0.0.0.0/0 | the rest of the internet |
Your table
| Prefix | Sends to |
|---|---|
| 198.51.100.0/26 | office network, connected |
| 0.0.0.0/0 | the provider, 203.0.113.1 |
Send a packet to the address you don't use yet, with the blackhole route off. Then turn the blackhole route on and send it again. Send one to the office server too.
The packet only stops when its Time to live: a counter in every IP packet that each router lowers by one. At zero the packet is dropped, so it cannot loop forever. runs out. Every trip crosses your link to the provider, which you pay for. With the blackhole route, your router drops the packet when it first arrives.
The office server still gets its packets, because 198.51.100.0/26 is longer than 198.51.100.0/24. The blackhole catches only the addresses that no more specific route holds.
A blackhole route has one more use. It keeps your whole block in the routing table, even when nothing in it is up. BGP needs that to announce the block to the internet.
Routes in Circuit
Circuit makes the connected routes from the addresses of ports and interfaces, so you never write them. Static routes are a list in routing.static_routes:
interfaces: {
ethernet: {
"25g-1": { description: "Internet provider", addresses: ["203.0.113.2/30"] },
},
vlan_interface: {
office: { vlan: "office", addresses: ["10.0.10.1/24"] },
guests: { vlan: "guests", addresses: ["10.0.20.1/24"] },
},
},
routing: {
static_routes: [
{ prefix: "0.0.0.0/0", next_hop: "203.0.113.1", description: "Internet provider" },
{ prefix: "0.0.0.0/0", next_hop: "10.0.10.253", distance: 10, description: "Backup line" },
{ prefix: "10.20.0.0/16", next_hop: "10.0.10.254", description: "Lab, behind the lab router" },
{ prefix: "10.0.0.0/8", blackhole: true, description: "Private space we do not use" },
],
},| Field | What it says |
|---|---|
prefix | The block the route is for. |
next_hop | The next hop's address. |
interface | The interface to send out of, for a link without a next hop address. |
blackhole | Drop whatever matches. |
distance | The preference against other routes to the same prefix. Lower wins. |
description | Why the route exists. |
The list is the whole truth. When you apply it, Circuit removes every static route on the device that the file doesn't have.
Not every platform takes every field
For example, Raisecom ROS takes only IPv4 routes with a next_hop, so it can't have a blackhole route. circuit validate reports each field a platform refuses. See Platforms.