Routing: how a router chooses a path
What actually happens to a packet inside a router, how to read a routing table, why the longest prefix always wins, what administrative distance settles, and how to write static, default and floating routes that behave.
On this page
A router's job description fits on one line: for each packet, find the most specific matching route and send the packet to whatever that route names. Everything else — routing protocols, administrative distance, floating statics, load sharing — exists to fill the table that this lookup reads. Get comfortable with the table and the lookup, and the protocols in the rest of this series become details rather than mysteries.
The map
Read this first when short on time. Every branch is a section below.
What happens to a packet inside a router
A router is not doing anything mysterious. Follow one packet through:
- A frame arrives. The router checks the frame's checksum, confirms the destination MAC is its own, and discards the frame header. What remains is an IP packet.
- It reads the destination IP address and looks it up in the routing table.
- No match, and no default route? The packet is dropped and an ICMP destination unreachable is sent back.
- A match gives an outgoing interface and, usually, a next-hop address.
- The TTL is decremented. If it reaches zero the packet is dropped and an ICMP time exceeded is returned — which is exactly what traceroute exploits.
- The router needs the next hop's MAC address. It checks its ARP cache; if absent, it sends an ARP request and waits.
- A new frame is built: source MAC is this router's outgoing interface, destination MAC is the next hop. New frame checksum.
- The frame is transmitted.
Two consequences follow. First, a router must have a route for the return traffic too, and a great many "the network is broken" reports are actually a missing return route. Second, routing is per packet and stateless — nothing in the router remembers that this packet belongs to a conversation, which is what makes routers fast and firewalls slow.
Reading the routing table
Router# show ip route
Codes: L - local, C - connected, S - static, R - RIP,
O - OSPF, IA - OSPF inter area, D - EIGRP, B - BGP
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
10.0.0.0/8 is variably subnetted, 4 subnets, 2 masks
C 10.1.1.0/24 is directly connected, GigabitEthernet0/0
L 10.1.1.1/32 is directly connected, GigabitEthernet0/0
O 10.2.2.0/24 [110/20] via 10.255.0.2, 00:14:22, GigabitEthernet0/1
S 10.3.3.0/24 [1/0] via 10.255.0.6
203.0.113.0/24 is directly connected, GigabitEthernet0/2Every line has the same shape once you know where to look.
| Piece | In O 10.2.2.0/24 [110/20] via 10.255.0.2 |
|---|---|
| Source code | O — learned from OSPF |
| Prefix | 10.2.2.0/24 — the destination network |
| Administrative distance | 110 — how much this source is trusted |
| Metric | 20 — how good this path is, within OSPF |
| Next hop | 10.255.0.2 — where to send it |
| Age and interface | How long it has been known, and which port to use |
Routes come from exactly three places:
- Connected — the router has an interface in that subnet, and the interface is up. Free, automatic, and the foundation everything else builds on. The
Centry is the subnet; theLentry is the router's own address as a/32, so packets addressed to the router itself are recognised as local rather than forwarded. - Static — typed by a human.
- Dynamic — learned from a routing protocol: OSPF, EIGRP, BGP, RIP.
A connected route exists only while the interface is up and its line protocol is up. Shut an interface, or let the far end go down, and the subnet disappears from the table — taking with it any static route that used that interface, and any protocol adjacency across it. A surprising number of routing incidents begin as an interface incident.
Longest prefix match
When several routes match a destination, the one with the longest prefix wins. Not the first in the table, not the one with the best metric, not the one from the most trusted protocol — the most specific one.
Suppose the table holds all of these and a packet arrives for 10.1.1.50:
| Route | Matches? | Prefix length | Chosen? |
|---|---|---|---|
0.0.0.0/0 | Yes, everything matches | 0 | No |
10.0.0.0/8 | Yes | 8 | No |
10.1.0.0/16 | Yes | 16 | No |
10.1.1.0/24 | Yes | 24 | No |
10.1.1.48/30 | Yes | 30 | Yes |
This single rule is what makes the internet tractable. A default route can coexist with millions of specific ones, and adding a more specific route steers a subset of traffic without touching anything else. It is also how traffic engineering works between providers — advertise a longer prefix and traffic follows it, regardless of what the shorter one says.
Router# show ip route 10.1.1.50
Routing entry for 10.1.1.48/30
Known via "static", distance 1, metric 0
Routing Descriptor Blocks:
* 10.255.0.10
Route metric is 0, traffic share count is 1show ip route with an address is the fastest way to settle an argument: it tells you which entry the router will actually use, with the lookup already done.
Administrative distance: which source do you believe?
Longest prefix match settles competition between routes of different lengths. Administrative distance settles competition between routes of the same length from different sources.
If OSPF offers 10.2.2.0/24 and a static route offers 10.2.2.0/24, the router cannot compare their metrics — an OSPF cost of 20 and a static metric of 0 measure completely different things. So each source carries a trust rating, and lower is more trusted.
| Source | Distance |
|---|---|
| Connected interface | 0 |
| Static route | 1 |
| External BGP | 20 |
| EIGRP (internal) | 90 |
| OSPF | 110 |
| RIP | 120 |
| EIGRP (external) | 170 |
| Internal BGP | 200 |
| Unusable | 255 |
The ordering encodes a judgement: a directly attached network is certainly true, a human's typed route is probably deliberate, and among protocols the ones with more information about the topology are trusted more. External BGP sits high in the list because a route learned from another organisation is usually about a destination you have no other way to reach.
A static route with distance 1 silently beats every routing protocol. Leave one in place after a migration and traffic keeps taking a path the protocol thinks it abandoned — with no error, no log, and a routing table that looks reasonable until you compare it with the protocol's own database. Old static routes are among the most persistent sources of confusion in a network.
The key distinction to keep straight: administrative distance chooses which source to believe; metric chooses the best path within one source. They are never compared with each other.
Metrics and multiple paths
Within one protocol, the metric picks the best path — and each protocol measures something different.
| Protocol | Metric | Consequence |
|---|---|---|
| RIP | Hop count | A 64 kbps link two hops away beats a 10 Gbps link three hops away |
| OSPF | Cost, derived from bandwidth | Prefers faster links; needs tuning above the reference bandwidth |
| EIGRP | Bandwidth and delay combined | More nuanced, and Cisco-specific |
| BGP | A sequence of attributes, not a number | Chosen by policy rather than by speed |
When two paths tie on both distance and metric, the router installs both and shares traffic — equal-cost multipath. The sharing is per flow, hashed on addresses and ports, for exactly the reason EtherChannel balances per conversation: splitting one TCP flow across two paths reorders it and triggers spurious retransmissions.
Unequal-cost load sharing — using a slower path in proportion to its capacity — is possible only in EIGRP, using the variance command. No other common protocol supports it.
Static routes, written properly
A static route says "to reach that prefix, send packets here". There are three forms, and the differences matter.
! next-hop address — the normal form, correct everywhere
Router(config)# ip route 10.3.3.0 255.255.255.0 10.255.0.6
! exit interface — only sensible on point-to-point links
Router(config)# ip route 10.3.3.0 255.255.255.0 Serial0/0/0
! both — the most explicit and the safest on multi-access links
Router(config)# ip route 10.3.3.0 255.255.255.0 GigabitEthernet0/1 10.255.0.6
! default route
Router(config)# ip route 0.0.0.0 0.0.0.0 203.0.113.1
! floating static: distance 200 keeps it out of the table
! until the OSPF route with distance 110 disappears
Router(config)# ip route 10.2.2.0 255.255.255.0 10.255.0.14 200Why the exit-interface-only form is a trap on Ethernet. On a point-to-point serial link, "send it out this interface" is unambiguous — there is exactly one device at the other end. On a multi-access link like Ethernet there could be fifty. The router responds by treating every destination in that prefix as if it were directly attached, and ARPing for each one individually. With proxy ARP enabled on the neighbour it works, slowly, with an enormous ARP cache. Without it, nothing works at all. Specify the next hop.
Floating statics are the neat trick in that block. Configure a backup path with an administrative distance higher than the protocol supplying the primary route, and it stays out of the routing table entirely — invisible, inert — until the primary route disappears. Then it appears instantly. It is the standard way to arrange a backup WAN link without running a routing protocol over it.
A default route is just a static route to 0.0.0.0/0: it matches everything, and because it has the shortest possible prefix, it loses to every other match. That is precisely why it is safe as a catch-all, and why it is described as the gateway of last resort.
- Branch offices typically run one default route towards head office or the internet and nothing else, because a site with one exit has no decision to make.
- Internet edge routers do the opposite: they hold the full BGP table of roughly a million prefixes, precisely so they can make a decision for each one.
- Cloud VPCs use the same model with a different interface — an AWS route table is a list of prefixes and targets resolved by longest prefix match, exactly as here.
When traffic does not arrive
Routing faults come in a small number of shapes, and a fixed sequence of checks finds nearly all of them.
- Is there a route at all?
show ip route 10.2.2.50gives the exact entry that will be used, or tells you the subnet is not in the table. - Is the next hop reachable? A route pointing at an address the router cannot reach is useless, and IOS will generally not install it — but a recursive lookup through a default route can produce one that looks valid and is not.
- Is there a route back? Ping from the router using the source address the real traffic will use:
ping 10.2.2.50 source 10.1.1.1. A router's default source is its outgoing interface, so a plain ping can succeed while real traffic fails. - Where does it stop?
traceroutenames the last hop that answered. The problem is usually at the next one. - Is something more specific stealing it? A leftover static or an unexpected summary can pull traffic somewhere else entirely, and the table will look perfectly healthy.
The single most common cause is the third one. Traffic reaches its destination and cannot get back, so every symptom points at the outbound path while the fault is entirely on the return. Asking "does the far end have a route to my source?" before anything else saves a great deal of time.
- Longest prefix match
- The rule that the most specific matching route is used, regardless of source or metric.
- Administrative distance
- A trust rating per route source, used to choose between identical prefixes learned different ways. Lower wins.
- Metric
- A protocol's own measure of path quality, only comparable within that protocol.
- Floating static
- A static route with a deliberately high administrative distance, so it is used only when the primary route disappears.
- Gateway of last resort
- The default route,
0.0.0.0/0, used when nothing more specific matches.
Recap
- A router strips the frame, looks up the destination IP, decrements TTL, and builds a brand new frame for the next link.
- IP addresses stay the same end to end; MAC addresses change at every hop.
- Routes come from connected interfaces, static configuration, or a routing protocol — nowhere else.
- A
Centry is the connected subnet; theLentry is the router's own address as a/32. - Longest prefix match always wins: the most specific route, not the first or the cheapest.
- Administrative distance decides between identical prefixes from different sources; lower is more trusted.
- Connected is 0, static is 1, eBGP 20, EIGRP 90, OSPF 110, RIP 120.
- Distance chooses the source; metric chooses the path within one source. They are never compared.
- Equal-cost paths are load-shared per flow, not per packet; only EIGRP does unequal-cost sharing.
- Static routes should name a next-hop address; exit-interface-only routes misbehave badly on Ethernet.
- A floating static is a backup route with a raised distance, invisible until the primary route fails.
- Most routing faults are a missing return route, so test with the real source address before looking anywhere else.
Questions
Say the answer out loud before opening it.
What does a router change in a packet as it forwards it?
It decrements the TTL and rebuilds the layer 2 frame entirely; the source and destination IP addresses do not change.
- The old frame header is discarded and a new one built with this router's outgoing MAC as source and the next hop's as destination.
- The frame checksum is recalculated because the frame is new.
- The IP header checksum is also recalculated, since the TTL changed.
NAT is the exception that proves the rule: it is precisely a device that does change the IP addresses, and everything that assumes they are stable has to be worked around.
Explain longest prefix match with an example.
When several routes match a destination, the one with the most network bits is chosen, regardless of source or metric.
- For 10.1.1.50, a /30 covering it beats a /24, which beats a /16, which beats the default route.
- This lets a default route coexist safely with any number of specific routes.
- It also means adding a more specific route redirects a subset of traffic without touching anything else.
Providers exploit it deliberately: advertising a longer prefix attracts traffic away from a shorter one, which is the basis of both traffic engineering and prefix-hijacking incidents.
What is administrative distance and how does it differ from a metric?
Administrative distance is a trust rating per route source, used to choose between the same prefix learned from different sources; a metric compares paths within a single source.
- Lower distance wins: connected 0, static 1, eBGP 20, EIGRP 90, OSPF 110, RIP 120.
- Metrics from different protocols measure different things and cannot be compared.
- Distance is evaluated only after longest prefix match has already narrowed the candidates.
It exists because there is no meaningful way to compare an OSPF cost of 20 with an EIGRP metric in the tens of thousands — a human judgement about trust has to stand in.
What is the difference between the C and L entries for the same interface?
C is the connected subnet as a whole; L is the router's own address on that subnet, installed as a /32.
- The C entry tells the router how to reach any host on that network.
- The L entry lets the router recognise packets addressed to itself rather than forwarding them.
- Both disappear if the interface goes down.
The local /32 was added in later IOS versions; before it, the router matched its own address through the connected route, which made some corner cases in policy routing ambiguous.
Why is a static route pointing only at an exit interface a problem on Ethernet?
Because the router treats every destination in that prefix as directly attached and ARPs for each one individually, instead of ARPing once for a next hop.
- On a point-to-point serial link this is harmless, since there is only one possible receiver.
- On Ethernet it depends on the neighbour running proxy ARP, which may be disabled.
- Even when it works, the ARP cache grows with the number of destinations rather than the number of neighbours.
Specifying both the interface and the next hop is the safest form, because it avoids the recursive lookup as well as the ARP behaviour.
What is a floating static route and when would you use one?
A static route configured with an administrative distance higher than the primary source, so it stays out of the routing table until the primary route disappears.
- Backing up an OSPF route with distance 110 means giving the static a distance above that, such as 200.
- It is invisible in
show ip routewhile the primary is present. - It is the standard way to arrange a backup WAN or broadband link without running a protocol over it.
Its weakness is that it only reacts to the route disappearing, so a link that stays up while failing to pass traffic will not trigger it — which is what IP SLA tracking is added for.
What is a default route and why is it safe as a catch-all?
A route to 0.0.0.0/0, which matches every destination; it is safe because its zero-length prefix loses to every other matching route.
- It is used only when nothing more specific matches, hence "gateway of last resort".
- A branch office with one exit needs nothing else in its table.
- It can be static, or advertised by a routing protocol from the router that owns the exit.
Its danger is diagnostic rather than functional: with a default route present, a missing specific route does not fail loudly — traffic simply goes the wrong way and reaches something unhelpful.
Traffic goes one way but not back. What is the likely cause and how do you test it?
A missing or wrong route for the return path; test by pinging from the router with an explicit source address.
ping 10.2.2.50 source 10.1.1.1uses the address real traffic will carry, rather than the outgoing interface.- If the sourced ping fails while a plain ping succeeds, the return path for that subnet is missing.
- Checking
show ip routeon the far router for the source subnet confirms it directly.
This asymmetry is the single most common routing fault, and it is easy to miss because every symptom appears on the outbound side.
How does a router share traffic across two equal-cost paths?
It installs both routes and hashes each flow onto one of them, so all packets of a conversation take the same path.
- The hash typically covers source and destination addresses, and sometimes ports.
- Per-packet sharing is possible but reorders segments and triggers TCP fast retransmit.
- Only EIGRP supports unequal-cost sharing, using the variance command.
Because balancing is per flow, two paths do not double the speed of one transfer — the same limitation that applies to EtherChannel members.
Why does RIP's metric cause bad decisions?
Because it counts hops and ignores bandwidth, so a slow two-hop path beats a fast three-hop one.
- A 64 kbps backup link two hops away would be preferred over a 10 Gbps path three hops away.
- Its maximum of 15 hops also limits network size.
- OSPF derives cost from bandwidth and EIGRP combines bandwidth with delay, both of which reflect reality better.
RIP survives mainly in exams and in very small networks, where the topology is simple enough that hop count and best path happen to coincide.
A migration finished but traffic still takes the old path. What would you look for?
A leftover static route, whose administrative distance of 1 beats anything a routing protocol offers.
- The routing table looks healthy, because the static route is valid.
- The protocol's own database shows the new path, which does not match the installed route.
show ip route 10.2.2.50names the source of the entry actually in use.
Comparing the routing table with the protocol database — show ip ospf database for example — is the reliable way to spot a route the protocol knows about but is not being allowed to install.
Why does a router send ICMP time exceeded, and what uses it?
Because a packet's TTL reached zero at that router, so it was discarded; traceroute deliberately provokes this to map a path.
- Traceroute sends packets with TTL 1, then 2, then 3, collecting the address of each router that replies.
- The TTL exists to prevent packets circulating forever in a routing loop.
- The final destination replies differently — a port unreachable or an echo reply — which is how traceroute knows to stop.
Routers that are configured not to send ICMP appear as asterisks, which is why a traceroute with gaps in the middle is usually a filtering policy rather than packet loss.
Where do routes in the table come from?
Connected interfaces, static configuration, or a dynamic routing protocol. There is no fourth source.
- Connected routes appear automatically when an interface has an address and is up.
- Static routes are typed and persist regardless of what the network is doing.
- Dynamic routes are learned from neighbours and withdrawn when the topology changes.
Redistribution moves routes between protocols, but the route still enters the table through one of these three doors, with the administrative distance of whichever protocol installed it.
What happens to a packet with no matching route?
It is dropped, and the router sends an ICMP destination unreachable back to the source.
- If a default route exists, there is always a match, so this only happens where none is configured.
- The unreachable message is what makes a failure visible rather than a silent black hole.
- Routers rate-limit these messages, so a flood of unroutable traffic does not generate a flood of replies.
A null route — pointing a prefix at Null0 — uses the same mechanism deliberately, discarding traffic for a prefix without generating a reply if configured to stay quiet.
Why do routers need to be fast in a way firewalls do not?
Because routing is stateless — each packet is looked up independently — so the work per packet is small enough to do in hardware.
- A route lookup is a longest-prefix match against a table that changes rarely.
- A firewall must track connection state, which means memory writes per flow and inspection above layer 4.
- This is why a router forwards at line rate and a firewall of the same price does not.
It is also why routers converge quickly after a change: there is no per-flow state to rebuild, only a table to update.