Learning Networkings Basics - 4


In this new blog post, I'll explain what I learned about how data is transmitted within and outside a LAN on Layer 2 of the TCP/IP model. It's important to remember that on Layer 2, a host can send data to other hosts if it knows their IP addresses.
To illustrate this, let's use this network schema as an example.
Transmission inside a LAN
Let’s say Host 1 wants to send data or packets to Host 2. Both hosts are in the same LAN. There are two possible scenarios: either Host 1 already knows Host 2's MAC address, or Host 1 only knows Host 2's IP address but not the MAC address.
MAC Addr is known
In this case, Host 1's ARP table already contains the IP and MAC addresses of Host 2, making the process straightforward.
In fact:
Host 1 will put its IP and MAC addresses in the source address sections, then use Host 2's IP and MAC addresses as the destination and send the packet to switch S1.
At switch S1, after it de-encapsulates the frame, it will check its MAC Address Table to see if there is a port associated with the destination MAC. If yes, it will forward the frame to this port. If not, it will broadcast the message to all ports except the sending port. In this last case, the frame will be processed only by the host with the corresponding MAC address; others will discard it.
You can get more information about this case in this article I wrote on Ethernet and Switch.
MAC Addr is not known
Host 1's ARP table doesn’t contain the IP and MAC addresses of Host 2. Therefore, before sending the packets, Host 1 will try to discover the MAC address of Host 2 with an ARP request. ARP stands for Address Resolution Protocol. So, what happens is an intermediate step where:
Host 1 will send a Broadcast MAC addr frame with the IP address of Host 2.
Switch S1 will forward it to all ports (except the sending one), and each of the hosts connected to the switch will compare the IP addr in the frame to their own IP.
The corresponding host (Host 2) will be the only one to reply and will send back its MAC addr.
Host 1 will then store the MAC addr in its ARP Table and will set the destination MAC addr of the frame with this new value.
Then the packets will be sent following the steps in the case of MAC Addr is known (above).
Note: Routers do not forward broadcast domain packets, so Host A and B will not receive these packets.
Here is the Ethernet Frame that will be sent (this is a simplified version)
Following text summarise what’s said in this paragraph.
The method that a host uses to send messages to a destination on a remote network differs from the way a host sends messages on the same local network. When a host needs to send a message to another host located on the same network, it will forward the message directly. A host will use ARP to discover the MAC address of the destination host. The IPv4 packet contains the destination IPv4 address and encapsulates the packet into a frame containing the MAC address of the destination and forwards it out.
from cisco
Transmission outside a LAN
The transmission of a packet outside a LAN is done at Layer 2 of the TCP/IP model; the Internet Layer. So we need the IP of the destination host. At this layer, we necessarily need a Router to route the traffic (transmit the packet) outside the LAN. So what happens?
Let's suppose that Host 1 wants to send a packet to Host B.
The Destination MAC addr will be the Default Gateway MAC addr in the LAN: XXXX, then
The Router will:
get the frame,
de-encapsulate it,
evaluate the best path by which to send the packet,
replace the DEST MAC Addr with the next Router MAC Addr,
replace the SRC MAC Addr with its MAC Addr in the new LAN: QQQQ,
send the packet.
The next router does the same thing, but if it’s the last router and the packet is already in the DEST LAN, it puts its MAC Addr in the LAN as SRC MAC addr.
As the last router and the destination host are in the same LAN, the steps in “Transmission inside a LAN” will be executed to either get and set the destination MAC addr of the packet or send the packet if the destination MAC addr is already known.
The following text summarizes what's said in this paragraph.
When a host needs to send a message to a remote network, it must use the router. The host includes the IP address of the destination host within the packet just like before. However, when it encapsulates the packet into a frame, it uses the MAC address of the router as the destination for the frame. In this way, the router will receive and accept the frame based on the MAC address.
from cisco
The picture below shows changes in the packet sent from a LAN to another.
Magic, right? I really like how networking is designed, and I wonder how the field developed like that.
Subscribe to my newsletter
Read articles from Tawaliou ALAO directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Tawaliou ALAO
Tawaliou ALAO
Software Developer | Love Low Level Eng. | Python, Javascript, C, Linux I'm learning backend development and system programming.