How subnetting an IPv4 address works

In: 4

One way to explain it non-technically would be to compare it with a real address like `123 Exampelstreet`.

You try to write down the address in a paper form and you have only so many fields to write down all the letters and numbers.

If you have a street that has more houses on it or just housenumbers that are higher you may not have enough room room to write all the numbers down, but you can steal some space from the name of the street if it is small enough.

123 Exampelstreet

12345 Somestreet

Both fir in the same space but the way they use the space to write down house numbers and street name differs.

This is more or less the way suneettignworks.

If you have an address like

192.168.123.3

than the first three numbers are normally the network and the last number is the address in that network. `192.168.123` is the name of the street and `3` is the house number on that street.

Such a network has only room for 256 address and the first describes the network as a whole and the last is a way to say “all of them”. So really you only have 254 usable addresses in that network.

You can shrink or grow the network by taking part of the space used to describe the network away and give that space to write longer addresses in that network (or do the opposite fewer addresses and more networks.)

The way that works in practice can be a bit counterintuitive if you look at it from the way humans write things down, but if you want to go beyond ELI5:

if you write things down in Binary 192.168.123.3 becomes:

11000000.10101000.1111011.00000011

if you take the break between network and address one to the side it goes from

v

11000000.10101000.1111011 00000011

to

v

11000000.10101000.111101 1.00000011

or

v

11000000.10101000.1111011.0 0000011

One way to write down where the break between network and address in the network is is to count the bits.

Normally the address would have 24 bits be part of the network and 8 bits be part of the address and we write a little /24 at the end: 192.168.123.3/24

If we move the divider to the left we get a /23 and if we move it to the right we get a /25

In very short, everything is translated to binary bits consisting of 1s and 0s, and then the network address you have you run through a filter of another string of 1s and 0s where on each position the 1 lets the value through and 0 cancels it: putting 10101 through 11000 will give you 10000, and putting it through 00111 would have given you 00101. You can select some bits in your address this way to label the subnetwork, so for example 11000 filter would show you the network address, and 11*1*00 would show you the subnetwork. Kind of like adding to the address: first two 1s would filter value like Main Street 2, and the third 1: building B. That leaves you the two last digits for the apartment numbers in specific building.

Example:

You have an address made of 3 digits. You can filter it like this: 1.0.0 – so the only information you receive is the value of X, and you disregard the rest: x.0.0

The 1 will give you the street name, and it leaves you plenty of range in those two positions marked by 0s to full with apartment numbers. It suggests there’s only one, self-explanatory building assigned to this street address (your network).

But you can filter it like this: 1.1.0

Then the first 1 from the left still gives you the street name, but the second one you can use as building number, leaving the 0 as the range for apartment numbers. You can have two apartments labelled “1”, if the building number is different, the address remains unique, so you aren’t actually limiting your range like it may seem.

Whether you have apartment 1 and apartment 2 in one self-explanatory building under Main Street address, or you have apartment 1 in Main Street, building A, and apartment 1 in Main Street, building B, you still have 2 apartments. They’re just in different buildings (subnetworks).

You can filter it like this 0.0.1 to achieve only values of specific apartment numbers, without the context of the street name or building number, without knowing if there’s a building number that suggests there’s more than 1 building or not. The building is your subnet.

In IPv4 the addressing is done within 4 bytes, and each has 8 bits, that is slots for a 1 or a 0, so it looks like this:

10000000.00000000.00000000.00000000

The filtering allows you to extract specific information for such longs strings of 1s and 0s and is called masking.

In order to understand subnetting you have to understand what a subnet mask is. An IP address has two parts, a network address and the host address. Routers route based on the network address and the last router, the one that ‘owns’ the network, is the one that will ARP (address resolution protocol) to find the MAC address of the host at the host address in question.

But…how does a router know where the network address ends? By something called a subnet mask. Think of an IP address and a subnet mask as two 32 bit parallel arrays divided into 4ths. So you have 4 groups of 8 bits per array. An IP address is going to look like any other binary number, it might look like: 11000000.10101000.00000001.10101000 or in decimal, [192.168.1.55](https://192.168.1.55). The subnet mask is an array that, starting from position 0 (far left) will count out how many bits the network address is by filling in those bits with ones. So the subnet mask might be 11111111.11111111.11111111.00000000 or decimal [255.255.255.0](https://255.255.255.0). That tells us the network address is 192.168.1 and the host address is anything between 1-254. 255 is unusable for reasons that aren’t important to just understand subnetting. The subnet mask is merely a measuring stick.

Say you have been given a /24 network [192.168.1.0](https://192.168.1.0) and you want to *subnet* it. What do you do? You add a bit to the subnet mask. Now your subnet is 11111111.11111111.11111111.10000000 or [255.255.255.128](https://255.255.255.128). You have now divided your network [192.168.](https://192.168.0.1/24)1.0/24 into [192.168.1.0](https://192.168.1.0) AND [192.168.1.128](https://192.168.1.128). You now have two different network addresses i.e. two different networks. You subdivided one larger network into two smaller ones.

This is what confuses people is that when you *subnet* a network, the bit you steal changes your network address and you get a smaller network, but the rest of your range doesn’t just poof into nothingness. The more bits you steal from the host addresses, the more networks you create, but they get smaller in successive half minus two increments. You can do the opposite and take bits away from the subnet mask and you create fewer networks with larger host ranges in increments of double minus two.