IP address and subnet mask (from classified addressing to unclassified addressing CIDR)

IPs classified in the early stage

In the early days of the Internet, classified IP addresses were used. That is to say, it is 0.0.0.0 ~ 255.255.255.255divided into five categories: ABCDE.
Class A address space accounts for 50% of the total number of IPs, and we fixed the first position at 0.
Class B address space accounts for 25% of the total number of IPs, and we fix the first two digits at 10.
Class C address space accounts for 12.5% ​​of the total number of IPs, and we fixed the top three 110.
Class D address space accounts for 6.25% of the total number of IPs, and we fixed the first four digits 1110.
Class E address space accounts for 6.25% of the total number of IPs, and we fixed the first four digits 1111.

According to the proportion of each type of IP address, we can more easily remember the beginning of each type of IP address . In binary, each bit has only two forms: 0 and 1. You can imagine it with the help of a tree diagram. From left to right (highest bit to lowest bit), the IP address is bifurcated layer by layer according to 0 and 1, and then Locate specific branches according to categories A to E.

According to the function of the IP address, the composition of the IP address can be divided into a two-level structure: network number + host number.
It can be recorded as: IP 地址 ::= {<网络号>,<主机号> }("::=" means "defined as") The network number is used to locate the network where the host is located, and the host number is used to locate the host from the local network.

At this point, the problem arises. The first part is the network number, and the last part is the host number. What is the boundary between network number and host number? Where does the network number end? Where does the host number start?
An IP address is defined like this:

Class A address: The first byte (first 8 bits) is used as the network number.
Class B address: The first two bytes (first 16 bits) serve as the network number.
Class C address: The first three bytes (first 24 bits) serve as the network number.

According to the rules, there is no host number when divided further down. Therefore, there are some differences between Class D, Class E and Class ABC.
The three categories ABC are unicast addresses, class D is a multicast address, and class E is a reserved address.

Based on the beginning of each type of IP address and the classification of IP addresses , we can determine which type an IP address belongs to, what the network number is, and what the host number is.
But something to note is:

  1. The number of available network numbers for Class A IP addresses is 2^7-2 = 126, and all 0s and 127, two network numbers with special uses, need to be removed.
  2. The number of assignable host numbers for each type of IP address also needs to be reduced by 2, because "all 0s" in the host number are used to represent the network address of the IP network. "All 1s" is used to represent broadcast addresses . IP addresses in these two cases are generally not assigned for use.

Therefore,
the available network address range for Class A IP addresses is 1.0.0.1 to 127.255.255.254, the
available network address range for Class B IP addresses is 128.0.0.1-191.255.255.254, and the
available network address range for Class C IP addresses is 192.0.0.1-223.255.255.254.

If classified according to ABCDE, it will cause a waste of resources. Under this classification, in a 32-bit IP address, there are only three situations in which the boundary between the network number and the host number is divided: 8+24 16+16 24+8. This means that if an organization wants to apply for a network, after being assigned a network number, there are only three situations in which the number of assignable hosts under its name can be assigned: 2^24-2 2^16-2 2^8-2.
If the organization is a medium-sized enterprise, the lowest number selected (Category C) is 254, which may not be enough; the highest number (Category A) selected will not be used, resulting in many idle host numbers; the middle number is selected (Category B), there are more than 60,000, which will also lead to waste.
You may also think this way: If you choose the minimum situation (that is, apply for category C) but apply for more network numbers, then the host number may be just right. But in this way, every medium-sized enterprise thinks so, and Category C will face insufficient allocation.

In other words, the "gradation value" of the IP address is too large, and many of the network numbers assigned to an organization will be idle and wasted.
Therefore, we need to adjust the accuracy of the "gradation value" smaller.

Classless addressing CIDR

The CIDR compilation method was born and is still in use today and is widely adopted.

The full name of this addressing method is Classless Inter-Domain Routing (CIDR).

In order to distinguish it from the previous classified addressing , CIDR uses the word "network prefix" instead of "network number". That is IP地址 ::={<网络前缀>,<主机号>}, the number of digits in the network prefix is ​​no longer limited to 8 16 24any integer between 0 and 32. .
CIDR uses "slash notation", that is, adding a slash after the IP address /, and adding a number after the slash to indicate the number of digits in the network prefix. For example:
128.14.35.7/20, which means that the first 20 digits of the binary IP address are the network prefix and the last 12 digits are the host number.
CIDR notation requires us to write out both the IP address (128.14.35.7) and the number of network prefixes (/20).

IP addresses under the same network prefix form a " CIDR address block ". The smallest address in a CIDR address block is the case where the host number is all 0, and the maximum address is naturally the case where the host number is all 1.
We continue to use the host number with all 0 as the network address and all 1 as the broadcast address . The number of available addresses in a CIDR address block also needs to be reduced by 2.

In order to facilitate the allocation of CIDR address blocks, we usually use an IP address with a host number of all 0s (the minimum address), and then attach the number of digits in the network prefix as the representative of the CIDR address block.

Under CIDR addressing, the division of network numbers and host numbers becomes more flexible, so that IP addresses are properly utilized.
The computer does not use slash notation to determine a CIDR address block (IP address when the host number is all 0), but uses a 32-bit address mask to perform a bitwise AND operation with the IP address, which is what we often hear. subnet mask . (For example, the common 255.255.255.0)

As the name suggests, a mask is a bunch of numbers that look like masks. We need to convert it into binary to appreciate its usefulness.

The network address can be obtained by performing a bitwise AND operation on the binary mask and the binary IP address .

We should understand the AND operation, 1&0 = 0;
the bitwise AND operation is to convert the number into binary and perform AND operations on the corresponding positions. 01&11=01

IP address: 128.14.35.7
Subnet mask: 255.255.255.192
are converted into binary (every 8 bits are separated by dots .. For certain readability, I write the network prefix and host number separately):
IP address: 10000000.00001110.0010 0011.00000111
Subnet mask Code: 11111111.11111111.1111 0000.00000000
The front of the subnet mask is all 1, and all the following are 0, so you only need to keep the number corresponding to the 1 position of the IP address, and change the following number to 0. (This is the meaning of mask, just like mask, the part of 1 is transparent and the part of 0 is covered.)
So the final conversion result is: 10000000.00001110.0010 0000.00000000, that is128.14.32.0/20

It can be found that since the form of the IP address is 网络前缀+主机号, and the function of the subnet mask is to determine the network address, then its binary form must be all 1s in the front and all 0s in the back, so the decimal number of each byte must be as follows One of several:

255 254 252 248 240 224 192 128 0

The subnet mask we are more familiar with 255.255.255.0is 11111111.11111111.11111111.00000000the decimal number. From the perspective of classified addressing, it is the subnet mask of Class C IP. The first 24 digits are the network number and the last 8 digits are the host number.
Similarly, we can also find out that the subnet masks of class AB IP addresses are: 255.0.0.0and 255.255.0.0.
Therefore, the subnet masks of ABC class IPs are fixed.

Now, we often use these two forms to represent an IP address:

  1. slash notation192.112.0.1/20
  2. Mask notation:IP:192.112.0.1 + 子网掩码:255.255.255.192

Finally, why do we keep talking about network addresses (host numbers are all 0)?
If the router takes the host number into consideration when forwarding packets, the number of routing tables will be greatly increased, which will increase the time to look up the routing table. Let the router only consider network prefixes, which will greatly reduce lookup time.

Guess you like

Origin blog.csdn.net/m0_67313306/article/details/124777301