We will look into the operations involved in sending a (ping/ICMP) packet from Computer A (192.168.1.1) to Computer B. Both are in a same LAN.[A]$ ping B
To form a ICMP packet destination and source IP addresses are needed. We know only the name(B) of the destination Computer. To get the IP of B, the computer A will send DNS Request packet to the Domain Name System(DNS) Server. DNS Server resolve the Name to IP and send DNS Reply packet with the IP address(192.168.1.2) of B to the Computer A.
Now the ICMP packet will be formed with destination(B), source(A) IP addresses and ICMP control informations (payload). Next step is, adding the Ethernet header to this packet. The Ethernet header consist of destination, source MAC addresses, protocol and Checksum. It is easy to find the source MAC address. Because it is the MAC address of local interface, which is holding the IP 192.168.1.1.Next task is finding the destination MAC address. It is not straight forward operation. Because the currently we know only the IP address from the DNS Reply packet and not the MAC address. The MAC address is embedded into the NIC (Network Interface Card). If the NIC changed, the MAC address also will get changed and MAC address is useful only with-in a LAN. That is the main reason for not using the MAC for regular communication. So we need some technique to find the MAC address of the Computer B. Address Resolution Protocol (ARP) is designed to solve this issue. Once the Computer A found that, it is not having the MAC of B. It will immediately send an ARP Request packet with Broadcast destination MAC address and payload as IP address of the Computer B. This packet will reach each and every computer in the LAN. The computer, which received the ARP Request packet, will check its IP address with the IP address in the payload.
If both are not equal, then that computer will simply ignore the ARP Request packet.
If both are equal, then the request is to know the MAC of this computer. So it will send an ARP Reply to indicate its MAC Address to the requesting computer(A). The ARP Reply packet will be an unicast packet with destination MAC(A), source MAC(B) and MAC of B in the payload.The computer A will receive the ARP Reply packet and learn the MAC address of the B and store it in the Kernel ARP Cache. So that, there is no need to send ARP Request for every IP packets. Now computer A know the MAC of B, it will add this as the destination MAC in the Ethernet header of the ICMP packet and transmit in the LAN. The Ethernet is a broadcast medium. So even with proper destination MAC address, this ICMP packet will land in the NIC of every computer in the LAN. The NIC will check the destination MAC, if it is equal to it’s own MAC then the packet will allowed into the computer. Otherwise it will be discarded. Finally this ICMP packet will get accepted only in Computer B and it will reply to this ICMP Request.