One, the introduction

Route selection is one of the most important functions of the IP layer. The core point of this route selection rule is that each host maintains a routing table. Routing at the IP layer is actually a routing mechanism that searches the routing table and decides which network interface to send packets to.

Second, the principle of

In the routing table, there are certain search rules to determine the network port to which the packet is sent:

  • Search for the matched host address first.
  • If no network address is found, search for a matching network address.
  • If no, search for the default entry. The default entry is usually specified as a network entry in the routing table, and its network number is 0.

You can use netstat -rn to list the routing table for the current host:

  • Column 1 (Destination) : Specifies the Destination and searches the column.
  • Column 2 (Gateway) : Gateway address of the network/host.
  • Column 3 (Genmask) : subnet mask of the host/network.
  • Column 4 (Flags) : flag bits. There are 5 different flag bits
    • U: If the value exists, the current routing entry is available. If the value does not exist, the current routing entry is unavailable.
    • G: The value indicates that the current route is to a gateway (router), but does not indicate that the destination is directly connected to the host.
    • H: If the value exists, the destination of the current routing entry is a host. If the value does not exist, the destination of the current routing entry is a network.
    • D: If the value exists, the entry is created by routing redirection packets.
    • M: If this value exists, the entry is modified by redirection packets. If no, the entry is modified.
  • 第5 column (MSS) :Default maximum segment size for TCP connections over this route
  • Column 6 (Window) :Default Window size for TCP connections over this route.
  • Column 7 (IRTT) :Initial RTT (Round Trip Time). The kernel uses this to guess about The best TCP protocol parameters without waiting on (possibly slow) answers.
  • Column 8 (Ifac) : Which network interface the route entry belongs to (a host can have multiple network interfaces)

3. Create routing tables

It has never been said how these routing tables are created. Here is how to create routing tables. There are three ways to create routing tables -route (active addition), ICMP redirection error packets (passive modification), and ICMP route discovery request/reply packets (active query).

1. The routing table can be passed during system initializationrouteCommand to add a default route.

2. Use ICMP to redirect error packets

When an IP packet should be sent to another router, the receiving router sends an ICMP redirection error message to the sender of the ICMP packet. ICMP redirection packets can only be generated by routers and used by hosts.

  • The host wanted to send an IP packet to R2, but only found the entry of R1 by searching the routing table. The host sends IP packets to R1
  • R1 receives the IP packet through network port A.
  • R1 searches for the entry from its routing table to R2 and finds that the forwarding port is also A. An ICMP error redirection packet is sent to the source (host) of the IP packet.
  • R1 forwards IP packets to R2.
  • After receiving the ICMP error redirection packet sent by R1, the host modifs its routing entry based on the packet. IP packets sent from the host to R2 can be sent directly.

Redirection is generally used to allow hosts with little routing information to gradually build a more complete routing table. There can be only one default entry in the routing table when the host is started. If the default route fails, the default router notifies it to redirect and allows the host to make changes to the routing table.

ICMP error redirection packet format is as follows:

Recipients of ICMP redirect packets must look at three IP addresses :(1) the IP address that caused the redirect (the destination address of the original IP packet); (2) IP address of the router that sends the redirection packet (the next-hop address in the host routing table); (3) The IP address of the router to be used (the IP address to be changed).

3. ICMP routes discover request/reply packets

The host broadcasts or multicasts an ICMP router request message after booting. One or more routers respond to a router reply message. In addition, routers periodically broadcast or multicast their router reply messages, allowing each listening host to update its routing table accordingly.

The IP address must be an address of the sending router. Priority is a signed 32-bit integer that indicates the priority of the IP address as the default router address, compared to other routers on the subnet. A larger value indicates a higher priority. The priority is 0 x 8 0 0 0 0 0 0 0 0 0 0 indicates that the corresponding address cannot be used as the default router address, although it is also included in the advertisement message. The default value of priority is 0.

See here, do you want to scan the QR code to follow the wechat public account Linwan Village Dragon cat.