Routing Basics
Routing Basics¶
Overview¶
This document covers the fundamental concepts of network routing. Routing is the core of data transmission between networks, the process by which packets find the optimal path from source to destination.
Difficulty: βββ Estimated Learning Time: 2-3 hours Prerequisites: 07_Subnetting_Practice.md
Table of Contents¶
- What is Routing?
- The Role of Routers
- Routing Table Structure
- Static Routing vs Dynamic Routing
- Default Gateway
- Longest Prefix Match
- Practice Problems
- Next Steps
- References
1. What is Routing?¶
1.1 Definition of Routing¶
Routing is the process of determining the optimal path to forward packets from source to destination across a network.
Basic Concept of Routing
Source Destination
[192.168.1.10] βββββ?βββββ?βββββ?βββββ?ββββββββΊ [10.0.0.50]
Router determines path
Router A ββββ Router B ββββ Router C
β β β
[192.168.1.10] β β β [10.0.0.50]
βββββββββββββββ΄ββββββββββββββ
Selected optimal path
1.2 Routing vs Switching¶
| Aspect | Switching (L2) | Routing (L3) |
|---|---|---|
| Operating Layer | Data Link Layer | Network Layer |
| Address Used | MAC Address | IP Address |
| Scope | Within same network | Between different networks |
| Device | Switch | Router |
| Table | MAC Address Table | Routing Table |
Switching (within same network) Routing (between networks)
βββββββββββββββββββ Network A Network B
β 192.168.1.0/24 β ββββββββββ ββββββββββ
β β β β β β
β PC1 βββ Switch βββ PC2 β PC1 β β PC2 β
β β β β β β β β
β MAC-based β β ββββββΌβββββββΌβββββ β
β forwarding β β Router β β Router β
βββββββββββββββββββ ββββββββββ ββββββββββ
IP-based forwarding
1.3 Purpose of Routing¶
- Network Connectivity: Connect different networks
- Path Determination: Select optimal path to destination
- Traffic Distribution: Load balancing across network
- Fault Recovery: Maintain connectivity through alternate paths
2. The Role of Routers¶
2.1 Basic Functions of Routers¶
Core Functions of Routers
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Router β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β 1. Receive packet ββββββΊ Receive packet at interface β
β 2. Check destination ββββββΊ Check destination IP in headerβ
β 3. Routing table lookup βββββΊ Search for optimal path β
β 4. Forward packet ββββββΊ Send to next hop β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
2.2 Router Components¶
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Router Internal Structure β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β ββββββββββββ ββββββββββββ ββββββββββββββββββββββββ β
β β CPU β β RAM β β Routing Table β β
β β(Processing)β β(Temp Storage)β β (Path Info) β β
β ββββββββββββ ββββββββββββ ββββββββββββββββββββββββ β
β β
β ββββββββββββ ββββββββββββ ββββββββββββββββββββββββ β
β β NVRAM β β Flash β β ARP Cache β β
β β (Config) β β (OS) β β (IP-MAC Mapping) β β
β ββββββββββββ ββββββββββββ ββββββββββββββββββββββββ β
β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Interfaces (Ports) β β
β β ββββββ ββββββ ββββββ ββββββ ββββββ β β
β β βEth0β βEth1β βEth2β βSer0β βSer1β β β
β β ββββββ ββββββ ββββββ ββββββ ββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββ β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
2.3 Packet Processing Flow¶
[PC A: 192.168.1.10] βββββββΊ [Router] βββββββΊ [PC B: 10.0.0.50]
Step 1: Receive Packet
ββββββββββββββββββββββββββββββββββββββββββββββββ
β Ethernet Header β IP Header β Data β
β Dst MAC: Router β Dst IP: 10.0.0.50 β
β Src MAC: PC A β Src IP: 192.168.1.10 β
ββββββββββββββββββββββββββββββββββββββββββββββββ
Step 2: Remove L2 Header, Check IP Header
ββββββββββββββββββββββββββββββββββββββββββββββββ
β IP Header β Data β
β Dst IP: 10.0.0.50 β
β Src IP: 192.168.1.10 β
β TTL: 64 β 63 (decrement) β
ββββββββββββββββββββββββββββββββββββββββββββββββ
Step 3: Routing Table Lookup
ββββββββββββββββββββββββββββββββββββββββββββββββ
β Destination: 10.0.0.50 β
β Match: 10.0.0.0/24 via 192.168.2.1 Eth1 β
β Next Hop: 192.168.2.1 β
β Output Interface: Eth1 β
ββββββββββββββββββββββββββββββββββββββββββββββββ
Step 4: Add New L2 Header, Forward Packet
ββββββββββββββββββββββββββββββββββββββββββββββββ
β Ethernet Header β IP Header β Data β
β Dst MAC: Next Hopβ Dst IP: 10.0.0.50 β
β Src MAC: Router β Src IP: 192.168.1.10 β
β (Eth1 MAC) β TTL: 63 β
ββββββββββββββββββββββββββββββββββββββββββββββββ
2.4 Router vs L3 Switch¶
| Feature | Router | L3 Switch |
|---|---|---|
| Primary Use | WAN connectivity, complex routing | VLAN inter-routing in LAN |
| Processing Method | Software-based | Hardware (ASIC)-based |
| Port Count | Few (2-8) | Many (24-48) |
| Features | Rich (NAT, VPN, etc.) | Limited |
| Price | Expensive | Relatively cheaper |
3. Routing Table Structure¶
3.1 Components of Routing Table¶
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Routing Table β
ββββββββββββββββ¬βββββββββββββ¬βββββββββββ¬ββββββββββββ¬ββββββββββββββββββ€
β Destination β Subnet Maskβ Next Hop β Interface β Metric/Distance β
β Network β β β β β
ββββββββββββββββΌβββββββββββββΌβββββββββββΌββββββββββββΌββββββββββββββββββ€
β 10.0.0.0 β /24 β 192.168.1.1β Eth0 β 10 β
β 172.16.0.0 β /16 β 192.168.2.1β Eth1 β 20 β
β 192.168.0.0 β /24 β directly β Eth0 β 0 (direct) β
β 0.0.0.0 β /0 β 203.0.113.1β Eth2 β 1 (default) β
ββββββββββββββββ΄βββββββββββββ΄βββββββββββ΄ββββββββββββ΄ββββββββββββββββββ
3.2 Routing Table Entry Types¶
| Type | Description | Example |
|---|---|---|
| Directly Connected (C) | Network directly connected to router | C 192.168.1.0/24 |
| Static Route (S) | Manually configured by admin | S 10.0.0.0/8 via 192.168.1.1 |
| Dynamic Route | Learned via routing protocol | R, O, B, etc. |
| Default Route (S*) | 0.0.0.0/0 (default route) | S* 0.0.0.0/0 via 203.0.113.1 |
3.3 Real Routing Table Examples¶
Linux (ip route)
$ ip route show
default via 192.168.1.1 dev eth0 proto dhcp metric 100
10.0.0.0/8 via 192.168.1.254 dev eth0
172.16.0.0/16 via 192.168.1.253 dev eth0
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.10
192.168.2.0/24 dev eth1 proto kernel scope link src 192.168.2.1
Cisco Router
Router# show ip route
Codes: C - connected, S - static, R - RIP, O - OSPF, B - BGP
Gateway of last resort is 203.0.113.1 to network 0.0.0.0
C 192.168.1.0/24 is directly connected, GigabitEthernet0/0
C 192.168.2.0/24 is directly connected, GigabitEthernet0/1
S 10.0.0.0/8 [1/0] via 192.168.1.254
O 172.16.0.0/16 [110/20] via 192.168.2.254, 00:05:32, Gi0/1
S* 0.0.0.0/0 [1/0] via 203.0.113.1
3.4 Metric and Administrative Distance¶
Metric: Route preference within same protocol
Destination: 10.0.0.0/24
Route 1: via 192.168.1.1 (metric: 10) β Selected
Route 2: via 192.168.2.1 (metric: 20)
Administrative Distance (AD): Trustworthiness between different protocols
| Route Source | Administrative Distance |
|---|---|
| Directly Connected | 0 |
| Static Route | 1 |
| EIGRP (internal) | 90 |
| OSPF | 110 |
| RIP | 120 |
| External BGP | 20 |
| Internal BGP | 200 |
Route Selection Example for Same Destination
Destination: 10.0.0.0/24
OSPF route: AD=110, metric=20
RIP route: AD=120, metric=2
Static route: AD=1, metric=0
Selected: Static route (lowest AD)
4. Static Routing vs Dynamic Routing¶
4.1 Static Routing¶
Administrator manually configures routes.
Static Routing Configuration
βββββββββββ βββββββββββ
β R1 β β R2 β
β β 10.0.0.0/30 β β
β .1ββββββββββββββββββββ.2 β
β β β β
ββββββ΄ββββββ β β βββββββ΄βββββ
β192.168.1.0β β β β172.16.0.0β
β /24 β β β β /16 β
ββββββββββββ β β ββββββββββββ
R1 Configuration:
ip route 172.16.0.0 255.255.0.0 10.0.0.2
R2 Configuration:
ip route 192.168.1.0 255.255.255.0 10.0.0.1
Advantages: - Simple configuration - No CPU/memory overhead - Security (no routing information exchange) - Bandwidth saving
Disadvantages: - Manual update required for network changes - Difficult to manage in large networks - No automatic failover
Use Cases: - Small networks - Stub networks with single path - Default gateway configuration - Security-critical segments
4.2 Dynamic Routing¶
Routing protocols automatically learn and update routes.
Dynamic Routing Operation
βββββββββββ βββββββββββ
β R1 βββββββββββββββΊβ R2 β
β OSPF β Routing Info β OSPF β
β β Exchange β β
ββββββ¬βββββ ββββββ¬βββββ
β β
ββββββββ΄βββββββ ββββββββ΄βββββββ
β192.168.1.0/24β β172.16.0.0/16β
β Advertise β β Advertise β
βββββββββββββββ βββββββββββββββ
R1 and R2 automatically learn each other's networks
Advantages: - Automatic route learning and updates - Automatic failover - Easy management of large networks - Adapts to network changes
Disadvantages: - CPU/memory usage - Bandwidth consumption (routing updates) - Complex configuration - Security considerations needed
4.3 Comparison Summary¶
| Feature | Static Routing | Dynamic Routing |
|---|---|---|
| Configuration | Manual | Automatic |
| Scalability | Low | High |
| Fault Response | Manual | Automatic |
| Resource Usage | Low | High |
| Security | High | Requires configuration |
| Management Complexity | Small: Low / Large: High | Initial: High / Operation: Low |
4.4 Hybrid Approach¶
Real networks often use both approaches together.
Enterprise Network Example
ISP ISP
β β
Static route Static route
(default gateway) (default gateway)
β β
ββββββ΄βββββ ββββββ΄βββββ
β HQ βββββββββ OSPF βββββββββββββΊβ Branch β
β Router β (Dynamic Routing) β Router β
ββββββ¬βββββ ββββββ¬βββββ
β β
Internal Network Internal Network
5. Default Gateway¶
5.1 What is Default Gateway?¶
Default Gateway is the IP address of the router used by hosts to send packets to other networks.
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β PC Network Configuration β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β IP Address: 192.168.1.100 β
β Subnet Mask: 255.255.255.0 β
β Default Gateway: 192.168.1.1 βββ Router's LAN Interface β
β DNS Server: 8.8.8.8 β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
5.2 Default Gateway Operation¶
Destination Determination Process
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β PC (192.168.1.100/24) β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Packet transmission request: destination 10.0.0.50 β
β β
β 1. Is destination in same network? β
β ββ Destination IP AND my subnet mask β
β 10.0.0.50 AND 255.255.255.0 = 10.0.0.0 β
β ββ My network: 192.168.1.0 β
β ββ 10.0.0.0 β 192.168.1.0 β Different network! β
β β
β 2. Send to default gateway β
β ββ Destination IP: 10.0.0.50 (unchanged) β
β ββ Destination MAC: Gateway MAC (via ARP) β
β β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββ
β Router β
β192.168.1.1β
βββββββββββββ
β
Forward to other network
5.3 Default Route¶
Router's default route is represented as 0.0.0.0/0.
Default Route in Routing Table
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Destination β Next Hop β Interface β
βββββββββββββββββββΌββββββββββββββββΌβββββββββββββββββββββ€
β 192.168.1.0/24 β directly β Eth0 β
β 10.0.0.0/24 β 192.168.2.1 β Eth1 β
β 172.16.0.0/16 β 192.168.2.2 β Eth1 β
β 0.0.0.0/0 β 203.0.113.1 β Eth2 β Default routeβ
βββββββββββββββββββ΄ββββββββββββββββ΄βββββββββββββββββββββ
Packet Processing:
- 192.168.1.50 β Forward directly to Eth0
- 10.0.0.100 β Forward to 192.168.2.1
- 8.8.8.8 β No matching route β Use default route
Forward to 203.0.113.1
5.4 Multiple Gateways¶
Load Balancing / Fault Tolerance
βββββββββββββ βββββββββββββ
β Router 1 β β Router 2 β
β192.168.1.1β β192.168.1.2β
β (primary) β β (backup) β
βββββββ¬ββββββ βββββββ¬ββββββ
β β
ββββββββββββ¬βββββββββββ
β
ββββββββ΄βββββββ
β Switch β
ββββββββ¬βββββββ
β
ββββββββββββββββββΌβββββββββββββββββ
β β β
ββββ΄βββ ββββ΄βββ ββββ΄βββ
β PC1 β β PC2 β β PC3 β
βGW:.1β βGW:.1β βGW:.2β
βββββββ βββββββ βββββββ
VRRP/HSRP: Gateway redundancy via virtual IP
Virtual IP: 192.168.1.254 (VRRP)
Router 1: 192.168.1.1 (Master)
Router 2: 192.168.1.2 (Backup)
All PCs' gateway: 192.168.1.254
β Automatically switches to Router 2 if Router 1 fails
6. Longest Prefix Match¶
6.1 Concept¶
Select the route that most specifically matches the destination IP from the routing table.
Longest Prefix Match Principle
Routing Table:
βββββββββββββββββββ¬ββββββββββββββββ
β Destination β Next Hop β
β Network β β
βββββββββββββββββββΌββββββββββββββββ€
β 10.0.0.0/8 β Router A β
β 10.1.0.0/16 β Router B β
β 10.1.1.0/24 β Router C β
β 0.0.0.0/0 β Router D β
βββββββββββββββββββ΄ββββββββββββββββ
Destination: 10.1.1.100
Match Analysis:
- 10.0.0.0/8 match (8 bits) β
- 10.1.0.0/16 match (16 bits) ββ
- 10.1.1.0/24 match (24 bits) βββ β Selected (longest match)
- 0.0.0.0/0 match (0 bits) β
Result: Forward to Router C
6.2 Understanding in Binary¶
Destination IP: 10.1.1.100 = 00001010.00000001.00000001.01100100
10.0.0.0/8:
00001010.xxxxxxxx.xxxxxxxx.xxxxxxxx
^^^^^^^^ (8 bits match)
10.1.0.0/16:
00001010.00000001.xxxxxxxx.xxxxxxxx
^^^^^^^^.^^^^^^^^ (16 bits match)
10.1.1.0/24:
00001010.00000001.00000001.xxxxxxxx
^^^^^^^^.^^^^^^^^.^^^^^^^^ (24 bits match) β Longest match
6.3 Importance of Longest Prefix Match¶
Example: Traffic Engineering
Internet Traffic Path Control
ββββββββββββ
β Internet β
ββββββ¬ββββββ
β
ββββββββββββββββΌβββββββββββββββ
β β β
ββββββ΄βββββ ββββββ΄βββββ ββββββ΄βββββ
β ISP A β β ISP B β β ISP C β
ββββββ¬βββββ ββββββ¬βββββ ββββββ¬βββββ
β β β
ββββββββββββββββΌβββββββββββββββ
β
ββββββββ΄βββββββ
βEnterprise β
β Router β
β β
βRouting Policy:β
β 0.0.0.0/0 β
β β ISP A β
β 8.8.8.0/24 β
β β ISP B β
ββββββββββββββββ
Accessing Google DNS (8.8.8.8):
- 0.0.0.0/0 (ISP A) matches
- 8.8.8.0/24 (ISP B) matches β Selected by longest match
6.4 Route Summarization¶
Using longest prefix match for route summarization:
Before Summarization After Summarization
192.168.0.0/24 β R1 192.168.0.0/22 β R1
192.168.1.0/24 β R1 (4 networks into 1)
192.168.2.0/24 β R1
192.168.3.0/24 β R1
Advantages:
- Reduced routing table size
- Reduced routing updates
- Improved network stability
7. Practice Problems¶
Problem 1: Routing Table Analysis¶
Analyze the following routing table and answer the questions.
Router# show ip route
C 192.168.1.0/24 is directly connected, Eth0
C 192.168.2.0/24 is directly connected, Eth1
S 10.0.0.0/8 [1/0] via 192.168.1.254
S 172.16.0.0/16 [1/0] via 192.168.2.254
O 172.16.10.0/24 [110/20] via 192.168.2.253
S* 0.0.0.0/0 [1/0] via 192.168.1.1
a) What is the next hop for packets to destination 10.10.10.10? b) What is the next hop for packets to destination 172.16.10.50? c) What is the next hop for packets to destination 8.8.8.8? d) What do C, S, and O mean?
Problem 2: Static Routing Configuration¶
Write the necessary static routing commands for R1 and R2 in the following network.
Network A R1 R2 Network B
192.168.1.0/24 ββ[.1]ββ[.2]ββ[.1]ββ[.2]ββ 10.0.0.0/24
192.168.100.0/30
Problem 3: Longest Prefix Match¶
Determine the next hop for each destination IP using the following routing table.
Routing Table:
- 0.0.0.0/0 β 10.0.0.1
- 10.0.0.0/8 β 10.0.0.2
- 10.10.0.0/16 β 10.0.0.3
- 10.10.10.0/24 β 10.0.0.4
- 10.10.10.128/25β 10.0.0.5
a) 10.10.10.200 b) 10.10.10.50 c) 10.10.20.100 d) 10.20.30.40 e) 8.8.8.8
Problem 4: Network Design¶
Design routing for a company with 3 branch offices.
HQ
192.168.0.0/24
β
ββββββ΄βββββ
β Core R β
ββββββ¬βββββ
βββββββΌββββββ
β β β
Branch A Branch B Branch C
10.1.0/24 10.2.0/24 10.3.0/24
Write the necessary static routes for each router.
Answers¶
Problem 1 Answers¶
a) 192.168.1.254 (matches 10.0.0.0/8 route) b) 192.168.2.253 (172.16.10.0/24 is more specific than 172.16.0.0/16, OSPF route) c) 192.168.1.1 (uses default route 0.0.0.0/0) d) - C: Connected (directly connected) - S: Static (static route) - O: OSPF (dynamic routing protocol)
Problem 2 Answers¶
R1:
ip route 10.0.0.0 255.255.255.0 192.168.100.2
R2:
ip route 192.168.1.0 255.255.255.0 192.168.100.1
Problem 3 Answers¶
a) 10.10.10.200 β 10.0.0.5 (matches 10.10.10.128/25, 200 is in 128-255 range) b) 10.10.10.50 β 10.0.0.4 (matches 10.10.10.0/24, 50 is in 0-127 range) c) 10.10.20.100 β 10.0.0.3 (matches 10.10.0.0/16) d) 10.20.30.40 β 10.0.0.2 (matches 10.0.0.0/8) e) 8.8.8.8 β 10.0.0.1 (matches default route)
Problem 4 Answers¶
Core Router:
# Directly connected networks are automatically added
# Default route (to Internet)
ip route 0.0.0.0 0.0.0.0 [ISP-Gateway]
Branch A Router:
ip route 192.168.0.0 255.255.255.0 [CoreRouterIP]
ip route 10.2.0.0 255.255.255.0 [CoreRouterIP]
ip route 10.3.0.0 255.255.255.0 [CoreRouterIP]
ip route 0.0.0.0 0.0.0.0 [CoreRouterIP]
# Or simply:
ip route 0.0.0.0 0.0.0.0 [CoreRouterIP]
Branch B and C follow the same pattern
8. Next Steps¶
Once you understand routing basics, proceed to the next topic.
Next Lesson¶
- 09_Routing_Protocols.md - RIP, OSPF, BGP
Related Lessons¶
- 07_Subnetting_Practice.md - Subnet calculations
- 10_TCP_Protocol.md - TCP communication
Recommended Practice¶
- Check your routing table with
ip routeorroute print - Trace Internet paths with
traceroute - Configure static routing in Packet Tracer
9. References¶
Command Reference¶
# Linux - Check routing table
ip route show
route -n
netstat -rn
# macOS
netstat -rn
# Windows
route print
netstat -rn
# Path tracing
traceroute google.com # Linux/macOS
tracert google.com # Windows
# Add static route (Linux)
sudo ip route add 10.0.0.0/8 via 192.168.1.1
sudo ip route del 10.0.0.0/8
# Add static route (Windows)
route add 10.0.0.0 mask 255.0.0.0 192.168.1.1
route delete 10.0.0.0
Learning Resources¶
- RFC 1812 - Requirements for IP Version 4 Routers
- Cisco Networking Academy - Routing Concepts
- Juniper Networks - Understanding Routing
Cisco IOS Commands¶
# Check routing table
show ip route
show ip route summary
# Configure static route
ip route 10.0.0.0 255.0.0.0 192.168.1.1
ip route 0.0.0.0 0.0.0.0 203.0.113.1
# Interface information
show ip interface brief
Document Information - Last Updated: 2024 - Difficulty: βββ - Estimated Learning Time: 2-3 hours