The network layer mainly defines the network address and routing, and IP is the implementation of the network layer.

The IP address

Each network adapter on a TCP/IP network has an IP address. The address is defined by 32 bits. Usually, the address is divided into four segments in the format of 8 bits x 4. Each segment is displayed in decimal format, for example, 192.168.0.1.

IP Address Classification

An IP address consists of a network ID and a host ID.

However, it is not mandatory to specify which part of the 32 bits represents the network ID and which part represents the host ID. Some networks with more hosts have more bits of host IDS, and vice versa.

Generally, IP addresses are classified into the following types:

  • Type A: The first eight bits of an IP address indicate the network ID, the last 24 bits indicate the host ID, and the binary IP address starts with 0.
  • Type B: The first 16 bits of an IP address indicate the network ID, the last 16 bits indicate the host ID, and the binary IP address starts with 10.
  • Type C: The first 24 bits of an IP address indicate the network ID, the last 8 bits indicate the host ID, and the binary IP address starts with 110.

Note:

  • The IP address whose first segment is larger than 223 belongs to Class D and CLASS E, which are special and will not be introduced.
  • If all host ids are 0, this IP address represents the network itself. For example, 192.10.0.1 represents the class C address network 192.10.0.
  • For example, 192.10.0.255 is the broadcast address of the network whose ID is 192.10.0.
  • In class A addresses, the entire 127 network is loopback addresses, which are used to test whether TCP/IP is normal

PS: IP addresses are assigned by the NETWORK Information Center NIC of the U.S. Defense Data Network DDN.

subnet

Subnet ID

For example, A class A network can have 2^24 > 1600W hosts below, which is very flexible to control. We want to divide the network into smaller networks for regulation, and the subnet is to solve this problem.

As mentioned above, we divide an IP address into a network ID and a host ID. If you need to use a subnet, you need a subnet ID, which takes up space on the host ID.

For example, the school is assigned a class C network 192.10.10.0, whose network ID is 192.10.10 and the host ID is 8 bit, which can accommodate a total of 2^ 8-2 = 254 hosts. Now let’s divide the IP address into two subnets, one for teaching and one for office. Somebody said, why don’t you just build another class C network like 192.10.11.0? This is convenient, but it leads to a huge waste of IP addresses.

Subnet mask

We can divide the school network into 2 subnets by using subnet IP. The concept of ** subnet mask ** is introduced to facilitate the following explanation.

A subnet mask, like an IP address, is represented by 32 bits. It contains consecutive ones and zeros, and the first one must come first. 1 indicates the ID of a network whose subnet has been partitioned, and 0 indicates the ID of a host.

That’s a little abstract, but let’s do an example. For example, a Class C network, 193.10.10.0, can be considered as a subnet in the whole network. Its network ID is 24 bit, and its subnet mask is 255.255.255.0, which means 193.10.10.0/24.

Subnet segmentation

Now, we have split the school network into two subnets. The subnet ID needs to occupy 2 bits (why not 1 bit) of the host ID. At this point, we can split the current network into 2 subnets (why 2 subnets instead of 4 subnets is also discussed below).

Then the two-digit subnet information is as follows:

network The network address The broadcast address
192.10.10.128/26 192.10.10.128 192.10.10.191
192.10.10.64/26 192.10.10.64 192.10.10.127

The above results require some explanation.

First of all, why can only be divided into 2 subnets with 2 bit subnet ID? Shouldn’t there be 4 subnets in 00,01,10,11? Yes, but subnet ids with all zeros and ones are generally not available, so that leaves 01,10 (see this article).

Since there are only two cases of 01,10, the network 192.10.10.0 is divided into: For the two subnets 192.10.10.01000000 and 192.10.10.10000000, I’ve written the last 8 bits in binary and highlighted the subnet ID in bold to make it easier to see.

For the two subnets, there are only 6 host ID bits left. Zeros for all host ids correspond to the network address of the subnet, and ones for all host ids correspond to the broadcast address of the subnet.

ARP protocol

An IP address is only the IP address of a host at the network layer. To send the data packets transmitted at the network layer to the destination host, you must know the MAC address of the host. ARP, the address resolution protocol, maps IP addresses to MAC addresses. Conversely, RARP maps MAC addresses to IP addresses.

link: The MAC address

The IP routing

Having said that, how do hosts communicate with each other at the network layer?

Consider two cases: communication within network segments and communication between different network segments.

Network segment communication

Obtain the destination MAC address

For example, host A and server A are both on the 192.168.0.0 network. If host A wants to send data to server A, host A knows its IP address, MAC address and server A’s IP address, but does not know its MAC address.

In this case, host A calculates that the source IP address (IP address of host A) and target IP address (IP address of server A) are in the same network segment by using the subnet mask. Then host A sends an ARP request to the local network segment. The request packet contains the following key information:

Sender's IP address: 192.168.0.2 | sender MAC address: XX, XX, XX, XX, XX, XX recipient IP address: 192.168.0.3 | receiver MAC address: FF FF FF FF FF - - - - the FFCopy the code

All information in the packet is unknown except the MAC address of the receiver, which is set to the broadcast MAC address.

The ARP packet can be received on the entire network segment and server A can also receive the ARP packet. After receiving the packet, server A finds that the recipient’s IP address is its own. Then, server A adds its own MAC address and returns A complete ARP reply packet.

The sender IP address: 192.168.0.2 | sender MAC address: XX, XX, XX, XX, XX, XX recipient IP address: 192.168.0.3 | receiver MAC address: YY YY - YY - YY YY - YYCopy the code

Now host A has obtained the source IP address, source MAC address, destination IP address, and destination MAC address. At this point, the information and the initial packet encapsulated together can be sent out.

ARP cache

In fact, each host maintains an ARP cache table that records the MAC addresses of all directly adjacent devices or hosts that communicate with it. However, the ARP cache table is automatically deleted after a period of time.

The process is a bit like writing a letter, for example, to someone in his class.

– Li Lei’s name (IP) and address (MAC) I must know, home to live in Beijing milk house.

-Your classmate’s NAME (IP) is Han Meimei and he must know it too, but he doesn’t know where her family (MAC) lives.

– Then Li Lei shouted in the class QQ group: “Han Meimei, my name is Li Lei, live in Beijing milk room, what is your home address?” (ARP packet)

– By this time, everyone in the class must have received the message, but they thought, “It’s not my name again, I don’t care.” Only Han Meimei replied: “LI Lei from Beijing Wudaokou, I’m Han Meimei, I live at the west exit of East Square of Beijing South Railway Station ~” (ARP response package)

– And then everyone in class must have gotten the message again, but they thought, “Not my name again, not him.” Only Li Lei wrote down her address silently.

– Then Li lei was able to write down the sender, return address, addressee and return address, and then post the letter wrapped inside the envelope.

– Next time Li Lei wants to write a letter to Han Meimei, he doesn’t need to ask han Meimei’s address again, because he still remembers the address he wrote last time (ARP cache table), but if he hasn’t written for too long, Li Lei will forget and still have to ask han Meimei where she lives.

-Md, Li Lei true hypocritical, all use QQ also write, go to QQ love ah gan.

Different network segments communicate

Communication between devices on different network segments depends on routers.

For example, if host A wants to send data to host B, the two do not belong to the same network. In other words, host A does not know who to send the message to. In this case, host A will send the message to the gateway by default. Before sending the message to the gateway, of course, you need to know the MAC address of the gateway, which is known (192.168.0.1).

After obtaining the gateway MAC address, we can send the packet, but notice that the destination IP is the IP address of host B, but the destination MAC is the gateway MAC.

Source IP, the IP host A | source MAC: host A MAC | target IP: host B IP | target MAC: router interface MAC X | dataCopy the code

In the figure, we assume that the gateway itself is a router. If the gateway is a common PC, the data needs to be sent to the router for processing. After receiving the packet, the router finds that port Y and the destination IP address reside on the same network segment. In this case, the router uses ARP to determine the MAC address of the destination IP address and changes the destination MAC address and source MAC address in the packet. (Note that source MAC addresses and destination MAC addresses change during communication between different network segments.)

Source IP, the IP host A | source MAC: MAC | target IP router Y interfaces: the host B IP | target MAC: host MAC | B dataCopy the code

Of course, this network interface is very simple, and only one route is needed to achieve communication between two different networks (this kind of route is called direct routing), but in reality, packets may need to pass through many routes to reach the target host.