Switch, router packet forwarding

When the switch receives a data packet; the
switch will extract the destination MAC information of the packet header and compare it with its own MAC address table;
Case 1: If the corresponding item is found, it will be forwarded according to the MAC table;
Case 2: If If no corresponding item is found, it will be forwarded (broadcast) on all ports except the received data packet ;

Explanation: The MAC address table is the relationship table corresponding to the MAC address and the port; that is, a MAC address is connected to a certain port;
Question: When the second situation occurs, it needs to broadcast on all other ports (which is actually very bad), where will this broadcast end? Answer: - Router.

Why does it end with the router?
When the router receives a data packet, the working process is as follows:
First, extract the destination MAC information of the data packet header and compare it with its own MAC table:
Case 1: If the corresponding item is found, it will be forwarded according to the MAC table (same as the switch);
Case 2 (The highlight is here): If it is not found, extract the destination IP information of the data packet header and compare it with its own routing table (there are two cases)
      Case 2. 1 If the corresponding routing table is found, forward it according to the routing table (similar to finding the MAC table).
      Case two. 2 If the corresponding routing table is not found, it is forwarded according to the default route (did you find that there is no broadcast from beginning to end)

Explain : The difference between the routing table and the MAC is that the routing table stores the place where the destination IP will go next IP.

The broadcast can end on the router because the router will route according to the destination IP when it cannot find the corresponding MAC table. There are no broadcasts in either case of the routing process.


The article is reproduced from: http://blog.csdn.net/liuaibing/article/details/7263106


Communication between network devices goes through the following steps:

1. The data of the sender sends a data packet to the outside ;

2. The system determines whether the destination address of the data packet is in the same network segment;

3. If it belongs to the same network segment as the transmitter, the system directly encapsulates the data packet into a frame, and sends it to the target address in this network segment through the Layer 2 device;

4. If it is not in the same network segment, the system forwards the data packet to the gateway and re-encapsulates it;

5.网关查看数据包送达的目标ip地址;

6.系统根据目标ip地址查找路由表,决定转发端口;

7.重新封装转发到下一个路由器;

8.网关发现目标地址属于本网段,查找MAC表(ipmac对应关系),封装成帧发送到目标机器网卡;

9.目标主机验证后传送给上层应用。


IP数据包到路由器之后,它首先要读去IP包头的目标IP地址,然后查看路由表,根据路由协议算法,确定一条最佳的路径,为什么要这样做呢,那是因为要确定这个数据包应该从路由器上的那个接口上转发出去,很明显路由的每一条可用的路由都和路由嚣上的接口是对应的,就这样一个数据包就离开了这个路由器。事情就是这样。看见楼上有一个不太正确的说法,IP数据包头里的源IP和目标IP都不会被改变,如果改变数据包不可能到达或者回来,但只有一种情况,源和目标IP是被改变的,那就是NAT.

路由器转发数据包不会对它的IP源地址和目标地址做修改,只会修改MAC.

具体路由器转发规则如下:

当主机A发向主机B的数据流在网络层封装成IP数据包,IP数据包的首部包含了源地址和目标地址。主机A会用本机配置的24位IP网络掩码255.255.255.0与目标地址进行与运算,得出目标网络地址与本机的网络地址是不是在同一个网段中。如果不是将IP数据包转发到网关。

在发往网关前主机A还会通过ARP的请求获得默认网关的MAC地址。在主机A数据链路层IP数据包封装成以太网数据帧,然后才发住到网关……也就是路由器上的一个端口。

当网关路由器接收到以太网数据帧时,发现数据帧中的目标MAC地址是自己的某一个端口的物理地址,这时路由器会把以太网数据帧的封装去掉。路由器认为这个IP数据包是要通过自己进行转发,接着它就在匹配路由表。匹配到路由项后,它就将包发往下一条地址。

路由器转发数据包就是这样,所以它始终是不会改IP地址的。只会改MAC.

当有数据包传到路由器时,路由器首先将其的目的地址与路由表进行对比,如果是本地网络,将不会进行转发到外网络,而是直接转发给本地网内的目的主机,改变的只是数据包的源地址,(原源地址MAC变为了路由器的MAC);当然其中的过程关系到相关的协议的使用,像什么ARP,IP等。但是如果目的地址经路由表对比,发现不是在本网中,有nat就将改变源地址的IP(原源地址的Ip地址改为了路由器的IP地址),路由器将熟数据包转发到相应的端口,进行通信。

如:A访问B,首先对比是否同一子网,如果是,检查ARP表,有B的MAC就直接发送,没有就发送ARP请求.如果否,发送到默认网关C,源IP为A,源MAC为A,目的IP为B,目的MAC地址为C,C接收到这个包,检查路由表,发送到下一跳D,源IP为A,源MAC为C,目的IP为B,目的MAC为D.....如此循环,直到发送到B.NAT为特殊应用,会修改源IP为网关自己外网IP。

Guess you like

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