IP Routing Explained: How Routers Forward Packets
Learn how IPv4 routing works, including default gateways, routing tables, longest-prefix match, next hops, Cisco IOS verification, and common routing failures.
IP routing is the Layer 3 process of selecting a path and forwarding IP packets between different networks. Hosts use local delivery when the destination is in the same subnet. For a remote destination, the host sends the packet to a router, usually through its configured default gateway.
This lesson assumes familiarity with IPv4 addresses, subnet masks, CIDR notation, Ethernet frames, MAC addresses, and ARP. For a broader view of protocol layers, see the OSI Reference Model.
Why IP Routing Is Necessary
An IPv4 network is identified by a prefix such as 192.168.10.0/24. A host uses its subnet mask to determine whether a destination is local or remote.
- Local-network delivery: The source and destination are in the same subnet. The source uses ARP to learn the destination's MAC address and sends an Ethernet frame directly to that host.
- Remote-network delivery: The destination is in a different subnet. The source sends the frame to a Layer 3 device, normally its default gateway.
Hosts in different IP networks cannot communicate directly using ordinary local Ethernet delivery. A router connects separate networks and separate broadcast domains. It examines the destination IP address, consults its routing table, and chooses where to forward the packet next.
Routers do not forward Layer 2 broadcasts by default. A broadcast received on one router interface normally stops at that interface, which prevents a single broadcast domain from extending across every routed network.
Local Delivery and Remote Delivery
Consider PC-A at 192.168.10.10/24 and PC-B at 192.168.20.10/24. PC-A calculates that 192.168.20.10 is outside its local 192.168.10.0/24 subnet. PC-A therefore does not ARP for PC-B. Instead, it ARPs for the MAC address of its default gateway, 192.168.10.1.
If the destination were 192.168.10.20, PC-A would ARP for PC-B and send the frame directly. The default gateway is used only when the destination is outside the host's local subnet.
How a Router Forwards an IPv4 Packet
- The source host compares the destination IP address with its own address and subnet mask.
- If the destination is remote, the host selects its default gateway and places the IP packet inside an Ethernet frame addressed to the gateway's MAC address.
- The router receives the frame on an active interface and verifies that the frame is valid for that interface.
- The router removes, or decapsulates, the incoming Layer 2 header and trailer. The IP packet remains.
- The router looks up the packet's destination IP address in its IPv4 routing table.
- The router selects an outbound interface and, when required, a next-hop IP address.
- On Ethernet, the router uses ARP to resolve the next-hop IP address to a MAC address if it does not already know the mapping.
- The router creates a new Layer 2 frame for the next link and sends the unchanged-or-updated IP packet inside it.
During ordinary routing, the source and destination IP addresses normally remain the same from the originating host to the destination host. The Layer 2 source and destination MAC addresses change on every routed Ethernet segment.
At each IPv4 router hop, the router decrements TTL by one. TTL, or Time To Live, prevents a packet from looping forever. Because the IPv4 header changes when TTL changes, the router recalculates the IPv4 header checksum. If TTL reaches zero, the router discards the packet and commonly sends an ICMP time-exceeded message.
Routing Table Fundamentals
A routing table is a database of known destination prefixes and the information needed to reach them. A route identifies a destination network and a forwarding path. Routes can be created from active interfaces, administrator configuration, or routing protocols.
| Component | Purpose | Example |
|---|---|---|
| Route source/code | Shows how the route was learned | C connected, L local, S static, O OSPF |
| Destination prefix | Network matched by the route | 192.168.20.0 |
| Prefix length or mask | Defines which bits are network bits | /24 or 255.255.255.0 |
| Administrative distance | Local trust comparison between route sources | [110/2]: 110 is AD |
| Metric | Path cost used by a routing source | [110/2]: 2 is the metric |
| Next-hop address | IP address of the next Layer 3 device | 192.0.2.2 |
| Exit interface | Interface used to send the packet | GigabitEthernet0/1 |
| Age or update information | Indicates when a learned route was last updated, where applicable | Protocol-specific timer information |
Common Route Types
| Route Type | How It Is Learned | Typical Use | Key Behavior |
|---|---|---|---|
| Connected | Created when an addressed interface is operational | Reach the subnet attached to an interface | Installed automatically while the interface is up |
| Local | Created for an IP address configured on a router interface | Identify traffic destined to the router itself | Usually a host route with a /32 prefix |
| Static | Configured manually by an administrator | Predictable paths and small networks | Does not adapt unless changed or withdrawn |
| Default static | Configured manually as 0.0.0.0/0 | Send unknown destinations upstream | Acts as the gateway of last resort |
| Floating static | Static route configured with a higher administrative distance | Backup path | Remains inactive while a preferred route exists |
| Dynamic | Learned through a routing protocol | Medium and large networks | Can adapt to topology changes |
A connected route appears when an interface has an IPv4 address and is operational. A local route represents the router's own interface address, not the entire attached subnet. A static route is manually configured. Dynamic routing automatically exchanges and learns routes through a routing protocol.
Interior routing protocols exchange routes within an organization's routing domain. Examples include OSPF, RIP, and EIGRP. Routes learned from outside the organization or routing domain are generally described as externally learned routes; the exact behavior depends on the protocol and design.
Route Selection Logic
The router first asks which routes match the destination IP address. It does not simply choose the first entry displayed in the table. The primary rule is longest-prefix match: select the matching route with the greatest prefix length.
| Decision Stage | Question | Result |
|---|---|---|
| Destination matching | Which route prefixes contain the destination IP? | Discard nonmatching routes |
| Longest-prefix selection | Which matching prefix has the most network bits? | Prefer the most specific route |
| Administrative-distance comparison | Do equivalent prefixes come from different sources? | Prefer the lower administrative distance |
| Metric comparison | Does the same routing source offer multiple paths? | Prefer the lower protocol metric |
| Equal-cost handling | Are multiple best paths equal according to the source? | ECMP may use multiple paths |
| Default-route fallback | Is there no more specific match? | Use 0.0.0.0/0, if installed |
For example, given these routes:
10.0.0.0/8 via 192.0.2.2
10.20.0.0/16 via 192.0.2.3
10.20.30.0/24 via 192.0.2.4
0.0.0.0/0 via 192.0.2.1
10.20.30.50uses the/24route.10.20.40.50uses the/16route.10.99.1.1uses the/8route.203.0.113.10uses the default route.
Administrative distance is a local trust value. When equivalent prefixes are learned from different sources, the lower AD is preferred. For example, a connected route normally defeats a static or dynamic route to the same prefix. A metric is a path-cost value used to compare candidate routes from the same routing source or protocol. Do not compare metrics before determining the matching prefix and route-source preference.
ECMP, or Equal-Cost Multipath, allows a router to install and use multiple paths when they are equally preferred according to the routing source. Traffic distribution behavior depends on platform and forwarding configuration.
Default Routes and Floating Static Routes
A default route is the least-specific IPv4 route, written as 0.0.0.0/0. It matches every IPv4 destination, but any more specific route wins first. When installed, it is called the gateway of last resort.
ip route 0.0.0.0 0.0.0.0 192.0.2.2
A branch router with one upstream path commonly uses a default route to forward unknown destinations. Internal routes with longer prefixes still override it.
A floating static route is a backup static route with a deliberately higher administrative distance:
ip route 192.168.20.0 255.255.255.0 192.0.2.6 200
The backup is not selected while a preferred route to the same destination is active. It can become active when the preferred route disappears.
Next Hops, Exit Interfaces, and Recursive Lookup
A route can specify an exit interface, a next-hop IP address, or both. The exit interface identifies where to send the packet. The next hop identifies the next Layer 3 device on that path.
When a route points to a next-hop address, the router may perform a recursive lookup: it looks up the next-hop IP address to determine which interface and Layer 2 resolution method should be used. The next hop must itself be reachable through a valid route.
On an Ethernet link, ARP maps the next-hop IPv4 address to a MAC address. The router then builds a frame addressed to that MAC. On a point-to-point link, such as a serial or other point-to-point connection, an exit interface may be sufficient because there is only one possible neighbor on the link.
Routing Versus Switching
| Function | Layer 2 Switching | Layer 3 Routing |
|---|---|---|
| Data unit | Ethernet frame | IP packet inside a new frame per link |
| Primary lookup | MAC address table | IP routing table |
| Forwarding decision | Destination MAC address | Destination IP address |
| Broadcast behavior | Broadcasts are normally flooded within the VLAN | Broadcasts are not forwarded between interfaces by default |
| Domain boundary | Usually remains within a broadcast domain | Separates broadcast domains |
A switch learns source MAC addresses and uses its MAC address table to select a switch port. A router uses a routing table to select a Layer 3 path. A multilayer switch can perform both functions, but the lookup concepts remain distinct.
End-to-End Example: Two LANs Through Routers
Use this topology:
PC-A 192.168.10.10/24 -- R1 G0/0 192.168.10.1/24
R1 G0/1 192.0.2.1/30 -- R2 G0/0 192.0.2.2/30
R2 G0/1 192.168.20.1/24 -- PC-B 192.168.20.10/24
PC-A's default gateway is 192.168.10.1. PC-B's default gateway is 192.168.20.1. R1 needs a route to 192.168.20.0/24, and R2 needs a route to 192.168.10.0/24.
- PC-A determines that PC-B is remote because
192.168.20.10is not in192.168.10.0/24. - PC-A uses ARP to learn R1's MAC address at
192.168.10.1. - PC-A sends a frame with source MAC PC-A and destination MAC R1 G0/0. The packet has source IP
192.168.10.10and destination IP192.168.20.10. - R1 removes the Ethernet header, decrements TTL, recalculates the IPv4 checksum, and matches
192.168.20.0/24. - R1 forwards toward R2 over
192.0.2.0/30. It ARPs for192.0.2.2if necessary, then creates a new Ethernet frame with R1 G0/1 as the source MAC and R2 G0/0 as the destination MAC. - R2 repeats the process, matches its connected
192.168.20.0/24route, and ARPs for PC-B's MAC address. - R2 sends a new frame to PC-B. The final frame's source MAC is R2 G0/1 and its destination MAC is PC-B. The endpoint IP addresses are still PC-A and PC-B, while TTL is lower than when the packet started.
| Field | At Source Host | At Router Hop | At Destination Network |
|---|---|---|---|
| Source IP address | 192.168.10.10 | Normally unchanged | 192.168.10.10 |
| Destination IP address | 192.168.20.10 | Normally unchanged | 192.168.20.10 |
| Source MAC address | PC-A | Outgoing router interface | R2 G0/1 on final segment |
| Destination MAC address | R1 G0/0 | Next-hop device on each link | PC-B |
| TTL | Initial value | Decremented at each router | Lower than the initial value |
| Frame format | PC-A to R1 Ethernet frame | New frame for every link | R2 to PC-B Ethernet frame |
The return path is equally important. PC-B sends replies to its default gateway, R2. R2 needs a route to 192.168.10.0/24, and R1 must be able to deliver the reply to PC-A. A working forward route alone does not guarantee a successful ping or application session.
Basic Cisco IOS Verification
Use these commands from privileged EXEC mode to inspect interfaces, routes, neighbor resolution, and forwarding decisions:
show ip interface brief
show interfaces
show ip route
show ip route connected
show ip route static
show ip route 192.168.20.0
show arp
show ip cef 192.168.20.10
ping 192.168.20.10
traceroute 192.168.20.10
show ip interface brief gives a concise view of interface addresses and operational states. An interface should normally show an IP address and up/up status before its connected route is installed. show interfaces provides detailed physical, data-link, error, and configuration information.
show ip route displays route codes and entries. Common Cisco IOS codes include C for connected, L for local, S for static, and O for OSPF. An entry such as O 192.168.20.0/24 [110/2] via 192.0.2.2 indicates an OSPF route, a prefix of /24, administrative distance 110, metric 2, and next hop 192.0.2.2. The output also identifies the elapsed time and outbound interface where applicable.
show ip route 192.168.20.0 performs a destination-focused inspection. show arp checks IPv4-to-MAC mappings on Ethernet. show ip cef displays the Cisco Express Forwarding result for a destination. ping tests reachability, while traceroute reveals the Layer 3 hops that respond along the path.
Basic Cisco IOS Configuration
configure terminal
interface gigabitEthernet0/0
ip address 192.168.10.1 255.255.255.0
no shutdown
exit
ip route 192.168.20.0 255.255.255.0 192.0.2.2
ip route 0.0.0.0 0.0.0.0 192.0.2.2
The interface configuration supplies the address for a connected network. The static route supplies a path to a remote network, and the default route supplies a path for otherwise unknown destinations. For dynamic path learning, see Configure OSPF.
Common Routing Failures
| Symptom | Likely Cause | Verification |
|---|---|---|
| Local subnet works, remote networks fail | Incorrect default gateway or mask, down gateway interface, or missing router route | Check host settings, ping the gateway, inspect interface status and routing table |
| Forward traffic arrives but replies do not | Missing return route or far-side default route | Trace both directions and inspect the destination-side router |
| Unexpected route is selected | More specific prefix, different AD, protocol metric, stale static route, or default route | Use a destination-specific lookup and compare prefix, source, AD, and metric |
| Static route exists but forwarding fails | Unreachable next hop, wrong subnet, down exit interface, or failed ARP | Verify the next-hop route, ping the next hop, inspect interface state and ARP |
| Connected route is absent | Interface shutdown, physical or data-link failure, or missing address | Run show ip interface brief and inspect the interface configuration |
| Intermittent or confusing delivery | Overlapping or incorrect subnet masks | Recalculate the source and destination subnets and inspect route prefixes |
Layer 2 failure can prevent forwarding even when the IP route is valid. For Ethernet next hops, check ARP resolution, VLAN membership, interface state, and the link itself. A route is a forwarding instruction; it does not prove that the next-hop device can actually be reached.
Exam-Relevant Summary
- A host uses its default gateway only for destinations outside its local subnet.
- Routers make forwarding decisions using destination IP addresses and routing tables.
- Longest-prefix match is applied before comparing route-source preference and metrics.
- Lower administrative distance is preferred between equivalent prefixes learned from different sources.
- Metrics compare paths within the same routing source or protocol.
- Source and destination IP addresses normally remain unchanged during ordinary routing; Layer 2 addresses change at every hop.
- Routers decrement IPv4 TTL and recalculate the IPv4 header checksum.
- A default route is
0.0.0.0/0and is used only when no more specific route matches. - Successful communication requires a usable route in both directions.
Routing connects IP networks, while switching delivers frames within a Layer 2 domain. Understanding the distinction between these operations makes default gateways, routing tables, ARP, route selection, and troubleshooting much easier to reason about.