FORUM DARKERS

Tecnologia & Informática => Sistemas Operacionais => Linux => Topic started by: insanity on 06 de October , 2006, 02:03:05 PM

Title: Roteamento avanÇado com iproute2 e iptables no slackware
Post by: insanity on 06 de October , 2006, 02:03:05 PM
Roteamento avanÇado com iproute2 e iptables no slackware
 ========================================================

Colaboração: Fabricio Guzzy

Segue aqui, script totalmente funcional para roteamento avançado, fazendo
balanceamento de LINK, usando iproute e iptables.

Uso este script, modificado por mim há uns meses, e confesso que nunca tive
problemas... Excelente alternativa para quem tem 2 ou mais Links no mesmo
Firewall e deseja dividir o uso, filtrando por ORIGEM, PROTOCOLO, PORTA e etc.


 ###############################################################################
 # Script por: by Fabricio Guzzy - MCP* - LINUX - Security Consulting
 # Comentarios + Detalhes para Roteamento avançado
 # Usando 2 Links  -->  Intelig (PPP LP Dedicada) e  Speedy (ADSL BUSINESS)

 # Gerando variáveis para as interfaces eth0, eth1 eth2 e etc...
 # Interfaces da Intranet/LAN/DMZ
 IF_LAN='eth1'
 IF_DMZ='eth3'

 # Interfaces ADSL--> Speedy e Intelig
 IF_ADSL1='eth0'
 IF_ADSL2='eth2'


 # Gateways dos ADSL (IPs dos roteadores) Troque pelos seus IP´s de GATEWAY
 GW_ADSL1='201.X.X.X'
 GW_ADSL2='200.X.X.X'


 # Mascarar saídas para os dois ADSL / Caso não queira fazer NAT, troque o MASQUERADE por uma regra de ACCEPT...
 iptables -t nat -A POSTROUTING -o $IF_ADSL1 -j MASQUERADE
 iptables -t nat -A POSTROUTING -o $IF_ADSL2 -j MASQUERADE


 # Marca  os pacotes "PREOROUTING e OUTPUT"  portas 80 (HTTP) 443 (HTTPS) 25 (SMTP) e 110 (POP)
 iptables -t mangle -A PREROUTING -i $IF_LAN -p tcp --dport 80 -j MARK --set-mark 2
 iptables -t mangle -A PREROUTING -i $IF_LAN -p tcp --dport 443 -j MARK --set-mark 2
 iptables -t mangle -A PREROUTING -i $IF_LAN -p tcp --dport 21 -j MARK --set-mark 2
 iptables -t mangle -A PREROUTING -i $IF_LAN -p tcp --dport 25 -j MARK --set-mark 3
 iptables -t mangle -A PREROUTING -i $IF_LAN -p tcp --dport 110 -j MARK --set-mark 3


 iptables -t mangle -A OUTPUT -p tcp --dport 80 -j MARK --set-mark 2
 iptables -t mangle -A OUTPUT -p tcp --dport 443 -j MARK --set-mark 2
 iptables -t mangle -A OUTPUT -p tcp --dport 21 -j MARK --set-mark 2
 iptables -t mangle -A OUTPUT -p tcp --dport 25 -j MARK --set-mark 3
 iptables -t mangle -A OUTPUT -p tcp --dport 110 -j MARK --set-mark 3


 # (marcados com 3-> table 21 = ADSL2) (marcados com 2-> table 20 = ADSL1)
 ip rule add fwmark 2 table 20 prio 20
 ip rule add fwmark 3 table 21 prio 20


 ## Os Comandos abaixo, roteiam pacotes que vem do HOST 192.168.0.9 para sair pelo ADSL2 (table 20)
 # e pacotes que vem da DMZ (rede 201.70.8.X) a sair pelo ADSL1 (table 21)- Descomentar caso for usar...

 # ip rule add from 192.168.0.9 table 20
 # ip rule add from 201.X.X.X/28 table 21

 # Aqui são geradas as rotas padrões para o ADSL1 e ADSL2, ou seja, cada ADSL tem seu DEFAULT GATEWAY...
 ip route add default via $GW_ADSL2 dev $IF_ADSL2 table 20
 ip route add default via $GW_ADSL1 dev $IF_ADSL1 table 21


 # LIMPA tabela de roteamento CACHE
 ip route flush cache
 # ip route flush table cache
 ######################################  Fabricio Guzzy  2006