Network layer is layer 3 of OSI Model. Network layer adds support of connecting multiple networks with each other. Network layer uses its own unique addressing scheme for differentiating networks and hosts within that network. Internet widespread geography is possible only because of network layer and its addressing scheme. Network Layer provides services to upper layer which is Transport Layer and Layer 2 Data Link Layer provide services to Network Layer. In general layer 3 provide following services:
Connectionless Communication (Connection-oriented at above layers)
Message Forwarding (Inter-network communication)
In this article we will study about all the functions of network layer in detail with examples and how communication is done at network layer. For this you must have understanding how layer 2 network works, for this, please refer - Overview of Layer 2 Networks and Communication.
For purpose of addressing network layer uses a specific kind of addressing scheme called IP Address (Logical Address). IP address is composed of 32 binary bits. Its’ written in decimal form using 4 octets (1 octet is of 8 bits) (32 / 8 = 4). Example of IP address version 4 – 192.168.5.10. IP address contains two part in them – Network part and Host part. Host part defines unique address of host and network part define network of that host (to which network host belongs). Network is kind of group and hosts are people in them. Two different networks cannot communicate with each other at layer 2 but can communicate using layer 3 Message Forwarding devices (Layer 3 Devices). To better understand host addressing please refer to DS Understanding IP Address
Communication at Network Layer within Single Network
Single network at layer 3 means all those hosts whose network part is same in their IP address. For example all host with network 192.168.5.0 / 24. So in single network all hosts have same network address. Communication within a single network is done via switch. For details please refer Overview of Layer 2 Communication. In single network, inter-communication device is Switch which is a layer 2 device, so layer 3 addresses only plays role in ARP. Each packet sent to switch is complete packet, it contains layer 3 information also. At layer 3 source IP address and destination IP address is mentioned. But as Switch is layer 2 device, it reads only layer 2 information and does forwarding based on layer 2 information (MAC Address). Layer 3 information as mentioned above is used for connection between multiple networks, so let’s see how inter-communication is done between multiple networks and also how host machine connects to Internet.
Communication at Network Layer with Multiple Networks & How Host Machine Connects to Internet
When there are multiple networks, layer 2 device (Switch) cannot be used for inter-communication because switch works on MAC address and MAC addresses are obtained via broadcast, and we cannot send a broadcast in whole world to find a single machine. To solve this problem Layer 3 addressing was developed and Layer 3 device Router is used for inter-communication. When two hosts which are on different network want to communicate, Router is used for forwarding message from one network to another network. Router is connected to multiple networks and each interface which connects to a network is configured with IP Address and belongs to same network as to which it is connected. When we configure any host machine with Network Layer Addressing, there we mention IP Address, Subnet Mask, Default Gateway and DNS Servers. IP address and Subnet Mask are used for layer 3 addressing which you have understood from (this article); Default Gateway and DNS Server are used for inter-communication between two networks. Default Gateway – Default gateway is IP address of that device, to which machine forwards all those packets which are not in same network. Any packet which is out of network, machine forwards it to default gateway device. All unknown network packets are sent to default gateway. Routers act as default gateway. DNS Server – As we know internet works on IP addresses but we use names to open any websites. But behind the scenes DNS Servers convert site name into IP address. For example – we want to open Google, we simple type www.google.co.in and sites open, but at back end, first IP address of Google is obtained from DNS Server after that packet is formed with Google IP address as destination IP address and transmitted. Following is the process of communication between two networks:
Host machine opens any machine in same or other network.
Host machine operating system checks for other machine if it is in same network, then it will simply check for its MAC address based on Destination machine IP Address and forwards packet to switch and operation is performed as happens in Layer 2 Network.
If other machine is in another network, then host operating system needs to forwards packet to default gateway.
Machine checks for default gateway MAC address based on IP address configured.
If MAC address is not found, ARP Request is generated with default gateway IP Address and MAC address is obtained. (For detailed ARP process click here).
Then host operating system sends this packet to default gateway (which is generally IP address of router).
Router receives packet and checks layer 3 information (Switch checks packets layer 2 information – MAC Addresses) which is Source IP Address and Destination IP Address.
Based on Destination IP Address it decides on which port to forward packet based on router's internal routing information (Routing Table).
(Two interfaces of routers which are directly connected with each other must be configured with IP address in same network)
Each router when forwards packets to another network it changes its layer 2 information. It adds next network device MAC address of other network in destination MAC address and its own MAC address in source MAC address (because between routers packet is sent on layer 2 as both interfaces of router are in same network).
If there are multiple routers between two networks then each router forwards packet in same manner based on layer 3 information of packet and routers internal information (Routing Table) and changes layer 2 packet information.
At end network station either some switch receives packet or direct host machine receives packet.
If switch receives packet from router then it make forwarding decision based on Destination MAC Address (as Router is in same network (last network) so it adds machine MAC Address based on Layer 2 information table in router).
Router Internal Information Table (Routing Table)
Router internally has two tables one for layer 2 information (MAC Address / ARP Table) and layer 3 information (Routing Table). Routing table is used for inter-network communication. There are two types of routing protocol algorithms based on which routing table contains entries of other networks. These two algorithms are:
Distance Vector Algorithm
Link State Logic Algorithm
Router using Distance Vector algorithm contains information about networks through its neighbor router via frequent updates whereas in Link State Logic Algorithm each router maintain complete tree like structure of whole network keeping itself at root and get updates only when there is a change in network. Routing table contains network address (not hosts IP addresses) and port on which that network is available. Following is an example of routing table.
Suppose PC0 wants to send data to PC5, IP Address of PC0 is 192.168.5.10 /24 and IP Address of PC5 is 172.16.0.50 / 16, then following will be the series of operation:
Host of PC0 machine will see that PC5 is in another network.
So it will form packet with destination IP address of PC5 and destination MAC address of Default Gateway which is Router 0 in this case.
This packet will be received by Switch; switch will see the destination MAC address and forward packet to Router0.
Router0 will see destination IP address (network portion) and based on its routing table it will forward packet to Router 1. Packet will have destination MAC address of Router1 (S 1/0 Interface) and destination IP address of PC5 and source MAC address of Router0 (S 1/1 Interface).
Router1 will see destination IP address (network portion) and see its routing table and based on routing table it will forward packet to Router2. This time destination MAC address will be of Router2 (S 1/0 Interface) and destination IP address will be of PC5 and source MAC address of Router1 (S 1/1 Interface).
Router 2 will see the destination IP address and see that destination network is on its port which is connected to switch and forward packet to switch. This packet will have destination MAC address of PC5, destination IP address of PC5 and source MAC address of Router2.
Switch will forward packet based on destination MAC address to port on which PC5 is connected.
Routing Table of Router0
220.127.116.11 / 24
Directly connected via G1/0 (Gigabit Interface)
172.16.0.0 / 16
Routing protocol via S 1/1 (Serial Interface) (Router 1)