
Subnet mask
For every IP address, there's an accompanying subnet (work) mask. This address is used to define the following:
- The subnet mask is used to indicate the network and host portion of an IP address
- The subnet mask is used to determine the number of available IP addresses on a network
- If two or more devices, such as computers, are communicating over multiple networks, the subnet mask of each device determines whether a computer should send the packet to the default gateway/router
There are three default classes of the subnet mask:
- Class A: 255.0.0.0
- Class B: 255.255.0.0
- Class C: 255.255.255.0
Each IPv4 address class has their own default subnet mask. An IP address such as 10.10.10.1, which is a private Class A address, will use the subnet mask of 255.0.0.0. Another example we can use is the address 191.5.4.6, which is a public Class B address. This address will use a default subnet mask of 255.255.0.0 and so on.
In some of the earlier sections of this chapter, you will have noticed after some IPv4 addresses that there's a forward slash (/) with a number next to it such as /8, /16, and /24. This is a shorthand method of representing a subnet mask. This is known as a network prefix. How does /8, /16 or even /24 represent an entire subnet mask? To explain this further, I'll use some examples:
- First, let's convert a Class A subnet mask:
- Decimal: 255.0.0.0
- Binary: 11111111.00000000.00000000.00000000
Here, we can see that there are eight 1s in the subnet mask. From this denotation, we get /8.
- Second, we'll convert a Class B subnet mask:
- Decimal: 255.255.0.0
- Binary: 11111111.11111111.00000000.00000000
Here, we can see that there are sixteen 1s in the subnet mask. From this denotation, we get /16.
- Finally, we'll convert a Class C subnet mask:
- Decimal: 255.255.255.0
- Binary: 11111111.11111111.11111111.00000000
Now, we have twenty-four 1s, which means that we have a /24 subnet mask.
Furthermore, an IP address can have a custom subnet mask apart from the default address classes. Let's assume that we have an address of 192.168.100.1 with a customized subnet mask of 255.255.255.224.
Converting this subnet mask is much simpler than you think. First, we know that when all 8-bits are all 1s within an octet, we get the maximum value of 255. Therefore, the first three octets are 24-bits. Now we need to convert the last octet into binary:
The following is another graphical representation:
The last octet is 11100000. Now, putting it all together, we get 11111111.11111111.11111111.11100000, which is the binary equivalent of 255.255.255.224. As we can see, there are twenty-seven 1s in binary notation, and so we get a /27 network prefix. The new representation of the IP address is 192.168.100.1/27.