Subnet Shenanigans

Subnet Shenanigans

I was recently asked about subnets and masks and what they are. I'm not sure I did a spectacular job explaining them on the spot. It did get me thinking that it would make for a good blog post and would be a good excuse to refresh myself on the topic.

Let's start with the obvious question of "what is a subnet?". A subnet is really just the subdivision of a larger network into smaller networks. Pretty straight forward but now you might be thinking "Great! Why do we need them?". Excellent question.

IPv4 or Internet Protocol Version 4 has about 4.3 billion unique IP addresses. 1.0.0.0 to 255.255.255.255. These addresses are how computers find each other, like a mailing address. 4 billion might sound like a lot and it certainly sounded like a lot to the creators of the internet (TLDR; it wasn't). What they did was break that range of IP address up into Classes A, B, C, D, E. Class A networks are the largest, able to handle over 16,000,000 hosts per network. Class B are smaller, each class B network can have over 65,000 hosts. Finally we have the good ol' Class C network which can have 256 hosts. Classes D and E are all specialized and we're really just going to ignore them here. The issue here is that 16 million hosts, or even 65,000 hosts, is an awful lot for a single network. If a company is handed a Class A or B network and only uses a few thousand IP addresses then that's literally thousands or millions of IP addresses wasted. As a result the internet quickly started to run low on available IP addresses. Two things saved IPv4; subnetting is the first, NAT is the second (and deserves its own blog post). The size of the networks also made them inefficient and difficult to manage. Class A and Class B networks are massive, and a packet sent from one device will have a harder time finding the correct destination device because of the sheer size of the network. There are other problems but, we'll stick with these two for simplicity.

Lets have a look at an IP address.

1.0.0.0

This is a Class A network and it is made up of four "octets" starting at 1.0.0.0 and ending at 1.255.255.255. The "1" in this example is the Network ID and it never changes. The Network ID defines the specific network the devices are on. The last three octets make up the Host ID and these specify each device on the network. To make this network of over 16 million possible host IP addresses more manageable and responsive, the owner of the network might take 1.5.28.0 to 1.5.28.255 and make that its own network, or "subnet". Here the first three octets will never change, so when a packet is sent from a device with the IP of 1.5.28.12 to a device with the IP of 1.5.28.46, well now there are only 256 possible addresses so the network much smaller and can work more efficiently.

Wait you say, "how can the router know which set of numbers are the Network ID and which make up the Host ID?" That's where the Subnet Mask comes in. It clarifies for the switch or router which is which. In our example network of 1.5.28.X the subnet mask would be "255.255.255.0". For each octet of the IP address that has a corresponding octet of "255" in the subnet mask, the router knows that it's part of the Network ID and that the Host ID will correspond to the subnet mask value(s) of "0". In our earlier "1.0.0.0" example the subnet mask would be "255.0.0.0".

There is plenty more to this. Why do these numbers range from 0 to 255, why are they called octets, what is NAT, and we haven't even mentioned CIDR. These all all good questions and worthy of seperate blog posts.

For now though, the TLDR;

Subnets are smaller networks made out of a larger one. Subnet masks allow routers and switches to identify which part of an IP address belongs to the Network ID and which part belongs to the Host ID. Take for example the IP address of "192.168.1.1" with a subnet mask of "255.255.255.0". Each octet of the subnet mask with a value of "255" corresponds to an octet of the same position in the IP address and indicate it's part of the Network ID, "192.168.1". An octet of "0" in the subnet mask will indicate that the corresponding octet of the IP address is part of the Host ID. In this case, the final octet of the IP address, "1".

References

What is a Subnet - This is an excellent article from Cloudflare regarding subnets.

Introduction to Subnetting - Another solid article about subnets, this one by GeeksforGeeks.

You Suck at Subnetting - This is a phenomenal series by Network Chuck that covers pretty much everything and with better detail and examples. I highly recommend watching the whole playlist.