4. Routing
1. Routing table
If a host has multiple network interfaces, how does it decide which interface to use when sending packets to a specific IP address? The answer is in the routing table. Let’s take a look at the following example:
Purpose Subnet Mask Gateway Flag Interface
201.66.37.0 255.255.255.0 201.66.37.74 U eth0
201.66.39.0 255.255.255.0 201.66.39.21 U eth1
The host sends all data with destinations to the host within network 201.66.37.0 (201.66.37.1-201.66.37.254) through interface eth0 (IP address is 201.66.37.74), and all data with destinations to the host within network 201.66.39.0 is sent through interface eth1 (IP address is 201.66.39.21). Flag U indicates that the routing status is "up" (i.e., the activation status). For networks that are directly connected, some software does not give the IP address of the interface as in the above example, but lists only the interface.
This example only involves directly connected hosts, so how about the destination host in a remote network? If you connect to the network 73.0.0.0 through a gateway with IP address 201.66.37.254, you can add this item to the routing table:
Purpose
Mask
Gateway
Logo
interface
73.0.0.0
255.0.0.0
201.66.37.254
UG
eth0
This entry tells the host all packets that are destinations within network 73.0.0.0 are routed through 201.66.37.254. The flag G (gateway) indicates that this item directs the packet to the external gateway. Similarly, you can also define the route to a specific host through the gateway, adding the flag H (host):
Purpose Mask Gateway Flag Interface
91.32.74.21 255.255.255.255 201.66.37.254 UGH eth0
Here is the basis of the routing table, except for special table entries:
Purpose Mask Gateway Flag Interface
127.0.0.1 255.255.255.255 127.0.0.1 UH lo0
default 0.0.0.0 201.66.37.254 UG eth1
The first item is the loopback interface, which is used by the host to send data to itself. It is usually used for testing and applications running on IP but requiring local communication. This is the host route to a specific address 127.0.0.1 (interface lo0 is a "fake" network card inside the IP protocol stack). The second item is very interesting. In order to prevent the host from defining every possible route to the Internet on the Internet, a default route can be defined. If there is no item matching the destination address in the routing table, the packet will be sent to the default gateway. Most hosts simply connect to the network through one network card, so they only go to other networks through one router, so there are only three items in the routing table: loopback item, local subnet item and default item (pointing to the router).
2. Overlapping routing
Assume that there are the following overlaps in the routing table:
Purpose Mask Gateway Flag Interface
1.2.3.4 255.255.255.255 201.66.37.253 UGH eth0
1.2.3.0 255.255.255.0 201.66.37.254 UG eth0
1.2.0.0 255.255.0.0 201.66.37.253 UG eth1
default 0.0.0.0 201.66.39.254 UG eth1
The reason why these routes overlap is that these four routes contain address 1.2.3.4. If data is sent to 1.2.3.4, which route will be selected? In this case, the first route will be selected through gateway 201.66.37.253. The principle is to choose the longest (most accurate) subnet mask. Similarly, select the second route for data sent to 1.2.3.5.
Note: This principle applies only to indirect routing (via gateway). Defining two interfaces on the same subnet is illegal in many software implementations. For example, the following settings are usually illegal (but some software will try to load balancing on both interfaces):
Interface IP address Subnet mask
eth0 201.66.37.1 255.255.255.0
eth1 201.66.37.2 255.255.255.0
The strategy for overlapping routing is very useful, which allows the default routing to work as routes with a purpose of 0.0.0.0 and a subnet mask of 0.0.0.0 without the need to be implemented as a special case of routing software.
Looking back at CIDR, we still use the above example: a service provider is given 256 Class C networks, from 213.79.0.0 to 213.79.255.0. The routing table outside the service provider understands all these routes in just one table entry: 213.79.0.0, and the subnet mask is 255.255.0.0. Suppose a user moves to another service provider and he has a new network address from the new service provider? If so, it means that he must reconfigure the IP address of each host, change the DNS settings, etc. Fortunately, the solution is simple, the original service provider keeps the route 213.79.0.0 (subnet mask is 255.255.0.0), while the new service provider broadcasts the route 213.79.61.0 (subnet mask is 255.255.255.0), because the new route has a longer subnet mask, it will overwrite the original route.
3. Static routing
Looking back at the routing table we have established, there are six table entries:
Purpose Mask Gateway Flag Interface
127.0.0.1 255.255.255.255 127.0.0.1 UH lo0
201.66.37.0 255.255.255.0 201.66.37.74 U eth0
201.66.39.0 255.255.255.0 201.66.39.21 U eth1
default 0.0.0.0 201.66.39.254 UG eth1
73.0.0.0 255.0.0.0 201.66.37.254 UG eth0
91.32.74.21 255.255.255.255 201.66.37.254 UGH eth0
How do you get these table entries? The first one is added by the routing software when the routing table is initialized, the second and third are automatically created when the network card binds an IP address, and the other three must be added manually. In UNIX systems, this is done through the command route, which can be executed manually by the user or at startup through the rc script. The above method involves static routing, usually created at startup, and will not change without manual intervention.
Router <5>
4. Routing
4. Routing protocol
Both hosts and gateways can use a technology called dynamic routing, which allows routing tables to be dynamically changed. Dynamic routing requires routing protocols to add and delete routing table entries. The routing table still works the same as static routing, but its addition and deletion are automatically.
There are two routing protocols: internal and external. Internal protocols are routed internally in the homemade system (AS), while external protocols are routed among the homemade systems. Homemade systems are usually managed under unified control, such as large companies or universities. Small sites are often part of their Internet service provider's homemade system.
Only internal agreements are discussed here, and few people involve or even hear about external agreements. The most common external protocols are the External Gateway Protocol (External Gateway Protocol) and the BGP (Border Gateway Protocol). BGP is a newer protocol that is gradually replacing EGP.
5. ICMP redirection
ICMP is not usually considered a routing protocol, but ICMP redirection works very similarly to the routing protocol, so I will discuss it here. Suppose there is now a routing table with the six table entries given above, and the packet is sent to 201.66.43.33. Look at the routing table, this does not match any route except the default route. The static route sends it through router 201.66.39.254 (trip 1), however, the router knows that all packets sent to subnet 201.66.43.0 should pass through 201.66.39.253, so it forwards the packets to the appropriate router (trip 2). However, if the host directly sends the packet to 201.66.39.253, the efficiency will be improved (trip 3).
Because the router sends the packet back from the same interface, it knows that there are better routes, and the router can instruct the host to use the new route through ICMP redirection. Although the router knows that all packets sent to the 201.66.43.0 subnet should go through 201.66.39.253, it usually only sends ICMP redirects for a specific host (in this case 201.66.43.33). The host will create a new table entry in the routing table:
Purpose Mask Gateway Flag Interface
201.66.43.33 255.255.255.255 201.66.39.253 UGHD eth1
Note flag D, set this flag for all routes created by ICMP redirection. In the future, such packets will be sent over a new route (trip 3).
6、RIP
RIP is a simple internal routing protocol that has existed for a long time and has been widely implemented (UNIX routed uses RIP). It uses the distance vector algorithm, so its routing is based on the number of "hops" between two points, and passing through a router is considered a hop. Both the host and the gateway can run RIP, but the host only receives information and does not send it. Routing information can be requested from a specified gateway, but is usually broadcast every 30 seconds to maintain correctness. RIP uses UDP to communicate between the host and the gateway through port 520. The information transmitted between gateways is used to establish a routing table, and the routes selected by RIP always have the least number of hops to the destination. RIP version 1 works well in simple, smaller networks, but in larger networks there are some problems, some of which have been corrected in RIP version 2, but some are due to limitations caused by its design. In the following discussion, when applicable to both versions, RIP v1 and RIP v2 refer to specific versions.
RIP does not have any concept of link quality, all links are considered the same, and low-speed serial links are considered the same as high-speed fiber links. RIP selects the route with the smallest number of hops, so when selected among the following two routes:
100Mbps fiber link, router, and then 10Mbps Ethernet
9600bps serial link
RIP will select the latter. RIP also does not have the concept of link traffic level. For example, for two Ethernet links, one is busy and the other has no data stream at all, RIP may choose the busy link.
The maximum number of hops in RIP is 15, and if it is greater than 15, it is considered unreachable. Therefore, in large homemade systems, the number of hops is likely to exceed 15, and it is unrealistic to use RIP. RIP v1 does not support subnets, and the information exchanged does not contain a subnet mask. The methods of determining the subnet mask for a given route are different. RIP v2 makes up for this disadvantage. RIP updates information every 30 seconds, so it may take some time for information to spread on large network interrupted links, the routing information may be stable for longer, and routing loops may occur during this period. There are some solutions to this, but not discussed here.
As can be seen, RIP is a simple routing protocol with some limitations, especially in version 1. However, it is often the only option for some operating systems.