Understanding CIDR - A Guide to Classless Inter-Domain Routing

Foreword

Classless Inter-Domain Routing (CIDR) revolutionized IP address allocation and routing on the internet. By moving away from the rigid class-based system (Classes A, B, and C), CIDR introduced a more flexible and efficient method for managing IP spaces. This article delves into what CIDR is, how it works, and how to compute CIDR blocks for network planning.

What is CIDR?

CIDR stands for Classless Inter-Domain Routing. It’s a method used for allocating IP addresses and routing Internet Protocol packets. CIDR allows for variable-length subnet masking which enables a more efficient allocation of IP addresses. It’s designed to replace the older system based on classes (A, B, C) to improve address space allocation and enhance routing scalability on the internet.

Key Concepts of CIDR

  • IP Address: A unique numerical label assigned to devices connected to a network that uses the Internet Protocol for communication. (e.g.192.168.1.0)
  • Subnet Mask: Defines a range of IP addresses considered to be in the same network segment. (e.g. mask 255.255.255.0 is represented as /24 in CIDR)
  • CIDR Notation: A compact representation of an IP address and its associated routing prefix in a format like 192.168.1.0/24.

How CIDR Works

CIDR introduces flexibility in the allocation of IP addresses by varying the length of the subnet portion of the address. Unlike the fixed subnet masks of the class-based system, CIDR notation allows the network boundary to be set anywhere, enabling both smaller and larger blocks of addresses to be allocated as needed.

Computing CIDR

To compute a CIDR block, you need the starting IP address and the size of the network (i.e., how many addresses you need).

Example

Suppose you have an IP address of 192.168.1.0 and need to support 254 devices. You would start with the base IP address 192.168.1.0 and then use a subnet mask that supports 254 devices. The CIDR block 192.168.1.0/24 uses a subnet mask of 255.255.255.0, allowing for 256 addresses total (the last part bits), which after accounting for the network and broadcast addresses, leaves 254 usable addresses for devices.

Calculating Subnets and Hosts

  • Subnets: The number of available subnets can be calculated based on the number of bits borrowed for subnetting, with more bits allowing for more subnets.
  • Hosts: The formula

$$ 2^{(32−\text{subnet mask length})}−2 $$

calculates the number of usable host addresses in a subnet, subtracting 2 for the network and broadcast addresses.

Extra

Network Address

The network address represents the start of an IP address range assigned to a network. It is used to identify the network itself.

The network address is calculated by applying the subnet mask to any IP address within the network, resulting in the lowest possible address in the range. In binary terms, the network address is formed by performing a bitwise AND operation between any IP address in the network and the subnet mask. This address is not assignable to any individual device within the network because it is used to identify the network as a whole.

Broadcast Address

The broadcast address is the last address in a network range and is used to send data to all devices within that network.

When a packet is sent to the broadcast address, it is delivered to all hosts in the network rather than a single recipient. The broadcast address is determined by inverting the subnet mask (turning all subnet mask 0 bits into 1s) and performing a bitwise OR operation with the network address. Like the network address, the broadcast address is not assignable to any device, as its purpose is to facilitate the broadcasting of messages to all devices on the network.

Subnet Mask

A subnet mask is a 32-bit number that masks an IP address and divides the IP address into network address and host address. Subnet masks are made up of two parts:

  1. The network part, which identifies a particular network and is represented by the binary 1s in the mask.
  2. The host part, which identifies a specific device (host) on that network and is represented by the binary 0s in the mask.

For example, in the subnet mask 255.255.255.0 or in CIDR notation /24, the first 24 bits are the network part (all 1s in binary), and the last 8 bits are the host part (all 0s in binary). This means any IP address with the same first 24 bits belongs to the same network, and the last 8 bits can vary to represent different devices within that network.

Detailed Example

Let’s consider the network 192.168.1.0/24:

  • IP Address Range: 192.168.1.0 to 192.168.1.255
  • Subnet Mask: 255.255.255.0 or /24 in CIDR notation
  • Network Address: 192.168.1.0 (the first address in the range, represents the network itself)
  • Broadcast Address: 192.168.1.255 (the last address in the range, used to broadcast to all devices on the network)

In this case, the subnet mask /24 indicates that the first 24 bits (the first three octets) are the network part, and the last 8 bits (the last octet) are for hosts. This allows for up to 256 IP addresses (from 192.168.1.0 to 192.168.1.255), but since the first and last addresses are reserved for the network and broadcast addresses, respectively, it leaves 254 addresses available for devices.

Resources

  • Wikipedia
  • 什麼是 CIDR?(AWS)