Skip to content
Visual explainer 2h 30m to learn 4 steps

Routing tables & longest-prefix match

Read a routing table and predict the next hop for any destination.

By Invicly Tech, , in Computer Networking. About 12 minutes to read.

Topic 15 of 51

In short

A router forwards each packet by looking up its destination IP in a routing table of prefixes and next hops. When several routes match, the most specific one, with the longest prefix, wins. A default route, 0.0.0.0/0, catches everything else.

Explained simply

A router is a device that connects different networks and passes packets between them. When a packet arrives, the router reads only one thing to decide where it goes: the destination IP address. Then it looks in its routing table, a list that says "for these addresses, send the packet that way".

Each line in the table has a prefix, such as 10.1.2.0/24, which is a group of addresses, and a next hop, which is the neighbour router to hand the packet to. Often several lines match the same address. The rule is simple: the most specific line wins. A /24 is more specific than a /16, which is more specific than a /8. This rule is called longest-prefix match.

Your laptop has a routing table too, usually with just two lines: "my own local network is right here" and "everything else goes to the default gateway". The default gateway is your home router, the door out of your network.

No router knows the whole path. Each one only picks the next hop, and the next router decides again. A packet crosses the internet like a parcel passed between post offices, each one only knowing which office is closer to the address.

Press Play on a diagram to walk through it one step at a time.

FlowForwarding a packet to 10.1.2.3
  1. 1
    Receive packet

    Destination 10.1.2.3.

  2. 2
    Match routes

    0.0.0.0/0, 10.0.0.0/8, 10.1.2.0/24 all match.

  3. 3
    Longest prefix

    10.1.2.0/24 wins.

  4. 4
    Decrement TTL

    Drop and send ICMP if it hits 0.

  5. 5
    Forward

    New frame out the chosen interface.

MapWhere routes come from
Routing table
  • Connected
    • Interface has an address and is up
    • Cisco AD 0
  • Static
    • Typed by an admin
    • Cisco AD 1
  • Interior protocols
    • OSPF AD 110
    • IS-IS AD 115
    • RIP AD 120
  • BGP
    • eBGP AD 20
    • iBGP AD 200
  • Host extras
    • DHCP default route
    • VPN client routes

In depth

Reading a real routing table

On Linux, ip route prints one route per line. default via 192.168.1.1 dev wlan0 proto dhcp metric 600 means: anything without a better match goes to 192.168.1.1 out of wlan0, the route came from DHCP, and its metric (preference, lower is better) is 600. A line like 192.168.1.0/24 dev wlan0 proto kernel scope link src 192.168.1.42 has no via, so that network is directly attached: scope link means destinations are reachable on the wire without a gateway, and src is the source address Linux will use.

On Cisco IOS, show ip route marks each route with a code: C connected, L local (the router's own address as a /32), S static, O OSPF, B BGP, and an asterisk for a candidate default. A route like O 10.1.0.0/16 [110/20] via 172.16.0.6 shows the administrative distance (110) and the metric (20) in brackets.

  • via = next hop; no via = directly connected
  • proto kernel, static, dhcp or a routing daemon name
  • Cisco [110/20] = [distance/metric]
  • Gateway of last resort = the default route

Longest prefix first, then distance, then metric

Lookups follow a strict order. First, find every route whose prefix contains the destination and keep only the longest prefix length. Prefix length always wins: a /24 from RIP beats a /16 from OSPF, even though OSPF is more trusted.

Only when two sources offer the same prefix does trust matter. Cisco calls this administrative distance (AD): connected 0, static 1, eBGP 20, OSPF 110, IS-IS 115, RIP 120, iBGP 200. The lowest AD gets installed in the table. If one protocol offers several paths to the same prefix, its own metric decides, such as OSPF cost. Equal metrics can all be installed, which is called ECMP (equal-cost multipath), and traffic is spread across them, usually per flow so packets of one connection stay in order.

Linux has no AD; it simply uses the metric field, and the proto field records where a route came from.

FlowHow one route is chosen
  1. 1
    All matches

    Prefixes that contain the destination.

  2. 2
    Longest prefix

    Most specific always wins.

  3. 3
    Lowest distance

    Only for the same prefix from different sources.

  4. 4
    Lowest metric

    Within one routing protocol.

  5. 5
    ECMP

    Equal metrics: spread flows across paths.

What the default gateway really does

Before sending, a host compares the destination with its own prefix. If the destination is on the same subnet, the host uses ARP to find the destination's MAC address and sends the frame straight to it. If not, it uses ARP to find the gateway's MAC address instead.

This is the key detail: the IP destination in the packet stays the final server, but the Ethernet frame is addressed to the gateway's MAC. The gateway strips the frame, looks up the IP destination in its own table, and builds a brand new frame for the next link. MAC addresses change on every hop; IP addresses do not (unless NAT rewrites them).

That is also why the gateway must be inside the host's own subnet. A host cannot ARP for an address that is not on its local link, so a gateway outside the subnet simply does not work.

SequenceHost 192.168.1.42 sends to 93.184.215.14
Host
Gateway R1
Router R2
Server
  1. 1ARP: who has 192.168.1.1?
    Host to Gateway R1: ARP: who has 192.168.1.1?
  2. 2ARP reply: R1's MAC
    Gateway R1 to Host: ARP reply: R1's MAC
  3. 3Frame to R1's MAC, IP to the server, TTL 64
    Host to Gateway R1: Frame to R1's MAC, IP to the server, TTL 64
  4. 4New frame to R2's MAC, TTL 63
    Gateway R1 to Router R2: New frame to R2's MAC, TTL 63
  5. 5New frame to the server's MAC, TTL 62
    Router R2 to Server: New frame to the server's MAC, TTL 62

Following a packet hop by hop

Every router makes an independent decision based on its own table. Nothing in the packet records the planned path. That has three consequences.

First, the return path is a separate decision made by other tables, so traffic can go out one way and come back another (asymmetric routing). That is normal on the internet but can confuse stateful firewalls.

Second, if two routers point at each other for some destination, the packet bounces between them. The TTL (time to live) field stops it: every router subtracts 1, and at 0 the packet is dropped and an ICMP Time Exceeded message (type 11) is sent back. Linux starts at TTL 64 and Windows at 128.

Third, you can test each decision directly. ip route get 8.8.8.8 asks the Linux kernel which route, interface and source address it would use. traceroute and mtr exploit the TTL to list each hop.

  • ip route get ADDRESS tests one lookup
  • traceroute -n skips slow DNS lookups
  • mtr combines ping and traceroute over time

Under the hood: RIB, FIB and multiple tables

Routers keep two tables. The RIB (routing information base) holds every route learned from every source, including the losers. The best ones are copied into the FIB (forwarding information base), a structure optimised for fast lookup. Hardware routers often use special memory called TCAM that checks all prefixes in parallel; software routers use tries (prefix trees). show ip route shows the RIB view.

Linux also has several routing tables. The local table holds the host's own and broadcast addresses (ip route show table local), and main is the one ip route shows by default. ip rule decides which table each packet consults, which enables policy routing: for example, traffic from one source address uses a different default gateway. VPN clients often add rules and tables this way, which is why ip route alone does not always explain what happens.

Learn it in 4 short steps

About 40m each. Track them with a timer in your plan.

  1. 1
    Read a host and router routing table~40m
  2. 2
    Apply longest-prefix match~40m
  3. 3
    Explain default gateways~40m
  4. 4
    Follow a packet hop by hop~40m

Key points

  • Longest-prefix match always beats a shorter matching prefix.
  • Hosts have routing tables too, usually a local subnet and a default gateway.
  • Routes come from connected interfaces, static config or routing protocols.
  • Return traffic needs a route back too; routing is per direction.

Key terms

Tap a card to flip it. 0/4 known

Examples

bash
ip route show
# default via 192.168.1.1 dev wlan0
# 192.168.1.0/24 dev wlan0 proto kernel scope link
ip route get 8.8.8.8      # which route and source IP will be used

Predicting the next hop on a Cisco router

Four lookups against the table below. 10.1.2.50 matches the /8, the /16 and the /24; the /24 wins, and it is directly connected, so the router ARPs for 10.1.2.50 itself. 10.1.9.9 matches the /8 and the /16; the /16 wins, so it goes via 172.16.0.6, even though the static /8 has a better AD. 10.200.0.1 matches only the /8, so it goes via 172.16.0.2. 8.8.8.8 matches only the default route and goes to the ISP at 203.0.113.1.

text
R1# show ip route
Gateway of last resort is 203.0.113.1 to network 0.0.0.0

S*    0.0.0.0/0 [1/0] via 203.0.113.1
S     10.0.0.0/8 [1/0] via 172.16.0.2
O     10.1.0.0/16 [110/20] via 172.16.0.6, 00:12:01, GigabitEthernet0/1
C     10.1.2.0/24 is directly connected, GigabitEthernet0/2
L     10.1.2.1/32 is directly connected, GigabitEthernet0/2

Why a laptop on a VPN uses two routes

The laptop is on home Wi-Fi and connected to a company VPN. Step 1: ip route shows a default via the home router, the home /24, and a 10.8.0.0/24 route on the tun0 VPN interface. Step 2: ip route get 10.8.0.10 picks tun0, because the /24 is longer than the default /0. Step 3: ip route get 1.1.1.1 matches only the default, so normal internet traffic leaves through Wi-Fi. This is called split tunnelling: only company prefixes enter the VPN.

bash
$ ip route
default via 192.168.1.1 dev wlan0 proto dhcp metric 600
10.8.0.0/24 dev tun0 proto kernel scope link src 10.8.0.2
192.168.1.0/24 dev wlan0 proto kernel scope link src 192.168.1.42 metric 600

$ ip route get 10.8.0.10
10.8.0.10 dev tun0 src 10.8.0.2 uid 1000

$ ip route get 1.1.1.1
1.1.1.1 via 192.168.1.1 dev wlan0 src 192.168.1.42 uid 1000

Quick reference

  • Longest prefix wins first; administrative distance only breaks ties for the same prefix
  • Cisco AD: connected 0, static 1, eBGP 20, OSPF 110, IS-IS 115, RIP 120, iBGP 200
  • ip route get 8.8.8.8 shows the route, interface and source IP Linux would use
  • ip route show table local lists the host's own and broadcast addresses
  • Each router lowers TTL by 1; at 0 it drops the packet and sends ICMP Time Exceeded (type 11)
  • The gateway must be in the host's subnet; the IP destination stays fixed while MACs change per hop
  • Cisco codes: C connected, L local /32, S static, O OSPF, B BGP, * candidate default
  • Routing is decided per direction: always check the return path

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1A router has 10.0.0.0/8 (static, AD 1) and 10.1.0.0/16 (OSPF, AD 110). Where does a packet to 10.1.4.4 go?

Question 2

2Host 192.168.1.42/24 sends a packet to 8.8.8.8. What is the destination MAC of the frame it sends?

Question 3

3In the Cisco route O 10.1.0.0/16 [110/20], what do 110 and 20 mean?

Question 4

4A static route and an OSPF route both exist for exactly 10.5.0.0/16. Which goes into the routing table on Cisco?

Question 5

5A routing loop makes a packet bounce between two routers. What eventually stops it?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    A router has routes 0.0.0.0/0 via A, 172.16.0.0/12 via B, 172.16.8.0/21 via C and 172.16.10.0/24 via D. Give the next hop for 172.16.10.5, 172.16.12.1, 172.20.1.1 and 192.168.0.1.

  2. Exercise 2medium

    Routes 172.16.4.0/23 and 172.16.5.128/25 exist. Show in binary which routes match 172.16.5.130 and 172.16.5.100, and which one wins for each.

  3. Exercise 3hard

    Write a Python longest-prefix-match lookup using the standard ipaddress module for a table with a default route, a /8, a /16 and a /24. Test four destinations.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

What is the difference between a router and a default gateway?
A router is any device that forwards packets between networks. The default gateway is the specific router a host sends traffic to when the destination is not on its own subnet.
Why does my laptop show two default routes?
Usually one per connection, such as Wi-Fi and Ethernet. Linux uses the one with the lowest metric, which is why wired connections normally get a lower metric than Wi-Fi.
What is the difference between the RIB and the FIB?
The RIB holds all routes learned from every source. The FIB holds only the chosen best routes, in a form built for fast forwarding lookups.
Does traceroute show the path my replies take?
No. It shows the forward path only. The return path is chosen by other routers' tables and can be different.
ShareXLinkedInWhatsApp