Understanding IP Addressing and Subnetting

Understanding IP Addressing and Subnetting

So today is your friend's birthday, and you need to call her to wish her a happy birthday. You go through your contact list and scroll to your friend's name to call her. Your friend's name has a phone number attached to it that helps you connect with her. IP addresses are the phone numbers of devices. A unique identifier and a way for other devices to communicate with your device over the internet or in your local area network. Your friend's phone number is unique to her, no one else has the same phone number as your friend. Every IP address is unique within its network.

What are IP Addresses?

IP (Internet Protocol) addresses are unique identifiers for devices over the Internet or a local network. An IP address can either be IPv4 or IPv6. IPv4 (IP version 4) is a 32-bit numerical address made up of 4 octets, where each octet, containing 8 bits, is separated by a dot. On the other hand, IPv6 (IP version 6) is a 128-bit hexadecimal alphanumerical address. IPV4 has a total number of 4,294,967,296 IP addresses (232). At the time IPV4 was created, no one imagined the internet would be as widely adopted as it is today. The IP addresses were not handled properly, which led to wastage, and eventually, IPv4 addresses will run out (kind of like fossil fuels). This led to the creation of IPv6, which has a total number of about 3.4 X 1038 IP addresses (2128) that will never run out (like renewable energy). IP addresses are not random; they are mathematically created and allocated by IANA (Internet Assigned Numbers Authority). The rest of the article will refer to IPv4 addresses in terms of IP addresses.

octets.jpg

Figure 1

Each octet in the IP address contains 8 bits. Each bit can either be 0 or 1 (machine-readable format). Each binary digit is a bit. This means that the lowest possible binary combination in that octet is 00000000 and the highest possible binary combination 11111111 when converted to decimal form, equals 0 and 255 (in human-readable format). Therefore, the numbers in each octet can only be between 0 and 255.

Every IP address is usually divided into two parts: the network ID and the host ID. The network id represents the network you are on. Every device connected to a particular network has the same network id, and the network id remains unchanged. The host ID represents individual devices connected to that network. For example, Nigerian landline numbers have area codes prefixed (01 for Lagos, 09 for Abuja, etc.). All of the landlines in a particular area like Lagos must have the prefix 01 and at least seven other numbers within a range of values. The area codes, in this case, are the network ids, they remain unchanged for every number in the Lagos area (network), and the host ids are the remaining numbers within the range of values that are allocated to different individuals (devices) in the Lagos area (network). This brings us to the classification of IP addresses. IP address classes help define which parts of the addresses are network ids and which are host ids.

Classes of IP Addresses

There are five classes of IP addresses, classes A, B, C, D, and E. Classes D and E are reserved as multicast and experimental, respectively.

classes_of_ip_addresses.jpg

Figure 2

The figure above shows classes A, B, and C, their range of IP addresses, the network id (in bold), the subnet mask, the number of networks, and the number of hosts in each class.

IP addresses are grouped into their various classes based on the first octet in binary form. In class A, the first octet values are a range between 1 and 126 when converted to binary, they are 00000001 and 01111110. All the values within this range start with 0, hence they are grouped in class A. 0.0.0.0 and 127.x.x.x are special addresses. 0.0.0.0 is a non-routable meta-address used to designate an invalid, unknown, or non-applicable target. 127.x.x.x is reserved for loopback addresses.
In class B, the range 128 - 191 in binary form is 10000000 - 10111111. All values within this range begin with 10, hence they are grouped in class B.
In class C, the range 192 - 223 in binary is 11000000 - 11011111. All values start with 110 and are grouped into class C. The same goes for classes D and E.

The network id of each class is shown in bold. For class A IP addresses, the network ids are the values in the first octet, and the second, third, and fourth octets make up the host ids; for class B IP addresses, the network ids are the first and second octets, and the third and fourth make up the host ids; for class C IP addresses, the network ids are the first, second, and third octets, the fourth octet makes up the host id.

Class A has up to 126 networks, which have up to 1.6 million hosts (or nodes) each. They are usually assigned to large organizations.
Class B has 16,384 networks with 65,534 hosts each. They are usually assigned to medium-sized organizations.
Class C has 2,097,152 networks with 254 hosts each. They are assigned to smaller organizations.

Public and Private IP addresses

Public IP addresses

Public IP addresses are used to directly access the internet. They are assigned to your network router by your internet service provider. Public IPs are unique.

public_and_private_ip.jpg

Figure 3

Private IP addresses

Private IP addresses cannot access the internet, they are used locally. Your network router assigns private IPs to the devices connected to that network. These devices use their private IPs to connect to the router, which in turn uses the public IP assigned by the ISPs to connect to the internet. Private IPs are not unique because they are only used locally (several devices connected to different networks have the same private IP, this is not a problem because they can only be used within their local network). However, they are unique locally; no two devices connected to your local network have the same IP address.
There are dedicated ranges of private IP addresses.

Class A => 10.0.0.0 - 10.255.255.255

CLass B => 172.16.0.0 - 172.31.255.255

Class C => 192.168.0.0 - 192.168.255.255

Subnetting

A single network can be divided into one or more smaller logical sub-networks (subnets) through a process called subnetting. Subnetting helps increase performance by reducing network traffic caused by broadcast traffic. When a network router receives data packets from another network, it will sort and route those packets by subnet so that the packets do not take an inefficient route to their destination within the network. Routers use subnet masks to sort data into subnetworks within the network. For example, in a class A network, there could be up to 16 million connected devices. It may take some time for the data to reach the appropriate device, but subnetting reduces that by narrowing it down to the range of devices within that subnetwork. So, instead of the data having to go through an inefficient route before it gets to its destination, the router hands over the data to the subnet router, which then delivers the data to its destination faster. Subnetting can also be used in network management when subnetworks are administratively controlled by different departments in larger organizations. For example, the HR department could be on a different subnetwork from the Finance department. However, they are still part of the larger network of the organization.
Subnets were initially designed as a solution to the IP address shortage because they prevent large numbers of IP addresses from remaining unused.

Subnet masks

Subnet masks (or netmasks) are 32-bit numerical addresses, divided into four octets separated by a dot. They are similar to IP addresses but are only used within a network. The subnet mask identifies which parts of the IP address belong to the network and which belong to the host. Subnet masks comprise contiguous bits of 1 starting from the left, while the rest of the bits are set to 0.
Each IP address class has a default subnet mask, as shown in [figure 2](./https://cdn.hashnode.com/res/hashnode/image/upload/v1664535988669/qWp2rReDk.jpg align="left").

Class A => 255.0.0.0 => 11111111. 00000000. 00000000. 00000000  
Class B => 255.255.0.0 => 11111111. 11111111. 00000000. 00000000  
Class C => 255.255.255.0 => 11111111. 11111111. 11111111. 00000000

The 1 bits represent the network id while the 0 bits represent the host id.
However, there are custom subnet masks that can be defined by an administrator to create subnetworks. This is done by borrowing some bits ( 0 bits) from the host id and changing them to network id (1 bits), in this case subnetwork id.

network_and_host_id.jpg

Figure 4

The figure above shows the default subnet mask of a class C network.

subnet_id.jpg

Figure 5

The figure above shows the subnet mask after subnetworks are created.

How to Subnet a Network

Example 1: Given this IP address - 193.16.20.0, subnet the network to create 5 subnetworks.

Step 1*: Identify the class.*

The first octet in the IP address is 193, we can identify from that that it is a class C network (class C networks have a range of 192 - 223. 193 falls within this range).

Step 2*: Convert the IP address and subnet mask to binary*

We can identify the network bits and the host bits from the subnet mask in binary form. We know that class C networks have a default subnet mask of 255.255.255.0

binarycalculator1.jpg

Figure 6

The figure above is the binary calculator to convert from decimal form to binary form and vice versa. We know each octet contains 8 bits, from 20 to 27. 20 is 1(decimal) all through to 27 which is 128(decimal). Adding all the decimal values (128+64+32+16+8+4+2+1) equals 255.

Converting the IP address: 193.16.20.0

The first octet: 193 => 11000001

first_octet.jpg

Figure 7

From the figure above, starting from the left-most part, 128 is subtracted from 193 to give 65, therefore the bit is 1 (think of 1 being true). Moving on to the next part, 64 is then subtracted from 65, which equals 1, so the bit is 1 (true). Moving on to the next part, 32 cannot be subtracted from 1, therefore the bit is 0 (false). This continues until the right-most part, where 1 can be subtracted from 1 to give 0. Therefore, the bit is 1 (true). Putting everything together will result in 11000001 (binary). This process will be repeated for the second, third, and fourth octets and the subnet mask.

The second octet: 16 => 00010000

second_octet.jpg

Figure 8

From the figure above, starting from the left-most part, 128, 64, and 32 cannot be subtracted from 16, therefore the bits are 0 (false). Moving on to the next part, 16 can be subtracted from 16 to give 0, therefore the bit is 1 (true). The remaining parts are 0 because we have nothing left to subtract (since we got to 0 when 16 was subtracted from 16).

The third octet: 20 => 00010100

third_octet.jpg

Figure 9

From the figure above, starting from the left-most part, 128, 64, and 32 cannot be subtracted from 20, therefore the bits are 0 (false). Moving on to the next part, 16 can be subtracted from 20 to give 4, therefore the bit is 1 (true). It skips 8 because 8 cannot be subtracted from 4, the bit there is 0 (false). 4 can be subtracted from 4 to give 0, therefore the bit is 1 (true). The rest of the bits are 0 because we reached the end of our subtraction.

The fourth octet: 0 => 00000000

When the decimal is 0, all the bits are equal to 0.
Therefore

193.16.20.0 (decimal) => 11000001. 00010000. 00010100. 000000 (binary)

Converting the default subnet mask: 255.255.255.0

The first octet: 255 => 11111111

sub_first_octet.jpg

Figure 10

From the figure above, from the left-most part, 128 can be subtracted from 255 to give 127, and the bit is 1 (true); 64 can be subtracted from 127 to give 63, the bit is 1 (true); 32 can be subtracted from 63 to give 31, the bit is 1 (true); 16 can be subtracted from 31 to give 15, the bit is 1 (true); 8 can be subtracted from 15 to give 7, the bit is 1 (true); 4 can be subtracted from 7 to give 3, the bit is 1; 2 can be subtracted from 3 to give 1, the bit is 1; 1 can be subtracted from 1 to give 0, the bit is 1. Therefore, converting 255 to binary is 11111111.

The second and third octets are 255 and 255, respectively, so they both equal 11111111
The fourth octet is 0, which equals 00000000 in binary

Therefore,

255.255.255.0 (decimal) => 11111111. 11111111. 11111111. 00000000

Step 3: Identify the network bits and host bits

193.16.20.0 => 11000001. 00010000. 00010100. 00000000  
255.255.255.0 => 11111111. 11111111. 11111111. 00000000

The network id (in bold), 11111111. 11111111. 11111111 (which is the first three octets), corresponds to the first three octets of the IP address, 1000001. 00010000. 00010100. Therefore, the network id of the IP in decimal form is 193.16.20 (remember the network id is part of the IP that remains unchanged). The host id is 00000000 (fourth octet in the subnet mask), which corresponds to the fourth octet in the IP, 00000000.

Step 4*: Define the bits to be borrowed from the host id of the default subnet mask*

We need to create five subnets from our IP address. To create subnets, we need to borrow bits from the 0 bits (host id) in the default subnet and make them 1. We need to borrow bits that will be greater than or equal to the number of subnets we are required to create. Borrowing one bit from the host id(binary form) will be less than the 5 subnets required (21 = 2; 2 < 5). Borrowing two bits is still less than the 5 subnets required (22 = 4; 4 < 5). However, borrowing 3 bits gives us 5 subnets and 3 extra (23 = 8; 8 > 5. Remember, we need to borrow bits that will be greater than or equal to our required subnet, which is 5), so we will borrow 3 bits from the host bits (host id). The subnet mask will now change from the default to a new value that includes the borrowed bits.

New subnet mask = 11111111. 11111111. 11111111. 11100000 => 255.255.255.244(decimal)

The borrowed bits become the subnet id.

new_subnet.jpg

Figure 11

Step 5*: Determine the number of IP addresses in each host bit*

Now we need to determine the number of IP addresses to which each of our created subnets will be assigned. Usually, the first and last IP addresses are not usable because the first IP of any subnet is used to identify the network (it is the network IP). The devices on that network use it to identify the network. The last IP of the subnet is used to broadcast (it is the broadcast IP). When a device on a network wants to send a message to every device connected to that network, it uses the broadcast IP to send the message.

The formula used to determine the number of IP addresses in each subnet is

2n
25 = 32

32 IP addresses

2n - 2
25 - 2 = 30

Step 6*: Show the ranges of IPs in each subnet*

ipaddresses.jpg

Figure 12

The table shows all 8 subnets created (5 will be used since we were asked to create 5 subnets), the network IP, the range of usable IPs, and the broadcast IP of each subnet.

CIDR

CIDR-Classless Inter-Domain Routing, introduced in 1993, was designed to solve the problem of wastage that occurred as a result of the classful addressing method (classes A-E). If an organization needed more than 254 addresses (the number of usable IP addresses in class C), it would have to switch to class B with over 65,000 usable IPs, leading to a lot of wastage. CIDR introduced an administrative process of allocating IP addresses to organizations based on their actual and short-term projected needs. CIDR introduced a new method of representing IP addresses called CIDR notation. In CIDR notation, the address contains a prefix and a suffix. The prefix is the IP address(in decimal form), and the suffix is a / and the number of network bits(1 bit) in the subnet mask. Example 1 above could be rewritten in CIDR notation as 193.16.20.0/24 as the default subnet mask has 24 network bits (1 bit). CIDR is also called supernetting.

Example 2: What is the Network IP, number of hosts, range of IP addresses, and broadcast IP from this subnet - 193.16.20.35/29?

In the previous example, we had to first identify the class, but there will be no need for that as this IP address is classless and in CIDR notation. We'll pick up from step 2 and convert the IP address to binary form, the /29 denotes that there are twenty-nine 1 bits in the netmask.

The first octet: Figure 5 shows the conversion.

193 => 11000001

The second octet: Figure 6 shows the conversion.

16 => 00010000

The third octet: Figure 7 shows the conversion.

20 => 00010100

The fourth octet: 35 => 00100011

example2_fourth_octet.jpg

Figure 13
Therefore,

193.16.20.35 => 11000001. 00010000. 00010100. 00100011
The subnet mask => 11111111. 11111111. 11111111. 11111000 => 255.255.255.248(decimal form).

From the subnet mask above, we know the network bits are the first 3 octets corresponding to 11000001. 00010000. 00010100 in the IP address, the subnet id is the 5 bits borrowed from the fourth octet, and the host bits are the remaining three 0 bits in the fourth octet.

In the question given, we are asked to find the network IP for the particular subnet in which the given IP is part. To do that, we will utilize the bitwise & operator.

bitwise_and_operator.jpg

Figure 14

network_ip.jpg

Figure 15

The network IP obtained from using the bitwise & operator to multiply the IP address given and the subnet mask(netmask) is 11000001. 00010000. 00010100. 00100000. Converting this to decimal using our binary calculator from figure 6

binary_to_decimal.jpg

Figure 16

From the figure above, the 8 bits in the fourth octet were placed on the binary calculator (we already know what the first three octets are in decimal form). The 1 bit corresponds to 32, therefore 00100000 in decimal form is 32.
11000001. 00010000. 00010100. 00100000 => 193.16.20.32

193.16.20.32 is the network IP for this subnet

From step 5 we know the formula to find the number of IP addresses (hosts) is

2n
2n - 2
Number of hosts = 23 = 8  
Number of usable hosts = 23 - 2 = 6

As earlier mentioned, the first and last IP addresses are not assignable. The first IP is the network IP (193.16.20.32) and the last IP is the broadcast IP.
Number of hosts is 6, as only 6 hosts are assignable.

The range of usable IP addresses is 193.16.20.33 - 193.16.20.38

193.16.20.33
193.16.20.34
193.16.20.35
193.16.20.36
193.16.20.37
193.16.20.38

Finally, the broadcast IP = 193.16.20.39

Conclusion

IP addresses are very important in networking. They provide a means for devices to be identified and for devices to communicate with each other over the internet or within a network. Subnetting helps us divide our network into smaller subnetworks to increase performance, security, and organization. The article only provides a basic introduction to IP addressing and calculating subnets.

Thank you for reading.