NAT no FreeBSD.

Referência de como implementar um firewall com suporte a compartilhamento de internet (natd) no FreeBSD;

Configurar o Kernel:

***** /usr/src/sys/i386/conf/Generic.kernel *****
options IPFIREWALL
options IPFIREWALL_FORWARD
options IPFIREWALL_VERBOSE
options IPDIVERT
options IPSTEALTH
options TCP_DROP_SYNFIN
options ICMP_BANDLIM
*****

recompilar o kernel.. (há um tutorial em http://www.primeirospassos.org/sessao4_1.html)

Configurar o arquivo de inicialização:

***** /etc/rc.conf *****
natd_program="/sbin/natd"
natd_flags="-s -u"
natd_enable="YES"
natd_interface="200.206.217.169"
firewall_enable="YES"
firewall_quiet="YES"
firewall_type="/etc/rc.firewall.rules"
*****

Configurar o firewall:

***** /etc/rc.firewall.rules *****
add 00010 unreach needfrag ip from any to any in frag
add 00020 allow ip from 200.206.217.170 to 200.206.217.169 in recv ed0
add 00100 allow ip from any to any via lo0
add 00200 divert 8668 ip from 192.168.0.0/28 to not 192.168.0.0/28 in recv dc0
add 00201 divert 8668 ip from 192.168.0.0/28 to not 192.168.0.0/28 out xmit ed0
add 00202 divert 8668 ip from any to 200.206.217.169 in recv ed0
add 00203 divert 8668 ip from not 192.168.0.0/28 to 192.168.0.0/28 out xmit dc0
add 00210 allow icmp from 200.206.217.170 to 200.206.217.169 in recv ed0
add 00220 allow icmp from any to 200.206.217.169 in recv ed0 icmptype 11
add 01000 check-state
add 01010 allow ip from 192.168.0.0/28 to any keep-state
add 01020 allow ip from 200.206.217.169 to any keep-state
# tinha a 1030 q era redundante com a 65535, deny from any to any
add 01040 allow tcp from any to 200.206.217.169 21,22,25,53,80,110 keep-state in setup
add 01041 allow tcp from any to 200.206.217.169 3333,6667,7000 keep-state in setup
add 01050 allow udp from any to 200.206.217.169 53 keep-state in
# tinha a 65000 q era unreach host from any to any,
# deixei o padrao do firewall q eh deny e eh 65535.
# O unreach retorna a mensagem de destino inatingível.. 
# enquanto a deny retorna uma negação explícita.
*****

Configurando opções do kernel do sistema:

***** /etc/sysctl.conf *****
net.inet.icmp.drop_redirect=1
net.inet.icmp.icmplim=50
net.inet.icmp.log_redirect=1
net.inet.ip.fw.debug=0
net.inet.ip.fw.dyn_ack_lifetime=240 (evita problemas com a botnet de eggdrops)
net.inet.ip.redirect=0
net.inet.tcp.blackhole=2
net.inet.tcp.recvspace=32768
net.inet.udp.blackhole=1
*****

Definições atribuídas:

(devem ser substituídas para refletir o seu cenário)
200.206.217.169 -> ip da internet
200.206.217.170 -> ip do modem (gateway)
dc0 -> interface da rede local (ip 192.168.0.1)
ed0 -> interface da net (ip 200.206.217.169)
192.168.0.0/28 -> rede local (ips de 192.168.0.1 até 192.168.0.14) 
6667,7000,7001 … 21,22,25,80… -> portas em que foram permitidas conexões de entrada .. (servidores irc, ftp, ssh, smtp, http… etc..)
a regra 220 permite fazer um traceroute para fora do servidor (está inacabada) 

Funcionando e aprimorando sempre, desde 2001 🙂

Escreva seu comentário: