28.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 的資訊可於下列網址查詢:http://pf4freebsd.love2party.net/.

28.4.1 啟用 PF

PF 在 FreeBSD 5.3 之後的系統中,就可以輕鬆使用 kernel 動態模組來載入。 在 rc.conf 中加入 pf_enable="YES" 後, 系統就會載入 PF 的 kernel 動態模組。這模組會在建立時也啟用 pflog(4) 記錄功能。

Note: 這個模組會假設 kernel 內已有 options INETdevice bpf。 除非編譯 kernel 時已在像是 make.conf(5) 設定檔中加入 NOINET6( FreeBSD 6.0 以後的版本則是 NO_INET6) 這樣才會避免不打開 IPv6 支援, 否則 pf 模組同時也需要 options INET6,也就是 IPv6 支援。

一旦載入 PF 的 kernel 模組或是靜態編譯入 kernel 內, 就可以使用 pfctl 來啟動或關閉 pf

下面這個例子示範如何啟動 pf

# pfctl -e

pfctl 是使用 pf 防火牆的指令。 若要了解更詳盡的 pfctl 運用,請查閱 pfctl(8) 線上手冊。

28.4.2 kernel 選項

在編譯 FreeBSD kernel 時,並不必完全加入下列的選項來啟用 PF。 在這裡只是要列出給你參考的一些資訊而已。 將 PF 編譯入 kernel 中,會導致無法使用 kernel 的動態載入模組。

設定 PF 的 kernel 選項範例在 kernel 原始碼中的 /usr/src/sys/conf/NOTES,轉貼內容如下:

device pf
device pflog
device pfsync

device pf 是用來啟動「packet filter(封包過濾)」 的防火牆支援。

device pflog,此功能要裝不裝皆可,它會啟動 pflog(4),以 bpf(4) 格式來記錄網路流量。 pflogd(8) daemon 則是用來紀錄這些訊息,並存在硬碟上。

device pfsync,此功能要裝不裝皆可,它會啟動 pfsync(4),可以用來監控「狀態的改變」。 請注意: device pfsync並不是 kernel 動態模組,要使用的話, 必須要編入自訂的 kernel 中才行。

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

28.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 啟動時附加的選項

如果您的防火牆後面有個 LAN(區域網路),並要透過它來轉送封包, 就必須要設定下列選項:

gateway_enable="YES"            # 啟用 LAN Gateway

28.4.4 啟用 ALTQ

ALTQ 只有在編入 FreeBSD kernel 中才能生效。 不是所有的網路卡驅動程式都支援 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 會啟用「CBQ」 (Class Based Queuing)支援。 CBQ 允許你 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.

28.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 郵遞論壇 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.

本文及其他文件,可由此下載:ftp://ftp.FreeBSD.org/pub/FreeBSD/doc/

若有 FreeBSD 方面疑問,請先閱讀 FreeBSD 相關文件,如不能解決的話,再洽詢 <questions@FreeBSD.org>。
關於本文件的問題,請洽詢 <doc@FreeBSD.org>。