The Network Layer : Connecting Networks
In the last chapter, we learned how we can connect computers to a network and make them talk to each other. Now, what if we want to talk to a computer located on the other side of the world?
To learn how to do that, we have to explore the third layer, The Network Layer.
Network Layer Roles
The network layer is the most important layer of the OSI model. It is the one that routes packets from their source to their destination.
We have seen in the last chapter how the data link layer helps machines to communicate on a network. Well, the network layer goes one step further and allows networks to communicate.
You might wonder how this would help two machines on different continents to communicate. Well, to answer this, we need to first understand how the Internet is connected. Let’s consider the image below:
If computer A sends a message to computer B, this message goes from one network to the next until it reaches its intended destination.
This is how Internet is actually connected. It is nothing but a bunch of networks that connect to each other and that cover the entire world. These interconnected networks provide pathways to all devices connected to the Internet.
You must have realized by now the importance of the network layer. It is the one layer that takes the burden to get the network packets to their rightful recipients, wherever they are in the world.
But there is still one thing that is not yet clear. How does the network layer know where the packets need to go?
To know where packets should go, the network layer relies on an address called the IP Address.
Earlier in this tutorial, we have seen how the MAC address provides a way for machines within a network to identify each other. In a similar way, the IP address provides a way to identify not only machines within a network, but also the network itself to which they belong.
Unlike the MAC address, IP addresses are logical, which means that they can be assigned and changed as needed.
The IP address is 32 bit long, generally represented as 4 decimal numbers separated by a dot.
In almost all situations where you will encounter an IP address, it will always be represented in this way. However, just to have a better understanding of it, let’s go ahead and break it down into a binary representation .
The IP address contains 4 blocks of 8 bits each. This means that the value of each block ranges between 0 (All bits equal to 0) and 255 (All bits equal to 1):
11111111 = 1 x 2^8 + 1 x 2^7 + 1 x 2^6 + 1 x 2^5 + 1 x 2^4 + 1 x 2^3 + 1 x 2^2 + 1 x 2^1 + 1 x 2^0 = 255
So theoretically, an IP address should range between 0.0.0.0 and 255.255.255.255. This provides a sufficient space for 2^32 = 4,294,967,296 addresses.
The Subnet Mask
Often, when you see an IP address, there’s an additional item that is always present next to it. This item is what we call the subnet mask.
As you may recall from earlier in this section, the IP address carries two pieces of information: The address of the machine, and the address of the network. The way to retrieve each of these pieces of information is by using, you guessed it, the subnet mask.
The subnet mask is similar in format to the IP address. It is also 32-bit long and, just like the IP address, it is represented in the decimal dotted notation.
The bits in the mask that are equal to 1 represent the network part of the IP address, whereas the bits equal to 0 represent the machine part.
This is very similar to an apartment’s address. It has the address of the building (Network address), and then the number of the apartment in that building (Host address).
Here is an example:
In the example above, we can deduce from the subnet mask that the IP address of the network is : 192.168.86.0. This information will lead us to the network to which this machine belong, and from there we can get to the machine by its complete IP address : 192.168.86.154.
Before we close this section and move on to the next, there is still one thing that you have to know. You will often encounter the IP address + mask represented this way:
The numbers after the slash sign indicate the number of bits in the subnet mask that are equal to 1. So, this representation is just another way to write:
IP: 192.168.86.154 Mask: 255.255.255.0
The Internet Protocol (IP) is the most common protocol for layer 3, just like how Ethernet is the most common one for layer 2.
The primary function of IP is to deliver packets from the source machine to the destination machine based on the IP address included in the IP header that encapsulates the data. This data, when encapsulated with the IP header, is called a datagram. This is how the datagram header is structured:
The IP Header contains the following data:
- Version (4 bits) : This is the version of the IP addressing that is used. It can be IPv4 or IPv6. The IP addressing format that we have talked about in this chapter is IPv4, we will address IPv6 in a future chapter. For the moment, you just need to know that IPv6 is an evolution of IPv4 and therefore it is much more better than its pedecessor.
- Header Length (4 bits) : This is the length of the IP header,
- Service Type (8 bits) : This field provides information about Quality of Service (QoS). This can help network devices to deal with the packets depending on their priority: Important packet types that require instant delivery have the highest priority, whereas the less important ones that can tolerate slow delivery have a lower one.
- Total Length (16 bits) : This is the total length of the datagram. Not only the header, but the entire content of the datagram including its payload. As the number of bytes for this field is equal to 16, the maximum length that the datagram can have is therefore equal to 2^16 = 65,535 bits. When the total length of data exceeds this value, then this data is fragmented into multiple datagrams. The length of each fragment is less than 65,535 bits.
- Identification (16 bits) : When multiple datagrams have the same value in this field, then the receiving host understands that they are all fragments of the same transmission.
- Flags (3 bits) : This field informs the network devices whether the packet should or should not be fragmented. It also informs it if the packet is the last fragment in a series of fragments.
- Fragment Offset (13 bits) : This field helps the receiving end to put in order all the received fragments that belong to the same transmission.
- TTL (8 bits) : This is the maximum number of hops, or layer 3 network devices that the datagram can go through, before it is discarded. At each hop, the network device decreases the value of this field by 1. When it reaches 0, it gets thrown away.
- Protocol (8 bits) : This field indicates the transport layer protocol used in the packet. We’ll talk about these protocols in the next chapter.
- Header Checksum (8 bits) : At each hop, the network device calculates this value depending on the content of the header. This way, if there was a change to the content of the header, then it will be detected in the next stop, since the value of the checksum will have been changed.
- Source IP Address (32 bits) : The IP address of the sending machine. Remember that the IP address is 32-bit long, so it is perfectly logical that this field is also 32-bit long.
- Destination IP Address (32 bits) : The IP address of the receiving machine.
- Options (variable length) : This field is optional, and is not very often used.
- Padding (variable length) : This field contains a series of zeros to ensure that the options+padding fields are always 32-bit long.
The Router: A Network Layer Device
We learned in the last chapter that if we want to connect machines to a network, we use a switch. Now, in order to connect our network to an external network, then a switch won’t do it. For this, we need to use a router.
A router is a network device that connect networks. It has at least two ports and can connect two or more networks.
The router is a layer 3 network device. This means that it can access the IP header and make decisions on how to forward the packets depending on the pieces of information that it can extract from the fields of the IP header.
Routers and switches are the most common network devices that you will encounter in network installations. You should by now have enough knowledge that will allow you to design a small network using these two.
In the next chapter, we will talk about the transport layer.