I recently wrote up some information on subnetting, and figured I would share it. For a more in depth overview I would highly recommend reading A Dive Into Subnetting, which @_py wrote. Hopefully this quick and dirty reference helps in it’s own way!
Subnetting is the product of logically dividing up a network up into smaller, modular networks. This has many benefits, such as speed increases, easier administration, and increased options for maintaining security. While this post will not go into detail on the benefits or implementation of subnetworks, it is important to be able to calculate subnetwork ranges, network addresses, and broadcast addresses. In other words, “What is the network for for IP XXX.XXX.XXX.XXX/XX? What is the IP range of this network?”, and so on.
Finding the Subnet MaskWhen running commands such as ipconfig or ifconfig you may be given your IP and subnet mask; but let's assume that we are working with a classless IP displayed as 192.168.0.23/21 and that you have to calculate the subnet mask yourself. The portion preceding the "/" would be the IP, and the portion afterwards indicates the amount of subnet mask bits. Like IPs, subnet masks are comprised of four binary octets. The bits are typically assigned left to right, so we would have a subnet mask of 11111111.11111111.11111000.00000000, or 255.255.248.000 in decimal format.
Simple Calculations with the Binary MethodNow that we have our subnet mask, lets calculate the network address, which is the very first IP in a subnet range. This is done with a bitwise AND operation, so the IP needs to be represented as binary as well, 11000000.10101000.00000000.00010111. Here we perform the AND operation and find our network address:
11000000.10101000.00000000.00010111 = 192.168.000.023 (Address) 11111111.11111111.11111000.00000000 = 255.255.248.000 (Mask) -----------------------------------AND--------------- 11000000.10101000.00000000.00000000 = 192.168.000.000 (Network Address)
This means that, for IP 192.168.0.23/21, our host 0.23 belongs to network 192.168.0.0. Now, lets look at the range of this network. We calculate this by once again looking at the bits assigned in our network mask octets, and keeping in mind the binary values of each. More specifically, we look at whichever our rightmost octet is which contains 1 values, and then the value of our rightmost bit.
The rightmost bit corresponds to 8, which indicates our range. In other words, the subnet range that 192.168.0.23/21 belongs to is 192.168.0.0 through 192.168.7.255, and 192.168.8.0 is the adjacent subnet. Now we also know our broadcast address, which is always the last IP address in the range (192.168.7.255). Since the first and last IP addresses are reserved for the network and broadcast addresses, the host address range (all valid addressable IPs) would be 192.168.0.1 through 192.168.7.254, giving a total of 2,046 valid host addresses (calculated as (8 x 256) - 2). We can calculate the amount of valid host addresses as rightmost bit value, 8, multiplied by 256 per octet to the right, subtracting 2 lastly.
Even Simpler Calculations with the Magic Number MethodNow that we have examined the binary method, let's take a look at a simpler way of calculating the same subnet properties. Assuming we have our subnet mask, perhaps using a lookup chart such as the cheatsheet at the bottom, we instead want to find our leftmost number that isn't 255. Let's assume we are working with an IP of 126.96.36.199 and a subnet mask of 255.255.255.240. So, for 255.255.255.240, we have the number 240. To get our "magic number", we subtract 240 from 256, resulting in 16. Our magic number is calculated from the 4th binary octet, so we know our subnets calculate in intervals of 16 from the 4th octet. In other words, we know we would have network addresses of 188.8.131.52, followed by 184.108.40.206, followed by 220.127.116.11, and so on. Using this logic, we know that address 18.104.22.168 is within the network of 22.214.171.124, because the host range of the subnet extends from 126.96.36.199 through 188.8.131.52. We now also know that our broadcast address is 184.108.40.206, and that the next subnet range will begin at 220.127.116.11, giving a valid host range of 14. We can calculate the amount of valid host addresses as our magic number, 16, multiplied by 256 per octet to the right, subtracting 2 lastly.
|Mask Bit Suffix||Subnet Mask||Host Addresses per Subnet|
Note that /31 and /32 are not frequently used, and would only be considered “valid” in hyper-specific circumstances which fall outside the scope of this post.