为什么我可以访问不同接口的 IP 地址?

Dan*_*iel 4 routing iptables

我想知道这个,我似乎无法在互联网上找到解释。我有一个具有多个接口的 linux 网关:

eth0: external ip
eth1: 172.16.1.1/24
eth2: 172.16.2.1/24
ip_forward is enabled.
Run Code Online (Sandbox Code Playgroud)

IPtables 配置为 NAT 流量来自eth1 -> eth0eth2 -> eth0。但配置为不转发eth1 <-> eth2.

我的问题是:为什么172.16.2.0/24子网上的电脑可以ping 172.16.1.1(eth1接口的ip地址)?

纳特

Chain PREROUTING (policy ACCEPT 647K packets, 52M bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain INPUT (policy ACCEPT 17582 packets, 1160K bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 14951 packets, 1214K bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 187 packets, 42984 bytes)
 pkts bytes target     prot opt in     out     source               destination         
 333K   25M SNAT       all  --  *      eth0    0.0.0.0/0            0.0.0.0/0            to:<external ip>
Run Code Online (Sandbox Code Playgroud)

筛选

Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     tcp  --  eth1 *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22
    0     0 ACCEPT     tcp  --  eth2 *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22
    0     0 ACCEPT     tcp  --  eth0   *       <some trusted ip>       0.0.0.0/0            tcp dpt:22
    0     0 ACCEPT     udp  --  eth1  *       0.0.0.0/0            0.0.0.0/0            udp dpt:53
    0     0 ACCEPT     udp  --  eth2  *       0.0.0.0/0            0.0.0.0/0            udp dpt:53
    0     0 ACCEPT     icmp --  eth1  *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     icmp --  eth2  *       0.0.0.0/0            0.0.0.0/0           

Chain FORWARD (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ULOG       all  --  eth1  eth0    0.0.0.0/0            0.0.0.0/0            state NEW ULOG copy_range 0 nlgroup 1 prefix "NEW: " queue_threshold 1
    0     0 ULOG       all  --  eth2  eth0    0.0.0.0/0            0.0.0.0/0            state NEW ULOG copy_range 0 nlgroup 1 prefix "NEW: " queue_threshold 1
   0 0 ACCEPT     all  --  eth1  eth0    0.0.0.0/0            0.0.0.0/0           
   0 0 ACCEPT     all  --  eth2  eth0    0.0.0.0/0            0.0.0.0/0           
   0 0 ACCEPT     all  --  eth0   eth1   0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
   0 0 ACCEPT     all  --  eth0   eth2   0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     all  --  *      lo      0.0.0.0/0            0.0.0.0/0           
Run Code Online (Sandbox Code Playgroud)

Bat*_*hyX 5

我的问题是:为什么172.16.2.0/24子网上的电脑可以ping 172.16.1.1(eth1接口的ip地址)?

因为您允许这样做,而 Linux 默认情况下会这样做。

Linux 使用所谓的弱主机模型。这意味着当它收到来自eth2的数据包时,如果目标地址是其任何接口的 IP 地址,而不仅仅是的 IP 地址,它将认为该数据包是给他eth2的。即使转发被禁用。

因此该数据包进入 PREROUTING 钩子,内核然后看到目标地址是它的所以继续 INPUT 钩子,并且您接受来自 的所有 ICMP eth2,因此该数据包被接受。