The TCP/IP Protocol is essential for accessing the Internet. This is the only protocol used for the Internet, so it is important to have some understanding.
Before we get to the math, let’s look at some basics.
Every system connecting to the Internet must run TCP/IP and each connection has a few standard items that you need to understand: IP Address, Subnet Mask, MAC Address, Gateway and DNS Servers. Other items are the DHCP Server, Lease Obtainment and Lease Expiration.
Every system on the Internet, or local network, needs an IP Address. This is like the ‘phone number’ of your computer. The IP Address is how your system connects to another system and vice versa. This number is used to route IP data packets to the required system. The remote system will receive a data packet that includes your IP Address and send the required data packets back to your system. Of course, your system is on the local network, but data packets are sent to your Gateway, which then goes to your system or out of your system to the Internet.
There is also included in the data packet your MAC Address. Each Network Interface Card (NIC) has a ‘unique’ address hard-coded on it. The address is included in all data packets. This also helps your system know that the data packet is meant for it. The MAC address is also necessary for obtaining an IP Address in the first place from the DHCP Server.
The DHCP Server is used on a network to hand out and manage IP Addresses. There is a ‘pool’ of addresses set up on the server for the network PCs to use. Each one is given out and then ‘reserved’. When the IP Address is given out, the date is noted on the server, this is the Lease Obtainment. A time is set on the DHCP Server for how long a lease can last, the Lease Expiration. On an open network, such as in a restaurant, the lease time may be 1-3 hours. On a closed network, such as an office, the lease can be set for hours, days or as long as required. When the lease is about to expire, the device will apply for a Lease Extension. The DHCP Server can allow an extension, or it may revoke the IP Address and give a new one.
The DNS Servers are used when your system does not know an IP Address for a ‘Friendly Name’. A ‘Friendly Name’ can be any website name, such as ‘www.linux.org’. If your system does not know the IP Address, it sends a request to the designated DNS Server in its settings, and waits for the response. The DNS Server will check its database and send back a data packet containing the requested IP Address. Your system can now contact the Internet Server with its IP Address.
This is a very basic rundown of these various aspects of TCP/IP, but the least understood is the Subnet Mask.
I did not mention any aspect of the Subnet Mask before, except the name. This is where the math can come into play.
Before that, let’s look at what the Subnet Mask is all about.
I mentioned the IP Address is like a phone number. If you look at the phone number, it comprises four parts: country code, area code, prefix and line number.
When you make a call, you must specify the area code, prefix and line code. The country code is only needed if you are calling a country other than the one you are in.
The area code specifies the general area you are calling. Each State in the US has one or more area codes. The area code will route your call to that State or general area of a State.
The Prefix will narrow down the general area to a more specific city or town. The line code locates a specific line that enters a house or a specific cell phone.
The IP Address is separated into two parts: Network Portion and Host Portion.
When you send a packet out of your NIC, it has an IP Address and Subnet Mask of the remote system. If the remote system is on the local network, it is routed to that system. If the remote system is not part of the local network, it is sent to the Gateway, which routes it to another router and so on until it reaches its intended system. Each router has a list of IP Addresses (Network Portion) it can reach and where to send data packets to reach it. If the information is not there, it will query the surrounding routers for the information. They may do the same until the proper address is found.
If your IP Address is 126.96.36.199, then what part is the Network Portion and which is the Host Portion? You notice that an IPv4 Address comprises four sections (octets) that are separated by decimals.
We can first look at the default Subnet Masks for specific IP Address Ranges. IP Addresses are split up into three usable categories. Class A, Class B and Class C are the three ranges that are usable. Class A address comprises address that range from the first octet being 1-126. A Class B address comprises the first octet being between 128-191. A Class C address is from 192-223.
If your IP Address is 188.8.131.52 then it is Class B. Class B has a default Subnet Mask of 255.255.0.0 (/16). Each octet comprises 8 bits, so the first two octets are used, which is 16 bits. Class A is 255.0.0.0 and Class C is 255.255.255.0 for the default Subnet Mask.
The way it is done, is that the Class A subnet starts with ‘0’ in the first octet of bits. Class B starts with ‘10’ and Class C is ‘110’.
So, now you are wondering about the bits. This is where the math comes in. Let’s look at the conversion of decimal to binary.
Decimal to Binary
Let’s look at the first octet of our IP Address (184.108.40.206) which is 172. Each octet is a byte, and each byte is 8 bits. Bits are on (1) or off (0). How can we represent 172 as 8 bits?
Each place in a byte has a decimal value. From the far right of the eight bits, we start with a value of 1 and double it until the eighth bit has a value of 128. The values should look like:
128 64 32 16 8 4 2 1
To convert, we start at the left (128). If our number is larger than 128, we place a one in that spot. Since we are using 128, we need to subtract 128 from our number to see what is left. So, 172-128 is 44. The next number is 64 which is larger than 44, so we place a 0 in the second spot. Next, we check the third slot. Since we have 44, we can place a ‘1’ in the third slot. We continue this procedure until we have the binary number of ‘1010110’. If we convert the second octet of 67, we get a binary value of ‘01000011’. The third octet (148) is ‘10010100’. The last octet (63) is ‘00111111’. Our whole IP Address is ‘1010110.01000011.10010100.00111111’.
By using the default Subnet Mask, we have 255.255.0.0 or ‘11111111.11111111.00000000.00000000’. Any values on the Subnet Mask that are ones, will represent the Network Portion of the address and the zeroes are the Host Portion. In our case, the Network Portion is ‘1010110.01000011’ and the Host Portion is ‘10010100.00111111’. So, any system on the same physical network with a Network Portion of the IP Address that is ‘1010110.01000011’ can communicate with one another. The systems with the same ‘Network Portion’ on a physical network are on the same ‘Logical Network’.
No two systems can have the same ‘Network Portion’ and ‘Host Portion’. Two systems that have the same addresses, would be like phones having the same phone number. The second system to be turned on would not work on the network until the IP Address was changed.
If you have a binary number and want to change it to the decimal number, you add up the values that have a ‘1’. For instance, if we had the values of ‘00000001’ and ‘011111111’, the values are ‘1’ and ‘127’. This is:
128 64 32 16 8 4 2 1
0 0 0 0 0 0 0 1 = 1
0 1 1 1 1 1 1 0 = 126
The range for Class A starts with a ‘0’. The remaining values would be all zeroes except the last value. This is the beginning of the range for Class A Addresses. Placing all ones in the remaining bits will show the end range of the Class A network. So a Class A Network is from 220.127.116.11 to 18.104.22.168.
NOTE: All the first octet of a ‘Network Portion’ cannot be 0 or 255, nor can it be all ones. For the ‘Host Portion’, if all the values are zero, this is a ‘network’ address that defines the whole Logical Network. And if all the ‘Host Portion’ are ones, this is a broadcast message meant for all systems on the ‘Logical Network’.
The first binary octet of Class B starts with ‘10’. The range is from ‘10000000’ to ‘10111110’ or 128-191. Class C is ‘110’ or a range of ‘11000000’ to ‘11011111’, or the decimal range of 192-223.
So any systems with different Subnet Masks cannot communicate directly with each other on the same physical network. For two different ‘logical networks’ to communicate, there needs to be a ‘bridge’ device to connect the two ‘logical networks’.
If the Subnet Masks are the same between two computers, you only need to look at the ‘Network Portion’ of the IP Address and make sure they match. So, on a Class A network, if both systems have IP Addresses that have the same first octet, they can communicate. A Class B Address would require the first two octets be the same. Class C needs identical values in the first three octets.
We assume our Subnet Mask is always a default value. There is what is called ‘Classless Inter-Domain Routing (CIDR)’, which uses values other than ‘255’ in the Subnet Mask. Keep in mind that all ones will be at the left of the Subnet Mask, and to the right all zeroes. Once the bits change from ones to zeroes, you cannot have a one again. For example, ‘224’ is ‘11100000’ in binary. There are three ones followed by 5 zeroes. All ones are to the left and zeroes to the right and none are mixed.
CIDR is where things can become more difficult to verify that two systems are on the same Logical Network. You need to really break both addresses down to binary and verify that the Network Portions are identical.
This article may clear up some issues that a few people may have in understanding TCP/IP Addressing.
Addressing is not too difficult once you understand the math behind the Subnet Mask. If you ever come across two system that will not ‘talk’ to each other on a network and you rule out everything else, check the IP Addresses.