IPv4 addressing, subnets and CIDR
Finding the network address, broadcast, host range and number of hosts from an IP address bit by bit; subnetting, VLSM and longest prefix match. A guaranteed calculation question on the midterm.
What takes a packet to the right place on the Internet is the IP address. Routers look not at the whole address but at its prefix. That is why the midterm almost always has questions like “what is the network address of this address, what is the broadcast, how many hosts are there, and which interface does it go to according to this table?” They all rest on the same few bit operations. Someone who knows these rules solves these questions in minutes:
- Network address = IP AND mask. The host bits are zeroed.
- Broadcast = all host bits set to 1. That is the network address + block size - 1.
- Number of hosts = , where is the number of host bits.
- Block size = 256 - the value of the mask octet. Boundaries are always multiples of this number.
- In routing, the longest prefix wins (longest prefix match).
The IPv4 address and the mask
An IPv4 address is a 32-bit number. To make it readable, it is split into four 8-bit parts (octets) and written in decimal: 192.168.10.77. An address consists of two parts: the network part and the host part. Interfaces with the same network part are on the same subnet and reach each other without a router.
The subnet mask tells where the split is. In the mask, the network bits are 1 and the host bits are 0. The short notation is the prefix length: /26 means “the first 26 bits are the network”.
| Prefix | Mask | Host bits | Usable hosts |
|---|---|---|---|
| /8 | 255.0.0.0 | 24 | 16777214 |
| /16 | 255.255.0.0 | 16 | 65534 |
| /20 | 255.255.240.0 | 12 | 4094 |
| /24 | 255.255.255.0 | 8 | 254 |
| /26 | 255.255.255.192 | 6 | 62 |
| /27 | 255.255.255.224 | 5 | 30 |
| /30 | 255.255.255.252 | 2 | 2 |
In every subnet two addresses cannot be given to hosts: the network address, whose host bits are all 0, and the broadcast address, whose host bits are all 1.
CIDR: classless addressing
In the past, addresses were divided into classes (A: /8, B: /16, C: /24). An organization that wanted 2000 hosts did not fit in a class C, so it got a class B, and 63 thousand addresses were wasted. CIDR (Classless Inter-Domain Routing) removed these fixed boundaries: the prefix can be any length, written as a.b.c.d/x. Today the classes are only a historical term. If “classful” appears on an exam, the old system is meant.
Network address, broadcast, host range
Quick method: call the octet whose mask is neither 255 nor 0 the interesting octet. The block size is . Find the multiple of the block size that contains that octet of the address.
Example 1: 192.168.10.77/26
mask 255.255.255.192 block size 256-192 = 64 (4th octet)
multiples of 64: 0, 64, 128, 192 77 → between 64 and 127
network 192.168.10.64
broadcast 192.168.10.127
hosts 192.168.10.65 ... 192.168.10.126
number of hosts 2^6 - 2 = 62
The same thing at the bit level: 77 = 01001101. The mask 192 = 11000000. AND: 01000000 = 64.
Example 2: 172.16.45.200/20
The interesting octet is the third: the /20 mask is 255.255.240.0, with block size 16.
multiples of 16: 0, 16, 32, 48 45 → between 32 and 47
network 172.16.32.0
broadcast 172.16.47.255 (3rd octet 47, 4th octet always 255)
hosts 172.16.32.1 ... 172.16.47.254
number of hosts 2^12 - 2 = 4094
PITFALL · the fourth octet. If the interesting octet is the third, the fourth octet of the network address is 0 and that of the broadcast is 255. Someone who writes 172.16.45.200 and says “network 172.16.32.200” forgot to zero the host bits.
Dividing into subnets (subnetting)
If you borrow of the host bits for the network part, you get equal subnets.
Example 3: split 200.10.20.0/24 into 4 equal subnets. , so borrow 2 bits: /26. The block size is 64.
| Subnet | Network address | Host range | Broadcast |
|---|---|---|---|
| 1 | 200.10.20.0/26 | .1 - .62 | .63 |
| 2 | 200.10.20.64/26 | .65 - .126 | .127 |
| 3 | 200.10.20.128/26 | .129 - .190 | .191 |
| 4 | 200.10.20.192/26 | .193 - .254 | .255 |
Example 3b: split 172.16.0.0/16 into /22 subnets. The question usually asks for three things: how many subnets, how many hosts in each, and which one is the n-th subnet.
borrowed bits 22 - 16 = 6 → 2^6 = 64 subnets
host bits 32 - 22 = 10 → 2^10 - 2 = 1022 hosts
block size 256 - 252 = 4 (in the 3rd octet)
subnet 1 172.16.0.0/22
subnet 2 172.16.4.0/22
subnet 3 172.16.8.0/22
subnet 64 172.16.252.0/22
The third octet of the n-th subnet is . Don’t forget to count from zero: subnet 3 starts at 8, not at 12.
VLSM: subnets of different sizes
In a real network, departments have different needs. VLSM (Variable Length Subnet Mask) gives each department the prefix that fits its need. Rule: start with the largest need, and make each subnet start at a multiple of its own block size.
Example 4: with 10.0.0.0/24, three LANs of 100, 50 and 20 hosts and a router link of 2 hosts.
100 hosts → 2^7-2=126 is enough → /25 → 10.0.0.0/25 (.1 - .126, bc .127)
50 hosts → 2^6-2=62 is enough → /26 → 10.0.0.128/26 (.129 - .190, bc .191)
20 hosts → 2^5-2=30 is enough → /27 → 10.0.0.192/27 (.193 - .222, bc .223)
2 hosts → 2^2-2=2 is enough → /30 → 10.0.0.224/30 (.225 - .226, bc .227)
first free address: 10.0.0.228
TIP · why do we start from the largest? If you start with a small one, a large block has to leave a gap to align itself to a multiple of its own size. Going from large to small, the blocks line up one after another with no gaps.
Route aggregation
The second gain of CIDR: advertising neighbouring networks with a single route. This is called route aggregation or supernetting. Router tables shrink.
Example 5: 198.51.96.0/24, .97.0/24, .98.0/24, .99.0/24 in one route.
96 = 01100000
97 = 01100001
98 = 01100010
99 = 01100011
^^^^^^ common first 6 bits
prefix = 16 + 6 = 22 → 198.51.96.0/22
Aggregation works cleanly only when the blocks are aligned and adjacent. You can’t aggregate 97 and 100 into a single /22.
Longest prefix match
Each row of a router’s forwarding table contains a prefix and an output interface. A destination address can match more than one row. The router chooses the longest prefix, because it is the more specific information.
11.20.0.0/16 → 0
11.20.64.0/18 → 1 (3rd octet 64 to 127)
11.20.72.0/21 → 2 (3rd octet 72 to 79)
0.0.0.0/0 → 3 (default)
11.20.75.9 → /16, /18, /21 match → longest /21 → 2
11.20.100.1 → /16, /18 match → /18 → 1
11.20.70.3 → /16, /18 (not 72-79)→ /18 → 1
11.21.0.1 → only default → 3
PITFALL · not the first match. It is wrong to read the table from top to bottom and pick the first row that matches. Find all the matches and look at the prefix length.
Private addresses and special-purpose blocks
RFC 1918 private addresses are not routed on the Internet. They are used in home and company networks, and are translated to a public address by NAT when leaving (next page).
| Block | Range | Note |
|---|---|---|
| 10.0.0.0/8 | 10.0.0.0 - 10.255.255.255 | large organizations |
| 172.16.0.0/12 | 172.16.0.0 - 172.31.255.255 | 172.32 is outside |
| 192.168.0.0/16 | 192.168.0.0 - 192.168.255.255 | home routers |
| 127.0.0.0/8 | loopback | 127.0.0.1 is yourself |
| 169.254.0.0/16 | link-local | when DHCP doesn’t answer |
Where do addresses come from? A host usually gets its address through DHCP (Dynamic Host Configuration Protocol): the four steps discover, offer, request, ACK. An organization gets its address block from its ISP, and ISPs get theirs from regional registries (under ICANN).
Common mistakes
| Wrong idea | Correct |
|---|---|
| /27 → 32 hosts | 30, the network and broadcast addresses are subtracted |
| The /21 mask is 255.255.240.0 | It is 255.255.248.0; 240 is /20 |
| Keeping the fourth octet in the network address | The octets after the interesting octet become 0 |
| Taking the start of the next block as the broadcast | Broadcast = next network address - 1 |
| In LPM, the first matching row in the table | The longest prefix wins |
| 172.32.x.x is a private address | 172.16/12 covers only 16 to 31 |
| Starting VLSM with a small subnet | Allocate from large to small, and alignment is not broken |
| Two addresses with different third octets are on different networks | With a /23 or shorter mask they can be on the same network |
Test yourself
Exam-style questions. Solve them on paper first, then reveal the answer.
How many usable host addresses does a /27 subnet provide?
Show answer
The host part is bits. There are addresses. The first is the network address and the last is the broadcast; these two cannot be given to hosts: .
Write the prefix length /21 as a dotted-decimal subnet mask.
Show answer
21 ones: the first two octets are 16 bits (255.255), and 5 bits of 1 remain in the third octet: 11111000 = 128+64+32+16+8 = 248. The fourth octet is 0. The result is 255.255.248.0.
What is the network address of the host 10.45.130.77/19?
Show answer
The /19 mask is 255.255.224.0. The interesting octet is the third, and the block size is . The multiple of 32 that contains 130 is 128 (the block from 128 to 159). The fourth octet is zeroed: 10.45.128.0. The broadcast is 10.45.159.255.
What is the broadcast address of the subnet containing 172.20.99.14/22?
Show answer
The /22 mask is 255.255.252.0, with a block size of 4 in the third octet. The block that contains 99 is 96 to 99. The network address is 172.20.96.0. The broadcast is the last address of the block: 172.20.99.255. The number of usable hosts is .
A company owns 192.0.2.0/24 and divides it into equal /28 subnets. How many subnets are created?
Show answer
4 bits were borrowed from the prefix: . The number of subnets is . Each carries hosts.
Which address is a valid (assignable) host address in 192.168.5.64/27?
- 192.168.5.64
- 192.168.5.95
- 192.168.5.90
- 192.168.5.96
Show answer
The /27 block size is 32. The subnet runs from 192.168.5.64 to 192.168.5.95. 64 is the network address, 95 is the broadcast, and 96 is the network address of the next subnet. The host range is .65 to .94, so .90 is valid.
A router has the forwarding table below. To which interface does it forward a datagram destined to 135.46.57.14?
135.46.56.0/22 eth0
135.46.60.0/22 eth1
135.46.56.0/21 eth2
0.0.0.0/0 eth3- eth0
- eth1
- eth2
- eth3
Show answer
Find the matching entries. The range of 135.46.56.0/22 is 56 to 59, and 57 is inside it: it matches. The range of 135.46.56.0/21 is 56 to 63: that matches too. 60.0/22 (60 to 63) does not match. The default matches everything. The longest prefix wins: /22 > /21, so the answer is eth0.
An ISP wants to advertise the four networks 57.6.104.0/24, 57.6.105.0/24, 57.6.106.0/24 and 57.6.107.0/24 as a single route. What is the aggregated prefix?
Show answer
Convert the third octets to binary: 104 = 01101000, 105 = 01101001, 106 = 01101010, 107 = 01101011. The common first 6 bits are 011010. The common prefix is 16 + 6 = 22 bits. Zero everything outside the common bits: 57.6.104.0/22.
A department needs 500 hosts in a single subnet. What is the longest prefix (smallest subnet) that fits?
Show answer
Look for the smallest such that . : 254, not enough. : 510, enough. The prefix is .
Give the first and last usable host addresses of the subnet containing 192.168.200.130/25.
Show answer
The /25 mask is 255.255.255.128, and the block size is 128. 130 is in the block from 128 to 255. The network address is .128 and the broadcast is .255. The first host is .129 and the last host is .254.
Are the hosts 10.1.5.10/23 and 10.1.4.250/23 on the same subnet?
Show answer
The /23 mask is 255.255.254.0, with a block size of 2 in the third octet. 5 and 4 are in the same block (4 to 5). The network address of both is 10.1.4.0. They are on the same subnet and communicate without needing a router. Don't say "different networks" just because the third octet differs.
Which of the following is a private (RFC 1918) IPv4 address?
- 172.32.10.5
- 192.169.1.1
- 172.20.10.5
- 11.0.0.1
Show answer
The private blocks are 10.0.0.0/8, 172.16.0.0/12 (172.16 to 172.31), and 192.168.0.0/16. 172.32 is outside this range, and 192.169 is not 192.168. 172.20 is inside 172.16/12.
Verification: All subnet answers were computed with node by converting to 32-bit integers and using AND/OR mask operations (verify/SEN2008/05.mjs).