Linux system IP routing basics [Part 1]

Maybe you know how to use the command netstat or route on a Linux system to find (even add / delete) an existing route. But when you do these operations, you may not know how IP routing works. 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 peer-to-peer links or via network sharing. 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 principle

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

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

Introduction to Linux IP Routing Fundamentals

The IP layer maintains a routing table. When receiving data packets, it uses this table to decide 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 host's own address.

If the IP address in the data packet is the address of the host itself, the packet 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 routing capabilities, the packet will be forwarded; otherwise, the packet is discarded.

The data in the routing table generally exists in the form of entries. A typical routing table entry contains the following main entries:

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

● IP address of the next router: why we use the term "next" because the next router is not always the last destination router, but it is likely to be an intermediate router. The entry gives the address of the next router used to forward IP data packets received from the corresponding interface.

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

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

How does basic routing work?

Therefore, if we now want to describe the routing process simply and visually, we will see that once the IP layer of the host (configured to have the routing function) receives a data packet from the network side, it will verify the packet The destination IP address, if this IP is not the IP address of the host, then 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), then it will indicate the IP address of the next router. This is an important piece of information, because it directly tells the host (with the routing function) to which "next router" the packet should be forwarded. All other fields in the entry will provide more auxiliary information to make a decision for routing and forwarding.

In the previous paragraph we established a basic understanding of the routing forwarding process, but if we try to learn more in-depth, we must take a look at the following details about the routing table algorithm.

● First, the routing table will search for an entry whose "destination IP address" field is exactly the same as the 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 data packet is sent to the corresponding interface or intermediate router.

● If a complete matching IP is not found, then search for the matching network ID. If found, the data packet 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 packet 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 eventually. 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? With more precise host entries, why do I need network-related entries? Hmm ... Including routing entries related to the network in the routing table is a great advantage. The advantage is that having an entry related to the complete network can avoid including all the individual host entries in this network (this data volume is very huge). This reduces the size of the routing table to an acceptable order of magnitude, which is very good.

Command to view 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 The output provides detailed information about the destination IP address and gateway. The symbol "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 and the target are directly connected.

Three Phase Ammeter

Three Phase Ammeter,Voltage Ammeter,Three-Phase Power Meter,Power Instrument

zhejiangjinyidianqiyouxiangongsi , https://www.jooeei.com