78 FreeBSD连载:设置和使用ipfw/natd( 三 )


rc.firewall
当启动了系统的包过滤能力之后,就需要在系统启动时将这些过滤规则自动载入内核,可以将这些添加过滤规则的指令写入启动文件rc.local或者其他启动文件中 。但系统的启动文件中本来就具备载入这些过滤规则的相关启动文件rc.firewall,这个文件中根据防火墙的基本要求,设置了常用的过滤规则,可以减轻防火墙管理员的设置工作 。
为了让系统执行rc.firewall中的设置,需要在rc.conf中设置参数firewall为YES,以及设置firewall_type的值,这个参数值将传递给rc.firewall,使rc.firewall执行不同的设置 。一般情况下可以将firewall_type设置为open,对网络访问不进行限制,而clIEnt主要用于计算机不对外提供网络服务,保护本计算机只用于访问外部系统而拒绝所有的访问请求,设置为simple则定义了简单的防火墙规则,可以使用这台计算机保护防火墙内部的计算机,这三个选项比较常用 。下面为缺省rc.firewall中的有关过滤规则的设置 。
############# Define the firewall type in /etc/rc.conf.Valid values are:#open- will allow anyone in#client- will try to protect just this Machine#simple- will try to protect a whole network#closed- totally disables IP services except via lo0 interface#UNKNOWN- disables the loading of firewall rules.#filename - will load the rules in the given filename (full path required)## For ``client"" and ``simple"" the entries below should be customized# appropriately.这里解释了rc.conf中对firewall参数的可能设置值,可以设置为不同的设置值,或者可以使用一个文件名作为firewall_type的值,指定一个包括了ipfw规则的文件,而设置使用者自己定制的过滤规则 。
############# Only in rare cases do you want to change these rules$fwcmd add 100 pass all from any to any via lo0$fwcmd add 200 deny all from any to 127.0.0.0/8这两个规则用于设置本地网络lo0的数据包能进行传输,但屏蔽所有从其他网络界面通向本地网络lo0的通信 。这是最基本的设置,防止在路由或IP地址设置不正确的情况下,外部计算机访问127.0本地网络 。
# Prototype setups.if [ "${firewall_type}" = "open" -o "${firewall_type}" = "OPEN" ]; then $fwcmd add 65000 pass all from any to any设置open为防火墙的类型时,即使没有设置IPFIREWALL_DEFAULT_TO_ACCEPT内核选项,也允许对数据包进行转发,因为这里设置的规则序号为65000,小于缺省的65535规则 。
elif [ "${firewall_type}" = "client" ]; then############# This is a prototype setup that will protect your system somewhat against# people from outside your own network.############# set these to your network and netmask and ipnet="192.168.4.0"mask="255.255.255.0"ip="192.168.4.17"当设置了client为防火墙的类型,那么就需要更改这里的IP地址和子网掩码的设置,使其符合自己计算机的实际情况 。client类型主要用于单机系统,保护本机不受外部计算机侵袭的设置 。它的保护与tcpwrapper类似,但比tcpwrapper更为基本和高效,但是它不能提供详细的连接记录日志 。
# Allow any traffic to or from my own net.$fwcmd add pass all from ${ip} to ${net}:${mask}$fwcmd add pass all from ${net}:${mask} to ${ip}这两个规则用于许可计算机与本地网络的通信 。
# Allow TCP through if setup succeeded$fwcmd add pass tcp from any to any established上面这两个规则允许连接已经设定成功的TCP连接的数据包能够保持连接,数据传输能够继续进行 。established参数就用于说明数据包为已经设置成功的TCP连接数据包 。
# Allow setup of incoming email$fwcmd add pass tcp from any to ${ip} 25 setup允许外部计算机能向内地smtp端口发起连接请求,setup用于说明定义的数据包为请求连接的数据包 。
# Allow setup of outgoing TCP connections only$fwcmd add pass tcp from ${ip} to any setup允许本地计算机能向外部网络发起连接请求 。

推荐阅读