26.4 OpenBSD 封包過濾器 (Packet Filter, PF)及 ALTQ

在 2003 年 6 月份,OpenBSD 的防火牆軟體 PF 被移植到 FreeBSD 中,並且收錄於 Ports Collection 內。 而 2004 年 11 月份所發行的 FreeBSD 5.3 版也是第一次將 PF 整合為基礎系統的一部分。 PF是個完備、全功能的防火牆, 並且具有選擇性 ALTQ (交錯佇列,Alternate Queuing) 的功能。 ALTQ提供了「服務品質」(QoS, Quality of Service)的頻寬管理功能, 其提供了源於過濾規則的模式來保障不同服務的頻寬。 OpenBSD 計劃中已經對 PF 的使用指南提供了詳盡的解說, 因此在這本手冊中我們不會做重複的闡述,而只介紹概要。

PF 在不同釋出版本之 FreeBSD 的可用情況摘錄於下:

FreeBSD 作業系統版本 取得 PF
4.X 之前的版本 早於 FreeBSD 4.X 之前的版本都不能使用 PF
所有 4.X 的版本 PF is available as part of KAME.
在 5.3-RELEASE 之前的 5.X release 在這些版本的 FreeBSD 中都可以透過 security/pf port 來安裝 PF。這些版本主要是針對開發者還有想要預覽早期 5.X 版本者。 強烈建議升級到 5.3-RELEASE 或是更新的 FreeBSD 版本。
5.3-RELEASE 以及之後的版本 PF 已經是系統中的一部分。 在這些版本裡頭,千萬不要從 ports 裡頭安裝 security/pf, 因為這是無法運作的。 請看系統中的 pf(4) 來支援。


更多關於 PF 的資訊可於下列網址查詢: http://pf4freebsd.love2party.net/.

26.4.1 啟用 PF

PF 在比 FreeBSD 5.3 還新的系統中,可以使用動態模組載入。 系統將會在你於 rc.conf 中加入 pf_enable="YES" 後動態地載入 PF 核心動態模組。 可載入模組在建立時也啟用了 pflog(4) 記錄。

Note: 這個模組假設核心中有 options INETdevice bpf。 除非編譯時在核心中定義 NOINET6 (FreeBSD 6.0 以後的版本為 NO_INET6) (例如在 make.conf(5) 中),否則 pf 模組同時也需要 options INET6

一旦載入 PF 核心模組或靜態地編譯入核心中, 就可以使用 pfctl 來啟動或關閉 pf

這個例子示範了如何啟動 pf:

# pfctl -e

pfctl指令提供了一個使用pf 防火牆的方式。 要了解更多使用 pfctl 的資訊, 查閱 pfctl(8) 的線上手冊會是個好方式。

26.4.2 核心選項

在編譯 FreeBSD 核心時加入下列的選項並不是啟用 PF 強制的要求。 在這裡只是要列出參考的背景資訊。 將 PF 編譯入核心中會讓可載入模組無法使用

設定 PF 的核心選項範例在核心源碼中的 /usr/src/sys/conf/NOTES, 並同時列舉如下:

device pf
device pflog
device pfsync

device pf 啟用了 「封包過濾(packet filter)」 的防火牆支援.

device pflog 啟動了選擇性的 pflog(4) 虛擬網路設備 pseudo network device),它可以透過 bpf(4) 的描述符號來記錄流量。 pflogd(8) 服務可以用來儲存訊息,並可以用日誌的形式記錄在硬碟上。

device pfsync 啟動了選擇性 的pfsync(4) 虛擬網路設備,它可以用來監控「狀態的改變」。 device pfsync並不是可載入模組, 要使用的話,必須要編入自訂的核心中才行。

這些設定將會在你編譯及安裝好新核心後才會生效。

26.4.3 rc.conf 可用的選項

你需要在 /etc/rc.conf 中加入下列的設定以便在啟動系統時同時啟用 PF:

pf_enable="YES"                 # 啟用 PF (如果需要的話載入模組)
pf_rules="/etc/pf.conf"         # PF 的規則定義檔案
pf_flags=""                     # pfctl 啟動時附加的選項
pflog_enable="YES"              # 啟動 pflogd(8)
pflog_logfile="/var/log/pflog"  # pflogd 儲存記錄檔案的地方
pflog_flags=""                  # pflogd 啟動時附加的選項

如果在這個防火牆後方你有個區域網路,並透過它來轉送封包, 你就必須要設定下列選項:

gateway_enable="YES"            # 啟用作為區域網路閘道器

26.4.4 啟用 ALTQ

ALTQ的選項只有在編入 FreeBSD 核心中才能生效。 不是所有的網路卡驅動程式都支援 ALTQ。 請看 altq(4) 線上手冊來了解你使用的 FreeBSD 版本中支援驅動程式的清單。 下面列出的選項將會啟用 ALTQ 及加入其他附加的功能。

options         ALTQ
options         ALTQ_CBQ        # Class Bases Queuing (CBQ)
options         ALTQ_RED        # Random Early Detection (RED)
options         ALTQ_RIO        # RED In/Out
options         ALTQ_HFSC       # Hierarchical Packet Scheduler (HFSC)
options         ALTQ_PRIQ       # Priority Queuing (PRIQ)
options         ALTQ_NOPCC      # Required for SMP build

options ALTQ 啟用了 ALTQ 主架構。

options ALTQ_CBQ 啟用「基於分類的佇列」 (Class Based Queuing, CBQ)支援。 CBQ 允許你 allows you to divide a connection's bandwidth into different classes or queues to prioritize traffic based on filter rules.

options ALTQ_RED enables Random Early Detection (RED). RED is used to avoid network congestion. RED does this by measuring the length of the queue and comparing it to the minimum and maximum thresholds for the queue. If the queue is over the maximum all new packets will be dropped. True to its name, RED drops packets from different connections randomly.

options ALTQ_RIO enables Random Early Detection In and Out.

options ALTQ_HFSC enables the Hierarchical Fair Service Curve Packet Scheduler. For more information about HFSC see: http://www-2.cs.cmu.edu/~hzhang/HFSC/main.html.

options ALTQ_PRIQ enables Priority Queuing (PRIQ). PRIQ will always pass traffic that is in a higher queue first.

options ALTQ_NOPCC enables SMP support for ALTQ. This option is required on SMP systems.

26.4.5 Creating Filtering Rules

The Packet Filter reads its configuration rules from the pf.conf(5) file and it modifies, drops or passes packets according to the rules or definitions specified there. The FreeBSD installation comes with a default /etc/pf.conf which contains useful examples and explanations.

Although FreeBSD has its own /etc/pf.conf the syntax is the same as one used in OpenBSD. A great resource for configuring the pf firewall has been written by OpenBSD team and is available at http://www.openbsd.org/faq/pf/.

Warning: When browsing the pf user's guide, please keep in mind that different versions of FreeBSD contain different versions of pf. The pf firewall in FreeBSD 5.X is at the level of OpenBSD version 3.5 and in FreeBSD 6.X is at the level of OpenBSD version 3.7.

The FreeBSD packet filter mailing list is a good place to ask questions about configuring and running the pf firewall. Do not forget to check the mailing list archives before asking questions.

This, and other documents, can be downloaded from ftp://ftp.FreeBSD.org/pub/FreeBSD/doc/.

For questions about FreeBSD, read the documentation before contacting <questions@FreeBSD.org>.
For questions about this documentation, e-mail <doc@FreeBSD.org>.