What Is Routing and How Do Routers Decide Where to Send Packets?

Updated

Routing is the process a router uses to forward a packet toward its destination by comparing the packet's destination IP address against a routing table and sending it out the best matching path. It applies whenever traffic crosses between networks — your LAN and the internet, two VLANs, or two sites connected over a VPN. If everything stays inside one subnet, no routing is involved; that's switching. The rest of this article explains how the decision is actually made, what fills the routing table, and how to reason about it when packets don't arrive.

The core decision: longest prefix match

A router does not "know the internet." It only knows a list of routes, each pairing a destination network with a next hop (or an interface). For every packet, it runs the same lookup:

  1. Read the destination IP address from the packet header.
  2. Compare it against every entry in the routing table.
  3. Pick the most specific match — the entry with the longest prefix (highest subnet mask length). This is the longest prefix match rule.
  4. Forward the packet to that entry's next hop, or drop it if nothing matches.

Example: a packet to 192.168.20.55 arrives at a router with these entries:

Destination Next hop Prefix length
0.0.0.0/0 (default) ISP gateway 0
192.168.0.0/16 Site router 16
192.168.20.0/24 VLAN 20 interface 24

All three match, but /24 is the longest, so the packet goes to the VLAN 20 interface directly. The default route only wins when nothing more specific matches — which is exactly why it's called the route of last resort.

What goes into a routing table

Routes get there in three ways, and the distinction matters when you troubleshoot:

  • Directly connected — the router automatically adds a route for each subnet on its own interfaces. These are the most trusted.
  • Static routes — you type them in manually. Predictable, no protocol overhead, but they don't adapt if a link fails.
  • Dynamic routes — learned from other routers via a routing protocol. Common ones:
    • OSPF — interior gateway protocol, typically inside one organization. Routers exchange link states and converge on shortest paths.
    • BGP — the protocol that exchanges routes between autonomous systems, i.e. how the global internet's routing table is assembled.
    • RIP — older, hop-count based, mostly of historical interest.

When multiple sources offer a route to the same prefix, the router prefers by administrative distance (trustworthiness) first, then by metric (cost). A static route usually beats a dynamically learned one to the same destination unless you deliberately change the distance.

Routing vs. switching, NAT, and firewalls

These get conflated constantly, so keep them separate:

Function Layer What it decides Typical device
Switching L2 Which port a frame goes out, based on MAC address Switch
Routing L3 Which next hop a packet goes to, based on IP address Router / firewall
NAT L3/L4 Rewrites source or destination addresses/ports Router / firewall
Firewalling L3–L7 Whether to permit or drop the packet at all Firewall

A home "router" is usually all four in one box. NAT is not routing — it's address translation applied after the routing decision. A firewall rule can drop a packet that routing already decided to forward. So "the route exists but traffic still fails" is a normal outcome: check NAT and firewall policy next.

How this looks in a home or small network

On platforms like pfSense or UniFi, the routing table is mostly built for you:

  • Each interface (LAN, WAN, VLANs) contributes a connected route.
  • The WAN interface gets a default route from the ISP via DHCP or PPPoE.
  • You add static routes only for things the router can't infer — for example, reaching a remote subnet behind a second router or across a VPN tunnel.

A typical static route entry says: "to reach 10.10.0.0/24, go via 192.168.1.2." If the next-hop address isn't reachable on a directly connected subnet, the route won't work — the router has nowhere to hand the packet.

When routing doesn't work: a checklist

Work outward from the host, because most "routing problems" are actually host configuration problems:

  1. Is the host's default gateway correct? A wrong gateway means the host can reach its own subnet but nothing beyond it.
  2. Is the destination in the same subnet as the host? If the host thinks it's local, it will ARP instead of routing, and fail silently.
  3. Does the router have a route to the destination? Check the routing table for a matching prefix, not just a default route.
  4. Does the return path exist? Routing is per-direction. A packet can arrive fine and the reply can have no route back — a classic asymmetric-routing symptom.
  5. Is NAT or a firewall rule blocking it? Confirm the route is being used before blaming the route.
  6. DNS is separate. If names fail but IP addresses work, you have a name-resolution problem, not a routing one.

A quick way to see the decision on most systems is traceroute (or tracert on Windows): each hop is a router that matched the destination and forwarded it onward. Where the trace stops is usually where the route or the policy ends.

The takeaway

Routing is a table lookup with one rule that matters most: longest prefix match, with the default route as fallback. Everything else — static vs. dynamic, OSPF vs. BGP, NAT, firewall rules — is about how that table gets populated and what happens to the packet afterward. When traffic fails, verify the gateway, the matching prefix, the return path, and the policy in that order.