When we use mobile phones to connect to the Internet, we use many network protocols. When the mobile phone connects to WiFi, it uses the 802.11 (WLAN) protocol to access the network through WLAN; the mobile phone automatically obtains the network configuration, using the DHCP protocol. After obtaining the configuration, the mobile phone can communicate normally. At this time, the mobile phone has been connected to the local area network and can access the devices and resources in the local area network, but it cannot use Internet applications, such as WeChat, Douyin, etc. If you want to access the Internet , you also need to implement relevant protocols on the mobile phone's upstream network device, that is, configure NAT , PPPOE and other functions on the wireless router, and then connect the local area network to the Internet through the Internet line provided by the operator, and the mobile phone can go online to play WeChat and watch Douyin.
Local area network : A private network within a small area. A network within a home, a network within a company, and a network within a campus are all local area networks.
Wide Area Network : A network that connects local area networks in different regions. Operators build wide area networks to achieve cross-regional network interconnection.
Internet : A network that connects the entire world. The Internet is an open, interconnected network that does not belong to any individual or organization. After connecting to the Internet, you can communicate with any host on the Internet.
Simply put, mobile phones, wireless routers and other devices communicate through a variety of network protocols . Network protocols are standards or rules defined for communication between parties. As long as the devices follow the same network protocol, they can communicate. Who defines the network protocol? ISO has developed an international standard OSI , in which the OSI reference model is often used in the formulation of network protocols.
OSI Reference Model
The OSI reference model divides the services provided by network protocols into 7 layers and defines the service content of each layer. The protocols implement the services of each layer , and the specific content of the protocols is the rules . The upper and lower layers interact through interfaces , and the same layers interact through protocols . The OSI reference model only roughly defines the services of each layer and does not define the protocols in detail, but many protocols correspond to one of the 7 layers. Therefore, to understand the network, you must first understand the OSI reference model.
Application Layer
Layer 7 (the highest layer) of the OSI reference model . The interface between the application and the network, providing services directly to users. Application layer protocols include email, remote login, etc.
Presentation Layer
Layer 6 of the OSI reference model . Responsible for the conversion of data formats, such as encoding, data format conversion, encryption and decryption, etc. It ensures that the information sent by the application layer of one system can be read by the application layer of another system.
Session Layer
Layer 5 of the OSI reference model . It is mainly responsible for managing and coordinating the communication (conversation) between various processes on different hosts, that is, it is responsible for establishing, managing and terminating sessions between applications.
Transport Layer
Layer 4 of the OSI reference model . Provides reliable and transparent data transmission services between communicating hosts for upper layer protocols, including handling of error control and flow control issues. Processed only on the communicating hosts, not on the routers.
Network Layer
Layer 3 of the OSI reference model . It transmits data to the destination address on the network and is mainly responsible for addressing and routing.
Data Link Layer
Layer 2 of the OSI reference model. Responsible for the communication transmission between two interconnected hosts at the physical level, dividing the bit stream composed of 0 and 1 into data frames and transmitting them to the other end, that is, the generation and reception of data frames. Communication transmission is actually achieved through physical transmission media. The role of the data link layer is to process data between these devices interconnected by transmission media.
Both the network layer and the data link layer send data to the receiver based on the destination address, but the network layer is responsible for sending the entire data to the final destination address, while the data link layer is only responsible for sending data within a segment.
Physical Layer
Layer 1 (the bottom layer) of the OSI reference model . It is responsible for the conversion between logical signals (bit streams) and physical signals (electrical signals, optical signals), and provides physical connections for the data link layer through transmission media.
TCP/IP Reference Model
Since the OSI reference model divides services into too many small parts, it is a bit idealistic to define the reference model first and then the protocol. The TCP/IP model is just the opposite. It is a model summarized from existing protocols and becomes the actual network protocol standard in the industry.
TCP/IP is a protocol recommended and standardized by IETF. It is a collection of protocols such as IP, TCP, HTTP, etc. TCP/IP is a protocol family developed for the use of the Internet, so the Internet protocol is TCP/IP.
First, let’s introduce the correspondence between TCP/IP and OSI layers, as well as the main protocols of each layer of TCP/IP.
Network access layer
TCP/IP is based on the premise that the functions of the physical layer and data link layer of the OSI reference model are transparent. The two layers are not defined, so the physical layer and data link layer can be combined and called the network access layer . The network access layer manages the network medium and defines how to use the network to transmit data. However, the two layers play different roles in the communication process, so the physical layer and data link layer are also called hardware and network interface layers respectively . TCP/IP can be divided into four or five layers, as long as you can understand the principles.
Devices are interconnected through physical transmission media, and MAC addresses are used to achieve data transmission between interconnected devices. The purpose of using MAC addresses is to identify devices connected to the same transmission medium.
Network Layer
It is equivalent to the third network layer in the OSI model and uses the IP protocol . The IP protocol forwards packet data based on the IP address and its function is to send data packets from the source address to the destination address.
The functions of the network layer and transport layer in the TCP/IP layer are usually provided by the operating system . The router forwards data packets through the network layer .
During network transmission, each node will determine which network card should send the message based on the address information of the data. Each address will refer to a list of outgoing interfaces. The table referenced in MAC addressing is called the MAC address forwarding table , while the table referenced in IP addressing is called the routing control table . The MAC address forwarding table is automatically generated based on self-study. The routing control table is automatically generated based on the routing protocol. The MAC address forwarding table records the actual MAC address itself, while the IP address recorded in the routing table is the network number (i.e., the network number and subnet mask) that is later concentrated.
IP
IP is a protocol that transmits data packets across the network, using IP addresses as host identifiers so that the entire Internet can receive data. The IP protocol is independent of the underlying medium and implements data forwarding from source to destination. The IP protocol does not have a retransmission mechanism and is an unreliable transmission protocol.
ICMP
Used to transmit control messages between IP hosts and routers to diagnose the health of the network.
ARP
A protocol that resolves the MAC address from the IP address of a packet.
Transport Layer
It is equivalent to the 4th transport layer in the OSI model. Its main function is to enable applications to communicate with each other. It identifies applications by port numbers and uses the connection-oriented TCP protocol and the connectionless UDP protocol.
Connection-oriented is to connect a logical communication link between the sending and receiving hosts before sending data. It is like making a phone call. After entering the other party's phone number and dialing, the call can only be made. After the call is over, hanging up the phone is like cutting off the power.
Connectionless does not require establishing and disconnecting connections. The sender can freely send data at any time. Just like sending a letter, there is no need to confirm whether the recipient's information really exists, nor is there any need to confirm whether the recipient can receive the letter. As long as there is a mailing address, you can send the letter.
TCP
TCP is a connection-oriented transport layer protocol that can control the connections it provides. It is suitable for applications that require reliable transmission , such as file transfer.
UDP
UDP is a connectionless transport layer protocol that does not control the connections it provides. It is suitable for real-time applications, such as IP telephony, video conferencing, live broadcasting, etc.
Application Layer
It is equivalent to the collection of layers 5 to 7 in the OSI model. It not only implements the functions of the application layer of the OSI model, but also the functions of the session layer and presentation layer. HTTP, POP3, TELNET, SSH, FTP, and SNMP are all application layer protocols.
The architecture of TCP/IP applications mostly belongs to the client/server model. The program that provides services is called the server, and the program that receives services is called the client. The client can send requests to the server at any time.
HTTP
It is an application layer communication protocol between WWW browsers and servers. The main format of the transmitted data is HTML. HTTP defines high-level commands or methods for browsers to communicate with Web servers.
POP3
Simple Mail Transfer Protocol, used by mail clients and mail servers.
TELNET and SSH
Remote terminal protocol, used for remote management of network devices. TELNET is plain text transmission, while SSH is encrypted transmission.
SNMP
Simple Network Management Protocol is used by network management software to monitor and manage network devices.
Encapsulation and decapsulation
Usually, the information provided to the protocol is the packet header , and the content to be sent is the data . In each layer, a header is attached to the data to be sent, and this header contains the necessary information for that layer, such as the destination address and protocol-related information. From the perspective of the next layer, all packets received from the previous layer are considered to be data of this layer.
Before data is sent, protocol message header information is added as the data passes through each layer from top to bottom according to the reference model. This process is called encapsulation .
After the data is received, the protocol header information is removed from each layer according to the reference model from bottom to top. This process is called decapsulation .
The data encapsulated by the transport layer protocol is called a segment , the data encapsulated by the network layer protocol is called a packet , the data encapsulated by the data link layer protocol is called a frame , and the data transmitted at the physical layer is bits .
In TCP/IP communication, MAC address, IP address, port number and other information are used as address identification . Even in the application layer, email addresses can be used as addresses for network communication.
Actual data transmission example
In real life, the Internet uses the TCP/IP protocol for network connection. Let's take visiting a website as an example to see how the network communicates.
Sending Data Packets
When accessing an HTTP website page, open the browser, enter the URL, and press Enter to start TCP/IP communication.
Application Processing
First, HTML format encoding is performed in the application , which is equivalent to the presentation layer function of OSI. After the encoding conversion, it may not be sent out immediately, which is equivalent to the function of the session layer. At the moment the request is sent, a TCP connection is established, and then data is sent on the TCP connection. The next step is to send the data to the next layer of TCP for processing.
TCP module processing
TCP will smoothly send the data sent by the application layer to the destination. To achieve the function of reliable transmission, the data needs to be encapsulated with TCP header information. The TCP header information includes the source port number and the destination port number (to identify the application on the host), the sequence number (to confirm which part is the data) and the checksum (to determine whether the data is damaged). The segment encapsulated with the TCP header information is then sent to IP.
IP Block Processing
IP treats the data segment transmitted by TCP as its own data and encapsulates the IP header information. The IP header information contains the destination IP address, source IP address, and upper layer protocol type information.
After the IP packet is generated, the data is sent according to the host routing table .
Network interface processing
The network interface encapsulates the Ethernet header information on the IP packet and performs the sending process. The Ethernet header information contains the destination MAC address, source MAC address, and upper layer protocol type information. Then the Ethernet data frame is transmitted to the receiving end through the physical layer. The FCS in the sending process is calculated by hardware and added to the end of the packet. The purpose of setting the FCS is to determine whether the data packet is damaged by noise.
Receiving Packets
The packet receiving process is the reverse process of the sending process.
Network interface processing
After receiving the Ethernet packet, first check whether the destination MAC address in the header information is a packet sent to itself. If it is not a packet sent to itself, discard it. If it is a packet sent to itself, check whether the upper layer protocol type is an IP packet, decapsulate the Ethernet frame into an IP packet, and pass it to the IP module for processing. If it is an unrecognizable protocol type, discard the data.
IP Block Processing
After receiving the IP packet, similar processing is performed. According to the destination IP address in the header information, it is determined whether the packet is sent to itself. If it is a packet sent to itself, the protocol type of the upper layer is checked. If the upper layer protocol is TCP, the IP packet is decapsulated and sent to the TCP protocol for processing.
If there is a router and the receiving end is not its own address, the data will be forwarded according to the routing control table.
TCP module processing
After receiving the TCP segment, first check the checksum to determine whether the data is corrupted. Then check whether the data is received according to the sequence number. Finally, check the port number to determine the specific application.
After the data is received, a " confirmation receipt " is sent to the sender. If the receipt information fails to reach the sender, the sender will think that the receiver has not received the data and will keep sending it repeatedly.
After the data is completely received, the TCP segment is decapsulated and sent to the application identified by the port number.
Application Processing
After receiving the data, the application parses the data content to learn the content of the web page requested by the sender, and then performs subsequent data interaction according to the HTTP protocol.
Network composition
Building a network involves various cables and network devices. Here are some common hardware devices . The layers mentioned in the hardware devices refer to the OSI reference model, not the TCP/IP model.
Communication media and data links
Devices are connected by cables. Wired cables include twisted pair cables, optical fibers, and serial cables. Select the corresponding cables according to the data link. Transmission media can also be divided into different types of electromagnetic waves, such as radio waves and microwaves.
Transmission rate : The unit is bps, which refers to the amount of data transmitted per unit time. It is also called bandwidth. The larger the bandwidth, the stronger the network transmission capacity.
Throughput : The unit is bpsthe actual transmission rate between hosts. The term throughput not only measures bandwidth, but also measures the CPU processing power of the host, the degree of network congestion, the share of data fields in the message, and other information.
Network Card
When any host connects to the network, it must use a network card. It can be a wired network card to connect to a wired network, or a wireless network card to connect to a WiFi network. Each network card has a unique MAC address , also called a hardware address or physical address.
Layer 2 switch
A layer 2 switch is located at layer 2 (data link layer) of the OSI model. It can identify data frames in the data link layer and forward the frames to another connected data link.
There is a data bit in the data frame called FCS , which is used to check whether the data is correctly delivered to the destination. The Layer 2 switch discards the damaged data by checking this value.
The Layer 2 switch determines whether to forward data frames based on the MAC address self-learning mechanism .
Router/ Layer 3 switch
A router is a device that connects two networks and forwards messages at the third layer (network layer) of the OSI model. A layer 2 switch processes based on MAC addresses, while a router/layer 3 switch processes based on IP addresses . Therefore, the network layer address in TCP/IP becomes an IP address.
A router can connect different data links. For example, it can connect two Ethernet networks, or connect an Ethernet network to a wireless network. The wireless router commonly found in homes is also a type of router.
Layer 4 to 7 switches
Layer 4 to 7 switches are responsible for processing data from the transport layer to the application layer in the OSI model. Based on the transport layer of protocols such as TCP and the application layer above, they analyze the sent and received data and perform specific processing on it. For example, if a server of a video website cannot meet the access requirements, the access is distributed to multiple servers in the background through a load balancing device , which is a type of layer 4 to 7 switch. There are also application scenarios such as bandwidth control, WAN accelerators, and firewalls.
Summarize
Application layer devices include computers, mobile phones, servers, etc. Application layer devices do not forward data . They are the source or destination of data and have functions of each layer below the application layer. When sending data, the data is encapsulated layer by layer from top to bottom and then sent out through Ethernet. When receiving data, the data is decapsulated layer by layer from bottom to top and finally restored to the original data.
Data link layer devices include layer 2 switches and bridges. Layer 2 network devices only forward data by identifying the MAC address of the data. After receiving the data, the layer 2 switch checks the Ethernet header information of the outermost layer of the data encapsulation, and after seeing the destination MAC address of the data, it sends the data frame out from the corresponding port. The switch does not decapsulate the data frame, and as long as it knows the MAC address information, it can correctly forward the data.
Network layer devices include routers and layer 3 switches. Layer 3 network devices only forward data by identifying the IP address of the data . After receiving the data, the router first checks the Ethernet header information of the outermost layer. When the destination MAC address is itself, it will decapsulate the Ethernet header and check the IP address of the data. When making a forwarding decision based on the IP routing table, the router will use the MAC address of the next-hop device as the destination MAC address of the Ethernet header, re-capsulate the Ethernet header and forward the data.
The network equipment that forwards data and the data at the application layer are like a courier and a package . The courier delivers the package to the destination address without having to know the specific contents of the package.
It is no longer applicable to distinguish network devices by layered functions. A switch with integrated layer 3 routing function is a layer 3 switch, and a wireless AP with integrated router function is a wireless router. However, for the sake of convenience, the functions and principles of a single device are usually introduced.