Saltar al contenido ForoHackers

Longest prefix match: the one rule your router follows

Administración de redesEnrutamientoTCP/IP
#1138 · packet_loss_poet · · Denunciar

Whenever somebody asks "which route will this packet take?", the answer starts with one rule: the most specific matching route wins, regardless of the order the routes were added.

Take this table on a Linux box:

$ ip route
default via 192.0.2.1 dev eth0
198.51.100.0/24 dev eth1
198.51.100.128/25 via 198.51.100.2 dev eth1

Now ask the kernel what it will do:

$ ip route get 198.51.100.200
198.51.100.200 via 198.51.100.2 dev eth1
$ ip route get 198.51.100.50
198.51.100.50 dev eth1
$ ip route get 203.0.113.9
203.0.113.9 via 192.0.2.1 dev eth0
  • 198.51.100.200 matches the /24, the /25 and the default. The /25 is the longest prefix (25 bits), so it wins: the next hop is 198.51.100.2.
  • 198.51.100.50 matches the /24 and the default, but not the /25 (it covers .128 to .255). The /24 wins: delivered directly.
  • 203.0.113.9 matches only the default route, which is /0, the shortest possible prefix. That is why it is the route of last resort.

Two consequences worth remembering:

  1. A more specific route overrides a broader one without deleting it. That is how you carve an exception out of a big block, and also how a mistaken /32 can send one host's traffic to the wrong place for weeks while everything else works.
  2. Routers do this lookup for every packet, so the hardware uses special memory (TCAM) or tries, built for exactly this rule.

Specific beats general. Every packet agrees on this. The default waits last.

#1877 · ElProfeDeRedes · · Denunciar

Perfecto para mis alumnos, y el haiku, de regalo. Lo cuento con el cartero: si hay una dirección "calle Mayor 12" y otra "calle Mayor", gana la más concreta. Voy a usar tus tres comandos ip route get en el laboratorio de la semana que viene.

#1878 · carrier_lost_1200 · · Denunciar

This rule dates back to classless addressing, when the old class-based scheme ran out of room and routing tables began to explode. Prefix lengths became part of every route, and the longest match became the tie-breaker everybody still uses. A very good idea that came from running out of address space.

#1879 · TortugaConRoot · · Denunciar

Me lo apunto: en el homelab tenía una ruta estática "temporal" a un /32 que se me olvidó durante un año, y un servicio concreto iba por otro camino. Con ip route get lo habría visto en un minuto.

Inicia sesión o únete para responder.