ARP protocol implementation principle
Author
ARP is an abbreviation for Address Resolution Protocol. Chinese translation is "address resolution protocol", the essence is a mapping of network address to physical address. It is to find a mapping method F to make "physical address = f (network address)". There are two basic types of physical addresses: Ethernet types and PRONET token ring network types, network addresses refer to IP addresses, and the requirements for mapping methods are efficient. Specific to Ethernet, it uses a dynamically binding conversion method. Why don't you use the same address directly, but so trouble? Because the TCP / IP network is invented to interconnect different types of computers, its architecture is hierarchical, and the layer and layers are independent, and the implementation of the physical layer does not affect the network layer.
32-bit IP address to the mapping of the Ethernet 48-bit physical address, the method of dynamic binding conversion encounters many details, such as reducing broadcast, ARP packet loss, physical address change (replacement network card), mobile (mobile device to Another subnet), disappear (shutdown), etc. These issues are typically set up by learning, aging, updating, and overflow algorithms to process ARP mapping tables. Among them, learning refers to ARP receiving an ARP / IP package that points to the IP address of this node, extracts the address pair, and no corresponding item in the ARP cache is added, and the ARP receiving part is added; the aging refers to each setting life domain, so that Metabolize the old address mapping item; update the ARP to extract the new address pair, use the corresponding item in which the cache has been used; the overflow algorithm is indicated by the buffer, and what method replaces the old address pair.
I found several TCP / IP source code, compared to their implementation, deep sense, flexible and flexible. Some code does not implement the ARP cache, only the IP address of the source of the global variable record the source of the source, and the global variables are directly operating before each communication. This is in use 51 single-chip microcomputer, which is not lost as a valid scheme when the point communication is performed; And some code is large and complex, and the details are treated. For example, an ARP cache, support multiple access nodes, support network management view / dynamic change ARP-related parameters, retransmission processing, support IPv6, etc. My opinion is that the essence of ARP is address transformation, as long as this soul is grasped, the direction of design is grasped. The specific implementation of the process is characteristic, depending on person, there is no uniform requirement, some functions can not be achieved, some advantages cannot be both, and the only constant is only thought.
I refer to several existing IP protocol stacks and combined with the characteristics of the 51 single-chip microcomputer, I have implemented my own UCOS51-based TCP / IP protocol stack scheme. It is just a specific implementation example, and the different people have different design methods. I guarantee that my program can use and have better intensity.
------------------------------
| Status | Life TTL | IP Address | MAC Address | Learning
------------------------------
| 0 | ff | x: x: x: x | xxxx | <--- aging
------------------------------
| 0 | ff | x: x: x: x | xxxx | Update
------------------------------
Figure 1 ARP Cache Table Full Process
As shown in Figure 1, the ARP cache table consists of 4 fields of state, life, IP address, and MAC address. The status field indicates whether the address is valid (0-idle 1-occupied); life field is used for aging operation, initial depositing maximum, after the OS time function is called, minus 1 per second, until 0 clearance; IP address and Mac The address field saves the mapping of the network address and physical address. Here, there is no design to send a data linked list header and a reorganization number, I will hand over the loop operation to the upper software unified processing, which is the characteristic of this program. Focusing on the ARP cache table, completed 4 operations: learning, aging, updating, formation processing, detailed information listing. Use the OS's shell command ls to view the contents of the ARP table, but do not support modifications, this feature is useful for testing. (Displayed, as shown in Figure 2)% LS
ARP TABLE:
STATUS TTL IP Address Mac Address
================================================================================★
01 78 172.18.92.86 0050BABD4C7E
%
Figure 2 ARP Cache Table Display Content
Full processing
|
v ARP request
---------------------------->
| | Learning / Update | | <- - - -
Aging ---> | ARP Table | <------------ | ARP Processing |
| | | | | - - - ->
------------------------------
^ ARP response
| Learning / Update
---------
| | |
| Ip_in |
| | |
---------
Figure 3 ARP processing process
0 8 16 24 31
-------------------------------------------------- -------------------
| Hardware Type | Protocol Type |
-------------------------------------------------- -------------------
| Hardware Address Length (HLEN) | Protocol Length (Plen) | Operation |
-------------------------------------------------- -------------------
| Send Side Department (eight group 0-3) |
-------------------------------------------------- -------------------
| Send Side section (eight groups 4-5) | send party IP address (eight group 0-1) | ------------------------ ---------------------------------------------
| Transmitter IP address (eight groups 2-3) | Target head (eight group 0-1) |
-------------------------------------------------- -------------------
| Target head (eight groups 2-5) |
-------------------------------------------------- -------------------
| Target IP address (eight groups 0-3) |
-------------------------------------------------- -------------------
Figure 4 ARP package structure
As shown in Figure 3, the entire ARP process, I mainly use 5 functions. ARP initialization (ARP_INIT), ARP request (ARP_REQUEST), ARP Answer (ARP_ANSWER), ARP response processing (ARP_PROCESS), IP packet receives pre-processing (IP_IN). After the network card driver is implemented, all ARP processing operations are filled in the ARP package (the ARP package structure is shown in Figure 4), see the vincea list.
ARP_INIT completes the initialization of the ARP table, summarizing the ARP table state field clear 0.
ARP_REQUEST completes the ARP request operation. The ARP protocol requires the program to determine if the IP address belongs to the same subnet according to the subnet mask. If the ARP requests the destination MAC address, otherwise the default gateway MAC address is requested.
ARP_ANSWER is relatively simple, just exchange the ARP request packet address content, fill in your own MAC address and rarely send it.
ARP_PROCESS completes the information processing that ARP responds back. Mainly in the ARP table learning and update.
IP_IN Completes IP packets receive prerequisites for extracting address mapping information for active learning and updating in time. My program doesn't actively learn the MAC address information that is sent to my IP address, because the capacity of the ARP table is limited in 51, only the frequently used address should be stored inside, otherwise "bump", ARP table It is invalid.
Some ARP implementations use data drivers, parameters configurable, and unified programs, perform different operations by loading different configuration data. This makes the program version uniform, and different applications can be used to load different configuration data, which does not need to replace the program, which is conducive to later maintenance. But considering 51 resource tensions and security, my solution can only display the ARP table does not allow for modification of its content, and users can play an imagination to add new features here. In addition, the ARP program should remember the last request to avoid retransmission, but also considering the nervousness of resources, it is also free. In fact, it doesn't matter, retransmit it. Full processing is fast and effective. In addition, this program cannot be used directly in embedded gateway products.
The UCOS51 operating system itself provides a good memory management function, I use it to set up different types of packets in large medium and small buffers. Before memory use, release, release, effectively utilize resources.
The system features are: 1. Prepatter priority; 2. Message driver; 3. Serial server mode.
The system advantages are: 1. Waiting for CPU resources when waiting; 2. Take timeout protection, no dead lock; 3. Thoughts are clear and easy to understand.
Based on interrupt drive, the system is used to use INT0 to do a network card interrupt input port. The ISR register is only 4 bits: OVW receordment error / TXE send success / PRX reception successfully / PRX reception. The TCP / IP protocol stack makes a task, detached from the kernel. The overall framework is shown in Figures 5, 6, and 7. Main program framework see the voweta list (RxSem and TXSEM initialize 0) ----------
| NIC interrupt |
------------
|
V
------------ |>>
| Send Semicone | | Receive / Advance Wrong
| SEMPOST | ----> -------------- RxSempost
------------ |>>
| | Failure / send is interrupted
----------> -------------- TXSEMPOST
Figure 5 Network card interrupt handler
enter
| ------
V |
---------- | Low priority
------> | Wait | <---
| | TXQPEND | <-------------------------
| ------------ | | | |
| | TXQFIFO non-empty | | | |
| V | --- <--- | | --- <---
| ---------- | Data Source | | Data sent by each task
| | Send Pack | | | | |
| ---------- | -----
| | | | TXQFIFO
| V |
| --------------------- |
| | Release Memory | |
| | (Package has been deposited in NIC RAM) | |
| --------------------- |
| | ----- |
| V | | | | |
| ----------- | | |
| | Waiting | <- | (Equivalent Send Pack is abandoned)
| | TXSEMPEND | <----------- |
| ----------- | | |
| | Fail / timeout | |
| V | |
| Y ---------------- ----------- |
- <--- | Is it successful? | | Reissue Nth | |
| (No error and no timeout) | | n ------------------------- | | N / ^ / | V n | | ------------------> ------ || I have sent N times? | ----------> -------- --------------- Y Figure 6 Sending flow chart enter | ------ V | | ----------- | High priority ------------------> | Wait | <- | ---------> | RXSempend | <--------------- | | ----------- / | / / | / | | | Receive package or | | | | V Get the wrong or | | | | | | | Timeout | | | | ----------- | ---------- | | | Save a clear ISR | | | Reset NIC | ----------- | ----------- | ---------- | Rsempost | | | | / ^ / / ^ / ----------- | V | | | | | - -------------------- | | | | | | Timeout and no new package and error free | Y | | | | | | | (Anti-dead lock) | -> - | | | | - -------------------- | | / | / | (Not executed | N | | | | RXSEMPOST) V | | | | ------------ Y | | | | Advance errors | ---> --------- | | | | ISR Ovw | | | Y | N ------------ | ------------------ | N | | Is there a package in the NIC? | V | | Curr! = Bnry 1 | ------------------------ Y | ------------------ | Read the header, check if there is no logic | ---> ------- | -------------------------- / | / | N | V | -------------------------- ---------- | Apply for a suitable large middle by package length | | Release memory | | Trumpet memory, and deposit the entire package | ---------- |, adjust bnry | / ^ / / ^ / ------------------------ | | | | | V | | N ---------------------------- | --- <--- Is it a package that is sent to my IP address? | | ---------------------------- | | Y | V | ---------------- | | Package distribution | | ---------------- | | | | V | ---------------------------- | | | | | | | | V ------------------------ ip_in filtering | | V v v | ARP ICMP (PING) UDP TCP | | | | | | | | ---------------------------- | | Serial processing | | (32Bitmcu can be designed into concurrent mode) | ----------- <------------- Figure 7 Receiving flow chart I have taken a closer look, it seems to be more complete, and it can work normally in a variety of circumstances. Under overload flow, will only be coated, and will not crash. Of course, because the contact information is limited and personal limitations, there must be mistakes and omissions, I hope that everyone will make comments and suggestions. Pseudo code list: ARP_INIT () // ARP Cache Initialization { For (i = 0; i Arptable [i] .status = 0; } ARP_REQUEST // ARP request { / / Determine whether the IP address belongs to the task of the same subnet to the upper layer software processing / / ((By determining the requesting network card IP address or the default gateway IP address), // This is conducive to reducing the amount of code. // Apply for small memory PARP = OSMEMGET (); // Fill in Ethernet Frame Ethernet protocol = 0x0806; // ARP protocol Destination MAC address = 0xfffff; // broadcast address Source MAC address = its own MAC address; // Fill in the ARP table Hardware type = 0x0001; Protocol type = 0x0800; Hardware address length = 0x06; Protocol length = 0x04; Operation = 0x0001; // Request Sending a header = its own MAC address; Transmitter IP address = source IP address; Target head = 0x0000; Target IP address = destination IP address; // Fill Pad No content is filled in 0; // Send an ARP package to TXQFIFO cache Osqsend (QID, * PARP); } ARP_ANSWER (* PARP) // ARP Answer { Learn / update the ARP cache table; / / Modify the received ARP package to form an ARP response // Fill in Ethernet Frame Destination MAC address = source MAC address from the other party (NIC / gateway); Source MAC address = its own MAC address; // Fill in the ARP table Target head = sending party header; sending header = own MAC address; Exchange sender IP address and target IP address; Operation = 0x0002; // ARP response // Send an ARP package to TXQFIFO cache Osqsend (QID, * PARP); } ARP_PROCESS (* PARP) // ARP Answer Processing { // Update For (i = 0; i IF (arptab [i] .status == 1) { IF (arptab [i] .ipadr == received ARP response package source IP address) { Arptab [i] .ttl = maximum life; Arptab [i] .ipadr = source IP address of the package received; Arptab [i] .macadr = The source MAC address of the package received; Return; } } } //Learn For (i = 0; i IF (arptab [i] .status == 0) { Arptab [i] .status = 1; Arptab [i] .ttl = maximum life; Arptab [i] .ipadr = source IP address of the package received; Arptab [i] .macadr = The source MAC address of the package received; Return; } } // Full-processed fast algorithm for processing, lossy performance Arptab [index] .status = 1; // Note: index is a global variable to save the ARP cache entry index. Plus 1 sample each time. Arptab [index] .ttl = maximum life; INDEX ; IF (INDEX> = arptabsize) index = 0; } IP_IN (* PIP) // IP package filter (ARP address learning) Note: The IP package is processed here, the pseudo code is similar to the above program, but the source code is very different. { // Update For (i = 0; i IF (arptab [i] .status == 1) { IF (arptab [i] .ipAdr == received IP package source IP address) { Arptab [i] .ttl = maximum life; Arptab [i] .ipadr = source IP address of the package received; Arptab [i] .macadr = The source MAC address of the package received; Return; } } } //Learn For (i = 0; i IF (arptab [i] .status == 0) { Arptab [i] .status = 1; Arptab [i] .ttl = maximum life; Arptab [i] .ipadr = source IP address of the package received; Arptab [i] .macadr = The source MAC address of the package received; Return; } } // Full-processed fast algorithm for processing, lossy performance Arptab [index] .status = 1; // Note: index is a global variable to save the ARP cache entry index. Plus 1 sample each time. Arptab [index] .ttl = maximum life; INDEX ; IF (INDEX> = arptabsize) index = 0; } Timer () // Soft timer task for ARP aging { For (;;) { Taskdelay (1 second); For (i = 0; i IF (arptab [i] .status == 1) { IF (arptab [i] .ttl == 0) Arptab [i] .status = 0; Else Arptab [i] .ttl--; } } } Main program frame: InitNic // Initialization Network Card // Create resources TxSem and RxSem semaphore TXQFIFO queue Large and medium-sized memory // Create a task Collect hair . . . references: 1. "Internet interconnection with TCP / IP" (3rd Edition), first, second, three volumes Douglas E.Comer Electronic Industry Press 2. Www.laogu.com 3. Www.sics.se/~adam/LWIP/ of UIP6 Qi Comments on this article 0 articles have been viewed 477 times