Network layer and Link layer: filling the gaps in understanding networking
This is 4th and final blog in series of Networking: how internet works. Make sure you have read previous ones
Network layer:
Quickly, what happened till now,
For client: data got encrypted, server IP determined by DNS, data divided into segments, each data segments got wrapped with TCP or UDP header with details like ports, sequence number etc.
For server: data reached to the server, re-assembled, it got to port mentioned in header, and handed over to application layer, and again response to client which client received in same way as server received.
So in this layer, we will learn, for client POV, process, after data get wrapped with TCP/UDP header, and for server POV, process, before data reaches to server port. - THE ROUTING OF DATA
Now the TCP header and data packet will be wrapped up by another header - IP header. So combining all we call it IP packet. This IP packet will have many things, first focusing on IP addresses.
IP Addressing and Subnetting in Local Networks
When your device connects to the local network through your mobile hotspot, it gets an IP address through DHCP (Dynamic Host Configuration Protocol). As seen below, both the IPv4 default gateway and the DHCP server have the same address, which is your mobile device's local IP. This shows that your mobile is managing the local network, acting as both the DHCP server and the default gateway.
How Subnetting Works:
Your laptop is assigned an IP address like 192.168.42.112/24.
/24 means that the first 24 bits (or the first three octets: 192.168.42) represent the network portion, while the last 8 bits (112) represent the device portion. This defines a subnet, which allows multiple devices to exist within the same local network, all sharing the same network portion (192.168.42.x), with the last 8 bits representing different devices (like your laptop or another device connected to the same mobile network).
Subnetting is represented by the subnet mask: 255.255.255.0. This mask means that the first three segments (or octets) represent the network portion, and the last segment is reserved for devices (as 0 means bits are available for device addressing). It can be also like 255.255.0.0
Classes of IP Addresses:
IP addresses are categorized into classes (A, B, C, D, and E) based on how the network and host portions are divided.
Class A: 1.0.0.0 - 126.255.255.255 (large networks)
Class B: 128.0.0.0 - 191.255.255.255 (medium-sized networks)
Class C: 192.0.0.0 - 223.255.255.255 (small networks)
Private IP Range (Local Networks):
Class A: 10.0.0.0 - 10.255.255.255
Class B: 172.16.0.0 - 172.31.255.255
Class C: 192.168.0.0 - 192.168.255.255 (where your mobile assigns local addresses like 192.168.42.112).
In our case, we are havingg a Class C private IP address, which is not globally routable and is confined to your local network.
Network Address Translation (NAT) and IP Packet Routing
Routing to the Default Gateway:
Your laptop sees that the destination IP (Amazon’s server IP) is not in the local network, so it routes the packet to the default gateway (192.168.42.129), which is your mobile device.
Network Address Translation (NAT) on the Gateway:
Since the local IP addresses like 192.168.42.112 are not routable on the internet, your mobile device uses NAT (Network Address Translation).
NAT replaces your laptop's local IP (192.168.42.112) with the mobile's public IP address (assigned by your ISP). Now this public IP address to your mobile is assigned by ISP using DHCP protocol. Basically they have storage of different available IPs. Which can be assigned to device after it’s lease time expires. In the pic, see our IP has lease obtained and expiry time.
NAT also keeps a table that maps your local IP to your mobile’s public IP, so responses can be correctly routed back to your device.
Sending the IP Packet to the ISP:
After the IP packet is updated with the public IP, it is sent to your ISP (Internet Service Provider). The ISP acts as an intermediary, forwarding the packet across the internet towards Amazon's server using routing protocols. These protocols rely on various graph algorithms to determine the best path from the ISP to the Amazon server. Once the packet reaches the Amazon server, the server prepares a response and sends an IP packet back. This response is routed through the same process in reverse: from the server to the ISP, then back to your mobile device, and finally to your laptop.
Key concepts include hop count, which refers to the number of routers a packet passes through from source to destination, and latency, the time it takes for a packet to travel between two points. Routers generally aim to minimize this delay. Every router maintains a routing table, a database that lists possible routes to various destinations, including information like the destination IP range, subnet mask, and the next hop.
On a larger scale, this functions similarly to how routing works within a LAN. One of the most important routing protocols is BGP (Border Gateway Protocol), which helps routers determine the best path for data to travel between different autonomous systems (e.g., between your ISP’s network and Amazon’s network).
Ip packet now contains TCP header, IP header and data. IP header includes the fields which we discussed above. Besides that it contains flags and checksum and other fields. There is field for TTL - time to live which determines the time a packet can live in network before it get discarded, preventing it from circulating endlessly for any reason.
Now, when we say IP packets will rout to the ISP and then to the amazon server, what actually routs is not packet, JSON and HTML are not flying over the sky, nor SQL or any images got wings even they drank red bull or black. This is where Data link layer and Phhysical layer comes into play.
Data Link Layer and Physical Layer
First of our example includes that our pc is connected to Hotspot of mobile, so there is Local routing happening, then from Mobile to ISP, and from ISP to different routers and Amazon server, bunch of routing will happen.
So before routing, It get encapsulated- a stylish word for wrapping up, into frames. Which has MAC address of destination router, so laptop will have MAC address of mobile, and of wifi type. Yes As we have different type of network adapters which are Data link layer devices. We have different type of MAC address, so here Wifi MAC address. Now our mobile will have MAC address of next router and next router will be having the MAC address of ISP and so on. This data is transimitted via signals, which are generated from the binary digits - phhysical layer generates from the data frames received by the DLL. This signals reach to each router and got decaapsulated and again framed with next IP or MAC address. That’s how data routs over the network.
The process of physical transmission of data is in itself big Subject, But as our interest was to learn about Networking in the context of How internet works. This is How it works….. Hope you got the answer.
Comments, corrections will be highly appreciated. Thanks for reading
Subscribe to my newsletter
Read articles from Danish Khan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Danish Khan
Danish Khan
I'm a backend devloper using python, django as my stack. I also have beginner level skills in DevOps like Linux, Virtual machines, OS, networking. Right now learning and expanding skills in DevOps