A subnet mask is a 32-bit number that divides an IP address into network and host portions. It uses contiguous 1s for the network portion and 0s for the host portion.
Subnet Mask Formats:
- CIDR Notation (Classless Inter-Domain Routing):
- Format: Slash followed by prefix length (e.g., /24)
- Prefix length = number of network bits (0-32)
- Most concise format, standard in modern networking
- Example: 192.168.1.0/24 means first 24 bits are network
- Decimal (Dotted Quad):
- Format: Four decimal octets (e.g., 255.255.255.0)
- Each octet is 0-255
- Traditional format used in older configurations
- Example: 255.255.255.0 = /24
- Hexadecimal:
- Format: 0x followed by 8 hex digits (e.g., 0xFFFFFF00)
- Used in some programming contexts and packet captures
- Compact representation of 32-bit value
- Example: 0xFFFFFF00 = 255.255.255.0 = /24
- Binary:
- Format: 32 binary digits, often dotted by octet
- Shows actual bit pattern
- Useful for understanding subnetting calculations
- Example: 11111111.11111111.11111111.00000000 = /24
Common Subnet Masks:
- /8 (255.0.0.0): Class A, 16,777,214 usable hosts
- Example: 10.0.0.0/8 (entire Class A private range)
- /16 (255.255.0.0): Class B, 65,534 usable hosts
- /24 (255.255.255.0): Class C, 254 usable hosts
- Example: 192.168.1.0/24 (common home/office network)
- Most common subnet size for small networks
- /30 (255.255.255.252): Point-to-point links, 2 usable hosts
- Used for router-to-router connections
- Example: 10.0.0.0/30 (hosts: .1 and .2, network: .0, broadcast: .3)
- /31 (255.255.255.254): RFC 3021 point-to-point, 2 usable hosts
- No network or broadcast address (both IPs usable)
- Used exclusively for point-to-point links
- /32 (255.255.255.255): Single host, 1 address
- Used for loopback interfaces and static routes
- Example: 127.0.0.1/32 (localhost)
Wildcard Masks:
Calculating Usable Addresses:
- Total addresses: 2^(host bits)
- Example: /24 has 8 host bits, so 2^8 = 256 total addresses
- Usable addresses: Total - 2 (network and broadcast)
- Example: /24 = 256 - 2 = 254 usable
- Network address: First address (all host bits 0)
- Broadcast address: Last address (all host bits 1)
- Exception /31: RFC 3021 allows both IPs as usable (no network/broadcast)
- Exception /32: Single host mask, 1 usable address
Subnetting Example (VLSM):
Given network 192.168.1.0/24, create 4 subnets:
- Calculate new prefix: /24 + 2 bits = /26 (2^2 = 4 subnets)
- Each subnet has 2^6 = 64 addresses (62 usable)
- Subnets:
- 192.168.1.0/26 (0-63, usable: 1-62)
- 192.168.1.64/26 (64-127, usable: 65-126)
- 192.168.1.128/26 (128-191, usable: 129-190)
- 192.168.1.192/26 (192-255, usable: 193-254)
Subnet Mask Rules:
- Contiguous 1s: Network bits must be contiguous (no gaps)
- Valid: 11111111.11111111.11111111.00000000 (/24)
- Invalid: 11111111.11110000.11111111.00000000 (gap in 1s)
- Network bits first: All network bits come before host bits
- Valid prefixes: /0 (0.0.0.0) to /32 (255.255.255.255)
Common Conversion Shortcuts:
- /8 = 255.0.0.0 (Class A default)
- /16 = 255.255.0.0 (Class B default)
- /24 = 255.255.255.0 (Class C default)
- /25 = 255.255.255.128 (128 = 2^7)
- /26 = 255.255.255.192 (192 = 128 + 64)
- /27 = 255.255.255.224 (224 = 128 + 64 + 32)
- /28 = 255.255.255.240 (240 = 128 + 64 + 32 + 16)
- /29 = 255.255.255.248 (248 = 128 + 64 + 32 + 16 + 8)
- /30 = 255.255.255.252 (252 = 128 + 64 + 32 + 16 + 8 + 4)
Binary Conversion Method:
- Write /24 as 24 ones followed by 8 zeros: 11111111.11111111.11111111.00000000
- Convert each octet to decimal: 255.255.255.0
- Reverse process: Convert decimal to binary, count leading 1s
Use in Routing:
- Route aggregation: Summarizing multiple subnets with shorter prefix
- Example: 10.1.0.0/24, 10.1.1.0/24, 10.1.2.0/24, 10.1.3.0/24 to 10.1.0.0/22
- Longest prefix match: Routers use most specific (longest) subnet mask
Best Practices:
- Use CIDR notation (/24) for modern networks (cleaner, more standard)
- Use wildcard masks for Cisco ACLs (required by IOS syntax)
- Document subnet masks in network diagrams
- Plan IP address space with VLSM (Variable Length Subnet Masking) for efficiency
- Reserve room for growth when subnetting