Details of TCP/IP 2: Implementation -- ARP: Address Resolution Protocol

Source: Internet
Author: User

The implementation of ARP in net/3 is closely related to the route table and shows an example to describe arp.


Next, we will give a brief overview of the key points in the figure.

1. The two-way linked list of the llinfo_arp structure contains a small amount of information about each hardware address known to ARP. The global variable llinfo_arp with the same name is the head node of the linked list.
The first la_prev pointer is not drawn pointing to the last item, and the la_next pointer of the last item points to the first item. The linked list is processed every five minutes by the ARP clock function.

2. Each IP address of a known hardware address corresponds to a route table node (rtentry structure ). The la_rt pointer member of the llinfo_arp structure is used to point to the corresponding rtentry

Structure. Similarly, the rt_llinfo pointer member of the rtentry structure points to the llinfo_arp structure.

3. The fourth leftmost route table node in the figure does not have the llinfo_arp structure, which corresponds to the route entry of the local Ethernet (140.252.13.32. The

C-bit is set in rt_flags, indicating that the node is used to copy to another node.

4. The rt_gateway pointer member in the rtentry structure points to a sockaddr_dl structure variable. If the structure sdl_alen member that saves the length of the physical address is 6

The sockaddr_dl structure contains the corresponding hardware address information.

5. The corresponding pointer of the rt_ifp Member of the route node variable points to the IFNet structure of the corresponding network device interface. The two routing nodes in the middle correspond to other hosts on the Ethernet,

Both nodes point to le_softc [0]. The route node on the right points to the loif structure. Because rt_ifp.if_output points to the output function

It is reported to be routed to the loopback interface.

6. Each route Node also has a pointer variable pointing to the corresponding in_ifaddr structure.

7. The la_hold member is a pointer to the mbuf linked list. When you want an IP address to send data reports, you need to broadcast an ARP request. When the kernel waits for ARP to answer

The address information of the header node of the mbuf chain where the data to be sent is stored in la_hold. After receiving the ARP answer, la_hold points to the IP data in the mbuf linked list.

Sent out.

8. The rt_metric variable rmx_expire in the route table node stores the timing information related to the corresponding ARP node to delete the timeout ARP node.


Shows the relationship between ARP functions and other kernel functions.



1. ARP Structure

The format of the ARP Group transmitted over Ethernet is shown in:


The structure ether_header defines the Ethernet frame header; the structure arphdr defines the next five fields, and its information is used to transmit ARP requests on any type of media

And answer; The ether_arp structure not only contains the arphdr structure, but also contains the addresses of the source and target hosts.

The arphdr structure is defined as follows:


Displays the definition of the ether_arp structure, which includes the arphdr structure, IP addresses and hardware addresses of the source and target hosts.


Each ARP node has an llinfo_arp structure. The head nodes of the linked list composed of these structures are allocated as global variables. We often put this linked list

It is called ARP high-speed cache.

 

The first two items of the two-way linked list are updated by the insque and remque functions.


2. arpwhohas Function

Arpwhohas functions are generally called by arpresolve to broadcast an ARP request. It can also be called by each Ethernet device driver

This device sends an address association information (siocsifaddr) to actively send address association information, which can not only detect whether an IP address exists in the Ethernet.

Address conflict, and other machines can update their corresponding information (free ARP ). Arpwhohas simply calls the arprequest function.


3. arprequest Function

The arprequest function is called by the arpwhohas function to broadcast an ARP request. This function creates an ARP group and transmits it to the output function of the interface.

Displays the two data structures mbuf and sockaddr created by the function. There are also two pointers used in functions, eh and EA.


The approximate process of the function is as follows:

1. Allocate and initialize mbuf. Assign the mbuf of the Data header of a group and assign values to the two length fields.

2. initialize the pointer. Assign values to the pointers EA and eh, and assign the value of the ether_arp structure to 0.

3. Fill in the Ethernet frame header. Set the destination ethernet address to an Ethernet broadcast address, and set the Ethernet frame type to ethertype_arp. Different

The constant value of the Ethernet frame type field.


4. Fill in the ARP field. Fill all fields of ether_arp, except the destination hardware address to be queried by the ARP request.

5. Fill in sockaddr and call the interface output function. The value of the sa_family member in the interface address structure is set to the value of ap_unspec and sa_number members.

Set to 16. Call the interface output function ether_output.


4. arpintr Function

When the ether_input function receives an Ethernet frame whose Frame Type field is ethertype_arp, software interruption with a limited level of netisr_arp is generated and

This frame is behind the arpintrq of the ARP input queue. When the kernel processes the software interruption, it calls the arpintr function.

The approximate process of the function is as follows:

Only when the frame hardware type is specified as an ethernet address and the frame length is greater than or equal to the length of the arphdr structure plus two hardware addresses and two Protocol addresses

The frame can be processed. If the Protocol address type is ethertype_ip or ethertype_iptrailers, call the in_arpinput function,

Otherwise, the frame is discarded.


5. in_arpinput Function

This function is called by arpintr to process received ARP requests/answers. The concept of ARP is relatively simple, but after many rules are added, the implementation is compared.

Complex. Let's look at the two typical cases.

1. If you receive a request for a local IP address, send an answer. This is a common situation, obviously we will continue to receive

And then send the packet back to it. Therefore, if we do not have the corresponding ARP node, we should add an ARP node.

You already know the IP address and hardware address of the peer.

2. If you receive an ARP answer, the ARP node is complete at this time, so you know the hardware address of the other party. The address is stored in the sockaddr_dl structure,

All data sent to this address will be sent.

3. If another host sends an ARP request or response, and the sender's IP address is the same as that of the local host, there must be a host with incorrect configuration. NET/3 will detect

The error is returned and a message is sent to the Administrator.

4. If the host receives a request or answer from another host, the corresponding ARP node already exists, but the hardware address has changed, the ARP node will be updated.

5. The host can be configured as a proxy ARP Server. In this case, the host can respond to ARP requests on behalf of other hosts and provide the hardware address of other hosts in the response.

The host corresponding to the destination hardware address in the proxy ARP answer must be able to send the IP datagram to the destination address specified in the ARP request.


The general process of this function is as follows:

1. Search for matched interfaces and IP addresses. Search the Internet address linked list of the Local Machine (the linked list of the in_ifaddr structure ). Remember that an interface can have multiple IP addresses.

2. Verify the sender's hardware address. If the sender's hardware address is the same as the hardware address of the Local interface. This is because you have received a request from the local machine and ignored this group.

If the sender's hardware address is equal to the Ethernet broadcast address, it indicates an error has occurred. Record the error and discard the group.

3. Check the sender's IP address. If the sender's IP address is the same as the IP address that the sender is using, an error occurs in the local system.

4. Search the nodes that match the sender's IP address in the routing table. The arplookup function searches for IP addresses that match the sender in the ARP cache. When the ARP group is in

The destination address is the local machine. It always requires an ARP node.

5. update existing nodes or fill in new nodes. The statement is executed only when the following three conditions are true:

A. Locate an existing ARP node or create a new ARP node.

B. The ARP node points to a route table node.

C. The re_gateway field of the route table node points to a sockaddr_dl structure.

6. Check whether the sender's hardware address has changed. If it has changed, record the sender's hardware address and update the recently resolved ARP node.

7. If the ARP operation is not a request, discard the received group.

8. generate an answer corresponding to the ARP request. ARP responses are generated only in the following two cases:

A. the local host is the target host for this request.

B. The local machine is the ARP proxy server of the target host to be searched for in this request.

9. Fill the sockaddr with the Ethernet frame header.

10. Send the ARP answer to the interface output function and return it.


6. ARP timer function

ARP nodes are generally dynamic. They are created when needed and automatically deleted when timeout occurs. The Administrator is also allowed to create permanent nodes.

The arp_rtrequest function calls the arptimer function for the first time, and then the arptimer is called every five minutes. Arptimer view ARP node linked list

If the node is not a permanent node and the time has timed out, arptfree deletes the node.


7. arpresolve Functions

The ether_output function calls the arpresolve function to obtain the ethernet address corresponding to an IP address. The arpresolve function uses the la_hold of the llinfo_arp Structure

The member pointer "hold" the IP datagram to be sent and sends an ARP request. After receiving the ARP response, the member pointer sends the IP datagram to be sent.

Arpresolve should avoid ARP flooding, that is, it should not repeatedly send ARP requests when no ARP answer is received. In this case, there are mainly two

Cause: first, there are multiple IP datagram to be sent to the same host that has not resolved the hardware address; second, each part of an IP datagram will be used as an independent group

Call ether_output.

NET/3 adopts the following methods to avoid ARP flooding.

1. NET/3 does not send multiple ARP requests corresponding to the same destination within the same second.

2. If no answer is received after five consecutive ARP requests (that is, 5 seconds), set the rtf_reject flag of the route node to 1 to the next 20 seconds.

This will cause ether_output to reject IP datagram sent to the destination address within 20 seconds.

After 3.20 seconds, arpresolve will continue to send ARP requests to the target host.


8. arplookup Function

The arplookup function calls the routing function rtalloc1 to search for ARP nodes in the Internet routing table. We have seen three arplookup calls:

1. In in_arpinput, find the source IP address or create an ARP node after receiving the ARP group.

2. In in_arpinput, check whether there is a proxy ARP node for the destination hardware address after receiving the ARP request.

4. In arpresolve, find or create an ARP node corresponding to the IP address of the datagram to be sent.

If arplookup is successfully executed, a pointer pointing to the corresponding llinfo_arp structure is returned. Otherwise, a null pointer is returned.


9. In ARP, The arp_rtrequest function calls two route table functions. 1. arplookup calls rtalloc1 to find the ARP node. If no matching node is found, a new ARP node is created. 2. arptfree uses the rtm_delete command as a parameter to call rtrequest and delete the route table node of the corresponding ARP node. In addition, ARP commands manipulate ARP high-speed cache by sending and receiving Route packets on the route plug-in. ARP publishes route information with the rtm_resolve, rtm_delete, and rt_get commands. The first two parameters are used to call rtrequest, and the third parameter is used to call rtalloc1. Finally, when the driver of the Ethernet device obtains the IP address assigned to the interface, rtinit adds a network reason, so the rtrequest function is called. The parameter is rtm_add, and the flag is rtf_up and rtf_cloning. Each ifaddr structure has a pointer to the function (ifa_rtrequest member), which is automatically called when a route table node is created or deleted. For all Ethernet devices, in_ifinit points the pointer to the arp_rtrequest function. Therefore, when the routing function is called to create or delete a route table endpoint for ARP, arp_rtrequest is always called. When any route table function is called, the arp_rtrequest function is used to perform various initialization or exit operations. For example, when a new ARP node is created, allocate memory for the llinfo_arp structure in arp_rtrequest. Similarly, after the routing function processes an rtm_delete command, arp_rtrequest deletes the llinfo_arp structure.

Details of TCP/IP 2: Implementation -- ARP: Address Resolution Protocol

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.