[Network Programming·Network Layer] IP Protocol


Students who need cloud servers and other cloud products to learn Linux can move to / --> Tencent Cloud <-- / --> Alibaba Cloud <-- / --> Huawei Cloud <-- / official website, lightweight cloud servers are low-cost to 112 yuan/year, and new users can enjoy ultra-low discounts on their first order. 


Table of contents

1. The concept of IP protocol

2. Header of IP protocol

1. Length of four heads

2. 16-bit total length (unpacked)

3. 8-bit protocol (separate use)

4. 16-bit header checksum

5. 8-bit survival time

6. 32-bit source IP and 32-bit destination IP

7. 4-bit version/8-bit service type

8. 16-bit identification

9. 3-digit mark

10, 13-bit slice offset

3. Network segment division of IP protocol

1. Why subnetting is necessary?

2. Network segmentation rules

2.1 Old network number and host number division scheme (compatible with the new scheme)

2.2 New network number and host number division scheme CIDR

4. Special IP address

5. Solve the limit on the number of IP addresses

6. Private IP and public IP

6.1NAT

6.2NAPT

6.3 Defects of NAT

7. Routing and routing table

1. Routing

2. Routing table

3. Routing table generation algorithm


1. The concept of IP protocol

IP: The address of a target host in the target network

Host: a device equipped with an IP address, but not capable of routing control; Router: equipped with an IP address, but capable of routing control; Node: a collective name for host and router;

The role of the IP address: locate the host and reliably send the datagram from host A across the network to host B (the data has a high probability of being sent successfully. If the sending is unsuccessful, TCP's various strategies let IP perform data transmission again. The combination of TCP and IP ensures reliable network communication)

2. Header of IP protocol

1. Length of four heads

Like the TCP protocol, the four-bit header length of the IP protocol represents the total length of the message = 4-bit header length * 4 bytes (the value range of the 4-bit bits is 0-15, and the header length is at least 20 bytes, so the total length of the message Length ranges from 20-60 bytes)

Looking at the message in the picture above, there is a 20-byte prompt on the right. This is a standard IP. For standard IP, the binary data of the four-digit header length is 0101 (5*4 equals 20)

2. 16-bit total length (unpacked)

The 16-bit total length of the IP protocol includes the header and payload, so you can get the length of the payload through the 16-bit total length - header (for the application layer, its payload is the message given by the transport layer)

3. 8-bit protocol (separate use)

The protocol number of the transport layer is written in the 8-bit protocol, so that when delivering upwards, the IP protocol knows which protocol of the upper layer it wants to hand over the payload to.

4. 16-bit header checksum

If the verification fails, the message will be discarded directly. TCP has a timeout retransmission mechanism and will resend it to IP.

5. 8-bit survival time

During the transmission of messages over the network, once the message is sent from the host, it is no longer under the control of the host. The message will be passed between routers, and problems such as loop forwarding may occur. If the router is not given the right to discard the message, the message will be forwarded in a loop, occupying network resources in vain. .

The 8-bit lifetime of the IP protocol specifies the maximum number of hops for this datagram to reach its destination, which is generally 64. (Every time it passes a route, the TTL is reduced, and when it reaches 0, the packet is discarded directly)

6. 32-bit source IP and 32-bit destination IP

The port number provided by the application layer is for the transport layer, and the IP provided is for the IP protocol. slightly.

7. 4-bit version/8-bit service type

The 4-digit version is filled with 4, which represents ipv4 (ipv6 and ipv4 are incompatible)

8-bit type of service: 3-bit priority field (deprecated), 4-bit TOS field, and 1-bit reserved field (must be set to 0). The 4-digit TOS respectively represents: minimum delay, maximum throughput, maximum reliability, and minimum cost. These four conflict with each other, and only one can be selected. For applications such as ssh/telnet, the minimum delay is more important; for programs such as ftp, the maximum throughput is more important.

8. 16-bit identification

16-bit identification (id): uniquely identifies the message sent by the host. If the IP message is larger than the maximum transmission unit of the data link layer, causing the IP layer to be fragmented, then the ID in each fragment will be the same.

9. 3-digit mark

3-digit flag field: The first bit is reserved (reserved means not used yet, reserved). The second bit is 1, which means fragmentation is prohibited. At this time, if the length of the message exceeds the MTU, the IP module will discard the message. The third bit means "more fragments". If fragmented, the last fragment is set to 0, others are 1. If the bit of the fragment is 1, it means that the fragment is not the last fragment of the entire message.

For the fragmentation prohibition flag, if set to 1, fragmentation is prohibited. When the packet passes through a router on the road, if the maximum MTU of the router is less than the payload of the packet, the router on the road cannot fragment it because the fragmentation prohibition flag is set to 1, and the packet will be discarded. The transport layer will timeout and retransmit the message and select a routing path that can satisfy data transmission. (Finding the way to greater throughput). Then wouldn’t it be good to set the maximum MTU of all routers to 1500 bytes? In fact, routers with smaller maximum MTU are more suitable for small-capacity payloads to pass through, and the speed is faster.

10, 13-bit slice offset

13-bit fragmentation offset (framegament offset): It is the offset of the fragment relative to the beginning of the original IP message. In fact, it indicates where the current fragment is in the original message. The actual number of bytes of the offset is This value is obtained by * 8. Therefore, except for the last message, the length of other messages must be an integer multiple of 8 (otherwise the messages will not be continuous).

The MAC frame protocol of the data link layer stipulates that its payload cannot exceed 1500 bytes (MTU (maximum transmission unit, which can be modified)). This 1500 byte limit includes the IP header + the payload in the IP message.

The size of the data packet can only be controlled by the transport layer, but sometimes it still exceeds 1500 bytes, which can only be solved by fragmentation and assembly of the IP protocol. The sending end IP layer fragments, and each fragment will have an IP header. The end IP layer is assembled (the byte limit causes the IP layer to be fragmented and assembled, which is not good, because fragmentation will increase the probability of packet loss ). TCP and MAC frames do not care about IP fragmentation and assembly of data packets. This is just the behavior of the IP layer itself.

Fragmentation: If More Fragments is 1, it means that the message has been fragmented. If more fragments are 0 and the fragment offset is equal to 0, it means there is no fragmentation, and vice versa.

Assembly: Just sort the fragmented IP packets according to the photo offset in ascending order.

How to ensure correct assembly: The IP protocol has a 16-bit header checksum, and the TCP protocol also has a checksum.

Retransmission of packet loss: If a certain piece of packet is lost due to fragmentation, the splicing assembly will fail. Then the peer needs to reissue. The reissue mechanism is only available at the TCP layer. IP does not have the reissue function. However, for TCP, TCP does not care how IP is fragmented and which piece is lost. It doesn't matter, TCP will only resend a complete message for the IP layer.

3. Network segment division of IP protocol

The IP address has a total of 32 bits and is divided into two parts, the network number and the host number.

Network number: Ensure that the two network segments connected to each other have different identities; (just like different colleges in the school have different numbers)

Host number: In the same network segment, hosts have the same network number, but they must have different host numbers;

1. Why subnetting is necessary?

Every host on the Internet must belong to a certain subnet. This is to facilitate the location of this host. Host A sends data to Host B. When the datagram passes through the router, the correct network number will be selected, instantly excluding a large number of IP addresses, greatly improving the search efficiency.

2. Network segmentation rules

1. Data forwarding between different network segments relies on routers, so routers are devices that belong to multiple network segments. The router is also a host. It has an IP address of the current network segment in multiple network segments. Normally, the subnet IP address of the router is the network segment identifier.1 (LAN port)

2. In a subnet, the IP device in the management subnet is usually a router.

3. There is a technology called DHCP, which can automatically assign IP addresses to new host nodes in the subnet, avoiding the inconvenience of manual IP management. General routers have DHCP function. Therefore, the router can also be regarded as a DHCP server.

2.1 Old network number and host number division scheme (compatible with the new scheme)

All IP addresses are divided into five categories:

Class A 0.0.0.0 to 127.255.255.255

Class B 128.0.0.0 to 191.255.255.255

Class C 192.0.0.0 to 223.255.255.255

Class D 224.0.0.0 to 239.255.255.255

Class E 240.0.0.0 to 247.255.255.255

This division method is unreasonable. For example, for a Class A IP address, the network number only has 7 digits, but the host number has 24 digits. There are very few scenarios where there are so many hosts under one subnet, causing Class A addresses to be A lot of waste; Class B addresses are more reasonable, which results in most organizations applying for Class B addresses, causing Class B addresses to be allocated early.

In response to this unreasonable division scheme, people have proposed a new division scheme CIDR (Classless Interdomain Routing)

2.2 New network number and host number division scheme CIDR

Introduce an additional subnet mask to distinguish the network number and the host number:

The subnet mask is also a 32-bit positive integer. It is usually terminated by a string of "0";

Perform a "bitwise AND" operation on the IP address and subnet mask, and the result is the network number;

The division of network numbers and host numbers has nothing to do with whether the IP address is Class A, Class B or Class C;

Note: The network number is determined by the position of the lowest bit of the subnet mask binary 1. The digits following the lowest binary digit of the subnet mask, 1, belong to the host number.

Example 1:

Perform a bitwise AND operation on the binary numbers of the IP address and subnet mask to obtain the network number. The host number is 0-255 (0000 0000-1111 1111), but the two host numbers 0 and 255 will not be used, because 0 represents the network number and 255 represents the broadcast address.

Example 2: 

4. Special IP address

1. Set all the host addresses in the IP address to 0, which becomes the network number, representing this LAN;

2. Set all the host addresses in the IP address to 1, which becomes a broadcast address, used to send data packets to all hosts connected to each other on the same link;

3. The IP address of 127.* is used for local loop back testing, usually 127.0.0.1

5. Solve the limit on the number of IP addresses

IP addresses only have 32 bits. Due to the existence of some special-purpose IP addresses, the available IP addresses are far less than 4.29 billion. However, the TCP/IP protocol stipulates that every host needs an IP address (actually every Each network card needs to be configured with one or more IPs), so the IP address is far from enough. Although CIDR alleviates the problem of low IP utilization of the old standard to a certain extent, the total number of IPs has not increased.

Use the following methods to solve the problem of insufficient IP addresses:

1. Dynamic allocation of IP addresses: Only assign IP addresses to devices connected to the network. Therefore, devices with the same MAC address may not necessarily get the same IP address every time they connect to the Internet;

2. NAT technology

3. IPv6: IPv6 is not a simple upgraded version of IPv4. These are two unrelated protocols and are not compatible with each other; IPv6 uses 16 bytes and 128 bits to represent an IP address; however, IPv6 is not yet popular;

6. Private IP and public IP

If an organization establishes a LAN internally, the IP address is only used for communication within the LAN and is not directly connected to the Internet. In theory, any IP address can be used, but RFC 1918 stipulates private IP addresses for establishing a LAN.

1. 10.*, the first 8 digits are the network number, a total of 16,777,216 addresses (used by enterprise-level routers)

2. 172.16. to 172.31., the first 12 digits are the network number, a total of 1,048,576 addresses (used by enterprise-level routers)

3. 192.168.*, the first 16 digits are the network number, a total of 65,536 addresses (used by home routers)

Those included in this range become private IPs, and the rest are called global IPs (or public IPs)

The role of router:

1. Data packet forwarding

2. DHCP function, setting up a subnet (such as configuring a wireless network. Note: LAN is an intranet IP)

3. NAT function

6.1NAT

1. The second point mentioned that routers can build subnets. For home routers, the built subnet contains multiple hosts. For operator routers, the built subnet contains multiple home routers. When we send data from the host, the data is not necessarily in the public network, but may also be in the operator's subnet, and the data packet may still be in the intranet environment.

2. The router will be equipped with at least two IPs, one of which is the subnet IP, also called the LAN port IP, which corresponds to the subnet built by the router; the other IP is the WAN port IP, which is the external IP. Yes, it is connected to the upper-level router. The WAN port of the outermost carrier router is connected to the public IP.

3. Different routers have the same subnet IP (usually 192.168.1.1). Host IP addresses within the same subnet cannot be repeated, but IP addresses between different subnets can be repeated.

4. When the internal network IP192.168.1.201 sends data to the public network IP122.77.241.3, the subnet finds that the destination IP does not belong to the current network segment, and will hand the data packet to the router, allowing the router to proceed layer by layer. Search for delivery and finally reach the destination IP. According to the third point, if the server with the destination IP gets the intranet IP, it cannot return it by touching the network cable. Because this intranet IP may exist in multiple intranets, it requires the host to hand over the data packet to the router. Replace the IP step by step with the router’s WAN port IP. This WAN port replacement technology is called NAT (Network Address Translation) .

In the LAN, there are multiple hosts accessing the same external network server. In the data returned by the server, the destination network number is the same. So how does the NAT router determine which LAN host to forward this data packet to? It may even be Different client processes in a host in the LAN initiate data transmission to the remote end through different client port numbers. In order to send the correct packets to the correct process, the NAT router needs to use NAPT to solve this problem, using the IP+port form to establish the association.

6.2NAPT

 The NAT router maintains a K/V conversion table. For example, the source address is 10.0.0.11:1025 and the destination address is 163.221.120.9:80. After the NAT router WAN port conversion, the destination address remains unchanged and the source address is replaced by 202.244.174.37:1025.

There is another set of source addresses and destination addresses in the figure. When passing through the NAT router, the WAN port IP will be replaced with 202.244.174.37. In order to distinguish, different port numbers will be used to establish the mapping relationship.

Therefore, the source address + destination address before conversion and the source address and destination address after conversion form a mapping table that is a key value for each other. This table is called a NAT conversion table. This conversion table can be queried in both directions.

6.3 Defects of NAT

Unable to establish connection from outside NAT to internal server;

The generation and destruction of translation tables require additional overhead;

Once the NAT device becomes abnormal during the communication process, all TCP connections will be disconnected even if there is hot backup.

7. Routing and routing table

1. Routing

The routing process is a hop by hop "asking for directions" process.

The so-called "one hop" is an interval in the data link layer. Specifically, in Ethernet, it refers to the frame transmission interval from the source MAC address to the destination MAC address.

The transmission process of IP data packets is also the same as asking for directions:

1. When the IP data packet reaches the router, the router will first check the destination IP;

2. The router decides whether the data packet can be sent directly to the target host or needs to be sent to the next router. Repeat in turn until the target IP address is reached; (if a host cannot find an available gateway, it will send the data packet to the default designated gateway, and this gateway will process the data packet. The gateway used by the host now generally refers to Default gateway.)

3. Use the routing table to determine where the data packet should be sent.

2. Routing table

Use the route command to view the routing table of the Linux machine:

[jly@VM-4-11-centos linux-code]$ route
Kernel IP routing table
目标网络			下一跳(网关)	子网掩码		   使用状态					哪个接口
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    0      0        0 eth0 //默认网关
10.0.4.0        0.0.0.0         255.255.252.0   U     0      0        0 eth0
link-local      0.0.0.0         255.255.0.0     U     1002   0        0 eth0

[jly@VM-4-11-centos linux-code]$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.4.11  netmask 255.255.252.0  broadcast 10.0.7.255
        inet6 fe80::5054:ff:fe9c:67a2  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:9c:67:a2  txqueuelen 1000  (Ethernet)
        RX packets 171933080  bytes 33378182758 (31.0 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 165679727  bytes 28636998057 (26.6 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 12503288  bytes 1870189412 (1.7 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12503288  bytes 1870189412 (1.7 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

At this time there is a destination IP10.0.4.0

1. Traverse the routing table

2. The destination IP & subnet mask configured in the routing table determine the destination network to which the data packet is destined.

3. Compare the results from & with the target network (Destination)

4. Find the target network in the router. This target network is the next hop destination.

At this time there is another destination IP202.10.1.2

1. Repeat the above steps and find that there is no target network in the routing table.

2. Then the next hop destination of the router is the default gateway configured on the router.

3. Send messages through the Iface interface

Note: The network numbers of datagrams located in different routers are different. The subnet mask is getting longer and longer, indicating that the destination network for the datagram is becoming more and more specific.

3. Routing table generation algorithm

The routing table can be manually maintained by the network administrator (static routing), or automatically generated through some algorithms (dynamic routing), such as distance vector algorithm, LS algorithm, Dijkstra algorithm, etc.

Guess you like

Origin blog.csdn.net/gfdxx/article/details/132241708