The network layer is the third layer in the TCP/IP model – it provides host-to-host communication services. Segments from the transport layer are received by the network layer, which encapsulates them into packets to be sent to the nearest router. Routers then forward the packets from their input links to output links on the path towards the receiving system.
Therefore, the network layer is responsible for sending data packets from source to destination, and it uses intermediate routers to do so. It performs both forwarding and routing to achieve this.
Forwarding
Forwarding is a function of the router. A packet received at an input link needs to be forwarded to the appropriate output link so that it reaches the destination. Hence, forwarding is implemented individually in each router. A forwarding table is used to determine which output link the data is to be sent to.
Routing
The complete path from source to destination is determined by routing algorithms that try to find the path with minimum distance and least traffic. It is these routing algorithms that determine the values to be fed into the forwarding tables of routers.
Just like the transport layer, the network layer provides connectionless and connection-oriented data transfer.
Virtual circuit
Virtual circuit (VC) provides connection-oriented services for data transfer. A connection is established between the source and destination and, for the transmission of data, the entire path is reserved for this connection. No other packets can use the links in this path. Therefore, the guaranteed transmission of all packets in order, without any delay, is provided. It also provides congestion control such that when a lot of traffic is detected, new connections are not allowed to be set up.
Datagram network
A datagram network provides connectionless data transfer service. Every time a packet is to be sent, the destination address is added to the packet’s header and it is sent through the network. No connection setup is required, and no path is reserved for this transfer. Intermediate routers use forwarding to send the packet from an incoming link to an outgoing one. Since the forwarding table can be modified at any time, packets may not follow the same route and may not arrive in order.
Let’s look at how the network layer works, specifically on the internet.
An IP address is a unique identifier of the hosts that communicate using the internet protocol. It is 32-bits long, and each byte is separated by a period, as follows:
194.13.16.1
This IP address is divided into two parts, a subnet mask and the device within the subnet.
A subnet is a sub-network within a larger network.
Inside a subnet, individual IP addresses of devices are assigned using the dynamic host configuration protocol. When a device joins the network, the following procedure is followed:
As we have seen earlier, the IP address is comprised of 32-bits, meaning that there can only be around 4 million unique addresses. However, the number of devices using the internet is much more than this. To cater to this, private networks can re-use IP addresses that are already allocated, but these addresses are only used within the private network.