Tutorials

How to calculate subnet mask (definitive guide to subnetting)

Table of contents:

Anonim

The topic we are dealing with today is not for everyone, since if we intend to create a good guide on networks, it is essential to have an article explaining how to calculate the subnet mask, a technique called subnetting. With it, IT administrators are able to design the network and subnet structure anywhere.

Index of contents

To do this we will have to know very well what a netmask is, the IP classes and how to transform IP addresses from decimal to binary, although for this we already have an article we did a while ago.

For now we are going to focus on calculating the netmask on IPv4 addresses, since IPv6 is not yet implemented enough to put it into practice, perhaps we will do so in a later article. Without further ado, let's get to the task.

IPv4 address and IP protocol

Let's start at the beginning, a decimal numerical set IP address that identifies logically, uniquely and unrepeatably and according to a hierarchy, a network interface. IPv4 addresses are created using a 32-bit address (32 ones and zeros in binary) arranged in 4 octets (groups of 8 bits) separated by dots. For a more comfortable representation we always use decimal notation, this being directly what we see in the hosts and network equipment.

The IP address serves the addressing system according to the IP or Internet Protocol. IP operates at the network layer of the OSI model, being a non-connection-oriented protocol, so data exchange can be done without prior agreement between receiver and transmitter. This means that the data packet will search the fastest path on the network until it reaches the destination, hopping from router to router.

This protocol was implemented in 1981, in it the frame or data packet has a header, called an IP header. In it, among other things, the IP addresses of the destination and the origin are stored, so that the router knows where to send the packets in each case. But in addition, IP addresses store information about the identification of the network where they operate and even its size and the distinction between different networks. This is done thanks to the netmask and the network IP.

Representation and range

An IP address will then have this nomenclature:

Because each octet has a binary number of 8 zeros and ones, translating this to decimal notation we can create numbers ranging from 0 to 255.

We will not explain in this article how to convert from decimal to binary and vice versa, you will find this here:

Definitive guide on how to make conversions between numbering systems

Then we can never have an IP address with numbers less than 0 or greater than 255. When 255 is reached, the next number will be 0 again, and the next octet will be the one digit up to start counting. It is exactly like the minute hand of a watch.

How networks are created

We know what an IP address is, how it is represented and what it is for, but we must know some special IPs to know how to calculate the subnet mask.

Netmask

The netmask is an IP address that defines the scope or extent of a network. With it we will be able to know the number of subnets that we can create and the number of hosts (computers) that we can connect to it.

So the netmask has the same format as the IP address but is always distinguished by having the octets that delimit the network part filled with ones and the host part filled with zeros like this:

This means that we cannot arbitrarily give IP addresses to fill a network with hosts, but we must respect the network part and the hosts part. We will always work with the host part once we calculate the network part and assign an IP to each subnet.

Network IP address

We also have an IP address that is responsible for identifying the network to which the devices belong. Let's understand that in every network or subnet there is an identifying IP address that all hosts must have in common to denote their membership in it.

This address is characterized by having the common network part and the hosts part always at 0, in this way:

We will be able to 0 the octets of the host part that the network mask of the previous section has indicated to us. In this case it would be 2, while the other 2 would be for the network part, being a reserved IP.

Broadcast address

The broadcast address is just the opposite of the network address, in it we set to 1 all the bits of the octets that address hosts.

With this address a router can send a message to all the hosts connected to the network or subnet regardless of their IP address. The ARP protocol is used for this, for example to assign addresses, or to send status messages. So it is another reserved IP.

Host IP address

And finally we have the host IP address, in which the network part will always remain invariant and it will be the host part that will change on each host. In the example that we are taking would be this range:

We could then address 2 16 -2 hosts, that is , 65, 534 computers subtracting the two addresses for network and broadcast.

IP classes

Until now it has been simple, right? We already know that certain IP addresses are reserved for network, broadcast, and mask, but we have not yet seen the IP classes. effectively these addresses are divided into families or classes, to distinguish the purposes for which they will be used in each case.

With IP classes we are delimiting the range of values that this can take on the network part, the number of networks that can be created with them and the number of hosts that can be addressed. In total we have 5 IP classes defined by the IETF (Internet Engineering Task Force):

Mind you, we are not yet talking about calculating subnet mask, but about the ability to create networks. This is when we will see subnetting and its details.

  • Class A Class B Class C Class D Class E

Case A IPs are used to create very large networks, for example the Internet network and the allocation of public IPs to our routers. Although we can really have any of the other IPs of class B or C, for example I have one of class B. Everything will depend on the IPs that the ISP provider has contracted, something that we will explain just below. In class A we have a class identifier bit, so we can only address 128 networks and not 256 as would be expected.

It is very important to know that in this class there is an IP range reserved for Loopback, being from 127.0.0.0 to 127.255.255.255. Loopback is used to assign IP to the host itself internally, our team internally has an IP 127.0.0.1 or "localhost" with which it checks that it is capable of sending and receiving packets. So these addresses we will not be able to use them in principle.

Class B IPs are used for medium networks, for example in the range of a city, this time having two octets to create networks and another two to address hosts. Class B is defined with two network bits.

Class C IPs are the best known, since practically every user with home internet has a router that assigns a class C IP to their internal network. It is oriented to small networks, leaving 1 single octet for hosts and 3 for network. Make an ipconfig to your PC and sure that your IP is class C. In this case , 3 network bits are taken to define the class.

Class D is used for multicast networks, where routers send packets to all connected hosts. So all traffic that enters such a network will be replicated to all hosts. Not applicable for networking.

Finally class E is the last remaining range and is only used for networking for research purposes.

Something quite important regarding this topic is that currently the assignment of IP addresses in networks meets the principle of (CIDR) Classless Inter-Domain Routing or Classless Inter-Domain Routing. This means that IPs are assigned regardless of the size of the network, so we can have a public IP of class A, B or C. So what is all this for? Well, to understand how subnets are created correctly.

What is subnetting or subnetting

We get closer to calculating subnet mask, eye, not network. The subnetting technique consists of dividing the networks into different smaller networks or subnets. In this way a computer or network administrator can divide the internal network of a large building into smaller subnets.

With this we can assign different functions, with different routers and for example implement an Active Directory that only affects one subnet. Or differentiate and isolate a certain number of hosts from the rest of the network in a subnet. It is extremely useful in the field of networks, since each subnet works independently of the other.

Router work is also easier with subnets, as it eliminates congestion in data exchange. And finally, for the administration, it is much easier to correct faults and perform maintenance.

We are going to do it with the IPv4 address, although it is also possible to make subnets with IPv6, having no less than 128 bits to address hosts and networks.

Advantages and disadvantages of subnetting

For this technique, it is certainly necessary to be very clear about the IP address concepts, the classes that exist and everything that we have explained above. To this we add the need to know how to go from binary to decimal and vice versa, so if we intend to do the process manually, it can take a long time.

Advantage:

  • Isolations in network segments Packet routing in independent logical networks Design of subnets to suit the client and flexibility Better administration and localization of errors Greater security by isolating sensitive equipment

Disadvantages:

  • By dividing the IP by classes and hops many IP addresses are wasted Relatively tedious process if it is done by hand Its network structure changes it would have to be recalculated from the beginning If you do not understand it, you may suspend the subject of networks

Subnetting technique: calculate subnet mask and IP addressing

Fortunately, the subnetting process deals with a series of simple formulas to remember and to apply and we have things clear. So let's look at it in steps.

1. Number of subnets and quick notation

The notation with which we will find a subnet calculation problem will be the following:

This means that the network IP is 129.11.0.0 with 16 bits reserved for network (2 octets). We will never find a class B IP with an identifier less than 16, like the other classes, for example:

But if we can find superior identifiers until we reach 31, that is, we would take absolutely all the remaining bits except the last one to create subnets. The last one would not be taken because it will be necessary to leave something to address hosts, right?

Being the subnet mask:

In this way we are taking 16 fixed bits for network, another two extras for subnet and the rest for hosts. This means that the capacity of hosts is now reduced to 2 14 -2 = 16382 to the benefit of subnet capacity with the possibility of doing 2 2 = 4.

Let's look at it in a generic way in a table:

2. Calculate subnet and network mask

Taking into account the subnet limit that we have depending on the IP classes, we are going to present the example step by step to see how it would be solved.

In it we intend to use our Class B IP 129.11.0.0 to create 40 subnets in one large building. Could we have done it with a class C? of course, and also with a class A.

127.11.0.0/16 + 40 subnets

Being a class B we would have a netmask:

The second question to solve will be: How many bits do I need to create 40 subnets (C) in this network? We will know this by going from decimal to binary:

We need 6 extra bits to create the 40 subnets, so the subnet mask would be:

3. Calculate number of hosts per subnet and network hop

Now it is time to know the number of computers that we can address in each subnet. We have already seen that needing 6 bits for subnets decreases the space for hosts. We only have 10 bits left for them m = 10 where we must download the network IP and broadcast IP.

What if each subnet should have 2000 hosts what would we do? Well, obviously upload to a class A IP to get more bits from hosts.

Now it is time to calculate the network hop, this is what is intended to assign a number to the IP for each subnet that is created respecting the bits for hosts and the bits for subnet. We must simply subtract the subnet value obtained in the mask from the maximum value of the octet, that is:

We need these jumps in case each subnet is filled with its maximum host capacity, so we must respect these jumps to ensure the scalability of the network. In this way we will avoid having to restructure in case it increases with the future.

4. We just need to assign IP to our subnets

With everything we have calculated before, we already have everything ready to create our subnets, let's see the first 5 as they would be. We would continue to subnet 40, and we would still have plenty of room to get to 64 subnets with the 6 bits.

To apply the subnet IP we must take into account that the 10 host bits must be at 0 and that the calculated subnet jump is 4 in 4. Therefore, we have those jumps in the 3rd octet and therefore the last octet is 0, how good network IP it is. We can fill this entire column directly.

The first host IP is simply calculated by adding 1 to the subnet IP, this has no secrets. We can fill this entire column directly.

Now the most natural thing would be to place the broadcast IP, since it is only a matter of subtracting 1 from the next subnet IP. For example, the previous IP of 127.11.4.0 is 127.11.3.255 so we would continue with all of them. With the first column filled in, it's easy to get this one out.

Finally we will calculate the last host IP by subtracting 1 from the broadcast IP. This column will be filled in the last one in a simple way if we already have the broadcast addresses made.

Conclusions about subnetting

The process of calculating the subnet mask is quite simple if we are clear about the concepts of subnet, network IP, netmask and subnet and the broadcast address. In addition, with a couple of very simple formulas we can easily calculate the capacity for subnets of an IP, whatever the class, and the host capacity depending on the networks we need.

Obviously if we do this by hand and we do not have much practice doing decimal to binary conversions it can take a little longer, especially if we are studying this for a career networking or vocational degree course.

This same procedure will be carried out with class A and C IPs exactly as the example with class B. We only have to take into account the range of addresses to take and their identifier, the rest is practically automatic.

And if instead of giving us the IP and class , they simply give us the number of subnets and number of hosts, we will be the ones to decide the class, making the corresponding conversions to binary and using the formulas so as not to fall short in the forecasts.

Without further ado, we leave you with some links of interest that cover other network concepts in greater detail:

How did your body look with our tutorial on how to calculate the subnet mask ? We hope that everything is clear, otherwise there you have the comment box to ask us any questions or if you see any typo.

Tutorials

Editor's choice

Back to top button