FREE LESSON · Networks & distributed systems · 6 OF 13
IP prefixes and longest-prefix routing
IP prefixes and longest-prefix routing: Compute address ranges and choose routes from bits.
Prerequisites: Binary integers and bitwise operations.
Learning objectives
- Compute an IPv4 subnet range from a prefix.
- Apply longest-prefix matching to overlapping routes.
- Distinguish routing reachability from address assignment.
An IPv4 address contains 32 bits. A prefix length /p fixes the first p bits and leaves 32−p bits available within the address block. Thus a /24 contains 256 addresses, while a /26 contains 64. Prefix notation describes a block; it does not by itself specify which addresses a particular network can assign to ordinary hosts. Traditional subnet conventions reserve network and broadcast addresses, with special rules for point-to-point /31 links and other contexts.
For 192.0.2.130/26, the final octet 130 is binary 10000010. The /26 mask fixes the first two bits of that octet, leaving six host bits. Clearing those six bits gives 128, so the block runs from 192.0.2.128 through 192.0.2.191. Its mask is 255.255.255.192. Under conventional broadcast-subnet rules, usable host addresses run 129 through 190. Count the address block first, then apply the actual link's assignment rules.
A forwarding table can contain overlapping prefixes. The router chooses the matching route with the longest prefix, because it describes the most specific destination range. Suppose routes include 0.0.0.0/0 through gateway A,10.0.0.0/8 through B,10.12.0.0/16 through C, and 10.12.4.0/24 through D. Destination 10.12.4.9 uses D;10.12.7.9 uses C;10.99.1.1 uses B;203.0.113.7 uses A. A /0 default matches every address but loses to every matching longer prefix.
This is not a rule to choose the numerically largest mask or the first route printed by an administrative command. Matching first matters. A /24 for another network is irrelevant. Multiple equally specific routes may be resolved using metrics, policy, or multipath forwarding. Those mechanisms operate after or alongside the prefix-selection rule according to the implementation.
Aggregation combines adjacent aligned prefixes into a shorter announcement. The two blocks 10.4.0.0/24 and 10.4.1.0/24 can form 10.4.0.0/23. Blocks 10.4.1.0/24 and 10.4.2.0/24 cannot be represented by one /23 without including other addresses because the boundary is misaligned. Announcing an aggregate also claims reachability for the whole covered space; planning must account for holes and failures.
IPv6 extends addresses to 128 bits and uses the same prefix logic, with different address and neighbor-discovery conventions. Do not transplant IPv4 broadcast assumptions into IPv6. CIDR calculations help explain routing, but real connectivity also depends on interfaces, neighbor resolution, filtering, return paths, and service availability. A correct prefix calculation is one necessary piece of the trace, not an end-to-end success proof.
Practice problem
Find the block and conventional usable-host range containing 203.0.113.77/27. Then apply routes 0.0.0.0/0→A,203.0.113.0/24→B,203.0.113.64/26→C, and 203.0.113.96/27→D to destinations 77,100, and 150 in that final octet. Finally explain whether 203.0.113.64/27 and 203.0.113.96/27 aggregate exactly into one /26.
Worked solution
A /27 has 32 addresses. The block containing 77 begins at 64 and ends at 95; conventional hosts are 65 through 94. Destination 77 matches the /24 and the /26 but not 96/27, so it uses C. Destination 100 also matches 96/27, so the more specific D wins. Destination 150 lies outside 64/26 and 96/27, so B wins. The 64/27 and 96/27 blocks are adjacent 32-address ranges aligned within 64/26, so they aggregate exactly to 203.0.113.64/26. This arithmetic does not establish that every address is assigned or reachable; it only describes coverage and route selection.
Check your understanding
Routes are 10.0.0.0/8→A,10.8.0.0/16→B,10.9.3.0/24→C. Where does 10.8.3.2 go?
- No route because prefixes overlap.
- B because /16 is the longest prefix that actually matches.
- C because /24 is the longest route in the table.
- A because /8 covers the most addresses.
Explanation
The /24 is more specific but does not match this destination. Longest-prefix matching compares only matching entries.
Which range contains 203.0.113.77/27, before host-address reservations?
- 203.0.113.0–203.0.113.255
- 203.0.113.64–203.0.113.95
- 203.0.113.77–203.0.113.108
- 203.0.113.65–203.0.113.94
Explanation
A /27 has 32 addresses aligned at multiples of 32. The range 64 through 95 contains 77;65 through 94 is the conventional usable-host subset.
Primary reading
- Classless Inter-domain Routing, RFC 4632: IETF / RFC Editor