What is a Subnet mask?

Given an IP address, how will the router identify what is the network ID of the network to which this IP address belongs? The router has a routing table for this. The subnet mask helps the router in doing so. In this blog, we will start from the basics and see how this is done by the router. You should know about what are the various classes of IP and how it is divided before reading this blog. So, let's get started.

Subnet(SubNetwork)

A subnet is a logical partition of an IP network into smaller networks.

Subnetting

Dividing the network into smaller networks or subnets is called subnetting.

Why are we dividing?

Suppose we take a network of class A. So, in class A, we have 2²⁴ hosts. So to manage such a large number of hosts is a tedious job. So if we divide this large network into the smaller network then maintaining each network would be easy.

How we do the subnetting?

Suppose we have a class C network having network ID as 201.10.1.0(range of class C 192–223). So the total number of hosts is 256(for class C host is defined by last octet i.e. 2⁸). But, the total usable host is 254. This is because the first IP address is for the network ID and the last IP address is Direct Broadcast Address(for sending any packet from one network to all other hosts of another network).

So, in subnetting we will divide these 254 hosts logically into two networks. A class C network has 24 bits for Network ID and the last 8 bits for the Host ID. We are going to borrow the left-most bit of the host ID and declare for identifying the subnet. If the leftmost bit of the host address is 0 then it is the 1st subnet network and if the leftmost bit is 1 then it would be 2nd subnet network. Using one bit we can divide it into 2 networks i.e. 2¹. If we want to divide it into four subnet networks then we need 2 bits(2²=4 networks).

The range of IP address which is in 1st subnet network is from 201.10.1.0 to 201.10.1.127. The range of IP address that lies in the 2nd subnet network is from 201.10.1.128 to 201.10.1.255.

In 1st subnet network(S1), we have a total of 128 hosts. But, the first IP address (201.10.1.0)is the network ID of the first subnet and the last IP address(201.10.1.127) is the Direct Broadcast Address of the first subnet. So, there are actually 126 usable hosts in the first subnet.

Similarly, in the 2nd subnet network(S2), we have a total of 128 hosts. But, the first IP address (201.10.1.128)is the network ID of the first subnet and the last IP address(201.10.1.255) is the Direct Broadcast Address of the first subnet. So, there are actually 126 usable hosts in the second subnet.

Overall, there are 252 usable hosts after subnetting. So, because of subnetting, there is a loss in the number of IP addresses.

The Network ID of the whole network is 201.10.1.0. Also, the network ID of S1 is 201.10.1.0. Which network are we referring to when the IP address is 201.10.1.0? It depends on where you are in the network. If we are inside the network we are referring to the subnet (S1)and if we are outside the network we are referring to the entire network.

We have an internal router which is connected to the two subnet network. Suppose a packet is arriving with a destination IP address of 201.10.1.130 at the internal router. Now, how will the router identify that this IP address will belong to which subnet network? Or, given an IP address how will the router identify the what is the network ID of the network to which this IP address belongs.

Here, by seeing the range of each subnet we can easily tell that it belongs to subnet S2. But, how the router will find it. For this, we have Subnet Mask.

Subnet Mask

A subnet mask is 32 bits numbers in which the series of 1’s represents the Network ID part and the Subnet ID part whereas the series of 0’s represents the Host ID part.

So, in the above example of the Class C IP address, we represent all the network ID bits by 1. We have reserved 1 bit of the host ID to represent the Subnet ID. So this leftmost bit of the last octet will also be represented by 1. Rest all the bits which represent the host are represented by 0.

Combining all these bits the subnet mask is represented as 11111111.11111111.11111111.10000000 i.e. 255.255.255.128.

If we know the subnet mask of the network then we can find the network of the IP address by bitwise ANDing the binary bits of the address.

Example: If a packet has arrived on the router having the IP address as 201.10.1.130. The router knows the subnet mask(255.255.255.128) of the network. First, convert both the address into its binary equivalent. The network to which this IP address belongs can be easily be found by bitwise ANDing the subnet mask and the incoming IP address.

Using the subnet mask we have found the network ID of the IP address and hence found that this IP address belongs to the subnet S2 network.

The router has the subnet mask stored in the routing table. The routing table contains the network ID, subnet mask and the corresponding interface to which it has to forward the packet if the network ID matches the table. In this case, the size of all the networks is the same. This is called Fixed Length Subnet Masking. If the result matches the network ID then it sends the packet to the corresponding interface. If it doesn't match the first entry then it is matched with the next entry. If it doesn't match any of the entry then the packet has to be sent out of the network i.e. default entry. The subnet mask for default entry is 0.0.0.0. The significance of all zeroes is that ANDing any entry with 0 produces the result as zero.

This is all about subnet masks. Hope you learned something new today.

Do share this blog with your friends to spread the knowledge. Visit our YouTube channel for more content. You can read more blogs from here.

Keep Learning :)

Team AfterAcademy!