A Brief Explanation of Subnet Masks

If you have ever searched for the IP Address of your computer, you have probably used one of the following commands: ipconfig, ifconfig, or ip addr. If you are really fancy and decided to use Windows PowerShell, you might even have used get-netipconfiguration. Whichever one you used on whichever is your preferred operating system, you will have received three pieces of information: the IPv4 Address, the Subnet Mask, and the Default Gateway. Yes, you might have gotten much more than this… but these three bits will be the most common between all environments.

Actual IP Addresses blurred for security

The IPv4 Address is obvious. Every computer that is connected to a network (really, every Network Interface Card, or NIC) will have its own unique address that is assigned to it, either manually or automatically. Most home users will never think about this because their Internet Service Provider (ISP) provided them a wireless router, but that device is really a Multi-Function Appliance, or MFA. Among myriad other things, it functions as a DHCP Server which provides the IP address to every connected device. It will usually look like 192.168.1.101 – four octets (numbers between 0-255, or 28). Even if you do not understand it, you probably know that computers actually speak in binary numbers (1s and 0s). This number, therefore, would actually be, to the computer: 1100 0000 1010 1000 0000 0001 0110 0101. If you did not understand before, this is why we do the conversion from binary to decimal for you.

The Default Gateway is the connection to the outside world. Remember that the connected devices connected to your router (on the inside ports of your network) make up a LAN, or Local Area Network. These devices can communicate with each other using the Physical Address of the NIC, rather than the assigned address. While the IP Address might look familiar to you if you have sat on the phone with your ISP troubleshooting connectivity issues, the Physical Address will look foreign to most of us, looking like: B4-B6-2D-35-CE-24. This address will never change; each NIC is assigned a unique Physical Address (or MAC Address) by the manufacturer. This is what devices on a LAN use to communicate with one another. It is only when a device tries to communicate with another device outside of the LAN that they send the traffic to the Default Gateway, a router which is connected to your LAN on one end, and the outside world on the other. The outside world can be the Internet… or it could be another LAN within your organization.

The Subnet Mask is the piece that most of us do not understand. It is what a device uses to determine what other IP Addresses are on the same subnet (on the same LAN), and which are not. It is also a set of four octets (like the IP Address), but unlike the IP Address, the ones will always be contiguous from the left to the right; once you have a zero, the rest of the bits will be zeros. Why:

The Subnet Mask is divided into two parts: the Network ID and the Host ID. In a four-octet number, there are 32 bits, and as such we can have anywhere from 1-32 ones. The number of zeros is that number subtracted from 32. If our Network ID is 24 bits (24 ones) then our Host ID is 8 bits (8 zeros). Let’s break down an octet quickly by showing you what each bit represents in each octet:

1

1

1

1

1

1

1

1

255

27

26

25

24

23

22

21

20

28

When each bit is a 1, we add the values of each and come to 255 (28, but we count from 0-255 rather than 1-256).

1

1

0

0

0

0

0

0

192

27

26

25

24

23

22

21

20

27+26

I could continue, but there are a great number of tutorials on binary math for you to explore online that explain this, so let’s move on.

We determined earlier that the Subnet Mask must consist of contiguous 1s from the left, and that the zeros (from the right) represent the number of possible hosts on a network.

When my Subnet Mask is 255.255.255.0 then the number of possible networks is represented by the 1s, and the number of possible hosts on each of those networks is represented by the 0s. That means that there are 16,777,214 possible networks (224-2, as the first octet cannot be a 0 or a 255). We determine that number by taking the number of 1 bits in the mask (1111 1111 1111 1111 1111 1111 0000 0000) which is 24, and calculating 2 to that power. Don’t worry, you do not have to do this in your head.

On the same Subnet Mask, we then count the number of 0 bits in the mask, which we can either get by counting the zeros, or by subtracting the number of 1s from 32. Either way, we get 8. The number of possible hosts on a network with this mask is therefore determined by calculating 28-2, which means it is 254. Note that the –2 represents the Network ID and the Broadcast ID, which I will talk about in another article.

Let’s create a simple example, using IP addressing that will be familiar to anyone who has sat any Introduction to IT class:

IP Address: 192.168.1.107

Subnet Mask: 255.255.255.0

Default Gateway: 192.168.1.1

Our Subnet Mask tells us that the first 24 bits of the IP Address is the Network ID, which means that any other computer with an IP Address starting with 192.168.1.x will be on the same network, and that there are 254 possible hosts on the network. Therefore, the Network ID of the network that this device is connected to is 192.168.1.0/24. The Host ID is 107.

If this device has to send information to a device with the IP Address 192.168.1.185 then it knows it is on the same network. I will not go into how it uses its ARP table to determine the physical address of the destination, but it sends the packet to the network switch and says, ‘send this packet to the NIC in the Packet Header.’ Great.

If the same computer needs to send information to a device with the IP Address of 10.201.100.47 then it immediately knows it is not on the same network. The Network ID is not in the same subnet. Rather than sending the packet to the switch to forward to another computer, it sends that packet to the switch with the instructions to forward it to the port connected to the Default Gateway, a router which will forward the packet on, either to the connected subnet, or, based on the Routing Information Protocol (RIP), to another router that will know where to direct the traffic. If the destination needs to be routed over the Internet, it will use Network Address Translation (NAT) to ensure that the non-routable source IP Address is converted into a routable packet.

Of course, most of us, in our own homes connected to our own LANs, will never need 254 possible addresses. How many do we need? Add the number of devices you have (including computers, tablets, smartphones, smart TVs) and then add a few for however many guests you might invite to connect their phones to your Wi-Fi network. If you decide that you never need more than six devices connected, then you might opt for a Subnet Mask of 255.255.255.248. Despite this, most SOHO (Small Office, Home Office) routers are configured with a Subnet Mask that allows up to 254 devices to connect, but this is only for the sake of simplicity. If they took security into account, they would make this a consideration.

On the other end of the spectrum, you might have a small or mid-sized business that needs more than 254 devices on a LAN. If we only used classful subnets, then the jump from 255.255.255.0 (254 devices) to 255.255.0.0 (16,384 devices) would not be the best practice, so we can do a little bit of fancy subnetting, using the table attached below. Using that table, once you know how many devices you need, you can easily determine what the best Subnet Mask for your organization will be. I will write another article about calculating all of the requirements for those subnets later.

Conclusion

The Subnet Mask is what a NIC uses to determine if another device is on it’s same LAN, or if it needs to send the packets to a router. It is divided into the Network ID and the Host ID as listed above. It may seem complicated to many, but it is a lesson that every IT Professional must eventually learn if they are to understand networking. Of course, an IT Professional can go through their entire careers not having to work with these numbers. Most larger companies will have different people or teams to manage the networking and the servers and the desktops, so a desktop and server specialist will simply connect their devices to the network that someone else has configured for them. They will still need to be able to troubleshoot network connectivity issues and recognize when a device’s networking is not properly configured, but they will never have to go into the calculations of it all. There is nothing wrong with that.

With that said, understanding how subnetting works has to be a prime consideration for even entry level network technicians. Not only will they need a solid grasp of these concepts (and both the numbering systems and the math behind them) to do their jobs, but they will also likely not even be able to pass their basic certification exams without them.

A Simple Table of Subnet Masks

/CIDR Subnet Mask Total Addresses Useable Addresses
32 255.255.255.255 1 1
31 255.255.255.254 2 2
30 255.255.255.252 4 2
29 255.255.255.248 8 6
28 255.255.255.240 16 14
27 255.255.255.224 32 30
26 255.255.255.192 64 62
25 255.255.255.128 128 126
24 255.255.255.0 256 254
23 255.255.254.0 512 510
22 255.255.252.0 1024 1022
21 255.255.248.0 2048 2046
20 255.255.240.0 4096 4094
19 255.255.224.0 8192 8190
18 255.255.192.0 16384 16382
17 255.255.128.0 32768 32766
16 255.255.0 65536 65534
15 255.254.0 131072 131070
14 255.252.0 262144 262142
13 255.248.0 524288 524286
12 255.240.0 1048576 1048574
11 255.224.0 2097152 2097150
10 255.192.0 4194304 4194302
9 255.128.0 8388608 8388606
8 255.0.0.0 16777216 16777214
7 254.0.0.0 33554432 33554430
6 252.0.0.0 67108864 67108862
5 248.0.0.0 134217728 134217726
4 240.0.0.0 268435456 268435454
3 224.0.0.0 536870912 536870910
2 192.0.0.0 1073741824 1073741822
1 128.0.0.0 2147483648 2147483646
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: