在 VirtualBox 中完全開放 FreeBSD 路由器

在 VirtualBox 中完全開放 FreeBSD 路由器

TL;DR - 我想設定一個FreeBSD 虛擬機,其中一張網卡位於我的家庭LAN (192.168.1.0/24) 上,一張網卡位於私人內部到虛擬盒網路(10.9.9.0/24) 上,然後透過兩者之間來回的任何和所有流量。

長期 Linux 使用者(伺服器上的 Debian)但只使用 FreeBSD 大約一天:)

無論如何,對於我的實驗材料,我有一台帶有 2 個網路介面的虛擬機 - 一個橋接到我的家庭 LAN,一個位於僅限內部網路上。這台機器被設定為無塊路由器,只需在 eth0 和 eth1 之間傳遞資料包,無論來源或目的地如何。使用 iptables 很容易做到 -

iptables -A FORWARD -i eth0 -o eth1 -j ACCEPT
iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT

但我一直在嘗試讓它與 pf 一起工作,但只取得了部分成功。

gateway_enable="YES"
pf_enable="YES"
pf_rules="/etc/pf.conf"

在我的/etc/rc.conf/etc/pf.conf包含的

pass from em1:network to any keep state
pass from em0:network to any keep state
pass in inet proto tcp to any keep state
pass in inet proto udp to any keep state
pass out inet proto tcp to any keep state
pass out inet proto udp to any keep state

我可以啟動僅連接到內部的即時光碟虛擬機,並將 em1 的 IP 設為預設網關,並且能夠 ping em1、ping em0,但我無法 ping 正在運行的主機 vbox 或我的任何其他電腦LAN 或透過http 、ssh 等連接。

[root@bsdtest ~]# pfctl -sa
FILTER RULES:
pass in inet proto tcp all flags S/SA keep state
pass in inet proto udp all keep state
pass out inet proto tcp all flags S/SA keep state
pass out inet proto udp all keep state
pass inet from 10.9.9.0/24 to any flags S/SA keep state
pass inet from 192.168.1.0/24 to any flags S/SA keep state

STATES:
all tcp 192.168.1.90:22 <- 192.168.1.10:48102       ESTABLISHED:ESTABLISHED
all udp 192.168.1.2:53 <- 10.9.9.5:59075       NO_TRAFFIC:SINGLE
all udp 10.9.9.5:59075 -> 192.168.1.2:53       SINGLE:NO_TRAFFIC
all udp 192.168.1.2:53 <- 10.9.9.5:34207       NO_TRAFFIC:SINGLE
all udp 10.9.9.5:34207 -> 192.168.1.2:53       SINGLE:NO_TRAFFIC
all udp 192.168.1.2:53 <- 10.9.9.5:43515       NO_TRAFFIC:SINGLE
all udp 10.9.9.5:43515 -> 192.168.1.2:53       SINGLE:NO_TRAFFIC
all udp 192.168.1.2:53 <- 10.9.9.5:1636       NO_TRAFFIC:SINGLE
all udp 10.9.9.5:1636 -> 192.168.1.2:53       SINGLE:NO_TRAFFIC
all udp 192.168.1.2:53 <- 10.9.9.5:60124       NO_TRAFFIC:SINGLE
all udp 10.9.9.5:60124 -> 192.168.1.2:53       SINGLE:NO_TRAFFIC
all udp 192.168.1.2:53 <- 10.9.9.5:8866       NO_TRAFFIC:SINGLE
all udp 10.9.9.5:8866 -> 192.168.1.2:53       SINGLE:NO_TRAFFIC
all udp 192.168.1.2:53 <- 10.9.9.5:25534       NO_TRAFFIC:SINGLE
all udp 10.9.9.5:25534 -> 192.168.1.2:53       SINGLE:NO_TRAFFIC
all udp 192.168.1.2:53 <- 10.9.9.5:30141       NO_TRAFFIC:SINGLE
all udp 10.9.9.5:30141 -> 192.168.1.2:53       SINGLE:NO_TRAFFIC

INFO:
Status: Enabled for 0 days 00:08:28           Debug: Urgent

State Table                          Total             Rate
  current entries                       17               
  searches                            1990            3.9/s
  inserts                              253            0.5/s
  removals                             236            0.5/s
Counters
  match                                253            0.5/s
  bad-offset                             0            0.0/s
  fragment                               0            0.0/s
  short                                  0            0.0/s
  normalize                              0            0.0/s
  memory                                 0            0.0/s
  bad-timestamp                          0            0.0/s
  congestion                             0            0.0/s
  ip-option                              0            0.0/s
  proto-cksum                            0            0.0/s
  state-mismatch                         0            0.0/s
  state-insert                           0            0.0/s
  state-limit                            0            0.0/s
  src-limit                              0            0.0/s
  synproxy                               0            0.0/s
  map-failed                             0            0.0/s

TIMEOUTS:
tcp.first                   120s
tcp.opening                  30s
tcp.established           86400s
tcp.closing                 900s
tcp.finwait                  45s
tcp.closed                   90s
tcp.tsdiff                   30s
udp.first                    60s
udp.single                   30s
udp.multiple                 60s
icmp.first                   20s
icmp.error                   10s
other.first                  60s
other.single                 30s
other.multiple               60s
frag                         30s
interval                     10s
adaptive.start             6000 states
adaptive.end              12000 states
src.track                     0s

LIMITS:
states        hard limit    10000
src-nodes     hard limit    10000
frags         hard limit     5000
table-entries hard limit   200000

OS FINGERPRINTS:
758 fingerprints loaded
[root@bsdtest ~]# 

有任何想法嗎?有關從 10.9.9.5(我的即時光碟)到 192.168.1.2 的 udp 流量的線路將用於 DNS 到我的家庭 LAN 名稱伺服器,但沒有回應到達...這是 http 請求顯示的內容 -

[root@bsdtest ~]# pfctl -sa | grep 80
all tcp 192.168.1.10:80 <- 10.9.9.5:59436       CLOSED:SYN_SENT
all tcp 10.9.9.5:59436 -> 192.168.1.10:80       SYN_SENT:CLOSED
all tcp 192.168.1.10:80 <- 10.9.9.5:59438       CLOSED:SYN_SENT
all tcp 10.9.9.5:59438 -> 192.168.1.10:80       SYN_SENT:CLOSED

有想法嗎?

答案1

好的,找到解決方案。

我的 /etc/rc.conf 沒問題...

/etc/pf.conf 需要是

# cat /etc/pf.conf

ext_if="em0"
int_if="em1"
boxnet = $int_if:network
homenet = $ext_if:network

nat on $ext_if from $boxnet to any -> ($ext_if)
pass quick from { lo0, $boxnet, $homenet } to any keep state

可能變數太多了,我只能使用原來的 em0/em1。不管怎樣,這給了你——

[root@bsdtest ~]# pfctl -vnf /etc/pf.conf
ext_if = "em0"
int_if = "em1"
icmp_types = "echoreq"
boxnet = "em1:network"
homenet = "em0:network"
nat on em0:network inet from 10.9.9.0/24 to any -> 192.168.1.0/24
nat on em1:network inet from 192.168.1.0/24 to any -> 10.9.9.0/24
pass quick inet from 127.0.0.0/8 to any flags S/SA keep state
pass quick inet from 192.168.1.0/24 to any flags S/SA keep state

答案2

以下是一些猜測,所以買者自負。然而,這是一個非常典型的設置,讓許多一開始就將路由、封包過濾(防火牆)和 NAT(網路位址轉換)混為一談的人感到困惑。

你沒有明確說明 - 但我猜你的網路看起來像這樣:

Internet <-A-> SOHO Router <-B-> Server/workstation <-C-> VM

您的 DNS 伺服器位於 B 網絡,即 192.168.1.0/24

我猜您的 Internet SOHO 路由器是 192.168.1.1,並且被設定為網路的預設閘道。這將是一種非常常見的設定。

您聲明 DNS 伺服器位於 192.168.1.2,伺服器橋接介面為 192.168.1.10。其後面有網路 10.9.9.0/24。

您的 iptables 設定將會轉發全部上的數據包介面。實際上,您將所有資料包從一個網路發送到另一個網路 - 甚至是本機資料包。這是重要的區別。

在你的 pf 設定中你做不是向前全部上的數據包介面。您已指定一個網路 em1:network。我們沒有完整的配置,但我猜您實際上有一個很好且可以正常工作的準系統配置。困擾你的是缺失的路線。

當您從 10.9.9.0/24 發送資料包時,它將到達 192.168.1.0/24 網路。您的伺服器已進入該網絡,因此您將直接到達您的 DNS。但 B 網路上的 DNS 伺服器不知道如何到達非本地 C 10.9.9.0/24 網路。然後所有回應都會發送到“預設路由器”,我猜這是您的 SOHO 路由器。該路由器也只知道在哪裡可以找到 192.168.1.0/24 網路(而不是 10.9.9.0/24),通常會將所有內容路由到您的外部 Internet 連結。在這種情況下,您使用的是正確的私有位址 - 因此封包將被丟棄,因為私有位址未在 Internet 上路由。

「正確」的解決方案是在 SOHO 路由器上設定一條路由,告訴它將 10.9.9.0/24 的封包路由到 192.168.1.10。一個像樣的路由器可以讓你做到這一點。不幸的是,許多廉價的 SOHO 路由器沒有。在這種情況下,您可以在 DNS 伺服器上新增路由來測試它。

  • 它對 iptables 起作用的原因是在 eth0 介面上可以看到回應資料包,並且所有資料包都會被轉送。 B 網路上的所有流量都會傳送到 C 網路(以及反向)。這包括可以/應該留在本地的流量。實際上您已經設定了一個網橋。
  • 它對您的第一個 pf 設定不起作用的原因是您已經指定了您期望看到的網路。 B網路上只有一台機器知道去哪裡找到C網。這是 192.168.1.10,因為它在 C 網路上有一個介面。實際上,您已經設定了基本的防火牆。過濾已準備就緒,但您尚未過濾任何內容。但缺少路由。
  • 它在您的第二個 pf 配置(您自己的答案)中工作的原因是您將 10.9.9.0/24 網路 NAT 到 192.168.1.0/24 的位址空間。所有來自 C 網路 10.9.9.0/24 的流量在 B 網路上將顯示為來自 192.168.1.10。應避免使用 NAT只要有可能,並且僅作為最後的手段。

如果您不需要/想要過濾包 - 那麼我建議您不要使用防火牆。你想做的事情應該由簡單路由

答案3

如果您希望擁有「全開放」網關,您只需使用一條規則即可做到這一點:

pass all allow-opts

不需要搞亂明確的「保持狀態」或標誌。

相關內容