How the linux route command works

  

Perhaps you know how to use the command netstat or route on a Linux system to find (or even add/delete) existing routes. But you may not know how IP routing works when you do these things. This article will help you understand the principles of IP routing and how it works. IP routing involves the forwarding of IP data packets. If the host is directly connected to the destination host, the host can directly send IP packets to the destination host. This process is relatively simple. For example, through a point-to-point link or through a network share. If the host is not directly connected to the destination host, the host will send the IP packet to the default router, and then the router will decide where to send the IP packet.

IP routing involves the forwarding of IP data packets. If the host is directly connected to the destination host, the host can directly send IP packets to the destination host. This process is relatively simple. For example, through a point-to-point link or through a network share. If the host is not directly connected to the destination host, the host will send the IP packet to the default router, and then the router will decide where to send the IP packet.

Routing Principles

The fundamental difference between a normal host and a router is that the host does not forward a message from one interface to another, and the router can forward the message.

Today, most multi-user systems can be configured to be used as routers. Therefore, a common routing algorithm can be used on a router, as well as on a normal host. When a host can be used as a router, we usually say that this host embeds the functionality of the router. This host with embedded router function usually does not forward messages unless we configure it to enable it.

The IP layer maintains a routing table that, when it receives a data message, uses this table to determine what to do next. When receiving a data packet from the network side, the IP layer first checks whether the IP address of the packet is the same as the address of the host itself.

If the IP address in the data message is the address of the host itself, the message will be sent to the corresponding protocol in the transport layer. If the IP address in the packet is not the address of the host itself, and the host is configured with the function of routing, the packet will be forwarded; otherwise, the packet will be discarded.

The data in the routing table is generally in the form of an entry. A typical routing table entry contains the following major entry:

● Destination IP Address: This field indicates the IP address of the destination. This IP address can be the address of a host or a network address. If the entry contains a host address, its host ID is marked as non-zero; if the entry contains a network address, its host ID is marked as zero.

● The IP address of the next router: Why do we use the phrase "next" because the next router is not always the last destination router, but it is probably an intermediate router. The entry gives the address of the next router to forward the IP datagram received from the corresponding interface.

● Flag: This field provides another important set of information, such as whether the destination IP address (previously mentioned) is a host address or a network address. In addition, it can be seen from the flag that the next router (previously mentioned) is really a router or a directly connected interface.

● Network interface specification: A network interface specification for some data messages. This specification is transmitted along with the message.

How does basic routing work?

So, if we want to describe the routing process simply and visually, we will see: once the host is configured to have routing capabilities. The IP layer receives the data message from the network side, it will verify the destination IP address in the data packet. If the IP address is not the host's IP address, the packet will be forwarded through the routing table.

If the first field of any entry exactly matches the destination IP address (host) or partially matches the destination IP address (network), it will indicate the IP address of the next router. This is an important piece of information, because this information directly tells the host (the routing function) that the packet should be forwarded to which "next router". All other fields in the entry will provide more auxiliary information to make decisions for routing forwarding.

In the previous paragraph we built a basic understanding of the routing and forwarding process, but if we try to delve deeper into more, we must look at some of the following details about the routing table algorithm.

● First, the routing table will search for an entry with the same destination IP address in the data packet. This means that the host ID of the IP address exactly matches the network ID. If found, the packet is sent to the corresponding interface or intermediate router.

● If a complete matching IP is not found, then the matching network ID is searched. If found, the data message will be forwarded to the designated router. So we see that all hosts on this network are managed by a single (this) entry in this routing table.

● If the above two conditions do not match, the data message will be forwarded to a "default router”.

● If the above steps fail, that is, there is no default router, the data packet cannot be forwarded. Any undeliverable data message will generate an ICMP host unreachable or ICMP network unreachable error and return this error to the application that generated the data message.

Sometimes people ask, why are there two types of entries in the routing table? Why do you need network-related entries with more precise host entries? Well … contains network-related entries in the routing table. Routing entries are a big advantage. The advantage is that having an entry related to the complete network avoids including all the individual host entries in this network (this amount of data is very large). This reduces the size of the routing table to an acceptable order of magnitude, which is very good.

Commands for Viewing Routing Table Information

You can use the netstat command to view routing table information as follows:

\\$ netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 192.168.2.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 0.0.0.0 192.168.2.1 0.0.0.0 UG 0 0 0 eth0 Its output is provided Destination IP address and gateway related details. The flag “U” indicates that the router is reachable; “>G” indicates that the router is connected to a gateway (router). If this flag is not set, then we can think that the host is directly connected to the target.

Gateway : The gateway address or & rsquo;*& rsquo; if none set.

Genmask : The netmask for the destination net; 255.255.255.255 for a host destination and 0.0.0.0 for the default Route

Configuration of static route:

ip router destination network mask {gateway address interface}

Example:

(1) ip router 192.168. 1.0 255.255.255.0 s0/0

Resolution: The meaning of this sentence is that the router sees the network segment with the destination network segment of 192.168.1.0, and sends the data packet out of interface s0/0.

(2) ip router 192.168.1.0 255.255.255.0 192.168.2.0

Resolution: The meaning of this sentence is: On HOSTA, the router sees the destination network segment as 192.168.1.0. The data packet is sent to the 192.168.2.0 network segment. That is, if you want to go to 192.168.1.0, you have to go through 192.168.2.0

Copyright © Windows knowledge All Rights Reserved