Subnet Mask What is it and what is it used for?
Let us say that a housing project company that builds houses and apartments got permission from government to build 350 houses. The housing project company decided to build 50 premium stand alone house, 100 Row House or TownHouses and 200 Apartments. When the builder divide the entire land area he got to build the houses he dont need to allocate the 350 plot/site each for premium and Row/Townhouse and Apartments right? If he does that then it will be waste of plots as he is planning only 50 premium and 300 will be extra wasted plots for premium. Similarly for the Row/TownHouse and apartments he needs to check how many plot required for each and accordingly assign the plots.
There is a similar thing in the network designing and planning. Instead of apartments and standalone houses in our networking world it is going to be network and hosts. Usually the devices will be grouped based on the department and the kind of task that the devices does for ease of management and operations for the administrator. Example all the printers in one network, fax machine in another network, HR applications in another network and so on. And in each of these network the number of device will differ, like the houses in plot.
The mechanism that allows this grouping of devices by making a set of of host and networks is called Mask, which we can loosely relate to the plot/site no. for the houses. The network mask helps us understand quite a lot of information. When there is a mask attached to the IP address it tells us which network that IP address belongs to. Mask also tells us how many number of hosts are possible in that network. We can also understand the IP address that we can use to broadcast the message to all the host in the same network. Mask also helps the network administrator to optimally use the IP Network that was assigned to him.
Let us understand more about subnet masks and how they are represented. Mask has 4 octets similar to the IP address. Subnet Mask can be represented in three formats
- Binary Format
- Dotted Decimal Format
- Prefix Format
It is important to understand all the three formats and to convert between each of the formats. Let us understand each one in detai
- Binary Format
This is the format that will be used by routers and computers. In binary there are only 2 bits that you can use 1 and 0. As mentioned earlier the mask is used to represent the network side and the host side. In binary format the mask is represented with continuous 1s and then follows the continuous zeros. The 1s bit represents the network and 0s represents the host side. Let us take an example to understand this better
As the examples above the bits 1 will be continuous and then 0s will follow. The 1s and 0s are never interleaved. For example, a binary format of “111 0011. 0000 0000. 0000 0000. 0000 0000” for the subnet mask is invalid. So is “1111 1111. 0110 0000. 0000 0000. 0000 0000”. The rule for mask is simple it should be continuous 1s followed by continuous 0s.
2. Dotted Decimal Format
As part of the previous exercise we just saw the dotted decimal format. Convert each binary octet to decimal and you get the dotted decimal format.
Please note the each octet carries this value, which means we need to convert each octet like this. The rule of binary should be still met which means all continuous 1s followed by continuous 0s. Some of the possible and valid subnet mask are listed below
255.0.0.0, 255.255.0.0, 255.192.0.0, 192.0.0.0
But there below set of mask are not valid
255.192.255.0, 192.255.0.248, because when you convert values to binary you can see that there is no continuous 1s
3. Prefix Format
The last method of representing the IP address mask is prefix format represented with a “/” and a number after “/” Examples for prefix format is /8, /16, /20
The number coming after “/” is total number of continuous 1s in the binary format of subnet mask. Example
1111 1111.0000 0000. 0000 0000. 0000 0000 has total number of eight 1s in the binary subnet mask and hence the prefix format is going to be /8.
Another example is 1111 1111.1111 0000. 000 0000. 0000 0000 has total of twelve 1s and hence the mask is going to be /12 and so on.
So the prefix can take value starting from /1 to all the way /32. Please note if we have all 0 then the prefix is going to be /0. We will cover more about that when we talk about default route/static route.
To convert from Binary to Dotted Decimal format is something we already so, summarized below:-
- Convert each of the octet from binary to decimal
- Write the decimal value with “.” between each octet
To convert from Dotted Decimal to binary convert each of the octet to binary with “.” between each octet.
To convert from Binary to prefix is pretty simple, just count the number of 1’s and the put / and the number after that /. 1111 1111.1111 1111.0000 0000. 0000 0000 has total of 16 hence /16 is the prefix format
Vice versa to convert from prefix format to binary, put as many 1s as the prefix says and rest all zeros. Example if there is a prefix /16 then put all the first 16 bits to 1 as below
1111 1111. 1111 1111.0000 0000.0000 0000
Finally if you want to convert the prefix to dotted decimal, the easiest way is to convert to binary first as it is easy to convert to binary and from binary to can convert to either easily. Another way to convert from prefix to dotted decimal is to device the prefix with 8. The quotient tells how many octets has 255 and if there is any reminder remember the table below
For example if you get a prefix /18 and you want to convert that to dotted decimal, then divide /19 by 8.
19/8 = 2 as quotient and 3 as the reminder. Since we got 2 as quotient the first 2 octet is going to be 255. For the third octet refer the table and if possible byheart the table; and the equivalent is 224. Hence the dotted decimal format is going to be 255.255.224.0
The same table can be used to convert the dotted decimal to prefix. First 2 octets are 255 hence 8+8 and for the third octet refer the table and you get the answer 3; hence 8+8+3 = 19. /19 is the prefix.
As we told at the starting the mask is going to tell the network to which IP address belongs to. So now if we combined the IP address and mask it will look like below
192.168.1.1/24 in prefix format
192.168.1.1 255.255.255.0 in dotted decimal format
192.168.1.1 1111 1111.1111 1111.1111 1111.0000 0000 in binary format
All these formats tell that the first 3 octets are part of network because 1s represent network and the last octet is host as all zeros represents host. So the IP address 192.168.1.1/24 belongs to network 192.168.1.0 which we call as the network ID and .1 which is last octet value represent the host in that network. To identify the broadcast address for that network make all the 0s(in binary) of host side into 1(in binary). Then you will get the value as 192.168.1.255 which is the broadcast address for the network.
Now let us take a real life scenario for a network admin who is designing his network. Assume you are appointed as a network admin for a Small Office Home Office(SOHO) which has 10 employees, 2 printers and 3 servers where there is some application running. We need to design this network and identify the subnet mask that we need to use for this network. So since we need very less amount of host, the maximum being 10, we should use the class C address as per the blog post on IP Addressing. So let us take 192.168.1.0/24(ie masl 255.255.255.0) Class C network for this exercise.