2014年7月28日 星期一

FreeBSD架設NAT,使用PF(Packet Filter,封包過濾器)

需有兩張網路卡,對外(WAN),對內(LAN)
目前10.0 RELEASE核心預設就有PF,但還是需重新編譯核心,加入ALTQ,此功能不知是否必須,但若不加入,會一直出現訊息

重新編譯核心
cd /sys/amd64/conf

複製一個核心
cp GENERIC GENERIC_1

編輯GENERIC_1,加入ALTQ
options         ALTQ
options         ALTQ_CBQ        # Class Based 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)

開始編譯
config GENERIC_1
cd ../compile/ GENERIC_1
make depend all install

編輯/etc/rc.conf
ifconfig_em0="inet 172.16.0.151 netmask 255.255.255.0"對外網卡
ifconfig_em1="inet 192.168.2.1 netmask 255.255.255.0"對內網卡
pf_enable="YES"開機啟動PF
gateway_enable="YES"架NAT要開啟此項

編輯/etc/pf.conf
此檔案預設是沒有的,可以新增或cp /usr/share/examples/pf/pf.conf範例檔案
這個檔案也可以訂定各種封包過濾規則
if_ext="em0"對外網卡
if_int="em1"對內網卡
net_lan="192.168.2.1/24"內網區段mask

轉送封包
set skip on lo0
nat on $if_ext from $net_lan -> ($if_ext:0)
no nat on $if_ext from !$net_lan to any

這兩條規則會將試圖從外網連進來的擋掉
block in on $if_ext
pass out

接下來內網電腦就可以指向對內網卡的IP, bsd會將送進來的封包從對外網卡轉出去,外部送進來的轉送到來源端

沒有留言:

張貼留言