Details of TCP/IP 2: Implementation -- IP: Internet Protocol

Source: Internet
Author: User

This chapter describes the structure and basic IP processing processes of an IP group, including input, forwarding, and output. Shows the common organization form of the IP layer.


In the previous article, we saw how the network interface put the IP Group to the IP Input Queue ipintrq and how to call a software interrupt, as shown in:


Because hardware interruption has a higher priority than software interruption, some groups may be put into the queue before a software interruption occurs. During software interruption, ipintr

The function keeps moving from ipintrq and processing the Group until the baseline is empty. In the final destination, the IP address reinstalls the Group as a data packet and directly calls the data packet through the function call.

To the appropriate transport layer protocol. If the group does not reach the destination, and the host is configured as a router, the IP address sends the group to ip_forward. Transmission

The protocol and ip_forward send the group to be output to ip_output. ip_output completes the IP header, selects the output interface, and partitions the group if necessary. Final grouping

Is passed to the appropriate network interface output function.

When an error occurs, the IP address discards the group and sends an error message to the source site of the group under certain conditions. These packets are part of ICMP.


1. IP group

We call the data transmitted from the transport layer protocol to the IP address a packet. A typical packet contains a transport layer header and application data. Shows the transport protocol UDP. IP reporting

Add its own header to form a data packet. If the datagram length in the selected network is too large, the IP address splits the packet into several shards, each of which

Contains its own IP header and a piece of data from the original datagram.


The structure of the IP header is displayed.


Contains the names of members in the IP structure.



The length of the standard IP header is 20 bytes, so ip_hl must be greater than or equal to 5. greater than 5 indicates that the IP option is followed by the standard header. For example, if the maximum value of ip_hl is 15, the maximum value is allowed.

40 bytes. Ip_hl is calculated in 4 bytes.


2. Input Processing: ipintr Function

When the interface puts the group on the ipintrq queue, it calls a Soft Interrupt through schednetisr. When this soft interrupt occurs, if the IP address processing process has been

The kernel calls ipintr. Before calling ipintr, the CPU priority is changed to splnet.

Ipintr is a big function, which is mainly divided into four parts:

1. Verify the arrival Group

2. Option processing and forwarding

3. Group reinstallation

4. Sub-Use

The option processing and reuse reinstallation are complex and will be described as separate chapters in the future.

. Verify that the group is taken out of ipintrq and their content is verified. Corrupt and erroneous groups are automatically discarded. IP version: If no IP address is assigned to the network interface, all IP groups must be dropped. Before ipintr accesses any IP header field, it must confirm that ip_v is the 4. IP header test and: ipintr checks the ip_sum field calculated by in_cksum and saves the header. An undamaged header should have 0 tests. If it is not 0, the group is automatically discarded. The following sections describe in_cksum. 3. byte order ntohs macro converts all 16-bit values in the IP header from the network byte order to the host byte order: group length (ip_len), datagram identifier (ip_id), and part offset (ip_off ). 4. group length: If the logical length of the group (ip_len) is larger than the data size stored in mbuf (m_pkthdr.len), and some bytes are lost, the Group must be discarded. If the mbuf score is large, remove the extra bytes. A common cause of byte loss is that the device discards the byte because the data arrives at a serial device with no or few caches. Redundant bytes may be generated. For example, on an Ethernet device, when an IP group is smaller than the minimum length required by Ethernet. The extra bytes added to the sent frame are discarded here. 2. Option processing and forwarding

Next, call ip_dooptions to process IP options, and decide whether the group reaches its final destination. If the group does not reach the final destination, net/3 will

Try to forward the group; if the group reaches the final destination, it will be delivered to the appropriate transport layer protocol.

1. Option Processing

Ip_dooptions: processing option. If ip_dooptions returns 0, ipintr continues to process the group. Otherwise, ip_dooptions forwards or discards the group

Ipintr can process the next group in the input queue.

After processing the option, ipintr compares ip_dst in the IP header with the IP addresses of all configured local interfaces to determine whether the group has reached the final destination. Ipintr

You must consider several broadcast addresses, one or more unicast addresses, and any number of multicast addresses related to the interface.

2. Forwarding

If ip_dst does not match all addresses, the Group has not reached the final destination. If Forwarding is not prepared, the group is discarded. Otherwise, ip_forward tries to route the Group route.

To its final destination.

2. 3. Group reinstallation and sub-Use

The ipintr function is used to reorganize and group groups. The following sections will detail the functions.

Finally, ipintr calls the pr_input function in the selected protosw structure to process the transport packets contained in the datagram. When pr_input returns, ipintr continues to process the transport packets in ipintrq.

Next group.

3. IP header check: Calculate the in_cksum function IP header checksum: 1. Clear the checksum field. 2. Then, the binary anticode summation is performed for every 16 bits (2 bytes). The anticode summation means that the summation of every 16 bits is performed first, and then the obtained sum is converted into an anticode. Next, we will describe the steps for reverse code summation in detail: Look at the following basic code algorithms:

SHORT checksum(USHORT* buffer, int size){    unsigned long cksum = 0;    while(size>1)    {        cksum += *buffer++;        size -= sizeof(USHORT);    }    if(size)    {        cksum += *(UCHAR*)buffer;    }    cksum = (cksum>>16) + (cksum&0xffff);     cksum += (cksum>>16);     return (USHORT)(~cksum);}

The buffer parameter is a 16-digit integer pointer, starting from the IP address header. The parameter size is the size of the IP address header. The while loop is to add the content of the IP header with 16 bits as the unit. If there is no division (that is, the size has less than 16 bits), the remaining part is added, in this case, cksum is the result after the sum. The result is usually more than 16 bits. Because the checksum is 16 bits, we need to re-process the high 16 bits and the calculated cksum. The first step of re-processing: cksum = (cksum> 16) + (cksum & 0 xFFFF); sum> 16 is to shift the 16-bit high to 16-bit low, sum & 0xffff is used to retrieve the lower 16 bits and add the new cksum. The second step is processed: cksum + = (cksum> 16). During the first step, a carry is likely to be generated, so we need to move the carry to a lower 16 bit to add it. In this way, the processing is completed. The next step is to take the inverse and forcibly convert it to 16 bits. In this way, the final checksum is obtained. After the verification is calculated, the next step is how to verify: When the Receiver performs the verification, it also calculates the binary inverse code for every 16 bits. Compared with the sender's checksum header, the receiver's checksum header adds a checksum calculated by the sender. Therefore, if there is no error in the first part during transmission, the result calculated by the receiver should be "all", because the value obtained by the receiver in addition to the checksum is the checksum inverse code, the extra checksum is of course full. Finally, we will give an example of the above process: IP header: 45 00 00 31 89 F5 00 00 6e 06 00 00 (verification field) de B7 45 5D-> 222.183.69.93 C0 A8 00 DC-> 192.168.0.220 calculation: 4500 + 0031 + 89f5 + 0000 + 6e06 + 0000 + deb7 + 455d + c0a8 + 00dc = 3 22c4 0003 + 22c4 = 22c7 ~ 22c7 = dd38-> This is the checksum to be filled. When an IP packet is received and the IP Address Header must be checked correctly, the IP address header is checked. The method is as follows: calculation: 4500 + 0031 + 89f5 + 0000 + 6e06 + dd38 + deb7 + 455d + c0a8 + 00dc = 3 fffc 0003 + fffc = FFFF the result is correct.
Related Article

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.