MAC table, ARP cache table, routing table and port mapping NAT

1. MAC address table

Speaking of the MAC address table, we have to talk about the working principle of the switch (working at the second layer: the data link layer), because the switch forwards data frames according to the MAC address table. There is a table in the switch that records the correspondence between the MAC address of the LAN host and the interface of the switch. The switch is responsible for transmitting data frames to the designated host according to this table.

The working principle of the switch: After the switch receives the data frame, firstly, it will record the source MAC address and the corresponding interface in the data frame to the MAC table, and then it will check whether there is the target MAC address in the data frame in its own MAC table. If there is, the data frame will be sent out according to the corresponding interface recorded in the MAC table (that is, unicast). If not, the data frame will be sent out from the non-accepting interface (that is, broadcast).

1.1: As shown in the figure below: Explain in detail the process of the switch to transmit data frames

a simple network

1) Host A will send a data frame with the source MAC address as itself and the destination MAC address as host B to the switch.

2) After receiving the data frame, the switch first records the source MAC address and the corresponding interface (the interface is f 0/1) in the data frame into the MAC address table.

3) Then the switch will check whether there is information about the target MAC address in the data frame in its MAC address table. If so, it will be sent from the interface recorded in the MAC address table. If not, the data frame will be sent from the non- All interfaces of the receiving interface are sent out (that is, except the f 0/1 interface) (broadcast).

4) At this time, all hosts on the LAN will receive this data frame (these hosts will learn the MAC address of host 1), but only host B will respond to this broadcast when it receives this data frame (responding to a data frame, this data frame including the MAC address of host B).

5) When switch 1 receives the data frame responded by host B, it will also record the source MAC address in the data frame (that is, the MAC address of host B). The records in the MAC address table enable one-to-one communication (unicast) too.

1.2: As shown in the figure below: When multiple switches are interconnected in the LAN, how is the MAC address table of the switches recorded?

an actual network

1) Host A sends a data frame with a source MAC address of itself and a destination MAC address of host C to switch 1

2) After the switch 1 receives the data frame, it will learn the source MAC address (port f0/1 corresponds to the MAC of host 1), and check the MAC address table, and find that there is no record of the MAC address of the target host C, it will send the data frame Broadcast out, both host B and switch 2 will receive this data frame.

3) After receiving the data frame, switch 2 will also record the source MAC address of host 1 and the corresponding interface in the data frame into the MAC address table, and check its own MAC address table, and find that there is no MAC address of the target host C. records, this data frame is broadcast.

4) After host C receives the data frame, it will respond to the data frame and reply a data frame with the source MAC address as its own. At this time, both switch 1 and switch 2 will record the MAC address of host C into their own MAC address table. , and send this data frame to host A in the form of unicast.

5) At this time, the communication between host A and host C is to transmit data frames in the form of unicast, and the communication between host B and host C is the same as the above process, so the MAC address table of switch 2 records the MAC address of host A and host B. The addresses all correspond to interface f 0/1.

Summary: As can be seen from the above two figures, the switch has the function of dynamically learning the source MAC address, and one interface of the switch can correspond to multiple MAC addresses, but one MAC address can only correspond to one interface.

TIP: The MAC address dynamically learned by the switch is only valid for 300S by default. If there is no communication with the recorded MAC address within 300S, the record will be deleted.


2. ARP cache table detailed

We have explained the working principle of the switch above, and we know that the switch communicates through the MAC address, but how do we obtain the MAC address of the target host? At this time, we need to use the ARP protocol. There is an ARP table in each host, which records the correspondence between the host's IP address and MAC address.

ARP protocol: ARP protocol is a protocol working at the network layer, which is responsible for resolving IP addresses to MAC addresses.

As shown below: Explain in detail the working principle of ARP.

a simple network

1) If host A wants to send data to host B, host A will first check its own ARP cache table to see if there is a correspondence between the IP address and MAC address of host B. If so, it will use the MAC address of host B as the The source MAC address is encapsulated into the data frame. If not, host A will send an ARP request message, the destination IP address of the request is the IP address of host B, the destination MAC address is the broadcast frame of the MAC address (ie FF-FF-FF-FF-FF-FF), the source The IP address and MAC address are the IP address and MAC address of host A.

2) After the switch receives the data frame, it finds that the data frame is a broadcast frame, so it will send the data frame from all non-receiving interfaces.

3) When host B receives the data frame, it will check whether the IP address is its own, record the correspondence between the IP address and MAC address of host A in its own ARP cache table, and send an ARP response at the same time. This includes its own MAC address.

4) After receiving the data frame of the response, the host A records the correspondence between the IP address and the MAC address of the host B in its own ARP cache table. At this point, the switch has learned the MAC addresses of host A and host B.

TIP: In the ARP cache table of the host, only the corresponding relationship between IP and MAC in the same network segment is stored (cmd: ARP -a)


3. Detailed routing table

The router is responsible for the communication between different networks. It is an important device in today's network. It can be said that there is no Internet today without the router.

There is also a table in the router. This table is called the routing table, which records the information to different network segments.

The process of data routing:

路由器是工作在网络层的,在网络层可以识别逻辑地址。
当路由器的某个接口收到一个包时,路由器会读取包中相应的目标的逻辑地址(ip地址)的网络部分,然后在路由表中进行查找。
如果在路由表中找到目标地址的路由条目,则把包转发到路由器的相应接口,
如果在路由表中没有找到目标地址的路由条目,那么,如果路由配置了默认路由,就按照默认路由的配置转发到路由器的相应接口;
如果没有配置默认路由,则将该包丢弃,并返回不可到达的信息。

As shown below: The working principle of the router is introduced in detail:

Communication between different network segments

1) At the network layer, HostA encapsulates the packets from the upper layer into IP packets according to the IP protocol. The source IP address is itself, and the destination IP address is HostB (192.168.2.2). HostA will use the 24-bit subnet mask configured by the machine. The "AND" operation between the code and the target address shows that the target address is not the same network segment as the local machine (naturally, there is no record of HostB in its own ARP cache table), so the data packets sent by HostB need to be routed through its own default gateway. A's forwarding.

2) HostA obtains the MAC address of the network card E0 interface of gateway router A through ARP request, and encapsulates the MAC address of the router network card E0 interface into the destination MAC address at the link layer, and the source MAC address is itself (the IP datagram has not changed).

3) Router A can receive the data frame from E0, remove the encapsulation of the data link layer, and check whether there is an entry matching the target IP address network segment (ie the network segment of 192.168.2.2) in the routing table, according to the route The data recorded in the table on the 192.168.2.0 network segment should be sent to the next hop address 10.1.1.2 (the APR table of the E1 interface of router A contains the IP and MAC records of the E1 interface of router B), so the data is in router A's E1 interface. The E1 port is re-encapsulated. At this time, the source MAC address is the MAC address of the E1 interface of router A, and the destination MAC address of the encapsulation is the MAC address of the E1 interface of router 2.

4) Route B receives the data frame from the E1 port, and also removes the encapsulation of the data link layer, detects the target IP address, and matches it with the routing table. At this time, it is found that the network segment of the target address is just its own E0 port. On the directly connected network segment, Router B learns the MAC address of Host B through ARP broadcast. At this time, the data packet is re-encapsulated on the E0 interface of Router B. The source MAC address is the MAC address of the E0 interface of Router B, and the destination MAC address is that of Host B. MAC address. After the encapsulation is completed, it is directly sent to HostB from the E0 interface of the router.

5) Only then will HostB receive the data sent from HostA.

Summary: Routing tables are responsible for recording the path from one network to another, so routers work according to routing tables.


4. Intranet, public network, port mapping (NAT)

Generally, the IP of our own computer at home is generally an intranet IP such as 192168.xx, and the intranet cannot access the external network. In fact, we can communicate with public network hosts, such as access to Baidu and so on. So how is it done? Port Mapping. '

principle:

1) If a host A in the intranet wants to access Baidu, it will send an IP datagram to the default gateway S (with NAT capability, general router TPlink, etc.), and the header of the message includes the target host's (that is, Baidu) IP, port and A's own intranet IP and port,

2) The NAT gateway S will replace the IP and port of A in the header of the IP datagram sent by A with its own public IP and an unused port P, and will note the mapping relationship (port number <== "A's IP and port mapping) for future packet forwarding.

3) Then send the data to Baidu, Baidu responds after receiving the request data, and sends the data to port P of NAT gateway S, and then NAT gateway S forwards the data of port P to the computer A in the intranet, Realize the communication between the intranet and the public network.

Classification:

动态的端口映射: 当连接关闭时,NAT网关S会释放分配给这条连接的端口,以便以后的连接可以继续使用。
静态的端口映射: 在NAT网关上开放一个固定的端口,然后设定此端口收到的数据要转发给内网哪个IP和端口,不管有没有连接,这个映射关系都会一直存在。

TIP: Network subcontractors at all levels such as the second and third levels, such as the Great Wall, are a very poor existence.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325606211&siteId=291194637