Subnetting and CIDR basics

This guide explains the fundamentals of the IP protocol, IPv4 and IPv6 addressing, and how to perform subnetting in both protocols.

The IP Protocol

IP (Internet Protocol) is the network layer protocol responsible for addressing and routing packets between devices. Every device on an IP network needs a unique address to communicate.

There are two versions in use:

  • IPv4: 32-bit addresses. Limited space of ~4.3 billion addresses.
  • IPv6: 128-bit addresses. Virtually unlimited space (~340 undecillion addresses).

IPv4

Structure of an IPv4 Address

An IPv4 address has 32 bits, represented as four octets in dotted decimal notation:

192.168.1.100

In binary (32 bits):
11000000.10101000.00000001.01100100

Each octet represents 8 bits and can have a value from 0 to 255.

Components of an IPv4 Address

An IP address is divided into two parts:

  • Network portion: Identifies the network to which the device belongs.
  • Host portion: Identifies the specific device within that network.

The subnet mask determines where the network portion ends and the host portion begins.

Subnet Mask

The subnet mask is a 32-bit number where:

  • Bits set to 1 represent the network portion.
  • Bits set to 0 represent the host portion.
IP Address:      192.168.1.100
Mask:            255.255.255.0

In binary:
IP:              11000000.10101000.00000001.01100100
Mask:            11111111.11111111.11111111.00000000
                 |------ Network (24 bits) --||Host|

Result:
Network address: 192.168.1.0
Host portion:    .100

CIDR Notation

CIDR (Classless Inter-Domain Routing) simplifies mask representation by indicating the number of network bits after a slash:

192.168.1.0/24

The /24 indicates that the first 24 bits are the network portion, equivalent to the mask 255.255.255.0.

IPv4 CIDR Reference Table

CIDR Mask Total Addresses Usable Hosts
/8 255.0.0.0 16,777,216 16,777,214
/12 255.240.0.0 1,048,576 1,048,574
/16 255.255.0.0 65,536 65,534
/20 255.255.240.0 4,096 4,094
/21 255.255.248.0 2,048 2,046
/22 255.255.252.0 1,024 1,022
/23 255.255.254.0 512 510
/24 255.255.255.0 256 254
/25 255.255.255.128 128 126
/26 255.255.255.192 64 62
/27 255.255.255.224 32 30
/28 255.255.255.240 16 14
/29 255.255.255.248 8 6
/30 255.255.255.252 4 2
/31 255.255.255.254 2 2*
/32 255.255.255.255 1 1

/31 is a special case (RFC 3021) used in point-to-point links where no network or broadcast address is needed.

Formula:

Usable hosts = 2^(32 - prefix) - 2

2 is subtracted because the first address is the network address and the last is the broadcast address.

Special Addresses in an IPv4 Subnet

Type Description Example (/24)
Network address First address, identifies the subnet. Not assignable. 192.168.1.0
First usable First address assignable to a host. 192.168.1.1
Last usable Last address assignable to a host. 192.168.1.254
Broadcast Last address, sends packets to all hosts in the subnet. Not assignable. 192.168.1.255

Private IPv4 Address Ranges

Defined in RFC 1918, these ranges are not routed on the Internet and are for internal use:

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

Other Reserved IPv4 Addresses

Range Purpose
0.0.0.0/8 Current network (source only)
127.0.0.0/8 Loopback (localhost)
169.254.0.0/16 Link-local (APIPA)
224.0.0.0/4 Multicast
240.0.0.0/4 Reserved for future use
255.255.255.255/32 Limited broadcast

Subnetting in IPv4

Subnetting is the process of dividing a network into smaller subnetworks. This allows:

  • Logical organization of the network.
  • Reduction of the broadcast domain.
  • Improved security through network segmentation.

How to Calculate Subnets

Required data:

  • Original network
  • Number of subnets needed or number of hosts per subnet

Method 1: By Number of Subnets

Example: Divide 192.168.1.0/24 into 4 subnets.

  1. Calculate bits needed for subnets:
    • 4 subnets = 2² → you need 2 bits
  2. New prefix: /24 + 2 = /26
  3. Size of each subnet: 2^(32-26) = 64 addresses
  4. Usable hosts per subnet: 64 - 2 = 62
Subnet Network Address Usable Range Broadcast
1 192.168.1.0/26 192.168.1.1 – 192.168.1.62 192.168.1.63
2 192.168.1.64/26 192.168.1.65 – 192.168.1.126 192.168.1.127
3 192.168.1.128/26 192.168.1.129 – 192.168.1.190 192.168.1.191
4 192.168.1.192/26 192.168.1.193 – 192.168.1.254 192.168.1.255

Method 2: By Number of Hosts

Example: You need subnets with at least 50 hosts each.

  1. Calculate host bits needed:
    • 50 hosts → you need 2^n ≥ 52 (50 + network + broadcast)
    • 2^6 = 64 ✓ → you need 6 host bits
  2. Prefix: 32 - 6 = /26
  3. Usable hosts: 64 - 2 = 62 ✓

VLSM (Variable Length Subnet Masking)

VLSM allows creating subnets of different sizes to optimize address usage.

Example: You have 10.0.0.0/24 and need:

  • Network A: 100 hosts
  • Network B: 50 hosts
  • Network C: 20 hosts
  • Network D: 2 hosts (point-to-point link)

Step 1: Sort from largest to smallest and calculate the required prefix.

Network Hosts Prefix Addresses
A 100 /25 (2^7=128, 126 usable) 128
B 50 /26 (2^6=64, 62 usable) 64
C 20 /27 (2^5=32, 30 usable) 32
D 2 /30 (2^2=4, 2 usable) 4

Step 2: Assign subnets sequentially.

Network Subnet Usable Range Broadcast
A 10.0.0.0/25 10.0.0.1 – 10.0.0.126 10.0.0.127
B 10.0.0.128/26 10.0.0.129 – 10.0.0.190 10.0.0.191
C 10.0.0.192/27 10.0.0.193 – 10.0.0.222 10.0.0.223
D 10.0.0.224/30 10.0.0.225 – 10.0.0.226 10.0.0.227

Remaining addresses: 10.0.0.228 – 10.0.0.255 (available for future use).

IPv6

Why IPv6?

IPv4 has approximately 4.3 billion addresses, insufficient for the current number of connected devices. IPv6 solves this with a vastly larger address space.

Structure of an IPv6 Address

An IPv6 address has 128 bits, represented as eight groups of four hexadecimal digits separated by colons:

2001:0db8:85a3:0000:0000:8a2e:0370:7334

IPv6 Address Simplification

Rule 1: Omit leading zeros in each group.

2001:0db8:0001:0000:0000:0000:0000:0001
2001:db8:1:0:0:0:0:1

Rule 2: Replace consecutive groups of zeros with :: (only once per address).

2001:db8:1:0:0:0:0:1
2001:db8:1::1

Examples:

Full:        2001:0db8:0000:0000:0000:0000:0000:0001
Simplified:  2001:db8::1

Full:        fe80:0000:0000:0000:0000:0000:0000:0001
Simplified:  fe80::1

Full:        0000:0000:0000:0000:0000:0000:0000:0001
Simplified:  ::1 (loopback)

Common IPv6 Prefixes

Prefix Type Description
::/128 Unspecified Unspecified address
::1/128 Loopback Equivalent to 127.0.0.1
fe80::/10 Link-local Communication on local link, not routable
fc00::/7 Unique local (ULA) Equivalent to IPv4 private addresses
2000::/3 Global unicast Public addresses routable on the Internet
ff00::/8 Multicast Multicast addresses

Key Differences Between IPv4 and IPv6

Characteristic IPv4 IPv6
Length 32 bits 128 bits
Notation Dotted decimal Colon hexadecimal
Addresses ~4.3 billion ~340 undecillion
Broadcast Yes No (uses multicast)
Configuration Manual or DHCP SLAAC, DHCPv6, or manual
IPsec Optional Built-in
Fragmentation Routers and hosts Source only

Subnetting in IPv6

Subnetting in IPv6 works the same as in IPv4, but with more bits available.

Typical structure of a global IPv6 address:

|  48 bits   | 16 bits |     64 bits      |
|  Routing   | Subnet  | Interface ID     |
|  Prefix    |   ID    |                  |
  • Routing prefix (48 bits): Assigned by the ISP.
  • Subnet ID (16 bits): To create up to 65,536 subnets.
  • Interface ID (64 bits): Identifies the host (auto-generated or manual).

How to Calculate IPv6 Subnets

Example: You have the prefix 2001:db8:abcd::/48 and need to create /64 subnets.

Bits available for subnets: 64 - 48 = 16 bits = 65,536 possible subnets.

Subnet Address
1 2001:db8:abcd:0000::/64 or 2001:db8:abcd::/64
2 2001:db8:abcd:0001::/64 or 2001:db8:abcd:1::/64
3 2001:db8:abcd:0002::/64 or 2001:db8:abcd:2::/64
... ...
65536 2001:db8:abcd:ffff::/64

Example with /56 prefix:

You have 2001:db8:abcd:ab00::/56 and want /64 subnets.

Bits available: 64 - 56 = 8 bits = 256 subnets.

Subnet Address
1 2001:db8:abcd:ab00::/64
2 2001:db8:abcd:ab01::/64
3 2001:db8:abcd:ab02::/64
... ...
256 2001:db8:abcd:abff::/64

IPv6 Subnet Formula

Number of subnets = 2^(new_prefix - original_prefix)

Example: From /48 to /64:

2^(64-48) = 2^16 = 65,536 subnets

Calculation Tools

ipcalc (IPv4)

# Ubuntu/Debian
sudo apt install ipcalc

# AlmaLinux/CentOS
sudo dnf install ipcalc

# Usage
ipcalc 192.168.1.0/24

sipcalc (IPv4 and IPv6)

# Ubuntu/Debian
sudo apt install sipcalc

# AlmaLinux/CentOS
sudo dnf install sipcalc

# IPv4 usage
sipcalc 192.168.1.0/24

# IPv6 usage
sipcalc 2001:db8::/32

References