Routing
Routing is the process by which a router decides where to forward a packet next, based on its destination IP address. It relays packets between different network segments so they ultimately reach their destination host, and it forms the foundation of communication in an IP network.
Unlike “switching,” which forwards frames within the same L2 segment based on MAC addresses, routing operates at L3 (the network layer) and determines the forwarding destination between different networks based on IP addresses.
In the explanations that follow, we use the configuration shown above as an example: routers (R1–R3) connecting multiple networks (the segments to which PC1–PC3 are attached).
How Routing Works
When a router receives a packet, it processes the forwarding as follows.
- Check the destination IP address of the received packet.
- Search the router’s own routing table for an entry that matches the destination IP address.
- If multiple matching routes exist, select the most specific one using longest match.
- Decrement the TTL (Time to Live) in the IP header by 1; if it reaches 0, discard the packet at that point (and return an ICMP Time Exceeded message to the source).
- Forward the packets that were not discarded to the next hop and outgoing interface of the selected route.
If no matching route exists in the routing table, the router returns an ICMP Destination Unreachable message to the source and discards the packet. However, if a default route (described below) is configured, that route is used instead.
Routing Table
The routing table is a list of routing information that a router keeps in order to perform routing. It mainly consists of the following fields.
| Field | Description |
|---|---|
| Destination network | The network address to which the packet is forwarded. |
| Netmask / Prefix length | A mask value indicating the range of the destination network. Expressed in CIDR notation, e.g. /24. |
| Next hop / Gateway | The IP address of the adjacent router to which packets for the given network should be forwarded next. |
| Interface | The physical or logical interface out of which the packet is sent. |
| Metric | A value representing the cost of a route within a single routing protocol. When multiple routes to the same destination come from the same source, the one with the lower value is preferred. Routes learned from different sources (static vs. OSPF, for example) are compared by administrative distance, described below, rather than by metric. |
| Protocol / Source | Indicates how the route information was learned (directly connected, static configuration, various routing protocols, etc.). |
How to Check Routing Information
On a router running Cisco IOS(-XE), you can check the contents of the routing table with the show ip route command. Below are the outputs from R1–R3 in the verification topology shown at the beginning of this article.
R1#show ip route
Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP
D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
E1 - OSPF external type 1, E2 - OSPF external type 2, m - OMP
n - NAT, Ni - NAT inside, No - NAT outside, Nd - NAT DIA
i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
ia - IS-IS inter area, * - candidate default, U - per-user static route
H - NHRP, G - NHRP registered, g - NHRP registration summary
o - ODR, P - periodic downloaded static route, l - LISP
a - application route
+ - replicated route, % - next hop override, p - overrides from PfR
& - replicated local route overrides by connected
Gateway of last resort is not set
10.0.0.0/8 is variably subnetted, 4 subnets, 2 masks
C 10.1.2.0/24 is directly connected, GigabitEthernet2
L 10.1.2.1/32 is directly connected, GigabitEthernet2
C 10.1.3.0/24 is directly connected, GigabitEthernet3
L 10.1.3.1/32 is directly connected, GigabitEthernet3
S 192.168.0.0/16 [1/0] via 10.1.3.3, GigabitEthernet3
192.168.1.0/24 is variably subnetted, 2 subnets, 2 masks
C 192.168.1.0/24 is directly connected, GigabitEthernet1
L 192.168.1.1/32 is directly connected, GigabitEthernet1
S 192.168.2.0/24 [1/0] via 10.1.2.2, GigabitEthernet2
R1#R2#show ip route
Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP
D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
E1 - OSPF external type 1, E2 - OSPF external type 2, m - OMP
n - NAT, Ni - NAT inside, No - NAT outside, Nd - NAT DIA
i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
ia - IS-IS inter area, * - candidate default, U - per-user static route
H - NHRP, G - NHRP registered, g - NHRP registration summary
o - ODR, P - periodic downloaded static route, l - LISP
a - application route
+ - replicated route, % - next hop override, p - overrides from PfR
& - replicated local route overrides by connected
Gateway of last resort is 10.1.2.1 to network 0.0.0.0
S* 0.0.0.0/0 [1/0] via 10.1.2.1, GigabitEthernet2
10.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
C 10.1.2.0/24 is directly connected, GigabitEthernet2
L 10.1.2.2/32 is directly connected, GigabitEthernet2
192.168.2.0/24 is variably subnetted, 2 subnets, 2 masks
C 192.168.2.0/24 is directly connected, GigabitEthernet1
L 192.168.2.1/32 is directly connected, GigabitEthernet1
R2#R3#show ip route
Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP
D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
E1 - OSPF external type 1, E2 - OSPF external type 2, m - OMP
n - NAT, Ni - NAT inside, No - NAT outside, Nd - NAT DIA
i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
ia - IS-IS inter area, * - candidate default, U - per-user static route
H - NHRP, G - NHRP registered, g - NHRP registration summary
o - ODR, P - periodic downloaded static route, l - LISP
a - application route
+ - replicated route, % - next hop override, p - overrides from PfR
& - replicated local route overrides by connected
Gateway of last resort is not set
10.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
C 10.1.3.0/24 is directly connected, GigabitEthernet2
L 10.1.3.3/32 is directly connected, GigabitEthernet2
S 192.168.1.0/24 [1/0] via 10.1.3.1, GigabitEthernet2
S 192.168.2.0/24 [1/0] via 10.1.3.1, GigabitEthernet2
192.168.3.0/24 is variably subnetted, 2 subnets, 2 masks
C 192.168.3.0/24 is directly connected, GigabitEthernet1
L 192.168.3.1/32 is directly connected, GigabitEthernet1
R3#Meaning of the Code Column (Leftmost Character)
The letter at the far left of the output is a code indicating how that route was learned.
| Code | Meaning |
|---|---|
C | A directly connected network (Connected) |
L | An address assigned to the router’s own interface (Local) |
S | A static route manually configured by an administrator (Static) |
S* | Candidate default. Assigned to a default route that has been statically configured. |
O | A route learned via OSPF |
B | A route learned via BGP |
0.0.0.0/0 (::/0 for IPv6) is called a default route, and is used as the forwarding destination for packets that don’t match any other route. The S* 0.0.0.0/0 [1/0] via 10.1.2.1, GigabitEthernet2 entry in R2’s output corresponds to this.What Each Router’s Output Tells Us
In R1’s output, GigabitEthernet2 (10.1.2.0/24) and GigabitEthernet3 (10.1.3.0/24) appear as directly connected networks with C, and each interface’s own address appears with L. In addition, the route to 192.168.2.0/24, where PC2 is attached, is configured as a static route (S) via R2, and a broader route summarizing the whole 192.168.0.0/16 range is configured as a static route via R3. The latter summarizes the route toward PC3 (192.168.3.0/24), but since its range overlaps with the route to 192.168.2.0/24, we use it as a concrete example of longest match in the next section.
R2’s output shows Gateway of last resort is 10.1.2.1 to network 0.0.0.0, indicating that for destinations it has no individual route for—such as 192.168.1.0/24 or 192.168.3.0/24—it uses the default route (S*) that forwards everything to R1 (10.1.2.1).
In R3’s output, as with R1, static routes (S) to 192.168.1.0/24 and 192.168.2.0/24 are configured via GigabitEthernet2, which faces R1.
How to Read Each Element of a Route Entry
A static route entry is displayed in the following format.
S 192.168.2.0/24 [1/0] via 10.1.2.2, GigabitEthernet2| Displayed element | Corresponding field | Description |
|---|---|---|
192.168.2.0/24 | Destination network | The destination network address and prefix length. |
[1/0] | [Administrative Distance/Metric] | The administrative distance (AD), which represents the route’s priority, and the metric value within that routing protocol. |
via 10.1.2.2 | Next hop | The IP address of the adjacent router to which the packet is forwarded. |
GigabitEthernet2 | Outgoing interface | The interface out of which the packet is sent. |
Longest Match
When a destination IP address matches multiple routes in the routing table, the router prioritizes the route with the longest prefix length (the number of bits in the subnet mask)—that is, the most specific one. This is called longest match (also known as longest prefix match).
R1’s routing table, shown earlier, contains the following two routes.
S 192.168.0.0/16 [1/0] via 10.1.3.3, GigabitEthernet3
S 192.168.2.0/24 [1/0] via 10.1.2.2, GigabitEthernet2When forwarding a packet destined for PC2 (192.168.2.0/24), the destination address matches both 192.168.0.0/16 (prefix length 16) and 192.168.2.0/24 (prefix length 24). Here, longest match applies, and the route to 192.168.2.0/24—with the longer prefix length and more specific range (next hop 10.1.2.2, GigabitEthernet2)—is preferred.
If longest match were not applied and the broader 192.168.0.0/16 route were preferred instead, a packet meant for PC2 would be mistakenly forwarded toward R3 (GigabitEthernet3). Longest match is the mechanism that lets a summarized route (a summary route) coexist with a more specific route, while still forwarding correctly toward the direction where the destination actually resides.
Administrative Distance (AD)
Longest match applies when comparing routes with different prefix lengths, but a destination network and prefix length can sometimes be identical across routes learned from multiple sources, such as static, OSPF, and BGP. In this case, a value called administrative distance (AD) determines which source’s route is preferred. The lower the value, the higher the priority, and that route is the one adopted into the routing table.
See Administrative Distance (AD) for details.
Static Routing and Dynamic Routing
Based on how route information is configured, routing is broadly classified into static routing and dynamic routing.
| Item | Static routing | Dynamic routing |
|---|---|---|
| Configuration method | An administrator manually configures the route information. | A routing protocol automatically generates routes by exchanging route information with adjacent routers. |
| Handling of failures | An administrator must manually change the route when a failure occurs. | Link failures and the like are detected, and the route automatically switches to an alternative. |
| Router load | Load is low, since no route computation occurs. | CPU and memory load can increase due to exchanging and recomputing route information. |
| Applicable scale | Suited to small configurations with few routes and little topology change. | Suited to large networks with many routes and redundant configurations. |
Classification of Dynamic Routing Protocols
Protocols that implement dynamic routing are classified by the scope over which they exchange route information and by their route-computation algorithm.
A collection of routers operated under a single administrative policy is called an autonomous system (AS). Dynamic routing protocols are broadly divided into IGP and EGP, depending on whether they are used within an AS or between ASes.
| Classification | Description | Representative protocols |
|---|---|---|
| IGP (Interior Gateway Protocol) | A protocol that exchanges route information within the same autonomous system (AS). | RIP, OSPF, EIGRP, IS-IS |
| EGP (Exterior Gateway Protocol) | A protocol that exchanges route information between different autonomous systems (ASes). | BGP |
They can also be classified as follows by their route-computation algorithm.
| Algorithm type | Description | Representative protocols |
|---|---|---|
| Distance-vector | Determines routes based on distance information, such as hop count, received from adjacent routers. EIGRP is classified as an advanced distance-vector protocol, using the DUAL algorithm for fast convergence and loop prevention. | RIP, EIGRP |
| Link-state | Each router holds topology information for the entire network and computes the shortest path using the SPF (Shortest Path First) algorithm. | OSPF, IS-IS |
| Path-vector | Determines routes based on path information consisting of the autonomous system (AS) numbers traversed. | BGP |
Configuration Example
Download R1 config (r1_routing.cfg)
Download R2 config (r2_routing.cfg)
Download R3 config (r3_routing.cfg)