
目前,我們正在嘗試將來自來賓 VLAN (eth1.251) 子網路的所有資料包透過wireguard 隧道路由到網際網路中。為了實現這一點,我們使用基於策略的路由,當流量來自我們的來賓子網路時,使用路由表 10 的規則:
32765: from 10.251.0.0/16 lookup 10
在路由表 10 中,我們建立一條通往隧道介面的預設路由:
default dev wg1 scope link
我們訪客網路中的所有用戶端都能夠透過預期的wireguard 隧道存取互聯網,但用戶端無法到達訪客網路的網關(10.251.0.1)
。 TCPDump 顯示 ICMP 回顯回應會透過介面路由回wg1
我們的隧道端點,這顯然不是預期的。對此的快速解決方案是將訪客 vlan 介面的作用域連結路由新增eth1.251
至路由table 10
:
default dev wg1 scope link
10.251.0.0/16 dev eth1.251 proto kernel scope link
現在客戶端可以到達路由器介面並提供服務。
該路由器上有另一個帶有子網路的介面 eth1 192.168.0.1/16
。當我們現在刪除新添加的10.251.0.0/16
路由時,table 10
我們無法再訪問路由器接口10.251.0.1
,但是我們仍然能夠達到192.168.0.1
來自子網路上客戶端的介面10.251.0.0/16
。子網路192.168.0.2
中路由器後面的用戶端(例如)192.168.0.0/16
無法從 進行復原10.251.0.0/16
。
主要問題:為什麼我們可以192.168.0.1
在沒有明確路由表條目的情況下到達路由器上的介面 ip,但不能10.251.0.1
從來賓子網路中的客戶端到達介面 ip 10.251.0.0/16
?
這是網路結構的概述。我認為這有助於理解我們的設置。
答案1
沒有一般性的解釋,只需遵循路由設定中發生的情況即可。
10.251.0.1 既是本地路由器位址,也是 10.251.0.0/16 的一部分。
當接收到一個資料包時當地的位址,路由器匹配當地的表使用第一個ip rule
最低優先權:0,在表 10 的規則之前,並且符合。請記住,路由表匹配目的地,而通常自訂規則配置為匹配來源。
當路由器回覆時,這次本地表不符:10.251.0.2 不是本地目的地。檢查下一條規則並匹配from 10.251.0.0/16
,查找表10,資料包通過工作小組1。
對於 192.168.0.1,接收資料包與之前完全相同當地的桌子。現在,答案與附加規則不匹配,並且主路由表適用:它照常工作:Linux 系統將從其任何 IP 進行回應。
再說一次,對於 192.168.0.2:它不是當地的IP,所以不匹配當地的表,但查詢確實符合新增的規則:封包透過工作小組1。
因此,將主路由表的一部分複製到額外的表以避免副作用會有所幫助。
ip route get
只要不涉及任何標記,其中的許多內容都可以使用正確的語法進行測試:
如果沒有表 10 中的附加條目:
# ip route get from 10.251.0.2 iif eth1.251 10.251.0.1
RTNETLINK answers: Invalid cross-device link
# sysctl -w net.ipv4.conf.eth1/251.rp_filter=2 #relax reverse path filtering
# ip route get from 10.251.0.2 iif eth1.251 10.251.0.1
local 10.251.0.1 from 10.251.0.2 dev lo table local
cache <local> iif eth1.251
# ip route get from 10.251.0.2 iif eth1.251 192.168.0.1
local 192.168.0.1 from 10.251.0.2 dev lo table local
cache <local> iif eth1.251
# ip route get from 10.251.0.2 iif eth1.251 192.168.0.2
192.168.0.2 from 10.251.0.2 dev wg1 table 10
cache iif eth1.251
回覆路線:
# ip route get from 10.251.0.1 10.251.0.2
10.251.0.2 from 10.251.0.1 dev wg1 table 10 uid 0
cache
# ip route get from 192.168.0.1 10.251.0.2
10.251.0.2 from 192.168.0.1 dev eth1.251 uid 0
cache
新增表 10 中的條目時:
# ip route get from 10.251.0.2 iif eth1.251 10.251.0.1 #even with strict reverse path filtering, since the reverse route is correct
local 10.251.0.1 from 10.251.0.2 dev lo table local
cache <local> iif eth1.251
# ip route get from 10.251.0.1 10.251.0.2
10.251.0.2 from 10.251.0.1 dev eth1.251 table 10 uid 0
cache