Routing

通過不同的介面連接到同一個伺服器(SO_BINDTODEVICE):Destination Host Unreachable

  • January 4, 2015

我有 2 個介面:eth0wlan0,每個介面都連接到不同的路由器。他們的網路規格如下:

eth0:
   ip: 192.168.1.7
   Gateway: 192.168.1.1
   Submask: 255.255.255.0

wlan0:
   ip: 192.168.2.21
   Gateway: 192.168.2.1
   Submask: 255.255.255.0

我以這種方式配置了路由:

ip route add table eth0 to 192.168.1.0/24 dev eth0 scope link
ip route add table eth0 default via 192.168.1.1 dev eth0
ip rule add from 192.168.1.7 table eth0

wlan0 使用他的值也是如此。所以路由輸出為:

ip rule
   0:      from all lookup local
   32764:  from 192.168.2.21 lookup wlan0
   32765:  from 192.168.1.7 lookup eth0
   32766:  from all lookup main
   32767:  from all lookup default

ip r s
   default via 192.168.1.1 dev eth0  proto static
   192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.7  metric 1
   192.168.2.0/24 dev wlan0  proto kernel  scope link  src 192.168.2.21  metric 9

ip r s table eth0
   default via 192.168.1.1 dev eth0
   192.168.1.0/24 dev eth0  scope link

ip r s table wlan0
   default via 192.168.2.1 dev wlan0
   192.168.2.0/24 dev wlan0  scope link

並且還改變了sysctl "net.ipv4.conf.all.rp_filter=0"sysctl -w "net.ipv4.ip_forward=1"。(真的不認為這ip_forward是必要的,但我已經改變了它以防萬一)。

現在,奇怪的是,當我 ping Google 強制界面wlan0時,它會顯示Destination Host Unreachable. 另一個界面工作正常。

ping -I wlan0 google.es
   PING google.es (173.194.45.183) from 192.168.2.21 wlan0: 56(84) bytes of data.
   From 192.168.2.21 icmp_seq=1 Destination Host Unreachable
   From 192.168.2.21 icmp_seq=2 Destination Host Unreachable
   From 192.168.2.21 icmp_seq=3 Destination Host Unreachable
   From 192.168.2.21 icmp_seq=4 Destination Host Unreachable

ping -I eth0 google.es
   PING google.es (173.194.45.191) from 192.168.1.7 eth0: 56(84) bytes of data.
   64 bytes from mad06s09-in-f31.1e100.net (173.194.45.191): icmp_seq=1 ttl=56 time=21.5 ms
   64 bytes from mad06s09-in-f31.1e100.net (173.194.45.191): icmp_seq=2 ttl=55 time=21.7 ms
   64 bytes from mad06s09-in-f31.1e100.net (173.194.45.191): icmp_seq=3 ttl=56 time=24.6 ms
   64 bytes from mad06s09-in-f31.1e100.net (173.194.45.191): icmp_seq=4 ttl=55 time=31.1 ms

我不確定在這種強制介面綁定的情況下如何確定源地址。如果源地址不是從設備中獲取的,那麼問題是您的ip rule選擇器不匹配,因此數據包會進入main路由表,即

default via 192.168.1.1 dev eth0  proto static

這不起作用wlan0

我建議你試試這個:

ip rule add from 192.168.1.7  table  eth0
ip rule add oif  eth0         table  eth0
ip rule add from 192.168.2.21 table wlan0
ip rule add oif  wlan0        table wlan0

並擴展

ip route add table eth0
ip route add table wlan0

src選項命令。

引用自:https://unix.stackexchange.com/questions/177398