The Network layer is the most important layer to realize the Internet. It is at the network level that lans are connected to each other according to the IP protocol, which eventually constitutes the Global Internet. Higher-level protocols, whether TCP or UDP, must transmit information through IP datagram at the network layer. The operating system also provides sockets for this layer, allowing users to directly manipulate IP packets.

IP packets are IP protocol compliant information (that is, 0/1 sequence), we later referred to IP packets as IP packets. An IP packet consists of header and Data. The data part is the information to be transmitted, and the header is the additional information to enable transmission (this is similar to the header function of Ethernet frames, see the speaker article if you are unfamiliar with frames).

Format of the IP packet

IP protocols can be classified into IPv4 and IPv6. IPv6 is an improved version intended to replace IPv4 in the future. For the purposes of this article, we can ignore the distinction and just use IPv4 as an example. Here is the IPv4 format

IPv4 packets we fold the entire sequence by 4 bytes for better display

IPv4 packets we fold the entire sequence by 4 bytes for better display

Like frames, the header of an IP packet has multiple areas. We focus on the source address and destination address in red. They’re all IP addresses. IPv4 addresses are 4 bytes long (32 bits). IPv4 addresses are divided into four decimal numbers ranging from 0 to 255. For example, 192.0.0.1 is an IP address. The IP address header is in the binary format.

An IP address is a global address that identifies the “community” (LAN) and the “house” (host). This is done by classifying IP addresses.

IP class    From          To                Subnet Mask

A           1.0.0.0       126.255.255.255    255.0.0.0

B           128.0.0.0     191.255.255.255    255.255.0.0

C           192.0.0.0     223.255.255.255    255.255.255.0

The 32 bits of each IP address are divided into two parts. The first part is used to distinguish the LAN, and the second part is used to distinguish the hosts on the LAN. A Subnet Mask tells us the boundary between the two parts. For example, 255.0.0.0(8 ones and 24 zeros) indicates that the first 8 bits are used to distinguish the LAN and the last 24 bits are used to distinguish the host. Since the A, B, and C categories are already defined, when an IP address belongs to the CLASS B range, we know that the first 16 bits of the IP address represent the LAN and the last 16 bits represent the host.

Network Card and Router

According to the Network Protocol Overview, an IP address is a “zip code” assigned to each house (computer). But that’s not exactly true. An IP address identifies a NIC (Network Interface Card). A nic is a piece of computer hardware that delivers network information to the computer (processor/memory) after receiving it. When the computer needs to send information, it also needs to send it through the network card. A computer can have more than one network card, such as a laptop with one Ethernet card and one WiFi card. When a computer receives or sends a message, it first decides which network card it wants to pass through.

NIC

A router, “router,” is actually a dedicated computer with multiple network cards. It allows the network card to connect to different networks, thus forming what is called the post office in the Network Protocol Overview. For example, the router in the middle of the figure has two network cards with addresses 199.165.145.17 and 199.165.146.3. They are connected to two networks: 199.165.145 and 199.165.146.

IP packet relay

IP packets are transmitted through a router relay. Each host and route has a routing table. The routing table defines the route for the IP packets to be sent based on the DESTINATION IP address. Like the road sign below, if the address is “Tokyo,” turn left. If the address is “Sydney”, turn right.

A real world routing table

For example, we generate an IP packet from host 145.17 to 146.21: Spread out the paper, write the beginning of the letter (the rest of the data can be TCP packets, UDP packets, or arbitrary words, we don’t care for the moment), specify the DESTINATION IP address (199.165.146.21) and the destination IP address (199.165.145.17). Host 145.17 then refers to its own routing table, which contains three lines:

145.17 Routing Table (Genmask is the subnet mask,Iface is used to specify which NIC interface to use)

Destination        Gateway             Genmask             Iface

199.165.145.0      0.0.0.0             255.255.255.0       eth0

0.0.0.0            199.165.145.17      0.0.0.0             eth0

There are two lines here.

The first line indicates that if the IP destination is a host on the 199.165.145.0 network, then you only need your network card on eth0 to send directly (” local community “: direct delivery) and do not need to go to the router(Gateway 0.0.0.0 =” local delivery “).

The second line indicates that all IP destinations that do not match the first line should be sent to Gateway 199.165.145.17, which is the IP address of the network card that the intermediate router accesses at eth0 (the post office branch on eth0).

Our IP packet destination is 199.165.146.21, which does not match the first line, so we send it to the intermediate router according to the second line. Host 145.17 adds an IP packet to the payload of the frame and writes the corresponding MAC address 199.165.145.17 in the header of the frame. In this way, it can be transmitted over the LAN as used in Ethernet and wifi protocols.

The intermediate router receives the IP packet and reads the IP packet payload from the frame. The intermediate router retrits the destination IP address and compares the destination IP address with its own routing table:

Destination        Gateway             Genmask             Iface

199.165.145.0      0.0.0.0             255.255.255.0       eth0

199.165.146.0      0.0.0.0             255.255.255.0       eth1

0.0.0.0            199.165.146.8       0.0.0.0             eth1

As you saw in the previous two lines, because the router spans two networks, eth0 and eth1, it can send IP packets directly through the network cards on eth0 and eth1.

The third line indicates that IP addresses other than the first two lines need to be sent to 199.165.146.8(router on the right) via eth1.

Our destination matches line 2, so we put the IP in a new frame,

Write the MAC address 199.165.146.21 at the head of the frame and send it directly to host 146.21.

$route-n ($route-n)

IP packets can be further relayed to further hosts. The IP packet starts from the host and is relayed between routers according to the routing table of the routers along the route. The IP packet eventually arrives at a router that resides on a local area network with the target host and communicates directly at the connection layer. Finally, the IP packet is sent to the target host. This process is called routing(let’s just call it IP packet routing, the word routing is too mixed up).

Throughout the process, the IP packet is continuously sealed by the host and the route into frames (envelopes) and opened, and then with the help of the connection layer, transmitted between the various NICS in the LAN. Throughout the process, the contents of our IP packet remain intact and unchanged. The net effect is that an IP packet is transferred from one host to another. With IP packets, we don’t have to worry about what happens at the bottom (like the connection layer).

ARP protocol

In the above process, we actually assume that each host and route can understand the mapping between IP addresses and MAC addresses on the LAN, which is a basic condition for IP packet encapsulation to frame implementation. IP addresses and MAC addresses are transmitted to each host and route on the LAN through ARP. Each host or route has an ARP cache to store the mapping between IP and MAC addresses on the LAN.

ARP (BETWEEN the connection layer and the network layer, ARP packets need to be wrapped in a frame) works as follows: The host sends an ARP packet, which contains its OWN IP address and MAC address. Through ARP packets, hosts broadcast to all hosts and routes on the LAN: I am IP address XXXX, my MAC address is XXXX, does anyone know the MAC address 199.165.146.4? The host with the IP address will reply to the requesting host: Oh, I know, this IP address belongs to one of my nics and its MAC address is XXXXXX. The host sends ARP requests in broadcast mode and has its own IP address and MAC address. Other hosts and routes check their OWN ARP cache and update their own ARP cache if they do not.

In this way, the ARP cache is stable after several ARP requests. If devices on the LAN change, ARP repeats the preceding procedure.

On Linux, you can use the $arp command to view the ARP process. ARP is used only for IPv4. IPv6 uses Neighbor Discovery Protocol to replace ARP.

Routing Table generation

Another assumption is that there is a proper routing table for each host and route. This Routint table describes the topology structure of a network. If you know your network connection, you can write your host’s routing table by hand. However, a router may have multiple egress, so the routing table may be very long. More importantly, other routers connected around may change (such as new routers or broken routers), so we need routing table to direct traffic to other exits in time. We need a more intelligent detection of the surrounding network topology and automatic generation of routing tables.

Let’s take the Beijing subway as an example. If you go to Chaoyang Gate from the airport, you can take Terminal 2 -> Sanyuan Bridge -> Dongzhimen -> Chaoyang Gate. Terminal 2 and Chaoyang Gate are the departure and destination hosts respectively. Sanyuan Bridge and Dongzhimen are the two routers in the middle. If the Sanyuan Bridge ->> Dongzhimen section is out of service due to maintenance, we need to change the Routing table of Sanyuan Bridge so as to instruct passengers (IP packets) to Chaoyang men: Please take the following route: Sanyuan Bridge ->> Shaoyaoju. Then proceed to Chaoyang Gate (Shaoyaoju ->> Dongzhimen ->> Chaoyang Gate) according to the Routing table of Shaoyaoju.

Routing Information Protocol (RIP) is used to generate routing tables. It determines the routing table by distance and therefore belongs to the distance-vector protocol. For RIP, the so-called distance is the hop number of routers from the origin to the destination. For example, from the airport to Chaoyang gate above, according to terminal 2 -> Sanyuan Bridge -> Dongzhimen -> Chaoyang gate route, through two routers, the distance is 2. We could initially generate the routing table of the tri-bridge manually. Then, according to RIP, Sanyuan Bridge broadcasts to the surrounding routers and hosts the distance to each IP address (for example, to airport =0, Tuanjiehu =0, Guomao =1, Wangjingxi =1, Jianguomen =2). The router and host that receive RIP packets calculate the distance to each IP address according to the distance between the router and the host that sends RIP packets. The distance between Dongzhimen and Sanyuan Bridge is 1. If Dongzhimen receives the RIP packet from Sanyuan Bridge (the distance to the airport is 0), the distance from Dongzhimen to the airport via Sanyuan Bridge is 1+0=1. If the RIP record of Dongzhimen is much higher than this (for example, Dongzhimen -> Shaoyaoju -> Sanyuan Bridge -> Airport = 2). Then Dongzhimen changed its routing table: all the traffic to the airport will be sent to Sanyuan Bridge instead of Shaoyaoju. If the RIP record of Dongzhimen is not bad, the Routing table of Dongzhimen remains unchanged. The preceding process repeats the RIP broadcast, distance calculation, and routing table update at each point. Finally, all hosts and routers can merge paths.

(The basic RIP logic is: if A is 6 away from B, and I am 1 away from A, then the distance from A to B is 7.)

RIP considers IP addresses whose distance exceeds 15 hops unreachable due to technical reasons (Looping hops). So RIP is more used in parts of the Internet (such as the entire Network of China Telecom). Such parts of the Internet often belong to the same ISP or have the same management agency, and are called AS autonomous systems. Hosts and routes within the AUTONOMOUS system communicate with other Autonomous systems based on the border router to the outside. The Border Gateway Protocol (BGP) is used to generate routing tables between Border routers to route to other ass. The AUTONOMOUS system (AS) uses RIP to determine routing tables by referring to Border routers. BGP works in a similar way to RIP. However, BGP determines the traffic routing table after considering the distance and other factors, such as the policy and connection performance.

conclusion

We started by talking about the process of IP packets being relayed according to the routing table. In order to implement relay smoothly, we further dive into ARP and RIP/BGP. All three protocols assist in IP transport. ARP enables each computer and router to know the mapping between IP address and MAC address in its LAN, thus successfully encapsulating IP packets into frames. RIP can generate a proper routing table in an AS. BGP can generate routing tables outside an AS.

Throughout the process, we paid attention to the large transfer process of IP packets and deliberately ignored some details. The IP relay process above applies to IPv6.