IP Addresses & Routing

How Routers Decide Where to Send a Packet

Follow one packet through a router: table lookup, longest prefix match, tie-breaking, next hop and what gets rewritten.

Key takeaways

  • A routing table lists destination prefixes, each with a next hop or outgoing interface.
  • When several entries match, the longest prefix wins, regardless of how the route was learned.
  • Preference values and metrics only break ties between routes to exactly the same prefix.
  • Routers keep the IP addresses largely intact but replace the link-layer header and reduce the TTL at every hop.

A router reads the destination IP address of a packet, finds the entry in its routing table that matches that address most specifically, and sends the packet out the interface and toward the next hop that entry names. If nothing matches, and there is no default route, it drops the packet.

Each router makes this choice on its own, with only its own table. No single device plans the whole journey. A packet crossing the internet is passed along by a series of independent decisions, and it arrives because those tables mostly agree with one another.

What is in a routing table

Each entry answers one question: for destinations in this range, where should the packet go? A simplified table for an office router:

Destination Next hop Interface Source of route
192.168.10.0/24 directly connected LAN Interface address
192.168.20.0/24 directly connected Guest Interface address
10.40.0.0/16 192.168.10.2 LAN Static
10.40.8.0/24 198.51.100.9 Tunnel Static
0.0.0.0/0 203.0.113.1 WAN From the provider

Routes come from three places. Connected routes appear automatically for networks on the router's own interfaces. Static routes are typed in by a person. Dynamic routes are learned from other routers through a protocol such as OSPF or BGP.

Longest prefix match

Several entries can match one destination. Take a packet for 10.40.8.25 using the table above. It matches 10.40.0.0/16, it matches 10.40.8.0/24 and it matches the default 0.0.0.0/0. The router picks 10.40.8.0/24, because 24 fixed bits describe the destination more precisely than 16 or 0. The packet goes into the tunnel.

A packet for 10.40.9.25 matches only the /16 and the default, so it goes to 192.168.10.2. A packet for 198.51.100.200 matches only the default.

Where does a packet for 10.40.8.25 go?
Is it inside a /24 or longer route the router holds? YES Use 10.40.8.0/24: send into the tunnel. 1 NO Else, is it inside a shorter specific route? YES Use the longest one, such as 10.40.0.0/16. 2 NO Else, is there a default route? YES Hand it to the provider's gateway. 3 NO Drop the packet Usually report that the destination is unreachable. Is it inside a /24 or longer route the router holds? 1 IF YES Use 10.40.8.0/24: send into the tunnel. NO Else, is it inside a shorter specific route? 2 IF YES Use the longest one, such as 10.40.0.0/16. NO Else, is there a default route? 3 IF YES Hand it to the provider's gateway. NO Drop the packet Usually report that the destination is unreachable.

The prefix notation is explained in CIDR notation. The order in which entries are listed does not matter for this choice.

The default route

The default route, 0.0.0.0/0 in IPv4 and ::/0 in IPv6, matches everything with zero bits, so it always loses to anything more specific. It is the "everything else" path. Your laptop has one pointing at your default gateway, and your home router has one pointing at your provider.

Routers in the core of the internet often run without a default. They carry routes for the whole public internet, so a destination with no entry truly is unreachable.

When two sources offer the same prefix

Suppose a router learns 10.50.0.0/16 from a static entry, from OSPF and from RIP. Prefix length cannot decide, since all three are /16. The router first prefers the source it trusts most. Cisco calls this value administrative distance, and other vendors call it route preference. Lower wins. Cisco's defaults include:

Source Default distance
Connected 0
Static 1
External BGP 20
OSPF 110
IS-IS 115
RIP 120
Internal BGP 200

Other vendors use different numbers, so look them up for your platform.

If two routes come from the same protocol, its metric decides. RIP counts hops. OSPF adds up link costs, which are usually derived from bandwidth. If routes are still equal, many routers install both and share traffic between them, usually keeping all packets of one conversation on one path so they do not arrive out of order. This is called equal-cost multipath.

Note the order of these rules. A /24 learned by RIP beats a /16 static route for addresses inside the /24. Preference values never override a more specific prefix.

The order a router applies its rules
1. Longest prefix The most specific matching route is chosen at forwarding time. 2. Route preference Among sources of the same prefix, the most trusted is installed. 3. Protocol metric Within one protocol, the lowest cost wins. 4. Equal-cost sharing Remaining ties may split traffic by flow. 1. Longest prefix The most specific matching route is chosen at forwarding time. 2. Route preference Among sources of the same prefix, the most trusted is installed. 3. Protocol metric Within one protocol, the lowest cost wins. 4. Equal-cost sharing Remaining ties may split traffic by flow.

Resolving the next hop

The winning route gives a next-hop address, but the router needs more to send a frame. On Ethernet it must know the hardware address of that next hop. It looks in its cache and, if necessary, asks on the local segment using ARP for IPv4 or Neighbor Discovery for IPv6.

Sometimes the next hop is not directly attached. The router then looks up the next hop itself in the table until it arrives at a connected interface. This is called a recursive lookup. If it cannot resolve the next hop, the route is unusable and typically removed from the active table.

What changes in the packet

Along the way:

  • The link-layer header is discarded and a new one is built for the outgoing link, with new source and destination hardware addresses.
  • The TTL (IPv4) or hop limit (IPv6) is reduced by one. At zero the packet is discarded and an ICMP message is sent back, as described in TTL and hop limits.
  • The IPv4 header checksum is recalculated. IPv6 has none.
  • The source and destination IP addresses stay the same, unless the router also performs NAT.

If the packet is too large for the next link, an IPv4 router may fragment it when allowed. Otherwise, and always with IPv6, it returns an error so the sender can send smaller packets.

Tables for deciding, tables for forwarding

Larger routers separate two jobs. The control plane runs routing protocols and builds the table of all candidate routes. From the winners it builds a compact forwarding table, which the data plane, often dedicated hardware, uses to handle packets very quickly. Policy features can add exceptions, such as sending traffic from one subnet out a second provider regardless of destination. That is usually called policy-based routing.

Firewall rules are a separate check. A packet can have a perfectly good route and still be dropped by a filter.

A packet's trip, step by step

Put the pieces together. A laptop at 192.168.20.40 sends a request to a web server at 203.0.113.80 through the office router.

The laptop compares the destination with its own subnet, sees it is remote and sends the packet to its gateway. The frame carries the router's hardware address, while the packet inside still names the web server as destination.

Inside the router, one packet at a time
1 Receive the frame Check it is addressed to this router, then unwrap it. 2 Look up destination Find the longest matching prefix. 3 Apply checks Reduce TTL; run filters and any translation. 4 Resolve next hop Find its hardware address on the outgoing link. 5 Send Wrap in a new frame and transmit. Receive the frame Check it is addressed to this router, then unwrap it. 1 Look up destination Find the longest matching prefix. 2 Apply checks Reduce TTL; run filters and any translation. 3 Resolve next hop Find its hardware address on the outgoing link. 4 Send Wrap in a new frame and transmit. 5

The router finds no specific route for 203.0.113.80, so the default applies. Because the laptop has a private address, the router translates the source to its public address and remembers the mapping. It lowers the TTL, builds a frame for the provider's gateway and transmits.

The provider's router repeats the process with its own, much larger table. Eventually a router attached to the server's subnet finds a connected route and delivers the frame directly. The reply makes its own series of decisions on the way back, possibly along a different path. At your router, the translation record turns the destination back into 192.168.20.40, and a connected route delivers it.

Static or dynamic routes

For a network with one way out, static routes are fine. They are predictable, use no bandwidth and can be read at a glance. Their weakness is that they do not notice failures beyond the local link. A static route keeps pointing at a dead path unless something monitors it.

Dynamic routing earns its complexity when there are several paths or many networks. Routers detect failed neighbors, share the news and converge on an alternative without anyone typing. The cost is configuration knowledge and a new class of faults, such as a misconfigured neighbor advertising routes it should not.

A common middle ground in small offices with two providers is a pair of default routes with different preference values, plus a health check that withdraws the primary when it fails. How well this works depends on the router's features.

See your own routing table

Every computer has one.

Windows:

route print

macOS:

netstat -rn

Linux:

ip route show
ip route get 203.0.113.5

The last command is especially useful: it shows which route, interface and source address Linux would use for that destination. VPN clients make good study material, since you can watch new, more specific routes appear when you connect. To follow the hops beyond your own network, use traceroute.

When routing goes wrong

  • No route. A router replies that the network is unreachable. Check for a missing static route or default.
  • No return route. Requests arrive but replies never come back, because the far side does not know how to reach the source. This is common after adding a new internal subnet behind a second router.
  • Loop. Two routers point at each other for some prefix. Packets bounce until the TTL runs out.
  • Too specific. A forgotten host route or small prefix quietly diverts part of a range.
  • Asymmetry. Forward and return paths differ. That is normal on the internet, but a stateful firewall that sees only one direction may drop the traffic.

When troubleshooting, check both directions. At each hop ask what route this device has for the destination, and then what route it has for the source.

A good habit is to save a copy of the routing table while everything works. Comparing it with the table during a fault often shows the missing or unexpected entry in seconds.

Common questions

Does a router choose the fastest path?

Not directly. It follows prefix length, preference and metrics. Interior protocols can approximate speed through link costs, while internet routing between organizations is driven largely by policy.

Is a switch doing the same thing?

A basic switch forwards frames within one network using hardware addresses. Routing moves packets between networks using IP addresses. Many switches can do both.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in IP Addresses & Routing →

Latest news on this topic