Sub topic 1
What a Router Does and Where It Sits
Prerequisites
This chapter assumes you understand IP addresses, subnets, hosts, links, Ethernet, and MAC addresses. You should also be comfortable reading terminal commands.
A router connects different IP networks. It receives an IP packet, examines its destination address, and sends the packet toward the next network. A router usually has multiple interfaces, with each interface attached to a different network.
A switch connects devices within one local network. It forwards Ethernet frames using MAC addresses. An end host, such as a laptop or server, creates or consumes application traffic. A router moves that traffic between networks.
Addresses, Prefixes, and CIDR Notation
An IPv4 address identifies an interface. A prefix identifies a group of addresses that belong to one network. CIDR notation writes the address followed by a slash and the number of network bits. For example, 192.0.2.0/24 has 24 network bits and 8 host bits.
The prefix length also determines how specific a network is:
0.0.0.0/0matches every IPv4 address.10.0.0.0/8matches addresses beginning with10.10.1.0.0/16matches only the smaller10.1network.
Reading a Routing Table
A routing table contains destination prefixes and forwarding instructions. A connected route means the router has an interface on that network. A next hop is the neighboring router to which the packet should be sent.
| Destination prefix | Type | Next hop or interface |
|---|---|---|
| `192.0.2.0/24` | Connected | `eth0` |
| `10.1.0.0/16` | Static | `192.0.2.2` |
| `0.0.0.0/0` | Default | `192.0.2.1` |
On Linux, inspect the table and ask which route matches a destination:
ip route show
ip route get 10.1.2.3Longest Prefix Match: Picking the Most Specific Route
When several entries match, the router uses longest prefix match. It selects the matching route with the largest prefix length, because that route describes the narrowest destination range.
For destination 10.1.2.3, both 10.0.0.0/8 and 10.1.0.0/16 match. The /16 route wins. If no more specific route matches, the router can use the default route.
Exercise
Which route should a router use for 10.1.2.3: 10.0.0.0/8 or 10.1.0.0/16?
Show answer
10.1.0.0/16, because it is the longest matching prefix.