Card 03/ 10

ExampleDifficulty: Intermediate1 min

Routing Example: Three Routes That Match One Address

A packet for 10.1.2.3 matches a /8, a /16 and a /24, and only one of them sends it.

Longest prefix match is clearest when several routes in one table contain the same address. A router has four routes, listed in the order an administrator added them, and a packet arrives for 10.1.2.3.

The table: 0.0.0.0/0 via router A, the default to the internet; 10.0.0.0/8 via router B, for all internal networks; 10.1.0.0/16 via router C, for one data centre; and 10.1.2.0/24 via router D, for one rack of servers inside it.

The destination 10.1.2.3 checked against each route

InputDestination 10.1.2.3 and four routes

  1. 0.0.0.0/0 via A

    1. matchescandidate
    2. length 0

    A /0 keeps no bits, so every address matches.

  2. 10.0.0.0/8 via B

    1. matchescandidate
    2. length 8

    The first byte is 10.

  3. 10.1.0.0/16 via C

    1. matchescandidate
    2. length 16

    The first two bytes are 10.1.

  4. 10.1.2.0/24 via D

    1. matcheschosen
    2. length 24

    The first three bytes are 10.1.2; this is the longest prefix of the four.

ResultThe packet goes to router D. Every route matched, and the /24 is the most specific.

Three things are worth noticing.

Every route matched, and that is normal. The ranges are nested, each inside the one before, so one address falls in all of them. Longest prefix match exists for exactly this.

The order the routes were added changed nothing. Router D's route was added last and still wins. A reader who stops at the first match would send the packet to the internet.

Changing the address changes the winner. A packet for 10.1.9.9 matches the /0, the /8 and the /16, not the /24, so it goes to router C. A packet for 10.200.0.1 matches only the /0 and the /8, so router B. A packet for 192.0.2.1 matches only the default, so router A.