Routers and IPv4 routing fundamentals
As we discussed in Chapter 2.1 Wired Network Access, a router allows devices inside a LAN to communicate with devices outside that LAN. That’s the most common use of a router, so it’s worth understanding why it’s needed and how it makes those connections possible.
Routers take IP packets from one network and route them to another network. They make routing decisions by:
- Looking at the destination IP address in the IP packet
- Checking the router’s routing table for a matching destination network
- Choosing the best available path for that packet
Each interface on a router (often called a router interface) creates a network boundary. Connecting this to Chapter 2.3 VLANs and Trunking: each router interface defines a broadcast domain. Every device connected to the same router interface is in the same network (broadcast domain). The diagram below shows how each router interface leads to a different network.
(Image 143)
Image Title - Router Interfaces leading to multiple networks
Because there’s a 1:1 relationship between router interfaces and networks, anything you connect to a given router interface becomes a member of that network. A different router interface leads to a different network.
A common question is: how do you connect multiple devices to a single router interface? You do it by connecting those devices to a switch, and then connecting the switch to the router. Each host in that network gets an IP address from that network’s address range, and the router interface also has an IP address in that same network.
(Image 144)
Image Title - Multiple hosts connecting to a Router Interface through a Switch
Why a router is needed in a LAN
A router is most commonly used as the gateway between hosts in one network and hosts in another network. From Chapter 2.3 VLANs and Trunking, remember that hosts can only send broadcasts and Ethernet frames directly to each other if they’re in the same broadcast domain (VLAN is synonymous with broadcast domain).
If two hosts aren’t in the same broadcast domain, they can’t use broadcasts to complete ARP with each other. That matters because ARP is how a host learns the destination MAC address it needs at Layer 2 to send an Ethernet frame.
This creates a problem: hosts often need to communicate with devices outside their own network. Routers solve this.
When a host needs to communicate with another host whose IP address is outside its own network, it sends the IP packet to the router. When the router receives the packet, it routes it toward the destination network. Even if the destination network is separated by multiple routers and networks, the packet can still get there - as long as each router along the path knows how to reach the destination.
(Image 145)
Image Title - Router receiving a packet from a host destined to a host in another network
(Image 146)
Image Title - Host configured with R1’s GigabitEthernet 0/0 Router Interface IP Address as its Default Gateway
A key detail is what happens at Layer 2. If a host is sending to a destination on another network, it can’t ARP for the destination host’s MAC address (ARP broadcasts don’t cross router interfaces). So what MAC address does it use in the destination MAC field of the Ethernet frame?
It uses the MAC address of its default gateway (the router). To do that, the host sends an ARP request for the MAC address associated with the default gateway’s IP address. Because the host and the router interface are in the same broadcast domain, the router receives the ARP request and replies directly with an ARP reply.
(Image 147)
Image Title - PC A sending an ARP Request to its Default Gateway
Now that the host (PC A in this case) has resolved it’s default gateway’s (R1’s) associated MAC address, it can then send the actual packet that it intended to send to the destination host, towards the router.
Once the router receives the Ethernet frame containing the data to be routed, it de-encapsulates the frame and extracts the IP packet. Then it looks up the destination network in its routing table. If it finds a matching route, it builds a new Ethernet frame for the next hop, using the outgoing router interface in the direction of the destination network.
(Image 148)
Image Title - R1 performing L2 Rewrite on IP Packet destined to PC B
How to read a routing table
As a CCNA and network administrator, you’ll need to read a routing table on a Cisco router. A routing table lists the networks the router knows how to reach and tells the router which interface to send packets out of to reach those networks.
When a router receives an IP packet, it inspects the destination IP address:
- If the destination IP address belongs to the router itself, the router processes the packet locally (and responds if needed).
- If the destination IP address is in a different network, the router looks in its routing table for:
- A route to the destination host address, or
- A route to the destination network where that host address belongs
Unlike a switch, if a router can’t find a matching host route or network route, it drops the packet.
By default, routers neither generate nor forward broadcasts. That’s why router interfaces are broadcast domain boundaries: broadcasts don’t get forwarded from one router interface to another.
When you configure an IP address on a router interface, the router automatically adds a routing table entry for the network that interface belongs to. That entry is called a route.
(Image 149)
Image Title - R1 Routing Table displaying the route to 192.168.1.0/24
Let’s break down what the routing table entry is saying:
Choosing the best route and the longest prefix match
Suppose a router has three routes in its routing table that overlap, but use different subnet masks (different prefix lengths):
- 172.31.0.0/16
- 172.31.0.0/24
- 172.31.0.0/27
They may look similar, but they represent three different networks. Because the prefix lengths differ, the address ranges differ - even though the networks overlap.
| Network Address | Assignable Range | Broadcast Address |
|---|---|---|
| 172.31.0.0/16 | 172.31.0.1 - 172.31.255.254 | 172.31.255.255 |
| 172.31.0.0/24 | 172.31.0.1 - 172.31.0.254 | 172.31.0.254 |
| 172.31.0.0/27 | 172.31.0.1 - 172.31.0.30 | 172.31.0.31 |
Now say the router receives an IP packet destined to 172.31.0.20. Which route does it choose?
In practice, this means the router prefers the subnet mask (prefix length) with the most on-bits (1s), because it describes the most specific destination.
One more important point: the destination IP address in the packet must actually fall within the destination network of the route. If it doesn’t, that route isn’t usable.
For example, if the router receives a packet destined to 172.31.0.20 and the routing table only contains this route:
- C 172.16.1.0/24 is Directly Connected, GigabitEthernet0/0
Would the Router be able to route the packet?
No