Routers and the routing table: how an IP packet is forwarded
This lesson is open to everyone. The other lessons open with a package: sign in or sign up, then choose a package.
Key points
- A switch forwards frames by destination MAC address (Data Link, layer 2); a router forwards packets by destination IP address (Network, layer 3).
- MAC addresses are not hierarchical (flat 48 bits), so switching does not scale. A router knows a whole network with one entry: 1 entry instead of 400 MACs.
- A switch uses a MAC address table, a router a routing table. A new router's routing table only holds the networks of its directly connected interfaces (
C= connected). - A computer checks with its own IP address and mask: if the destination is in another subnet, it sends the packet to its default gateway. The router looks up the destination IP in its routing table and sends the packet out the matching interface.
- ping tests connectivity, traceroute shows the routers a packet passes through. Windows
tracertsends ICMP echo, Cisco IOStraceroutesends UDP probes.
A switch "switches", a router "routes"
You already know that switches "switch" Ethernet frames based on MAC address information. A switch's only concern is this: when a frame enters one of its interfaces, look at the destination MAC address and know which interface to send it out of. Switches make decisions based on Data Link layer (layer 2) information.
Routers have a similar task, but this time we look at IP packets. IP lives on the Network layer (layer 3). A router looks at the destination IP address in an IP packet and sends it out the correct interface.
Why not switch with MAC addresses everywhere?
Both MAC addresses and IP addresses are unique per device. So why don't we just switch everywhere?
There are two switches, each with 200 computers connected. If all 400 computers want to talk to each other, each switch has to learn 400 MAC addresses.
Now think about a really large network, for example the Internet. There are millions of devices! Could you keep millions of entries in a MAC address table, one for every device on the Internet? No way!
The problem with switching is that it is not scalable: there is no hierarchy, just flat 48-bit MAC addresses.
Now look at the same example with routers. The 200 computers on the left are connected to Router A and are in the 192.168.1.0 /24 network. Router B has 200 computers behind it, using 192.168.2.0 /24.
Routers "route" based on IP information. All Router A needs to know is that 192.168.2.0 /24 is behind Router B. Router B only needs to know that 192.168.1.0 /24 is behind Router A. Instead of 400 MAC addresses, each router needs a single entry for the other side's network.
| Switch | Router | |
|---|---|---|
| Address it looks at | Destination MAC address | Destination IP address |
| OSI layer | Layer 2 (Data Link) | Layer 3 (Network) |
| Table | MAC address table | Routing table |
The routing table: only connected networks at first
When you take a brand new router out of the box it builds a routing table, but the only information in it is the directly connected interfaces. Let's start with a simple example:
- ComputerA: IP address 192.168.1.1, default gateway 192.168.1.254.
- ComputerB: IP address 192.168.2.2, default gateway 192.168.2.254.
- Router: 192.168.1.254 on FastEthernet 0/0, 192.168.2.254 on FastEthernet 1/0.
- Because a subnet mask is configured with each IP address, the router knows the network addresses and stores them in its routing table.
ComputerA → ComputerB, step by step
- ComputerA sends an IP packet with destination IP address 192.168.2.2.
- ComputerA checks its own IP address and subnet mask and concludes that 192.168.2.2 is in another subnet. So it forwards the packet to its default gateway.
- The router receives the packet, checks the destination IP address and scans its routing table. 192.168.2.2 matches the 192.168.2.0 /24 entry, and the router sends the packet out its FastEthernet 1/0 interface.
- ComputerB receives the packet.
Configuring it on a real router
The computers' configuration:
C:\Documents and Settings\ComputerA>ipconfig IP Address. . . . . . . . . . . . : 192.168.1.1 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 192.168.1.254 C:\Documents and Settings\ComputerB>ipconfig IP Address. . . . . . . . . . . . : 192.168.2.2 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 192.168.2.254
On the router we just configure IP addresses on the interfaces:
Router(config)#interface fastEthernet 0/0 Router(config-if)#no shutdown Router(config-if)#ip address 192.168.1.254 255.255.255.0 Router(config-if)#exit Router(config)#interface FastEthernet 1/0 Router(config-if)#no shutdown Router(config-if)#ip address 192.168.2.254 255.255.255.0
Let's check the routing table:
R1#show ip route Codes: C - connected, S - static, R - RIP, M - mobile, B - BGP D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area ... Gateway of last resort is not set C 192.168.1.0/24 is directly connected, FastEthernet0/0 C 192.168.2.0/24 is directly connected, FastEthernet1/0
The router knows both directly connected networks. The letter C means "connected".
Note (Cisco IOS 15 and later): newer IOS versions also show a /32 entry with the code L (local) for the interface's own IP address next to each connected network, for example L 192.168.1.254/32. Don't be surprised when you see it in Packet Tracer.
Testing with ping
Let's ping from ComputerA to ComputerB:
C:\Users\ComputerA>ping 192.168.2.2 Pinging 192.168.2.2 with 32 bytes of data: Reply from 192.168.2.2: bytes=32 time<1ms TTL=128 Reply from 192.168.2.2: bytes=32 time<1ms TTL=128 Reply from 192.168.2.2: bytes=32 time<1ms TTL=128 Reply from 192.168.2.2: bytes=32 time<1ms TTL=128 Ping statistics for 192.168.2.2: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
The ping works: we just routed our first IP packet!
Be aware: when you ping from one Windows computer to another, the firewall might be blocking ICMP traffic.
traceroute: which routers does the packet pass?
When you reach a server on the Internet you go through many routers. You can see which routers your packets travel through with traceroute. From the book author's computer to www.cisco.com:
C:\Users\Computer>tracert www.cisco.com Tracing route to e144.dscb.akamaiedge.net [95.100.128.170] over a maximum of 30 hops: 1 <1 ms <1 ms <1 ms 192.168.154.2 2 <1 ms <1 ms <1 ms 192.168.81.254 3 9 ms 7 ms 9 ms 10.224.124.1 ... 10 12 ms 11 ms 11 ms 95.100.128.170 Trace complete.
Here the packet passes 10 routers. You see the routers' IP addresses; the computer also did a hostname lookup, so you see the names of some routers too.
Traceroute also exists on Cisco routers: type the traceroute command and the IP address you want to reach.
Unlike the book: the book says "Traceroute uses the ICMP protocol". According to the Cisco documentation, the Windows tracert command sends ICMP echo requests as probes, while Cisco IOS traceroute sends UDP probe packets. In both cases the replies come back as ICMP messages.
Check yourself
5 questions · answers are shown instantly · 3 correct answers complete the lesson
1What information do a switch and a router base their decisions on?
2Why isn't switching alone used in a large network such as the Internet?
3You configured IP addresses and masks on a new router's interfaces. What will the routing table show?
4ComputerA (192.168.1.1 /24) sends a packet to 192.168.2.2. Who does it hand the packet to first?
5What does the Cisco IOS traceroute command send as probes?
tracert uses ICMP echo requests.Comments
Sign in to comment.