Subnetting
Learn how subnetting works
What is subnetting and how is a subnet mask interpreted?
Subnetting is a technique used to divide a network into smaller, more manageable subnetworks. This is achieved using network masks, which allow you to define which bit of the IP address corresponds to the network and which to the host. To interpret a netmask, you must first identify the bits that have the value “1.” These bits represent the portion of the IP address that corresponds to the network. For example, a mask “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 hosts.
255.255.255.0
- - - - - - - - - - - - - - - - - - - - - - - - - - -
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
- - - - - - - - - - - - - - - - - - - - - - - - - - -
| 1 1 1 1 1 1 1 1 | 255.
| 1 1 1 1 1 1 1 1 | 255.
| 1 1 1 1 1 1 1 1 | 255.
| 0 0 0 0 0 0 0 0 | 0
- - - - - - - - - - - - - - - - - - - - - - - - - - -
CIDR and total host calculation
Now, when we talk about CIDR (Classless Inter-Domain Routing), what we are referring to is a method of assigning IP addresses that is more efficient and flexible than using fixed IP network classes. With CIDR, an IP address is presented 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 with a netmask of 255.255.255.0 would be written as 192.168.1.1/24 using CIDR notation.
The network mask is represented in prefix notation, which indicates the number of bits equal to “1”.
To calculate the netmask from prefix notation, you must write “1” in the first few bits of a 32-bit IP address and “0” in the remaining bits. For example, the /24 netmask would be calculated as 11111111.11111111.11111111.00000000 in binary, which is equivalent to 255.255.255.0 in decimal.
255.255.255.0 - - - - -> /24
- - - - - - - - - - - - - - - - - - - - - - - - - - -
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
- - - - - - - - - - - - - - - - - - - - - - - - - - -
| 1 1 1 1 1 1 1 1 | 255. → 8 bits in 1 |1+2+4+8+16+32+64+128 = 255
| 1 1 1 1 1 1 1 1 | 255. → 8 bits in 1 |1+2+4+8+16+32+64+128 = 255
| 1 1 1 1 1 1 1 1 | 255. → 8 bits in 1 |1+2+4+8+16+32+64+128 = 255
| 0 0 0 0 0 0 0 0 | 0 → 0 bits in 1 |0 = 0
- - - - - - - - - - - - 8+8+8 - -> 24 bits in 1 = /24
Total number of hosts
To calculate the total number of hosts we must count the bits that are equal to zero and the number 2 is raised to that total number and with this operation, the total number of hosts is obtained according to the network mask.
255.255.255.0 - - - - -> 256
- - - - - - - - - - - - - - - - - - - - - - - - - - -
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
- - - - - - - - - - - - - - - - - - - - - - - - - - -
| 1 1 1 1 1 1 1 1 | 255. → 0 bits in 0
| 1 1 1 1 1 1 1 1 | 255. → 0 bits in 0
| 1 1 1 1 1 1 1 1 | 255. → 0 bits in 0
| 0 0 0 0 0 0 0 0 | 0 → 8 bits in 0
- - - - - - - - - - - - - - - - - Total - -> 2⁸ = 256
IP address classes
- Class A: Class A networks use a default subnet mask of 255.0.0.0 and range from 0 to 127 in their first octet. The address 10.52.36.11, for example, is a class A IP address since its first octet is 10.
- Class B: Class B networks use a default subnet mask of 255.255.0.0 and range from 128 to 191 in their first octet. The address 172.16.52.63, for example, is a class B IP address since its first octet is between 128 and 191.
- Class C: Class C networks use a default subnet mask of 255.255.255.0 and range from 192 to 223 in their first octet. The address 192.168.123.132, for example, is a class C IP address since its first octet is between 192 and 223.
It is important to note that in addition to these three types of netmasks, there are also custom netmasks that can be used to create subnets of different sizes within a network.
With CIDR, IP addresses can be assigned more accurately, reducing the number of wasted IP addresses and making network management easier.
The number following the base IP address in CIDR notation is called the prefix or prefix length and represents the number of bits in the netmask that are set to “1”.
For example, an IP address with the /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 hosts. Example:
Calculate the IP address 192.168.1.0/26, its network mask, the total number of hosts to distribute, the network identifier and the broadcast address.
Below is a step-by-step description of how we calculate each section:
1. Calculating the network mask
The IP address they gave us is 192.168.1.0/26, which means that the first 26 bits of the IP address correspond to the network and the last 6 bits correspond to the hosts.
To calculate the netmask, we need to set the first 26 bits to 1 and the last 6 bits to 0. In binary, this looks like this:
- 11111111.11111111.11111111.11000000
Each octet of the netmask is made up of 8 bits. The value of each octet is determined by converting the 8 bits to decimal. In this case, the first 24 bits are all ones, meaning that the decimal value of each of these octets is 255. The last octet has the last 6 bits set to 0, meaning that its decimal value is 192.
Therefore, the netmask for this IP address is 255.255.255.192.
2. Calculation of the total number of hosts to distribute
In this case, the remaining 6 bits can be used to represent the host part. In a 26-bit netmask, the remaining 6 bits represent 2⁶ - 2 = 62 hosts available to assign.
The maximum number of available hosts is calculated as 2^(n) — 2, where n is the number of bits used to represent the host portion of the netmask.
3. Network ID calculation
The broadcast address is the network address used to send packets to all hosts on the subnet. To calculate it, we need to know the network ID and the number of hosts available in the subnet.
In the example we are working on, we have already calculated the network ID as 192.168.1.0. The number of available hosts is calculated as 2^(n) — 2, where n is the number of bits used to represent the host portion of the netmask. In this case, n is equal to 6, since there are 6 bits available for the host part.
To calculate the broadcast address, we need to set all bits in the host part of the IP address to “1”. In this case, the IP address is 192.168.1.0 and is converted to binary as 11000000.10101000.00000001.00000000.
To find the Broadcast address, we fill the part corresponding to the host bits with ones, that is, the last 6 bits: 11000000.10101000.00000001.00111111 (IP address with host bits set to “1”)
Then we convert this binary value back to decimal and get the broadcast address: 192.168.1.63. This is the address where packets will be sent to reach all hosts on the subnet.
I hope this has been helpful to you.