Skip to content

IntermediateLesson 1 of 9

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:

PrefixSends toWhere the route comes from
10.0.10.0/24out of the office VLAN, to the deviceThe router has the address 10.0.10.1/24 on that VLAN
0.0.0.0/0the router at 203.0.113.1Someone 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:

  1. 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.
  2. Static routes are written by hand, such as "send 10.20.0.0/16 to 10.0.10.254".
  3. 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.
CheckYou remove the address 10.0.20.1/24 from the router's guests VLAN interface. What happens to the route for 10.0.20.0/24?

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:

  1. It reads the packet's destination address.
  2. 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.
  3. Of those routes, it picks the one with the longest prefix.
  4. 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.

Try itLook up addresses in a routing table

After a few lookups, add the route 10.0.0.0/8 to the blackhole, and look up 10.0.11.7 again.

1. Look up an address
PrefixSends toKindHolds the address?Remove
203.0.113.0/30out of 25g-1, straight to the deviceconnected
10.0.10.0/24out of office, straight to the deviceconnected
10.0.20.0/24out of guests, straight to the deviceconnected
10.20.0.0/16router 10.0.10.254static
0.0.0.0/0router 203.0.113.1static

Pick an address above, or type one.

2. Add a static route
office-router.ts, part of it
// 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.

CheckA router has four routes: 10.0.0.0/8 to router 1, 10.1.0.0/16 to router 2, 10.1.2.0/24 to router 3, and 0.0.0.0/0 to router 4. Where does a packet for 10.1.3.4 go?

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.

Try itSend a packet to an address nobody uses
Someone onlinesends with TTL 64Provider203.0.113.1Your router203.0.113.2Office server198.51.100.20

Provider's table

PrefixSends to
198.51.100.0/24your router, 203.0.113.2
0.0.0.0/0the rest of the internet

Your table

PrefixSends to
198.51.100.0/26office network, connected
0.0.0.0/0the 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.

CheckYour router has a blackhole route for 198.51.100.0/24 and a connected route for 198.51.100.64/26 on the servers VLAN. A packet for 198.51.100.70 arrives. What happens?

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:

ts
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" },
    ],
},
FieldWhat it says
prefixThe block the route is for.
next_hopThe next hop's address.
interfaceThe interface to send out of, for a link without a next hop address.
blackholeDrop whatever matches.
distanceThe preference against other routes to the same prefix. Lower wins.
descriptionWhy 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.

Discussion

Stuck, or found a mistake? Ask here. You sign in with GitHub.

Released under the Business Source License 1.1.