IPv4 - subnet mask

A subnet mask is a 32-bit number used to distinguish the network and host parts of an IPv4 address. It works in conjunction with an IP address to identify which portion of the address is dedicated to the network and which portion is dedicated to the host.

In an IP address and its associated subnet mask, the network bits are represented with '1's and the host bits are represented with '0's. The format is binary, but it's commonly written in dotted decimal format. For example, the subnet mask most commonly used in small networks is typically 255.255.255.0. In binary, that would be represented like so:

11111111.11111111.11111111.00000000

Thus, this subnet mask shows that the first three octets are network bits and the final octet is for host bits.

The subnet mask can also be represented in slash prefix format, which is simply a forward slash "/" followed by the number of ones in the subnet mask. The above subnet mask would be represented like so: /24 since it has 24 ones.

The subnet mask is crucial when a packet of information is being sent over a network. The sending machine uses the subnet mask to determine whether the destination IP address is on the same local subnet or whether the packet needs to be sent to the default gateway to reach a different network. If the destination IP address shares the same network bits as the source IP address (as determined by the subnet mask), then it's on the same network; otherwise, the packet needs to be forwarded to the router.

Examples

For example, let's say Host A with address 10.10.10.56/24 wants to send a packet to Host B with an address of 10.10.10.12/24. Let's take a look at these addresses in their binary formats. Note that the first 24 bits of both addresses which are the network portions as defined by their subnet masks, have been made bold:

10.10.10.56 = 00001010.00001010.00001010.00111000 10.10.10.12 = 00001010.00001010.00001010.00001100

Are these network portions (bolded bits) the same for both addresses? Yes, therefore, the destination IP address is on the same subnet, and the packet can be sent directly to the destination.

Let's take a look at another example. Let's say Host A with address 10.10.10.56/24 wants to send a packet to Host C with an address of 10.10.11.22/24. Let's take a look at these addresses in their binary formats. Again, the first 24 bits of both addresses which are the network portions as defined by their subnet masks, have been made bold:

10.10.10.56 = 00001010.00001010.00001010.00111000 10.10.11.22 = 00001010.00001010.0000101<font style="color:green" "bold">1.00010110

Note here that the network portions (bolded) are not the same, therefore the destination address is on a different subnet, therefore, the packet must be sent to the default gateway to be routed to that different subnet.

The corresponding entity for IPv6 is known as the prefix length.

Links:

https://networklessons.com/subnetting/what-is-subnetting

https://networklessons.com/subnetting/internet-protocol

https://forum.networklessons.com/t/ipv4-packet-header/1271/65?u=lagapides