Different kernel protocols do not directly use the functions provided earlier to access the routing tree. Instead, they call several functions: rtalloc and rtallocl, which are used to query route tables.
The rtrequest function is used to add or delete route table entries. In addition, most interfaces call the rtinit function when the interface is connected or disconnected.
Optional messages are transmitted in two directions. The process (route command) or daemon (routed or gated) writes the selected message to the selected plug-in port to add the kernel
Route, delete route, or modify an existing route. When a time occurs, such as interface disconnection or redirection reception, the kernel also sends a routing message. Process Routing
Plug-in to read the content they are interested in.
The kernel also provides another interface for accessing the route table, namely the sysctl call of the system.
1. rtalloc and rtalloc1 Functions
Generally, the route table is queried by investigating the rtalloc and rtalloc1 functions. Rtalloc calls rtalloc1 and rtalloc1 calls the rnh_matchaddr function.
For Internet addresses, this function is the rn_match function.
The general process of rtalloc1 is as follows:
Rn_match is called. If the following three conditions are met, the query is successful.
1) The route table of this protocol family exists.
2) rn_match returns a non-null pointer; and
3) The rnf_root flag is not set for the matched radix_node structure.
If the search is successful, the pointer pointing to the matched radix_node structure is saved in RT. If the second parameter of the call is not 0 and the matched route table has
Rtf_cloning flag, call the rtrequest function to send the rtm_resolve command to create a new rtentry structure, which is the query result
Clone.
2. Macro rtfree and rtfree Functions
Macro rtfree: The rtfree function is called only when the reference count is less than or equal to 1. Otherwise, the function only degrades the reference count.
3. rtrequest Function
The rtrequest function is the key point to add and delete route table entries. It provides some other functions that call it.
Rtrequest is a switch statement. Each case corresponds to one command: rtm_add, rtm_delete, and rtm_resolve.
For the rtm_delete command:
1. delete a route from the routing tree
2. Delete the reference to the network route table entry.
3. Call the interface to request a function. If this table item defines the ifa_rtrequest function, this function is called. ARP uses this function.
4. Return pointer or delete reference. If the caller needs to delete the rtentry structure pointer in the routing tree, the pointer is returned, but the table cannot be released at this time.
This requires you to call rtfree to delete the table after the table item is used.
For the rtm_resolve command:
Only rtalloc1 can carry this command parameter to call this function. Only when a new table item is cloned from a table item with the rtf_cloning flag,
This is only true for rtalloc1.
This command will jump to the makeroute mark and continue to be executed.
For the rtm_add command:
Locate the corresponding interface. Find an appropriate local interface and return a pointer to the ifaddr structure of the interface.
Go to the makeroute mark and run the command.
The approximate process of makeroute is as follows:
1. Allocate storage for route table items. Allocate the rtentry structure.
2. Allocate and copy the gateway address.
3. Copy the destination address.
4. Add Table items (rtentry structure) to the routing tree ).
5. Save the interface pointer. Increment the reference count of the ifaddr structure and save the pointers of the ifaddr and IFNet structures.
6. New cloned route replication metric. If it is rtm_resolve, the entire measurement structure in the cloned table item is copied to the new table item. If yes
Rtm_add, then the caller can set this metric value after the function returns.
7. Call the interface to request a function. If the ifa_rtrequest function is defined for this table item, this function is called.
8. Return the positive value and increase the reference count. If the caller needs to change the pointer of the new structure, return the pointer and increase the reference count value from 0 to 1.
4. rtinit Function
When the Internet Protocol adds or deletes a route for the relevant interface, there are four calls to rtinit.
1. When setting the destination address of the point-to-point interface, in_control calls rtinit twice. Call the rtm_delete command for the first time to delete all existing
The route entry of the destination address. The second call specifies the rtm_add command to add a new route entry.
2. in_ifinit calls rtinit to add a network route for the broadcast network or add a host route for the point-to-point link. If the route is added to the Ethernet interface.
3. in_ifscrub calls rtinit to delete the existing routes of an interface.
The approximate process of the function is as follows:
1. Obtain the destination address for the route. If it is a route to a host, the destination address is the other end of the point-to-point link. Otherwise, we are dealing with
The destination address of a network route is the unicast address of the interface.
2. If you want to delete a route, you must search for the destination address in the route table and obtain its route table.
3. Call rt_request to execute the rtm_add or rtm_delete command.
4. If the deletion is successful, a routing message is generated.
5. If the value is successfully added but the ifaddr pointer of the new route table entry interface is not the same as the call parameter, an error occurs. Perform the following steps: Output
Error message. If the ifa_rtrequest function (RT-> IFA-> ifa_rtrequest) in the rtentry obtained by rt_request is called with the rtm_delete Parameter
It. If the ifa_rtrequest function is defined by IFA in the function parameter, call it with rtm_add as the parameter. Finally, a routing message is generated.
5. rtredirdbms Function
After receiving an ICMP redirection, icmp_input calls rtredirect and pfctlinput. The next function calls udp_cltinput and tcp_ctlinput.
The function traverses all UDP and TCP Control Blocks (PCB ). If the PCB is connected to an external address and the direction to this external address has been changed, and
The PCB holds the route to the external address and calls rtfree to release the route. The next time you use these control blocks to send IP data reports of external addresses
Rtalloc, and find the destination address in the routing table. A new route may be found.
The rtredirect function verifies the information in the redirection and immediately updates the route table to generate a route entry message. The approximate process of this function is as follows:
1. New routes must be directly connected; otherwise, the redirection fails.
2. Find the route table entry of the target address and verify the redirection. Call rtalloc1 to find the route of the destination address in the routing table. When verifying redirection, the following conditions must be
True:
The rtf_done flag must not be set.
Rtalloc must have found a route table entry to DST.
The IP address of the router sending the redirection must be the rt_gateway currently set for the destination address.
The interface of the new gateway must be the same as the interface currently set for the destination address, that is, the new gateway must be on the same network as the current Gateway.
The new Gateway cannot change the route to this host to itself. That is to say, there cannot be an interface that is connected to the gateway with the same unicast or broadcast address.
3. Create a new host route. If the current route to the destination address is a network route and the redirection is host redirect instead of network redirection
This destination address creates a host route (call rtrequest, rtm_add) without having to manage the existing network route.
4. Change the existing host route. When the current route to the destination address is already a host route, you do not need to create new table items, but modify existing table items.
5. rt_missmsg generates an optional plug-in message.
6. Structure of the selected message
A routing Message consists of a fixed length header and a maximum of eight plug-in address structures. The Fixed Length header is one of the following three structures:
Rt_msghdr
If_msghdr
Ifa_msghdr
The data types and meanings of the first three members of the three header structures are the same: message length, version, and type. Each structure has
One member can encode the first eight possible plug-in addresses: rtm_addr, ifm_addrs, and ifam_addrs. They are all bit masks.
The most common structure, rt_msghdr, is provided.
The if_msghdr structure is used for rtm_ifinfo messages.
The ifa_msghdr structure is used for rtm_newaddr and rtm_deladdr messages.
The three variables rtm_addrs, ifm_addrs, and ifam_addrs are bit masks. They define the plug-in address structure after the header and provide the bit mask.
Some constants used.
The array subscript used by the kernel references the rt_addrinfo structure, as shown in.
If the rti_addrs member sets the rta_gateway bit, the rti_info [rta_gateway] member is the pointer to the plug-in address structure containing the gateway address.
7. rt_missmsg Function
The rt_missmsg function uses the rt_addrinfo structure, calls rt_msg1, creates a variable-length message for the process in the mbuf chain, and then calls raw_input
This mbuf chain is passed to all related optional plug-ins. The function processing process is as follows:
1. Create a message in mbuf. Rt_msg1 creates the corresponding message in the mbuf chain, and returns the pointer to the chain, which is an mbuf chain created for rt_msg1.
2. Complete Message creation. Set other rt_msghdr members.
3. Set the Message Protocol and call raw_input.
8. rt_ifmsg Function
Rt_ifmsg is called in both if_up and if_down. When an interface is connected or disconnected, this function is used to generate an optional plug-in message. Approximate process of functions
As follows:
1. Call the rt_msg1 function to create a message in the mbuf chain.
2. Complete Message creation. Set other members in the if_msghdr interface.
3. Set the Message Protocol and call raw_input.
9. rt_newaddrmsg Function
When you add or delete an address on an interface, rtinit calls rt_newaddrmsg with rtm_add or rtm_delete as the parameter. Approximate processing of functions
The process is as follows:
1. Call the rt_msg1 function to create a message in the mbuf chain.
2. Complete Message creation. Set other members in the ifa_msghdr interface.
3. Set the Message Protocol and call raw_input.
Details of TCP/IP 2: Implementation-Route Selection request and message