Subnetting¶
Subnetting is a technique used to divide an IP network into smaller, more manageable subnets. This is achieved through the use of netmasks**, which allow you to define which bits of the IP address correspond to the** network and which bits correspond to the hosts**.
To interpret a netmask, the bits that are in "1" must be identified. These bits represent the portion of the IP address that corresponds to the network. For example, a netmask of 255.255.255.0 indicates that the first three octets of the IP address correspond to the network, while the last octet is used to identify the hosts.
Now, when we talk about CIDR (acronym for Classless Inter-Domain Routing), what we are referring to is a more efficient and flexible method of assigning IP addresses than the use of fixed IP network classes. With CIDR, an IP address is represented by a base IP address and a netmask, which are written together separated by a slash (/).
For example, the IP address 192.168.1.1.1 with a netmask of 255.255.255.0 would be written as 192.168.1.1/24.
The netmask is represented in prefix notation, which indicates the number of bits that are in "1" in the mask. In this case, the netmask 255.255.255.0 has 24 bits in "1" (the first three octets), so its prefix notation is /24.
To calculate the netmask from a prefix notation, you must write the "1" bits in the first bits of a 32-bit IP address and the "0" bits in the remaining bits. For example, the /24 netmask would be calculated as 111111111111.111111111111.111111111111.00000000 in binary, which is equivalent to 255.255.255.0 in decimal.
It is important to note that in addition to these three types of network masks, there are also custom network masks that can be used to create subnets of different sizes within a network.
As mentioned in the previous class description of CIDRs (Classless Inter-Domain Routing), this is a method of IP address assignment that allows an IP address to be split into a part that identifies the network and another part that identifies the host. This is achieved through the use of a network mask, which is represented in CIDR notation as a base IP address followed by a number indicating the amount of bits that correspond to the network.
With CIDR, IP addresses can be assigned more precisely, reducing the number of wasted IP addresses and facilitating network administration.
The number following the base IP address in CIDR notation is called prefix or prefix length, and represents the number of bits in the netmask that are in "1".
For example, an IP address with a /24 prefix indicates that the first 24 bits of the IP address correspond to the network, while the remaining 8 bits are used to identify the hosts.
To calculate the number of hosts available on a CIDR network, one must count the number of "0" bits in the netmask and raise 2 to the power of that number. This is because each "0" bit in the netmask represents a bit that can be used to identify a host.
For example, a netmask of 255.255.255.0 (/24) has 8 bits in "0", which means that there are 2^8 = 256 IP addresses available to hosts on that network.
Some practical examples of CIDR are depicted below:
- An IP address with a /28 prefix (255.255.255.240) allows up to 16 IP addresses for hosts (2^4), since the first 28 bits correspond to the network.
- An IP address with a prefix of /26 (255.255.255.255.192) allows up to 64 IP addresses for hosts (2^6), as the first 26 bits correspond to the network.
- An IP address with a prefix of /22 (255.255.255.252.0) allows up to 1024 IP addresses for the hosts (2^10), as the first 22 bits correspond to the network.
for practice: