Linux

nftables 從 wlan0 轉發到 eth0,但沒有任何反應

  • June 22, 2021

我有一個用 wlan0 連接到無線路由器的樹莓派,還有一個連接到 eth0 的伺服器。這兩種連接都可以正常工作。

Pi 和伺服器之間的 eth0 是靜態配置的,因此 Pi 的 eth0 的 IP 為 192.168.3.23/24,伺服器的 IP 為 192.168.3.200/24。Pi 的 wlan0 的 IP 為 192.168.1.131/24。

現在,我正在嘗試進行轉發,以便在連接 Pi 的 88 埠時,它會轉發到它後面的伺服器。我按照以下方式啟用了轉發sysctl -a | grep forward

net.ipv4.conf.all.bc_forwarding = 0
net.ipv4.conf.all.forwarding = 1
net.ipv4.conf.all.mc_forwarding = 0
net.ipv4.conf.default.bc_forwarding = 0
net.ipv4.conf.default.forwarding = 1
net.ipv4.conf.default.mc_forwarding = 0
net.ipv4.conf.eth0.bc_forwarding = 0
net.ipv4.conf.eth0.forwarding = 1
net.ipv4.conf.eth0.mc_forwarding = 0
net.ipv4.conf.lo.bc_forwarding = 0
net.ipv4.conf.lo.forwarding = 1
net.ipv4.conf.lo.mc_forwarding = 0
net.ipv4.conf.wlan0.bc_forwarding = 0
net.ipv4.conf.wlan0.forwarding = 1
net.ipv4.conf.wlan0.mc_forwarding = 0
net.ipv4.ip_forward = 1
net.ipv4.ip_forward_update_priority = 1
net.ipv4.ip_forward_use_pmtu = 0

我也這樣nftables配置:

table ip nat {
       chain prerouting {
               type nat hook prerouting priority dstnat; policy accept;
               tcp dport { 88, 443 } dnat to 192.168.3.200
       }

       chain postrouting {
               type nat hook postrouting priority srcnat; policy accept;
       }
}

AFAIK 這應該將 TCP 埠 88 和 443 轉發到 192.168.3.200 的伺服器,但沒有任何反應。如果我這樣做curl http://192.168.1.131:88,請求就會無限期掛起。不過,nftables 規則正在做某事,因為如果我刷新規則集,請求會立即被拒絕。

我在這裡錯過了什麼嗎?

您的 NAT 規則無法指定數據包進入的介面(對於 DNAT 或出去(對於 SNAT),因此它們適用於雙向的所有路由流量,這不是您想要的。

您至少需要為 DNAT 指定傳入介面才能正常工作。例如:

               iif wlan0 tcp dport { 88, 443 } dnat to 192.168.3.200

您完全從您的postrouting鏈中失去了出站偽裝規則。例如:

               oif wlan0 masquerade

使用目標 NAT,僅重寫目標。源保持不變,因此任何到達伺服器的包都將在 192.168.1.0/24 中具有源。

您的伺服器可能沒有到達 192.168.1.0/24 的路由;它只有一條到達 192.168.3.0/24 的路由。於是它舉起雙手,扔掉了包裹。

你有兩種方法可以解決這個問題:

  1. 將 192.168.3.23 設備設置為伺服器上的預設網關,並應用適當的防火牆設置。
  2. 也使用源 NAT,並重寫轉發包的源和目標。

引用自:https://serverfault.com/questions/1067430