We’ve looked at direct communication between hosts and communication over switches, so it’s time to add another device, a router, to the communication process.

This article focuses on the practical application of the router as a key role in the packet sending process.

This article begins with an introduction to the two main functions of a router and shows how it works.

We will use the pictures below as we discuss the whole session. We will focus on how the router R1 forwards packets from host A to hosts B and C.

PS: For simplicity, I have shortened the MAC address of each NIC to a 4-bit hexadecimal number.

Functions of routers

We mentioned earlier that the function of a router is to transmit messages between two networks. In other words, each router creates a network boundary whose main function is to forward packets from one network to the next.

Note that in the figure above, router R1 creates the boundary between networks 11.11.11.x and 22.22.22.x, and router R2 creates the boundary between networks 22.22.22.x and 33.33.33.x. Each router has an interface to the network 22.22.22.22.x.

In order to forward between the two networks, the router must perform two actions: write and maintain the routing table and write and maintain the ARP table.

Writing to the routing Table

From the router’s point of view, the routing table maps the egress of all networks. The routing table is empty at the beginning. When a new routed network is accessed, information is written to the routing table.

There are many ways in which routers can detect other networks, and we will discuss only two of them.

The simplest method is direct routing. Basically, when a router is configured with a special IP address port, it knows what network that IP address is on.

For example, the IP address of the left interface of R1 is set to 11.11.11.1. It tells R1 that there is an 11.11.11.x network interface on the left. Similarly, R1 knows that network 22.22.22.x exists to its right.

Of course, routers can’t connect to all networks directly. Note that router R1 is not connected to 33.33.33.x, but it will one day send packets to this network. Therefore, there must be another way for the router to connect to the 33.33.33.x network.

The other method is static routing. Static routes are manually configured by the oil administrator. It’s as if you explicitly told R1, 33.33.33.33.x is behind R2, go find it, so R1 has to send packets to R2’s interface.

Finally, R1 puts the obtained information into the routing table through the direct route and static route, as shown in the figure below:

The routing table contains many routes, each of which maps the address of a network interface or next hop.

Each time a router receives a packet, it looks at the routing table to arrange for the packet to be forwarded.

However, when a router receives a packet destined for a destination that is not in the routing table, to the router, the route does not exist and therefore the packet is discarded.

In fact, there is a third method called dynamic routing, which involves detecting routes and informing each other of known routes. But there are too many protocols and different strategies to cover for this article.

The routing table tells the router which IP address the packet will go to. But as we said before, packets have to go through L2. In order for the router to create L2 headers and transmit packets to the next L3 address, the router must also maintain an ARP table.

Write the ARP table

The ARP table is a bridge between L3 and L2. When an IP address is encountered, ARP resolves to the MAC address associated with it. The device uses the ARP function to populate the ARP table, or sometimes called the ARP cache, which maps IP and MAC addresses.

The router uses the routing table to determine which IP address will receive the packet later. If the route is directed to an existing direct network, then the IP address that follows is the final IP address, which is the last hop of the packet

In other words, the router uses the L2 header to pass packets to the correct NIC.

Unlike the routing table, the ARP table is populated as needed. In the figure above, R1 does not initiate ARP requests to host B until a packet needs to be sent to host B.

The following figure shows the appearance of the ARP table:

Router operation

Now that we understand the router’s routing table and ARP table, let’s take a real look at how the router forwards messages between different networks.

In the R1 router table above, we can see that there are two types of routes: one to an interface, and the other to the IP address of the next hop. We’re going to talk about both of them.

First, let’s discuss host A delivering packets to the default gateway (R1). We then observe how R1 sends packets from host A to host B and then to host C.

Host A sends the message to R1

In either case, host A needs to send packets to the default gateway R1 to communicate with the extranet host.

Host A creates the L3 header, which includes the source IP address 11.11.11.77 and the destination IP address 22.22.22.88 (host B) or 33.33.33.99 (host C). The L3 header is used to ensure end-to-end data implementation.

But L3 alone is not enough to get packets to R1, so we need something else.

Host A encapsulates the L3 header into the L2 header, which contains the source MAC address aaaA.aaa. aaaa and the target MAC address aa11.AA11.aa11 — the IDENTIFIER of the R1 NIC. The purpose of the L2 header is to deliver the packet by this hop.

At this time, host A has been set to the default gateway IP address, and host A can communicate with external hosts. In this case, it is likely that the ARP table of host A has mapped THE MAC address of R1. On the other hand, if host A accesses the external host for the first time, then L2 header sends ARP request I to R1 to obtain THE MAC address of R1.

Next, the destination IP address can only be host B at 22.22.22.88 or host C at 33.33.33.99. Both the two targets are in the router table of R1. The difference is that one route points to the interface and the other to the IP address of the next hop.

Routing pointing interface

The typical scenario in which a route in the routing table points to an interface is the same network access we talked about earlier. If the packet’s destination IP address is within the network, it is directly connected to the router, which knows how to deliver the packet to the final hop (i.e., the destination IP address).

The whole process is the same as we discussed before. The router uses the L3 header information to determine where the packet is sent next, and then creates the L2 header and sends it to the destination. In the current discussion scenario, the next hop (or last hop) must go back to the NIC on host B.

The L3 header has not changed and is now the same as the L3 header created by host A.

What changes is L2’s header. The source MAC address is now BB11.BB11.BB11, which is the MAC address of the interface on the right of R1. The old L2 header is discarded during the R1 phase. After the packet is discarded, a new header is generated to send the packet to the next hop.

The router points to the next hop address

To send packets from host A to host C, the destination IP address is 33.33.33.99. When R1 looks at the routing table, it determines that the next hop is the R2 router at IP address 22.22.22.2.

When jumping between R1 and R2, the L2 and L3 headers are as follows:

The header of L3 is always the original source IP address and destination IP address of host A. But L2’s header changes at every hop.

R1 will not have R2’s MAC address initially. It sends AN ARP request to R2. After that, the packet is sent from R1 to R2 using the appropriate L2 header.

Continuing with the process, when R2 receives the packet, it faces the same problem as R1, sending the packet to the final hop.

The whole process can be extended. Host A is added and sent to host X through ten routers. Each process is the same. Each forwarding will have a route mapped to the next IP address of host X’s network until the router of host X’s network is finally found. Finally, the router sends the packet to the last hop, host X.