I. Why Is Subnetting Needed?
In computer networks, an IP address is not only used to identify a device but also serves to locate the network.
For example:
192.168.1.10
192.168.1.20
192.168.1.30
These three addresses appear to belong to the same subnet, but how does a computer determine whether they are on the same network?
Comparing IP addresses alone is not enough.
The computer also needs to know:
- Which part of the IP address represents the network?
- Which part represents the host?
- How many devices can a network accommodate at most?
- Does the current destination address belong to the local network?
- Should the packet be sent directly, or handed to the default gateway?
These questions must be resolved using the subnet mask and CIDR.
1. Why Do IP Addresses Need to Be Divided?
An IPv4 address is 32 bits long and is usually expressed in dotted decimal notation.
For example:
192.168.1.10
Converted to binary:
11000000.10101000.00000001.00001010
A 32-bit address can be divided into two logical parts:
| Part | Meaning |
|---|---|
| Network part | Identifies the network to which the device belongs |
| Host part | Identifies the specific device in the network |
However, there is no fixed boundary between the network part and the host part.
Different networks may use network prefixes of different lengths, so the division must be explicitly defined by the subnet mask or CIDR prefix length.
2. What Problems Does Subnetting Solve?
Suppose an enterprise has 500 devices.
If all devices are placed on a single network, the following problems may occur:
- Excessive broadcast traffic.
- Difficult network management.
- Lack of isolation between different departments.
- Inflexible IP address allocation.
- Failures affect a larger scope.
- Network security policies are difficult to refine.
Subnetting allows a larger network to be split into multiple smaller networks.
For example:
Enterprise Network
├── Technical Department: 192.168.10.0/24
├── Product Department: 192.168.20.0/24
├── Finance Department: 192.168.30.0/24
└── Office Network: 192.168.40.0/24
Each subnet has its own independent network address range and can be configured separately with a gateway, access control policies, and routing rules.
II. What Is a Subnet Mask?
A subnet mask is a 32-bit binary number used to distinguish the network bits and host bits in an IPv4 address.
Its basic rules are:
- Consecutive
1s indicate the network part. - Consecutive
0s indicate the host part.
For example:
IP address:
192.168.1.10
Binary:
11000000.10101000.00000001.00001010
Subnet mask:
255.255.255.0
Binary:
11111111.11111111.11111111.00000000
As can be seen, the first 24 bits are 1, and the last 8 bits are 0.
Therefore:
- The first 24 bits belong to the network part.
- The last 8 bits belong to the host part.
1. Calculation Principle of the Subnet Mask
The computer performs a bitwise AND operation on the IP address and subnet mask to obtain the network address.
Bitwise AND operation rules:
| A | B | A AND B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
Calculation formula:
Network address = IP address AND subnet mask
For example:
IP address:
192.168.1.10
Subnet mask:
255.255.255.0
Convert to binary:
IP:
11000000.10101000.00000001.00001010
Mask:
11111111.11111111.11111111.00000000
AND:
11000000.10101000.00000001.00000000
Convert back to decimal:
192.168.1.0
Therefore:
Network address: 192.168.1.0
2. Common Forms of Subnet Masks
| Subnet Mask | Binary Network Bits | Host Bits |
|---|---|---|
| 255.0.0.0 | 8 | 24 |
| 255.255.0.0 | 16 | 16 |
| 255.255.255.0 | 24 | 8 |
| 255.255.255.128 | 25 | 7 |
| 255.255.255.192 | 26 | 6 |
| 255.255.255.224 | 27 | 5 |
| 255.255.255.240 | 28 | 4 |
| 255.255.255.248 | 29 | 3 |
| 255.255.255.252 | 30 | 2 |
The network bits in a subnet mask must be contiguous; network bits and host bits cannot be interleaved.
For example:
11111111.11111111.11111111.00000000
is a valid subnet mask.
Whereas:
11111111.11111111.11111111.01000000
is not a standard IPv4 subnet mask.
3. How Does a Subnet Mask Determine Whether Devices Are on the Same Subnet?
Suppose there are two devices:
Device A:
IP: 192.168.1.10
Mask: 255.255.255.0
Device B:
IP: 192.168.1.20
Mask: 255.255.255.0
Calculate the network address for each:
Device A:
192.168.1.10 AND 255.255.255.0
= 192.168.1.0
Device B:
192.168.1.20 AND 255.255.255.0
= 192.168.1.0
Both devices produce the same network address, so they belong to the same subnet.
Now consider another device:
Device C:
IP: 192.168.2.20
Mask: 255.255.255.0
The result:
192.168.2.20 AND 255.255.255.0
= 192.168.2.0
Device C and Device A are not on the same subnet.
In general, when two devices use the same subnet prefix, you can compare their network addresses to determine whether they are on the same subnet.
III. What Is CIDR?
CIDR stands for Classless Inter-Domain Routing (in Chinese, 无类别域间路由).
It is an address allocation and route representation method that does not rely on traditional IP address classes.
CIDR uses a slash and a number to indicate the length of the network prefix.
For example:
192.168.1.10/24
Here:
192.168.1.10is the IP address./24means the first 24 bits are the network part.- The remaining 8 bits are the host part.
1. Relationship Between CIDR and the Subnet Mask
The CIDR prefix length actually describes the number of consecutive 1s in the subnet mask.
For example:
/24
corresponds to this binary subnet mask:
11111111.11111111.11111111.00000000
Converted to decimal:
255.255.255.0
Common CIDR-to-subnet-mask equivalents:
| CIDR | Subnet mask | Host bits |
|---|---|---|
| /8 | 255.0.0.0 | 24 |
| /16 | 255.255.0.0 | 16 |
| /20 | 255.255.240.0 | 12 |
| /21 | 255.255.248.0 | 11 |
| /22 | 255.255.252.0 | 10 |
| /23 | 255.255.254.0 | 9 |
| /24 | 255.255.255.0 | 8 |
| /25 | 255.255.255.128 | 7 |
| /26 | 255.255.255.192 | 6 |
| /27 | 255.255.255.224 | 5 |
| /28 | 255.255.255.240 | 4 |
| /29 | 255.255.255.248 | 3 |
| /30 | 255.255.255.252 | 2 |
| /31 | 255.255.255.254 | 1 |
| /32 | 255.255.255.255 | 0 |
2. Why is CIDR more flexible than traditional classful addressing?
Traditional IPv4 address classification divides addresses into classes such as A, B, and C.
Typical default network prefixes are:
| Address class | Default prefix | Default subnet mask |
|---|---|---|
| Class A | /8 | 255.0.0.0 |
| Class B | /16 | 255.255.0.0 |
| Class C | /24 | 255.255.255.0 |
This approach has obvious problems:
Suppose an organization needs 500 IP addresses.
With a traditional Class C network, a single network can provide only 254 conventional usable host addresses, which cannot meet the requirement.
With a traditional Class B network, the address space available far exceeds actual needs.
CIDR can allocate prefix lengths according to actual needs.
For example:
192.168.0.0/23
Total number of addresses:
2^(32-23) = 512
A typical broadcast-based IPv4 subnet usually has 510 assignable host addresses, enough to meet the needs of 500 devices.
The core value of CIDR is:
- More flexible allocation of IP addresses.
- Reduced waste of address space.
- Support for subnets of different sizes.
- Support for route aggregation.
- Smaller routing tables.
IV. How to calculate the address range of a subnet?
This is the most important part of understanding subnetting.
To calculate an IPv4 subnet, you usually need to determine:
- Network address.
- Broadcast address.
- First usable host address.
- Last usable host address.
- Total number of addresses.
- Number of usable hosts.
1. Formula for calculating the number of addresses
An IPv4 address is 32 bits long.
If the CIDR prefix length is n, then the number of host bits is:
Total number of addresses:
For a typical broadcast-based IPv4 subnet, you usually need to reserve:
- One network address.
- One broadcast address.
Therefore, the number of usable host addresses is:
For example:
192.168.1.0/24
Host bits:
Total number of addresses:
Number of usable host addresses:
2. /24 Subnet Calculation
Assume there is a network:
192.168.1.0/24
Its corresponding subnet mask is:
255.255.255.0
Address range:
| Item | Address |
|---|---|
| Network address | 192.168.1.0 |
| First usable address | 192.168.1.1 |
| Last usable address | 192.168.1.254 |
| Broadcast address | 192.168.1.255 |
| Total number of addresses | 256 |
| Number of usable hosts | 254 |
3. /26 Subnet Calculation
Assume the network is:
192.168.1.0/26
The CIDR prefix is 26, so:
Total number of addresses:
Number of usable hosts:
The corresponding subnet mask is:
255.255.255.192
The last byte in binary:
11000000
Converted to decimal:
192
Therefore, the address block size for each subnet is:
The subnet starting addresses increase by 64.
192.168.1.0/26
192.168.1.64/26
192.168.1.128/26
192.168.1.192/26
The specific division is as follows:
| Subnet | Network Address | Usable Host Range | Broadcast Address |
|---|---|---|---|
| Subnet 1 | 192.168.1.0 | .1 - .62 | .63 |
| Subnet 2 | 192.168.1.64 | .65 - .126 | .127 |
| Subnet 3 | 192.168.1.128 | .129 - .190 | .191 |
| Subnet 4 | 192.168.1.192 | .193 - .254 | .255 |
The original /24 network is divided into four /26 subnets.
4. General Subnetting Calculation Method
When the subnet mask is not on a whole-byte boundary, you can use the address block size method.
Take:
192.168.10.0/27
as an example.
Step 1: Calculate the subnet mask.
/27
= 255.255.255.224
Step 2: Find the byte that changes.
Last byte: 224
Step 3: Calculate the address block size.
Step 4: Determine the subnet starting addresses.
0
32
64
96
128
160
192
224
Step 5: Calculate the range of each subnet.
| Subnet | Network address | Host address range | Broadcast address |
|---|---|---|---|
| 1 | 192.168.10.0 | .1 - .30 | .31 |
| 2 | 192.168.10.32 | .33 - .62 | .63 |
| 3 | 192.168.10.64 | .65 - .94 | .95 |
| 4 | 192.168.10.96 | .97 - .126 | .127 |
| 5 | 192.168.10.128 | .129 - .158 | .159 |
| 6 | 192.168.10.160 | .161 - .190 | .191 |
| 7 | 192.168.10.192 | .193 - .222 | .223 |
| 8 | 192.168.10.224 | .225 - .254 | .255 |
A /24 network can be divided into eight /27 subnets.
Each /27 subnet has 32 addresses, of which 30 are ordinary usable host addresses.
V. How do you divide subnets based on the number of devices?
In real-world network planning, you usually don't start with a CIDR block and then calculate the number of addresses; instead, you first determine how many devices each department needs, and then decide on the subnet size.
For example:
An enterprise has three departments:
| Department | Number of devices |
|---|---|
| Technical department | 100 devices |
| Product department | 50 devices |
| Finance department | 20 devices |
Assume that you use:
192.168.10.0/24
as the address pool.
You need to choose an appropriate subnet based on the number of devices in each department.
1. Determine the number of host bits each department needs
Ordinary IPv4 subnets need to reserve a network address and a broadcast address.
Therefore:
The technical department needs 100 devices:
Therefore, at least 7 host bits are required.
The corresponding prefix is:
The technical department needs a /25.
The product department needs 50 devices:
Therefore, a /26 is required.
The finance department needs 20 devices:
Therefore, a /27 is required.
2. Use VLSM for division
VLSM is the abbreviation of Variable Length Subnet Mask, and in Chinese it is called 可变长子网掩码 (variable-length subnet mask).
It allows subnet prefixes of different lengths to be used within the same address plan.
According to the requirements, allocate starting from the largest subnet:
| Department | Subnet | Network address | Address range | Number of usable addresses |
|---|---|---|---|---|
| Technical department | /25 | 192.168.10.0 | .1 - .126 | 126 |
| Product department | /26 | 192.168.10.128 | .129 - .190 | 62 |
| Finance department | /27 | 192.168.10.192 | .193 - .222 | 30 |
| Reserved addresses | /27 | 192.168.10.224 | .225 - .254 | 30 |
Here, one /25, one /26, and two /27 subnets are used.
None of the subnets overlap, and they all remain within the original /24 address pool.
3. The VLSM division process
flowchart TD
A["Determine the original address pool"] --> B["Count devices in each department"]
B --> C["Calculate host bits required per department"]
C --> D["Convert to CIDR prefix length"]
D --> E["Sort subnets by size from largest to smallest"]
E --> F["Allocate contiguous, aligned address blocks"]
F --> G["Check whether addresses overlap"]
G --> H{"Is the address plan valid"}
H -->|Yes| I["Generate network and broadcast addresses"]
H -->|No| J["Adjust address blocks and subnet boundaries"]
J --> F
I --> K["Configure gateways and routes"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style C fill:#EDE5FA,stroke:#B7A1DC,color:#59457A
style D fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style E fill:#FCEFD8,stroke:#E5C68C,color:#765B24
style F fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style G fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style H fill:#FCEFD8,stroke:#E5C68C,color:#765B24
style I fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style J fill:#F8E4E7,stroke:#DDA8B2,color:#804553
style K fill:#EDE5FA,stroke:#B7A1DC,color:#59457A
Actual planning should also reserve addresses for gateways, printers, servers, network devices, and future expansion.
6. What Are VLSM and FLSM?
There are two important concepts in subnetting:
- FLSM: Fixed Length Subnet Mask, i.e., a fixed-length subnet mask.
- VLSM: Variable Length Subnet Mask, i.e., a variable-length subnet mask.
1. FLSM: Fixed Length Subnet Mask
FLSM means dividing a network into multiple subnets of the same size.
For example:
192.168.1.0/24
Divide it into four /26 subnets:
192.168.1.0/26
192.168.1.64/26
192.168.1.128/26
192.168.1.192/26
Each subnet has 64 addresses.
Advantages:
- Simple planning.
- Uniform subnet sizes.
- Easy to manage and calculate.
Disadvantages:
- The number of devices in different departments may vary greatly.
- Small departments may also occupy larger address blocks.
- Address utilization is not necessarily high.
2. VLSM: Variable Length Subnet Mask
VLSM allows subnets of different sizes to coexist within one network.
For example:
Technical department: 192.168.10.0/25
Product department: 192.168.10.128/26
Finance department: 192.168.10.192/27
Advantages:
- More flexible address allocation.
- Reduce address waste.
- Suitable for complex enterprise network planning.
- Can adapt to the scale of different departments.
Comparison of the two:
| Comparison dimension | FLSM | VLSM |
|---|---|---|
| Subnet size | Same | Can differ |
| Prefix length | Fixed | Variable |
| Planning complexity | Lower | Higher |
| Address utilization | May be lower | Usually higher |
| Use cases | Simple network structures | Enterprise and complex networks |
7. CIDR and Route Aggregation
In addition to subnetting, CIDR can also be used for route aggregation.
Route aggregation, also called route summarization, is the process of combining multiple contiguous networks into a larger network prefix.
It can reduce the number of entries in a routing table.
1. Why Is Route Aggregation Needed?
Suppose an enterprise has the following four networks:
192.168.0.0/24
192.168.1.0/24
192.168.2.0/24
192.168.3.0/24
If they are advertised separately to other routers, four routes need to be stored in the routing table.
However, these four networks have contiguous addresses and conform to an aggregation boundary.
They can be combined into:
192.168.0.0/22
Because the address range of /22 is:
192.168.0.0
to
192.168.3.255
It exactly contains the four /24 networks above.
2. Calculation Principle of Route Aggregation
Two conditions must be met:
- The network addresses are contiguous.
- The aggregated network address conforms to the prefix alignment rule.
For example:
192.168.0.0/24
192.168.1.0/24
192.168.2.0/24
192.168.3.0/24
Look at the third byte:
0 = 00000000
1 = 00000001
2 = 00000010
3 = 00000011
The first two bytes are exactly the same, and the first 6 bits of the third byte are the same.
So we can keep:
The final result is:
192.168.0.0/22
3. Illustrative Flow of Route Aggregation
flowchart TD
A["Multiple independent networks"] --> B["Check contiguity of network addresses"]
B --> C{"Contiguous and satisfying the alignment rule?"}
C -->|Yes| D["Compare network prefixes"]
C -->|No| E["Cannot be aggregated directly"]
D --> F["Determine the common prefix length"]
F --> G["Generate the aggregate route"]
G --> H["Advertise the summary route upstream"]
E --> I["Keep the independent routes"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style C fill:#FCEFD8,stroke:#E5C68C,color:#765B24
style D fill:#EDE5FA,stroke:#B7A1DC,color:#59457A
style E fill:#F8E4E7,stroke:#DDA8B2,color:#804553
style F fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style G fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style H fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style I fill:#F6F7F8,stroke:#C9D0D8,color:#475569
Note that an aggregate route may cover a larger address space than what has actually been allocated. Network administrators must ensure that every address within the aggregate range is handled correctly, so as to avoid creating incorrect routes or blackhole routes.
VIII. Longest Prefix Match: How Does a Router Choose a Route?
There is an important link between CIDR and route forwarding: longest prefix match.
After a router receives a packet, it must look up the routing table based on the destination IP address.
If multiple routes can all match the destination address, the one with the longest prefix length is usually chosen.
The longer the prefix, the more precise the match range it represents.
1. An Example
Suppose the routing table contains the following entries:
| Destination network | Next hop |
|---|---|
| 10.0.0.0/8 | Gateway A |
| 10.1.0.0/16 | Gateway B |
| 10.1.2.0/24 | Gateway C |
| 0.0.0.0/0 | Default gateway |
Destination IP:
10.1.2.50
It matches all of the following at the same time:
10.0.0.0/810.1.0.0/1610.1.2.0/240.0.0.0/0
Among them, the /24 prefix is the longest, so Gateway C is chosen.
2. Characteristics of Longest Prefix Match
/32can match exactly one IPv4 address./24matches an address block containing 256 addresses./16matches a larger address block./0matches all IPv4 addresses.
Default route:
0.0.0.0/0
This means that when the router cannot find a more specific matching route, it can hand the packet to the default next hop.
3. The Router's Route Lookup Process
flowchart TD
A["Receive IP packet"] --> B["Read destination IP address"]
B --> C["Look up routing table"]
C --> D["Find all matching prefixes"]
D --> E{"Is there a matching route?"}
E -->|Yes| F["Select longest matching prefix"]
E -->|No| G["Check whether a default route exists"]
G --> H{"Does a default route exist?"}
H -->|Yes| I["Use default next hop"]
H -->|No| J["Drop packet or return unreachable information"]
F --> K["Determine outbound interface and next hop"]
I --> K
K --> L["Forward packet"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style C fill:#EDE5FA,stroke:#B7A1DC,color:#59457A
style D fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style E fill:#FCEFD8,stroke:#E5C68C,color:#765B24
style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style G fill:#EDE5FA,stroke:#B7A1DC,color:#59457A
style H fill:#FCEFD8,stroke:#E5C68C,color:#765B24
style I fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style J fill:#F8E4E7,stroke:#DDA8B2,color:#804553
style K fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style L fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
9. Special IPv4 Prefixes and Address Ranges
Not all IPv4 addresses can be assigned arbitrarily to ordinary hosts.
Some addresses have special purposes.
1. Private IP Addresses
RFC 1918 defines three common private IPv4 address ranges:
| Address Range | CIDR | Number of Addresses |
|---|---|---|
| 10.0.0.0 - 10.255.255.255 | 10.0.0.0/8 | 16,777,216 |
| 172.16.0.0 - 172.31.255.255 | 172.16.0.0/12 | 1,048,576 |
| 192.168.0.0 - 192.168.255.255 | 192.168.0.0/16 | 65,536 |
These addresses are typically used in enterprise internal networks, home LANs, and cloud private networks.
Private IP addresses cannot be directly routed globally on the Internet as public IPv4 addresses.
If internal devices need to access the Internet, address translation is usually required through mechanisms such as NAT.
2. Common Special Addresses
| Address or Range | Purpose |
|---|---|
| 0.0.0.0/0 | Default route |
| 0.0.0.0 | Unspecified address; the specific meaning depends on the usage scenario |
| 127.0.0.0/8 | IPv4 loopback address |
| 127.0.0.1 | Common localhost loopback address |
| 169.254.0.0/16 | IPv4 link-local address |
| 255.255.255.255 | Limited broadcast address |
| 192.0.2.0/24 | Documentation example address |
| 198.51.100.0/24 | Documentation example address |
| 203.0.113.0/24 | Documentation example address |
| 224.0.0.0/4 | IPv4 multicast address |
Note that special addresses may have different usage scopes and forwarding rules; whether an address can be used as an ordinary host address cannot be judged solely by its appearance.
3. Special Cases of /31 and /32
The host count formula used earlier:
applies to conventional calculations for ordinary IPv4 broadcast subnets, but it does not apply to all prefixes.
/31:
255.255.255.254
There are only two addresses.
On point-to-point links compliant with RFC 3021, both addresses can be assigned to the two ends of the link, without subtracting the network address and broadcast address in the traditional way.
Therefore:
192.0.2.0/31
can be used for the two endpoints of a point-to-point link.
/32:
255.255.255.255
There is only one address.
/32 is typically used to represent a single host route, a loopback address, or an exact route match; it is not used for regular multi-host subnets.