Chapter 4

routing

By Nikhila Sattala
15 mins read
Now that you understand how devices within a single network communicate through switches, let's explore how a system from one network can talk to another system in a different network.
Communication across two different networks
Now that you understand how systems within a single network communicate through switches,
let's explore how a device from one network can talk to another device in a different network.

Here comes another fundamental feature of Networking, i.e, Layer-3 (L3) Routing.
As discussed in our previous section, we need a "Switch" for devices in the same network to communicate with each other.


Now, for a device in one network to communicate with a device in another network, we need another network equipment known as "Router". The router connects the switches of two different networks as shown in below picture.


We need router also to connect to internet. Here connecting to internet is nothing but connecting to another Router which is handled by our (Internet Service Providers)ISPs.
The data from one device gets routed to another device in a different network based on the information that is gathered in the following three tables:
  • Address Resolution Protocol (ARP) table,
  • MAC table and
  • Route table

All the hosts in a network and router uses both ARP and Route tables, where as a switch uses only MAC table in this routing process as shown in below picture.

In this section, let us understand, how the required information gets gathered in these tables and how that information helps in L3 routing process.
In the previous Switching section, we have learnt about the MAC table, let us understand ARP and Route tables in the section. Prior to it, we need to understand about Gateways in Networking.

Here we go !!!
Default Gateway:
In the context of Routing, a common example of a default gateway is a router, which connects a local network to the internet or other external networks.

For example:
You have a Linux computer with IP address: 192.0.1.10
Then your router’s IP address - 192.0.1.1 will be your default gateway.

Similarly for a network 10.10.20.0/24, which has ‘n’ computers and a router with IP 10.10.20.1 for that network. The computers in that network reach internet through this router's IP. This IP 10.10.20.1 is the default gateway or internet gateway for all the devices in that network.
Route Table
Route tables are used by routers and computers to make routing decisions. They determine how to forward packets based on the destination IP addresses.

Technically, a route table is a data structure that stores information about network destinations and the paths (or next hops or next routers) that should be taken to reach those destinations.
In the above picture, for the device 192.0.10.10 to communicate with the device 192.0.20.20, we need a Route table at source Device and an other Route table at the Router.

Route table of the source device:
When a device is initially configured for network access, it knows about its Gateway either through manual configuration or through DHCP.
So there exists a route on Device's (192.0.10.10) Route table as follows:
$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.0.10.1      0.0.0.0         UG    600    0        0 eth0
We use "route -n" command to view the Route table. Th above Route table shows a default route (0.0.0.0) on the device 192.0.10.10, that directs all traffic (not matching any other specific route) to the gateway 192.0.10.1 through the device's network interface eth0.

Route table of the Router:
For data to pass from Network A to Network B and vice-versa, there needs two routes to be added in the Route table of the Router. These routes can be added using "ip route add" command as follows:

ip route add <Destination-Network> via <Gateway-of-the-Destination-Network>

NOTE: Point to notice, its the Gateway-of-the-Destination-Network, which means, if you want to send data to Host - 192.0.20.20, then its Destination-Network is 192.0.20.0/24 and its Gateway is 192.0.20.1.
# Route 1 - To move traffic from Network A to Network B
sudo ip route add 192.0.20.0/24 via 192.0.20.1
This command adds a route in Router, stating that any traffic destined for the Network B - 192.0.20.0/24 should be forwarded to its gateway - 192.0.20.1.
As you can see in the picture, 192.0.20.1 is the IP address assigned to "eth2" ethernet interface of the router. And this 192.0.20.1 IP is the gateway for 192.0.20.0/24 network.
# Route 2 - To move traffic from Network B to Network A
$ sudo ip route add 192.0.10.0/24 via 192.0.10.1
This command adds a route in Router, stating that any traffic destined for Network A - 192.0.10.0/24 should be forwarded to its gateway - 192.0.10.1.
As you can see in the picture, 192.0.10.1 is the IP address assigned to "eth1" ethernet interface of the router. And this 192.0.10.1 IP is the gateway for 192.0.10.0/24 network.

NOTE: If these routes are not available in router, then all the traffic that reaches it, will be routed to internet through its "eth0" interface (as it is default route) and the communication between the two Networks A and B will never be established.
Route table
Address Resolution Protocol (ARP)

From Route tables, we understood that to hosts knows the IP of Router, to which it needs to send its data packets. But knowing IP address of the Router alone is not sufficient for a host to send data.
It also needs to know the MAC address of the Gateway ethernet interface.

Here comes the Address Resolution Protocol, using which any device in a network, knows the MAC address of its Default Gateway (i.e, Router's interface).

Suppose 192.0.10.1 is Gateway (Router's IP) of a network, then device 192.0.10.10 obtains the MAC address of its Router's interface using ARP.
How ARP Works
Initial Configuration
When a host is initially configured for network access, it typically includes information about its default gateway, which is the router that connects the local network to other networks or the internet. This information can be manually configured on the device or provided through DHCP (Dynamic Host Configuration Protocol).
Broadcast ARP Request
If the host's ARP table does not already contain the MAC address for the router's IP address (which is often the case when the host is initially powered on or freshly configured), the host generates an ARP request. This ARP request is a broadcast message sent to all devices within the local network segment, asking, "Who has IP address X (the router's IP address)?"
Router's Response
The router, which is the default gateway for the local network, receives the ARP request. Recognizing its own IP address in the request, it responds with its MAC address to the requesting host.
Updating ARP Table
The host receives the ARP response from the router, which contains the router's MAC address. Host updates its ARP table with the router's MAC address.
Switch's Role
Through the whole ARP process, the Switch learns the the MAC addresses of the host and the router and saves that info in its MAC table.
ARP table of the Host:
$ arp -n
Address            HWtype  HWaddress           Flags Mask       Iface
192.0.10.1         ether   00:0e:c6:ba:67:c0   C                enx00e04cb7f5c0
The above output shows the ARP table in host 192.0.10.10 where,
  • 192.0.10.1 is the IP address of Router's interface (which is also the Default Gateway of that network)
  • "00:0e:c6:ba:67:c0" is the MAC address of that Router's interface
  • "enx00e04cb7f5c0" is the ethernet interface of the host.
ARP table of a device stores the IP-to-MAC address mappings of all other devices, including the router and printer, that are configured in its local network.
Layer-3 Routing process
Once all the required information is available in ARP, MAC and Route tables as shown in the below image, then the routing process can be initiated, where two hosts from different networks can communicate with each other.
Let us understand the steps involved in for a data packet to move from one device to another device in a different network:

  1. Ethernet Frame Creation (by Device): Device forms a data packet with the router's MAC address as the destination. As device is connected to the switch through a cable, this data packet will be forwarded to switch.
  2. Switch's role: When the device sends data packets addressed to the router, the switch, using its MAC address table, determines the port to which the router is connected and forwards the packets to that port.
  3. Router Decision: The router, upon receiving the packets from the switch, processes them based on the destination IP address. It makes routing decisions and forwards the packets towards their final destination, which may be next-hop router or the destination network.

This is how data moves across different networks using the information that is available in corresponding ARP, MAC and Route tables.
Have you ever thought of this?
In a case like opening a web app from my browser, how packets reach server through internet.
I am not adding any routes in the Routers that exist between my computer in India and the Server in a data center in the US. Then how are packets being routed to the Server?

When you access a web application from your computer's browser to a server in a data center located in another country, you typically don't manually configure routes in the routers along the entire path. Instead, this routing is handled by a combination of dynamic routing protocols, Border Gateway Protocol (BGP) for the internet backbone, and the domain name system (DNS).