Topic Overview
Subnetting & CIDR
Master subnetting and CIDR notation for efficient IP address allocation, network segmentation, and network design.
Subnetting & CIDR
Why This Matters
Think of subnetting like dividing a large building into smaller apartments. Instead of one big space, you have separate units with their own addresses. Subnetting does the same for networks—it divides a large network into smaller subnets, each with its own address range. This improves organization, security, and efficiency.
This matters because large networks are hard to manage. If you have 10,000 devices on one network, broadcasts flood the network, and it's hard to isolate problems. Subnetting breaks this into smaller networks (e.g., 10 subnets of 1000 devices each), reducing broadcast traffic and improving security through isolation.
In interviews, when someone asks "How would you design a network?", they're testing whether you understand subnetting. Do you know how to calculate subnet ranges? Do you understand CIDR notation? Most engineers don't. They just use default subnets and wonder why the network is slow.
What Engineers Usually Get Wrong
Most engineers think "subnetting is just dividing networks." But subnetting requires understanding binary math, subnet masks, and CIDR notation. You need to calculate network addresses, broadcast addresses, and usable host ranges. Understanding this helps you design efficient networks and troubleshoot connectivity issues.
Engineers also don't understand that subnetting affects routing. Routers need to know which subnets exist and how to route to them. If you subnet incorrectly, routing breaks. Understanding subnetting helps you configure routers and design network architectures.
How This Breaks Systems in the Real World
A service was using a default subnet (192.168.1.0/24) for all devices. With 500 devices, broadcasts flooded the network. Performance degraded. The fix? Subnet the network. Divide it into smaller subnets (e.g., 192.168.1.0/26 for 64 devices each). This reduces broadcast traffic and improves performance.
Another story: A service was subnetting but calculated subnet ranges incorrectly. Some devices were assigned IPs outside their subnet range. Routing failed. Devices couldn't communicate. The fix? Calculate subnet ranges correctly. Use subnet calculators or understand binary math. Always verify IP addresses are within their subnet range.
What is Subnetting?
Subnetting is the process of dividing a network into smaller logical networks called subnets. Each subnet has its own network address, broadcast address, and range of usable host addresses.
Benefits:
- Efficient IP usage: Allocate only needed addresses
- Network segmentation: Isolate traffic, improve security
- Reduced broadcast domains: Smaller broadcast domains improve performance
- Better organization: Logical grouping of devices
CIDR Notation
CIDR (Classless Inter-Domain Routing) uses slash notation to specify the network prefix:
192.168.1.0/24
- 192.168.1.0: Network address
- /24: Prefix length (24 bits for network, 8 bits for hosts)
- Subnet mask: 255.255.255.0
CIDR to Subnet Mask Conversion
| CIDR | Subnet Mask | Host Bits | Usable Hosts |
|---|---|---|---|
| /24 | 255.255.255.0 | 8 | 254 |
| /25 | 255.255.255.128 | 7 | 126 |
| /26 | 255.255.255.192 | 6 | 62 |
| /27 | 255.255.255.224 | 5 | 30 |
| /28 | 255.255.255.240 | 4 | 14 |
| /30 | 255.255.255.252 | 2 | 2 (point-to-point) |
Subnetting Process
Step 1: Determine Requirements
- Number of subnets needed
- Number of hosts per subnet
- Future growth requirements
Step 2: Calculate Subnet Mask
- Determine number of subnet bits needed
- Calculate new prefix length
- Verify host count meets requirements
Step 3: Calculate Subnet Addresses
- Network address (all host bits = 0)
- Broadcast address (all host bits = 1)
- Usable host range
Examples
Example 1: Subnet 192.168.1.0/24 into 4 Subnets
Original Network: 192.168.1.0/24
- Network bits: 24
- Host bits: 8
- Total addresses: 256
- Usable hosts: 254
Requirement: 4 subnets
- Need 2 additional bits (2^2 = 4 subnets)
- New prefix: /26 (24 + 2)
- Hosts per subnet: 2^(32-26) - 2 = 64 - 2 = 62
Subnets:
| Subnet | Network | Broadcast | Usable Range | Hosts |
|---|---|---|---|---|
| 1 | 192.168.1.0/26 | 192.168.1.63 | 192.168.1.1 - 192.168.1.62 | 62 |
| 2 | 192.168.1.64/26 | 192.168.1.127 | 192.168.1.65 - 192.168.1.126 | 62 |
| 3 | 192.168.1.128/26 | 192.168.1.191 | 192.168.1.129 - 192.168.1.190 | 62 |
| 4 | 192.168.1.192/26 | 192.168.1.255 | 192.168.1.193 - 192.168.1.254 | 62 |
Calculation:
Subnet size = 256 / 4 = 64 addresses per subnet
Subnet 1: 192.168.1.0 - 192.168.1.63
Subnet 2: 192.168.1.64 - 192.168.1.127
Subnet 3: 192.168.1.128 - 192.168.1.191
Subnet 4: 192.168.1.192 - 192.168.1.255
Example 2: Variable Length Subnetting (VLSM)
Network: 10.0.0.0/16 Requirements:
- Subnet A: 1000 hosts → /22 (1022 hosts)
- Subnet B: 500 hosts → /23 (510 hosts)
- Subnet C: 250 hosts → /24 (254 hosts)
- Subnet D: 100 hosts → /25 (126 hosts)
Allocation:
10.0.0.0/16
├── 10.0.0.0/22 (Subnet A: 1000 hosts)
│ └── 10.0.0.0 - 10.0.3.255
├── 10.0.4.0/23 (Subnet B: 500 hosts)
│ └── 10.0.4.0 - 10.0.5.255
├── 10.0.6.0/24 (Subnet C: 250 hosts)
│ └── 10.0.6.0 - 10.0.6.255
└── 10.0.7.0/25 (Subnet D: 100 hosts)
└── 10.0.7.0 - 10.0.7.127
Subnetting Calculator
1import { IPv4CidrRange } from 'ip-num';23interface SubnetInfo {4 subnet: number;5 network: string;6 broadcast: string;7 netmask: string;8 cidr: string;9 hosts: number;10 firstHost: string;11 lastHost: string;12}1314function calculateSubnet(baseNetwork: string, numSubnets: number): SubnetInfo[] {15 // Calculate subnets from base network16 const network = IPv4CidrRange.baseNetwork
Determine if IP is in Subnet
1import { IPv4, IPv4CidrRange } from 'ip-num';23function isIPInSubnet(ip: string, subnetCidr: string): boolean {4 // Check if IP address belongs to subnet5 const ipObj = IPv4.fromString(ip);6 const network = IPv4CidrRange.fromCidr(subnetCidr);78 return network.contains(ipObj);9}1011// Usage12console.log(isIPInSubnet("192.168.1.100", "192.168.1.0/24"));
Common Pitfalls
- Forgetting network and broadcast addresses: Usable hosts = 2^n - 2, not 2^n. Fix: Always subtract 2
- Incorrect subnet mask calculation: Wrong binary math. Fix: Use subnet calculators or verify with tools
- Overlapping subnets: Subnets that overlap cause routing issues. Fix: Plan subnets carefully, verify no overlap
- Not accounting for growth: Subnets too small for future needs. Fix: Plan for 20-30% growth
- Wasting IP addresses: Using /24 when /26 would suffice. Fix: Use VLSM for efficient allocation
- Confusing network bits and host bits: /24 means 24 network bits, not 24 host bits. Fix: Remember CIDR = network prefix length
- Not reserving addresses: Forgetting to reserve IPs for routers, gateways. Fix: Reserve first few IPs in each subnet
Interview Questions
Beginner
Q: What is subnetting and why is it used?
A:
Subnetting is dividing a network into smaller logical networks (subnets). Each subnet has its own network address, broadcast address, and range of usable host addresses.
Why used:
- Efficient IP allocation: Allocate only needed addresses, reduce waste
- Network segmentation: Isolate traffic, improve security
- Reduce broadcast domains: Smaller broadcast domains improve performance
- Better organization: Logical grouping of devices (departments, floors)
- Security: Isolate sensitive networks, control traffic flow
Example:
Original: 192.168.1.0/24 (254 hosts)
Subnetted: 4 subnets of /26 (62 hosts each)
- Sales: 192.168.1.0/26
- Engineering: 192.168.1.64/26
- Marketing: 192.168.1.128/26
- Admin: 192.168.1.192/26
Benefits:
- Better security (isolate departments)
- Reduced broadcast traffic
- Easier management
- Efficient IP usage
Intermediate
Q: Given the network 172.16.0.0/16, create 8 subnets. What are the network addresses, subnet masks, and usable host ranges?
A:
Original Network: 172.16.0.0/16
- Network bits: 16
- Host bits: 16
- Total addresses: 65,536
- Usable hosts: 65,534
Requirement: 8 subnets
- Need 3 additional bits (2^3 = 8 subnets)
- New prefix: /19 (16 + 3)
- Hosts per subnet: 2^(32-19) - 2 = 8,192 - 2 = 8,190
Calculation:
Subnet size = 65,536 / 8 = 8,192 addresses per subnet
Increment = 8,192
Subnets:
| Subnet | Network | Subnet Mask | Broadcast | Usable Range | Hosts |
|---|---|---|---|---|---|
| 1 | 172.16.0.0/19 | 255.255.224.0 | 172.16.31.255 | 172.16.0.1 - 172.16.31.254 | 8,190 |
| 2 | 172.16.32.0/19 | 255.255.224.0 | 172.16.63.255 | 172.16.32.1 - 172.16.63.254 | 8,190 |
| 3 | 172.16.64.0/19 | 255.255.224.0 | 172.16.95.255 | 172.16.64.1 - 172.16.95.254 | 8,190 |
| 4 | 172.16.96.0/19 | 255.255.224.0 | 172.16.127.255 | 172.16.96.1 - 172.16.127.254 | 8,190 |
| 5 | 172.16.128.0/19 | 255.255.224.0 | 172.16.159.255 | 172.16.128.1 - 172.16.159.254 | 8,190 |
| 6 | 172.16.160.0/19 | 255.255.224.0 | 172.16.191.255 | 172.16.160.1 - 172.16.191.254 | 8,190 |
| 7 | 172.16.192.0/19 | 255.255.224.0 | 172.16.223.255 | 172.16.192.1 - 172.16.223.254 | 8,190 |
| 8 | 172.16.224.0/19 | 255.255.224.0 | 172.16.255.255 | 172.16.224.1 - 172.16.255.254 | 8,190 |
Verification:
- All subnets are /19 (consistent)
- No overlapping addresses
- Total addresses: 8 × 8,192 = 65,536 ✓
- Usable hosts: 8 × 8,190 = 65,520
Senior
Q: Design a subnetting scheme for a cloud provider that needs to allocate subnets to customers dynamically. How do you handle variable subnet sizes, prevent IP exhaustion, and ensure efficient allocation?
A:
1class DynamicSubnetAllocator {2 private ipPool: IPPool;3 private allocations: Map<string, Allocation>;4 private subnetTree: SubnetTree; // Binary tree for efficient search56 constructor() {7 // Large IP pool (e.g., 10.0.0.0/8)8 this.ipPool = new IPPool("10.0.0.0/8");9 this.subnetTree = new SubnetTree();10 }1112 // 1. Variable Length Subnet Allocation (VLSM)13 async allocateSubnet(customerId: string, size: number): Allocation
Features:
- VLSM: Variable length subnetting for efficient allocation
- Smallest fit: Allocate smallest subnet that fits requirements
- Subnet splitting: Split larger subnets when needed
- Subnet merging: Merge adjacent subnets when deallocated (defragmentation)
- Efficient search: Binary tree for O(log n) subnet search
- Pool health: Monitor utilization, prevent exhaustion
- Growth buffer: Allocate 10% extra for future growth
-
IP Addressing (IPv4/IPv6) - Understanding IP addresses is prerequisite for subnetting and CIDR notation
-
NAT & PAT - NAT often works with subnets, understanding subnetting helps configure NAT properly
-
Routing Protocols (OSPF/BGP) - Routing protocols use subnet information to make routing decisions
-
OSI Model (7 Layers) - Subnetting operates at Layer 3 (Network), understanding the OSI model provides context
-
Firewall Rules & ACLs - Firewalls use subnet masks to define network boundaries, understanding subnetting helps configure firewall rules
-
Subnetting: Divides network into smaller logical networks for efficient IP allocation
-
CIDR notation: /n specifies network prefix length (e.g., /24 = 24 network bits)
-
Subnet calculation: Number of subnets = 2^(subnet_bits - network_bits), hosts = 2^(32 - prefix) - 2
-
Network and broadcast: Network address (all host bits 0), broadcast (all host bits 1) are not usable
-
VLSM: Variable Length Subnet Masking for efficient allocation of different sized subnets
-
Subnet planning: Account for growth, reserve IPs for routers/gateways, avoid overlapping
-
Efficient allocation: Use smallest subnet that fits, merge when deallocated, monitor pool health
-
Best practices: Plan carefully, verify no overlaps, use subnet calculators, document allocations
Key Takeaways
Subnetting: Divides network into smaller logical networks for efficient IP allocation
CIDR notation: /n specifies network prefix length (e.g., /24 = 24 network bits)
Subnet calculation: Number of subnets = 2^(subnet_bits - network_bits), hosts = 2^(32 - prefix) - 2
Network and broadcast: Network address (all host bits 0), broadcast (all host bits 1) are not usable
VLSM: Variable Length Subnet Masking for efficient allocation of different sized subnets
Subnet planning: Account for growth, reserve IPs for routers/gateways, avoid overlapping
Efficient allocation: Use smallest subnet that fits, merge when deallocated, monitor pool health
Best practices: Plan carefully, verify no overlaps, use subnet calculators, document allocations
Related Topics
IP Addressing (IPv4/IPv6)
Understanding IP addresses is prerequisite for subnetting and CIDR notation
NAT & PAT
NAT often works with subnets, understanding subnetting helps configure NAT properly
Routing Protocols (OSPF/BGP)
Routing protocols use subnet information to make routing decisions
OSI Model (7 Layers)
Subnetting operates at Layer 3 (Network), understanding the OSI model provides context
Firewall Rules & ACLs
Firewalls use subnet masks to define network boundaries, understanding subnetting helps configure firewall rules
What's next?