[Network] Network layer protocol - IP

Network layer

Determine a legal path in a complex network environment.
insert image description here

IP protocol

As a vital protocol in the entire TCP/IP, the IP protocol is mainly responsible for sending data packets to the final target computer. Therefore, IP enables communication between any two computers in the world.

Network layer IP: Let the host have the ability to send data from host A to host B! "Realizing communication before terminal nodes" This kind of communication between terminal nodes is also called "point-to-end (end-to-end) communication".

insert image description here

What the network layer provides is: to send data from host A to host B across the network. TCP is used to ensure reliable transmission.

IP Basics

IP is roughly divided into three major functional modules, namely IP addressing, routing (forwarding up to the final node), and IP packetization and grouping.

IP address

In computer communication, an IP address is used to "identify the destination address for communication among all hosts connected to the network". Therefore, in TCP/IP communication, all hosts or routers must set their own IP addresses.

insert image description here

IP header format

insert image description here

  • 4-digit version number: specifies the version of the IP protocol. For IPV4, it is 4
  • 4 is the header length: the length of the IP header is 32bit. That is the length of our header. That is the length of length*4. 4bit means that the largest number is 15, so the longest IP header is 60 bytes
  • 8-bit service type: 3-bit priority field (deprecated), 4-bit TOS field, and 1-bit reserved field (must be set to 0). 4-bit TOS respectively represent: minimum delay, maximum throughput, highest reliability , the 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.
  • 16-bit total length: How many bytes are in the IP datagram as a whole.
  • 16-bit identifier: uniquely indicates the message sent by the host. If the IP packet is fragmented at the data link layer, the id in each fragment is the same.
  • 3-bit flag field: the first bit is reserved, and the second bit is 1, indicating that fragmentation is prohibited. At this time, if the packet length exceeds the MTU, the IP module will discard the packet. The third bit means "more fragments". If it is fragmented, the last fragment position is 0, and the others are 1 (otherwise the message is not continuous)
  • 13 is the fragmentation offset: it is the offset relative to the beginning of the original IP packet, which actually indicates the position of the current fragment in the original packet. The actual number of bytes offset is obtained by this value*8. Therefore, except for the last message, the length of other messages must be an integer multiple of 8 (otherwise the messages are not continuous).
  • 8-bit protocol: Indicates the type of the upper layer protocol.
  • 8-bit time-to-live (TTL): The maximum number of hops for a datagram to reach its destination. Generally, it is 64. Each time a route is passed. TTL -=1, has been reduced to 0 and has not arrived, so it has been discarded for a long time. This field is mainly used to cause routing loops. Due to uncontrollable factors such as network bugs or the unreachability of the peer host, IP packets wander in the network for a long time without disappearing.
  • 32-bit source IP address and 32-bit destination IP address: indicating the sender and receiver
  • 16-bit header checksum: use CRC to verify whether the header is damaged.

Network segment division

How does IP find the host of the other party?

  • The composition of IP: ip = destination network + destination host (FF:FF:FF:FF)
  • Network number: ensure that the two network segments connected to each other have different identities
  • Host number: In the same network segment, hosts have the same network number, but must have different host numbers.

insert image description here

Therefore, when IP is routing, it first routes according to the destination network, finds the destination network, and then finds the destination host according to the host number of the destination host.

  • Different subnets are actually hosts with the same network number put together
  • If a host is added in the subnet, the network number of the host is the same as that of the subnet, but the host number must not be the same as that of other hosts in the subnet.

By properly setting the host number and network number, it can be ensured that in the interconnected network, the IP address of each host is different .

Therefore, the essence of sending data is to first find the target machine (target network + target host). The essence of finding is the process of exclusion! And subnetting can exclude a large number of subnets at once. Ease of Finding – Increased efficiency in finding target hosts

CIDR

In order to solve the problem of insufficient IP, an additional subnet mask is introduced to distinguish the network number from the host number.

  • The subnet mask is also a 32-bit positive integer, usually ending with a string of "0"
  • Perform a "bitwise AND" operation on the IP address and the subnet mask, and the result is the network number

Example:
insert image description here

Therefore, the network number can be obtained by ANDing the IP address and the subnet mask, and the host number from all 0 to all 1 is the address range of the subnet.

There is also a more compact way to express IP addresses and subnet masks, such as 140.252.20.68/24. Indicates that the IP address is 140.252.20.68, and the upper 24 bits of the subnet mask are 1, which is 255.255.255.0

special IP address

  • Set all the host addresses in the IP address to 0, which becomes the network number, and the code is this LAN
  • Set all the host addresses in the IP address to 1, which becomes the broadcast address, which is used to send data packets to all hosts connected to each other in the same link
  • The IP address of 127.* is used for local loopback (loop back) test, usually 127.0.0.1

insert image description here

Limitation on the number of IP addresses

We know that an IP address (IPV4) is a 4-byte 32-bit positive integer, so there are only 232 IP addresses in total, which is about 4.3 billion. The TCP/IP protocol stipulates that each host needs to have an IP address.

This means that a total of only 4.3 billion hosts can access the network?

In fact, due to the existence of a special IP address, the number is far less than 4.3 billion. In addition, IP addresses are not configured according to the number of hosts. Instead, each network card needs to be configured with one or more IP addresses

CIDR has alleviated the problem of insufficient IP addresses to a certain extent (improved utilization and reduced waste, but the absolute upper limit of IP addresses has not increased), but it is still not enough. There are three ways to solve it at this time:

  • Dynamically assign IP addresses: only assign IP addresses to devices connected to the network. Therefore, devices with the same MAC address will not necessarily get the same IP address each time they connect to the Internet.
  • NAT technology
  • IPV6: IPV6 is not a simple upgraded version of IPV4. These are two separate agreements. They are not compatible with each other; IPV6 uses 16 bytes and 128 bits to represent an IP address. However, IPV6 is not popular yet.

Private IP address and public IP address

If a LAN is built within an organization, the IP address is only used for communication within the LAN and not directly connected to the Internet. In theory, any IP address can be used, but RFC 1918 specifies the private IP address used to build a LAN.

So how does private IP solve the problem of IP shortage?

  • 10.* The first 8 digits are the network number, a total of 16,777,216 addresses
  • 172.16. to 172.31. The first 12 digits are the network number, a total of 1048576 addresses
  • 192.168.* The first 16 digits are the network number, a total of 65536 addresses

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

insert image description here

  • A router can be configured with two IP addresses, one is the WAN port IP and the other is the LAN port IP (subnet IP)
  • The hosts connected to the LAN port of the router belong to the subnet of the current route.
  • Different routers have the same subnet IP (usually 192.168.1.1). The host IP addresses in the subnet cannot be repeated, but the IP addresses between subnets can be repeated.
  • Every home router is actually a node in the subnet of the operator router. Such an operator router may have many levels. The outermost operator router, the WAN port IP is a public network IP.
  • When the host in the subnet needs to communicate with the external network, the router replaces the IP address in the IP header (replace it with the WAN port IP), and replaces it step by step. The IP address in the final data packet is called a public network IP. This technology is called NAT (Network Address Translation). The data in the local area network is sent to the public network, which needs to be continuously replaced by the original IP – NAT technology
  • If we want the server program implemented by ourselves to be accessible on the public network, we need to deploy the program on a server with an external network IP. Such a server can be purchased on Alibaba Cloud/Tencent Cloud.

routing

In a complex network structure, find a route to the destination

The routing process is the process of "asking for directions" hop by hop (Hop by Hop)

The so-called "one hop" is an interval in the data link layer, specifically referring to the frame transmission interval between the source MAC address and the destination MAC address in Ethernet

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

  • When the IP packet arrives at the router, the router will first check the destination IP
  • The router decides whether the packet can be sent directly to the destination host or needs to be sent to the next router
  • Repeat in turn until reaching the destination IP address

So how to determine where the current data packet should be sent? This relies on maintaining a routing table inside each node

insert image description here

  • The routing table can be viewed using the route command

  • If the destination IP command is included in the routing table, it can be forwarded directly

  • The last line in the routing table is mainly composed of the next hop address and the sending interface. When the destination address does not match other lines in the routing table, it will be sent to the first-hop address according to the interface specified in the default routing entry.

insert image description here
insert image description here

  • Destination in the routing table is the destination network address, Genmask is the subnet mask, Gateway is the next hop address, Iface is the sending interface

    The U flag in Flags indicates that this entry is valid (some entries can be disabled), the G flag indicates that the next hop address of this entry is the address of a router, and the entry without the G flag indicates that the destination network address is the interface with the local machine Directly connected networks do not need to be forwarded by routers;

Summary of IP

  1. Familiar with the IP header, understand the role of IP and fragmentation and assembly
  2. Understand network segmentation. Build a macro picture of the network. What is network segment division, who does it, and how to do it

Guess you like

Origin blog.csdn.net/qq_58325487/article/details/129693167