本人(作者)属新手上路, 才学习了没几天, 刚刚配好了一台代理服务器, 对于高手来讲实在是小菜一碟, 本没有什么可以值得炫耀的. 应 href="http://www.linuxforum.net">中国Linux论坛 FreeBSD世界版主 href="mailto:yjs@oldhand.org">r00t 的要求, 对配置过程做了一个记录, 不免贻笑大方.
文中如有什么错误, 请与作者
联系. 本文着重介绍配置透明代理, 基本不涉及网络服务器, 含dns, web, email的配置, 这些服务器对于防火墙的要求, 请参照其它介绍.
采用本文内容, 如果造成任何损失, 作者概不负责.
Reference:
IP Filter Based Firewalls HOWTO [ href="http://www.obfuscation.org/ipf/ipf-howto.pdf" target=_blank>PDF
| target=_blank>HTML ] (本文中简称 HOWTO)
target=_blank>Quick CableNet Connections with FreeBSD - By, Leon
系统构成
Gateway:
系统安装FreeBSD 4.5. PENTIUM-S 100, 80M内存, 4G硬盘. 安装双网卡.
Dlink DFE5000TX一块古老的网卡, 老得Windows 98都需要厂家的驱动, Windows 2000厂家都不提供驱动了, 不过BSD/Linux还都支持这块网卡, FreeBSD中为dc0. 该网卡连接内部网络.
Dlink DFE530TX, FreeBSD中为vr0. 该网卡连接 ADSL Modem.
网内客户机
本文中有时称为Client, 安装Windows XP
网络结构
Gateway dc0分配地址192.168.0.1/24, 通过HUB连接内网Windows XP客户机
192.168.0.4/24, vr0连接ADSL Modem, 通过pppoe拨号上网.采用动态地址.
本文假定Gateway 的 pppoe和DNS已经正常工作, 有关这方面的介绍, 请参照其它文章.
Gateway设置
编译内核
cd /usr/src/sys/i386/conf
cp GENERIC KERNEL1
vi KERNEL1
修改该内核配置文件, 增加如下配置
options IPFILTER
# ipfilter support
options IPFILTER_LOG
# ipmon( log support
options IPFILTER_DEFAULT_BLOCK
# block all packets by default
options RANDOM_IP_ID
# RANDOM_IP_ID causes the ID field in IP packets to be
randomized
# instead of incremented by 1 with each packet generated.
options BRIDGE
options ICMP_BANDLIM
# Rate limit bad replies
#options TCP_DROP_SYNFIN
# drop TCP packets with SYN+FIN
# 该参数可以提高系统的安全性,但在作web server时不建议使用,详见LINT
取消内核中所有与IPFIREWALL有关的配置
# options IPFIREWALL
# firewall
# options IPFIREWALL_VERBOSE
# enable logging to syslogd(
# options IPFIREWALL_FORWARD
# enable transparent proxy support
# options IPFIREWALL_VERBOSE_LIMIT=100
# limit verbosity
# options IPFIREWALL_DEFAULT_TO_ACCEPT
# allow everything by default
# options DUMMYNET
调整网络参数
options NMBCLUSTERS=32768
系统安装时该参数很小, 会影响网络的性能. 这个数值只是我简单的配置, 做服务器可能要更大. 具体说明详见 href="http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/index.html" target=_blank>FreeBSD HandBook "http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/configtuning-kernel-limits.html" target=_blank>6.10.2 Network Limits
标示该内核
ident KERNEL1
编译内核
cd /usr/src
make buildkernel KERNCONF=KERNEL1
make installkernel KERNCONF=KERNEL1
(这两个命令可以合并为 make kernel KERNCONF=KERNEL1)
修改配置文件
/etc/rc.conf
删除该文件中有关ipfw的配置, 或者将
firewall_enable="NO"
增加如下配置
gateway_enable="YES"
# enable gateway
named_enable=YES"
# enable naming service
ipfilter_enable="YES"
# Stateful firewall
ipfilter_program="/sbin/ipf"
ipfilter_rules="/etc/ipf.conf"
# 新增加的规则文件, 有人习惯文件名 .rule
ipfilter_flag=""
ipnat_enable="YES"
# Network Address Translation
ipnat_program="/sbin/ipnat"
ipnat_rules="/etc/ipnat.conf"
# 新增加的规则文件, 有人习惯文件名 .rule
ipmon_enable="NO"
# Firewall logging, 我没有开放, 如果是重要的服务器, 应该开放.
ipmon_program="/sbin/ipmon"
ipmon_flag="-Ds"
/etc/ipf.conf
该文件为防火墙配置文件, 本文主要介绍实现透明代理, 从网络的角度讲是代理Client, 因此关闭了Internet方向的Server
In的所有端口. 这个配置, 允许内网dc0的所有操作, 允许对外Internet的所有请求, 但外网Internet只允许对内网请求的回应, 其他包一律丢弃.
# 默认规则, 关闭阻止所有的包, 关闭所有的通路, 再一步一步打开允许的通道.
# 如果内核设置了 IPFILTER_DEFAULT_BLOCK, 可以省略.
block in all
block out all
# 允许内网的信息自由通过.
pass in quick on dc0 from 192.168.0.0/24 to any
pass out quick on dc0 from any to 192.168.0.0/24
# 允许本机的信息自由通过.
pass in quick on lo0 all
pass out quick on lo0 all
# 禁止外网的无效地址通过
block in quick on tun0 from 192.168.0.0/16 to any
block in quick on tun0 from 10.0.0.0/8 to any
block in quick on tun0 from 172.16.0.0/12 to any
block in quick on tun0 from 127.0.0.0/8 to any
block in quick on tun0 from 0.0.0.0/8 to any
block in quick on tun0 from 192.0.2.0/14 to any
block in quick on tun0 from 204.152.64.0/23 to any
block in quick on tun0 from 224.0.0.0/3 to any
# 对外网信息的处理, 允许内网包发送到外网, 并且允许外网对这些包的回应信息通过
pass out quick on tun0 proto tcp from any to any flags S/SAFR keep state keep frags
pass out quick on tun0 proto udp from any to any keep state keep frags
pass out quick on tun0 proto icmp from any to any keep state keep frags
/etc/ipnat.conf
该文件为NAT配置文件.
本文中pppoe通过DHCP方式获得地址, 因此无法在规则中给出外网的地址, 先用0代替.
# ftp proxy, 为 active 方式的FTP使用, 稍后再作介绍. 请注意, proxy的设置一定要在portmap之前.
#map tun0 192.168.0.0/24 -> 0/32 proxy port ftp ftp/tcp
# IKE proxy, 为ESP (Encapsulating Security Protocol) 使用
# map tun0 192.168.0.0/24 -> 0/32 proxy port 500 ipsec/udp
# RealAudio proxy, 只可用于PNM模式, RealPlayer G2已经使用RTSP.
#map tun0 192.168.0.0/24 -> 0/32 proxy port 7070 raudio/tcp
# 允许内部的UDP/TCP包通过,并且允许外网回应包通过
# 到外网采用指定范围的端口
# map tun0 192.168.0.0/24 -> 0/32 portmap tcp/udp 40000:60000
# 到外网由系统自动分配端口
map tun0 192.168.0.0/24 -> 0/32 portmap tcp/udp auto
# 允许内部ICMP通过,并且允许回应包通过
map tun0 192.168.0.0/24 -> 0/32
# 允许net2phone, 将呼入的呼叫转到指定机器
# rdr tun0 0/0 port 6801 -> 192.168.0.4 port 6801 udp
# 允许squid
# rdr dc0 0/0 port 80 -> 127.0.0.1 port 3128 tcp
/etc/sysctl.conf
net.ip.accept_sourceroute=0
# To definding against sequence number attacks
# based on rfc 1948 by randomize initial sequence number
# To verisy that an incoming packet arrives on an interface
# that has an address matching the packets destination address
# To Drop SYN packets destine to non-listening tcp/upd port.
# This will create a blackhole and protect against stealth port scans
# Increase TCP Windows size for increase in network performance
# Ref: [url=http://www.pccode.net]/networking/perf_tune.html"http://www.psc.edu/networking/perf_tune.html
DNS
修改文件 /etc/resolv.conf, 增加一条
nameserver DNS_SERVER_IP
如果内网有DNS Server, 应指向该服务器. 如果gateway本身为DNS Server, 为127.0.0.1, 必须配置named.conf. 一般最简单的配置就是指向ISP的DNS Server.
客户机设置
将客户机的Gateway 和 DNS Server都设置为Gateway机器的地址 192.168.0.1
系统测试
Gateway修改完配置后需要启动. 命令 shutdown -r now
客户机Windows XP不需重启,