IP module assembles network packets and forwards network packet links

Table of contents

introduction

network packet

The composition of network packets

​Edit network packet forwarding

forwarding device

General process

Edit ip module to send network packets

Add header control information of network packets

Add the sender's IP address to the IP header

Routing table lookup rules

​Edit to add protocol number

Add mac header

Edit arp protocol to convert ip address to mac address

Edit arp cache

arp cache invalidation

​Edit the sending and receiving corresponding to the ip module

send

accept

Definition of responsibilities


introduction

The previous article in the protocol stack series explained the processing of the protocol stack module such as connecting, sending and receiving network packets, and disconnecting. However, the upper layer of the protocol stack will entrust the ip module to perform the actual processing.

network packet

The composition of network packets

A network packet consists of control information in the header and transmission data following the header.

The control information represents the destination to which the packet is sent, and the transmission data is the data to be sent.

Forwarding of network packets

The ip module will send the packet to the nearest forwarding device. The forwarding device will find the next forwarding device to be sent based on the control information in the header: a table is stored inside the forwarding device to record the sending direction corresponding to each address. That is to say, according to the destination address recorded in the header, query the next forwarding device to be sent in the table , and then when reaching the next forwarding device, the same query is performed and then sent. After forwarding one after another, the destination will finally be reached.

These are applicable to all communication methods, but tcp/ip will be more complicated.

forwarding device

As mentioned earlier, there are two different forwarding devices, routers and hubs , in the subnet . They have their own division of labor when transmitting network packets. The hub transmits according to Ethernet rules, and the router transmits according to IP rules; the header information will carry the mac header and ip header.

  • The router determines the location of the next router based on the target address (there is an internal routing table that stores the IP address of the router closest to the server IP )
  • The hub transmits the network packet to the next route in the subnet (the hub also has a table called the Ethernet protocol table because it works according to the mac address, so this table records the sending direction of the corresponding mac address )

General process

The sender writes the server's IP address into the IP header ; the IP protocol first finds the next router IP address to be sent based on the server IP, and then fills the mac address corresponding to this IP into the mac header, entrusting the Ethernet protocol. to send;

Next, when the packet is sent, it will go through the hub. The hub can find the next router through the MAC address filled in by the IP protocol and forward it to the corresponding router (there is a table inside);

After the next router receives it, it erases the mac header address, and then finds the next router IP according to the IP protocol. After finding the corresponding mac address, fill it in the mac header and entrust the Ethernet protocol before sending it.

By analogy, the server address is reached.

The purpose of each router is to find the IP of the next router, convert it into a mac, fill it into the mac header of the packet, and forward it to the next address through the hub until it is transmitted to the server

Illustration:

The Ethernet part can also be replaced by other things, such as wireless LAN, ADSL, FTTH, etc. They can all replace the role of Ethernet and help the IP protocol to transmit network packets. It’s just that the MAC header information that Ethernet relies on is not filled. When other networks other than Ethernet are used for transmission, the MAC header will also be replaced with other headers suitable for the selected communication specifications.

The ip module sends network packets

Add header control information of network packets

Although the tcp module entrusts the ip module to send data, the ip module also uses the forwarding devices entrusted by the network card to send it (explained later when analyzing the network card)

Therefore, the starting point of the packet sending and receiving operation is the operation of the TCP module entrusting the IP module to send packets.

The process of this delegation is that the TCP module adds TCP header information in front of the data block and passes the entire server IP to the IP module. This part is the content of the network packet sent by the tcp module.

Then the ip module fills in the ip header with the server ip sent through the tcp header. And query the ip and mac address mac header of the next forwarding device.

The IP header contains the control information required by the IP protocol to send the packet to the destination according to the IP address (server ip address); the MAC header contains the control required to transmit the packet to the nearest router through the Ethernet LAN. Information . (mac address of next router)


The tcp module adds tcp header information based on the original data, and the ip module adds ip header and mac header in front of the tcp header.

Add the sender's IP address to the IP header

The IP address is essentially assigned to the network card, but the computer has a network card integrated so it can also be said to be the computer's IP address.

A network card corresponds to an IP address. When there are multiple network cards on the computer, you must choose which network card (which IP address to use) to send network packets .

The "Receiver IP Address" in the IP header fills in the IP address of the communication partner.

The sender's IP address needs to determine the network card used for sending, and fill in the IP address of the network card .

The router uses the ip protocol to find the ip of the next router through the internal table. The ip module of the protocol stack also uses the ip protocol, so the two operations should be the same.

The ip module determines which ip address to use for sending by using routing tables

You can use the route print command to display the routing table and entrust the network card hardware to send data.

Routing table lookup rules

The first column destnation represents the server address

gateway represents the IP address of the next router to be sent, also called the gateway.

interface represents which network card the sender uses and which IP address sends the packet to the gateway

If gateway and interface are the same, ze represents the final destination.

  • First, find the network destination in the routing table through the destination ip address and find the corresponding entry (for example, if the destination ip is 195.6.32, then the destination found may be 195.6
  • If gateway and interface are the same, then the router can directly send it to the destination IP address of the recipient.

Select the IP address of the network card to be used through the interface and send the packet to the gateway (next router). If the two are the same, it means that the current router can send it directly to the recipient IP. There is no need to pick up the next router and forward it (that is, in the same subnet)

Add protocol number

Indicates which module the package content comes from.

If the content is delegated by the TCP module, it is set to 06 (hex). If it is the content delegated by the UDP module, it is set to 17 (hex). These values ​​are set according to the rules. Today's browsers all use tcp to send, so set it to 06

Add mac header

The MAC header is the header used by Ethernet. It contains information such as the MAC address of the receiver and the sender.

You can see that the mac address is 48 bits, and the ip address is 32 bits.

The Ether type in the mac header is similar to the protocol number in the ip address

It can be considered that the Ethernet type is followed by the content of the Ethernet packet , and the Ethertype represents the type of the following content. The contents of Ethernet packets can be packets of protocols such as IP and ARP. They all have corresponding values, which are also determined according to rules.

The ip protocol type is 0800 (hex)

Since the sender's mac address has been determined above which network card should be used for sending (interface), it can be obtained directly from the rom of the network card (the mac address will be written to the network card rom when the network card is produced).

The recipient's IP address (here refers to the IP address passed by the IP module, not the server's IP address, which is determined by the gateway item in the routing table), then how can we get the recipient's mac address?

The IP module determines who the packet should be sent to based on the content of the Gateway column in the routing table .

arp protocol converts ip address to mac address

There is a broadcast method in Ethernet that can send packets to all devices on the same subnet.

For example, arp will ask who this IP address belongs to and tell me the max address. The device will just answer that it is mine and tell you its mac address.

arp cache

If the mac address is broadcast every time, many arp packets will appear on the network. Therefore, there will be a memory space in the arp cache to store the searched mac address.

Every time you query the mac address, first check whether there is one in the cache. If there is, take it out and use it directly; if not, broadcast and send arp packets.

arp cache invalidation

The IP address will change. At this time, the corresponding mac address cannot be used and an error will occur and be sent to the wrong recipient. Therefore, the cached content will be deleted every few minutes.

Send and receive corresponding to the ip module

send

Adding network packet control information corresponds to sending 1 in the figure. Next, it will be explained that 2 sent packets will go to the network hardware.

It may be a board plugged into the computer motherboard, or it may be a PCMCIA card on a laptop, or a chip integrated on the computer motherboard. Different forms of hardware have different names, and they are collectively called network cards.

The network packet data received by the network card is composed of numbers 0 and 1. The network card converts the numbers into electrical signals or optical signals and sends them out through the network cable . Arrives at the forwarding device, which forwards the data and finally reaches the server.

accept

The same process is used to receive network packets. It is sent to the network card through the forwarding device, and the network card converts the electrical signal into a digital signal and sends it to the ip module; then the ip module will remove the mac header and ip header (that is, only the tcp header and data are left) ) package is handed over to the tcp module.

The ip module does not care whether the packet it forwards contains the tcp header or real data, nor does it care whether the operation of the tcp module is successful. It is only responsible for the forwarding operation.

Regardless of whether the packets to be sent or received are control packets or data packets, IP's sending and receiving operations for various types of packets are the same.

Definition of responsibilities

From the above understanding, we can see that the ip module has completed all operations and handed over the encapsulated packets to the network card. The network card is only responsible for receiving and sending packets.

The advantage of this is that it is the same for other types of packets except IP. If the packaging is completed before being handed over to the network card, then for the network card, the sending operation is exactly the same as sending IP packets . In this way , the same network card can support various types of packets.

The same goes for acceptance. No matter what type of packet this is, it is only responsible for delivering it to the corresponding network module, and you don't need to worry about what type of packet it is.

Guess you like

Origin blog.csdn.net/m0_65909361/article/details/132905671