系統更新之後 IPF 跑起來怪怪的,換成 PF 試試看會不會正常一點。兩者語法差異並不大,但 PF 多出比較方便的功能。
官方文件:
https://www.freebsd.org/doc/handbook/firewalls-pf.html
MAN:
https://www.freebsd.org/cgi/man.cgi?query=pf.conf
rc.conf 選項
1 2 3 4 5 6 7 8 |
pf_enable="YES" pf_flags="" # additional flags for pfctl startup pf_rules="/path/to/pf.conf" # default /etc/pf.conf pflog_enable="YES" pflog_logfile="/var/log/pflog" # where pflogd should store the logfile pflog_flags="" # additional flags for pflogd startup gateway_enable="YES" # Enable as LAN gateway for NAT |
port 表示法
1 2 3 4 5 6 7 8 9 |
= (equal) != (unequal) < (less than) <= (less than or equal) > (greater than) >= (greater than or equal) : (range including boundaries) >< (range excluding boundaries) <> (except range) |
語法:
[in/out] [quick] [on if] [proto tcp/udp/…] [from ip/CIDR] [to ip/CIDR] [port ports] [flags S/SA/…] [keep-state]
以前若是機器有多個 IP,需要根據 IP 來選擇路由的時候,使用 IPF 會這樣做:
1 |
pass out quick on def_if to real_if:gateway_ip from real_if_ip/24 to any |
PF 沒有支援類似的語法,同樣的功能需要用 reply-to 來達成:
1 |
pass in quick on real_if reply-to (real_if gateway_ip) |
若要加上詳細的條件:
1 |
pass in quick on real_if reply-to (real_if gateway_ip) proto tcp from any to any port {www, https} |
差別在原本是封包出去的時候再重導,現在變成封包進來時就先決定回應要往哪裡丟。
PF 的規則是最後比對到的優先,而且對於順序有一定的要求,必須是按照 options, normalization, queueing, translation, filtering 由上往下列出。因此像是變數的設定,或是選項的調整都要放在最前面,接著是 NAT 的功能,最後才是放過濾的條件。