[LUG.ro Mix] Dual WAN
WebMind: Adrián Rivelli
desarrollo en webmind.com.ar
Mie Mayo 20 16:11:10 ART 2009
Hola Amigos:
Finalmente logré mi cometido y pude instalarle a un linksys wrt54gl dos
conexiones de internet simultáneamente y todo funciona de maravilla.
El script de iptables se ocupa del balanceo de carga y hace que una petición
salga por un proveedor y la siguiente petición salga por el otro, es decir,
una vez a cada uno.
Les cuento todo esto porque necesitaría reemplazar esta forma de balanceo por
otra arbitraria. Lo que quiero es que el puerto 80, 443, 25 y 110 salgan por
un proveedor y el resto por el otro (sin balanceo... siempre así). El problema
es que no entiendo bien de iptables... ni siquiera se que buscar en google...
iptables es muy extenso.
Copio el script para quien quiera ayudarme:
#!/bin/sh
ip rule flush
ip rule add lookup main prio 32766
ip rule add lookup default prio 32767
ip rule add from $(nvram get wan_ipaddr) table 100 prio 100
ip rule add fwmark 0x100 table 100 prio 101
ip rule add from $(nvram get wan2_ipaddr) table 200 prio 200
ip rule add fwmark 0x200 table 200 prio 201
ip route flush table 100
ip route flush table 200
for TABLE in 100 200
do
ip route | grep link | while read ROUTE
do
ip route add table $TABLE to $ROUTE
done
done
ip route add table 100 default via $(nvram get wan_gateway)
ip route add table 200 default via $(nvram get wan2_gateway)
ip route delete default
ip route add default scope global equalize nexthop via $(nvram get
wan_gateway) dev $(nvram get wan_ifname) nexthop via $(nvram get wan2_gateway)
dev $(nvram get wan2_ifname)
insmod ipt_CONNMARK
IPTABLES="/usr/sbin/iptables"
#DD-WRT firewall rules #BEGIN
iptables -A PREROUTING -t nat -p icmp -d $(nvram get wan2_ipaddr) -j DNAT --to
$(nvram get lan_ipaddr)
iptables -A PREROUTING -t nat --dest $(nvram get wan2_ipaddr) -j TRIGGER --
trigger-type dnat
iptables -A FORWARD -i $(nvram get wan2_ifname) -o $(nvram get lan_ifname) -j
TRIGGER --trigger-type in
#DD-WRT END
$IPTABLES -F POSTROUTING -t nat
$IPTABLES -t mangle -N ETH1
$IPTABLES -t mangle -F ETH1
$IPTABLES -t mangle -A ETH1 -j MARK --set-mark 0x100
$IPTABLES -t mangle -N ETH2
$IPTABLES -t mangle -F ETH2
$IPTABLES -t mangle -A ETH2 -j MARK --set-mark 0x200
$IPTABLES -t nat -N SPOOF_ETH1
$IPTABLES -t nat -F SPOOF_ETH1
$IPTABLES -t nat -A SPOOF_ETH1 -j LOG --log-prefix " SPOOF_ETH1 "
$IPTABLES -t nat -A SPOOF_ETH1 -j SNAT --to $(nvram get wan_ipaddr)
$IPTABLES -t nat -N SPOOF_ETH2
$IPTABLES -t nat -F SPOOF_ETH2
$IPTABLES -t nat -A SPOOF_ETH2 -j LOG --log-prefix " SPOOF_ETH2 "
$IPTABLES -t nat -A SPOOF_ETH2 -j SNAT --to $(nvram get wan2_ipaddr)
$IPTABLES -A INPUT -p icmp -s 192.168.1.0/24 -d 192.168.1.1 -j ACCEPT
#Save the gateway in the connection mark for new incoming connections
$IPTABLES -t mangle -A PREROUTING -i $(nvram get wan_ifname) -m conntrack --
ctstate NEW -j CONNMARK --set-mark 0x100
$IPTABLES -t mangle -A PREROUTING -i $(nvram get wan2_ifname) -m conntrack --
ctstate NEW -j CONNMARK --set-mark 0x200
$IPTABLES -A POSTROUTING -t mangle -o $(nvram get wan_ifname) -j MARK --set-
mark 0x100
$IPTABLES -A POSTROUTING -t mangle -o $(nvram get wan2_ifname) -j MARK --set-
mark 0x200
$IPTABLES -t nat -A POSTROUTING -o $(nvram get wan_ifname) -j SPOOF_ETH1
$IPTABLES -t nat -A POSTROUTING -o $(nvram get wan2_ifname) -j SPOOF_ETH2
$IPTABLES -t mangle -A POSTROUTING -j CONNMARK --save-mark
$IPTABLES -t mangle -A PREROUTING -i br0 -m state --state RELATED,ESTABLISHED
-j CONNMARK --restore-mark
# Use the correct gateway for reply packets from local connections
$IPTABLES -t mangle -A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j
CONNMARK --restore-mark
#Acá seteo arbitrariamente que los puertos en cuestión salgan siempre por una
conexión
iptables -t nat -I POSTROUTING -p tcp -m multiport --destination-ports 80 -j
SPOOF_ETH2
iptables -t mangle -I PREROUTING -p tcp -m multiport --destination-ports 80 -
j ETH2
iptables -t mangle -I OUTPUT -p tcp -m multiport --destination-ports 80 -j
ETH2
iptables -t nat -I POSTROUTING -p tcp -m multiport --destination-ports 443 -j
SPOOF_ETH2
iptables -t mangle -I PREROUTING -p tcp -m multiport --destination-ports 443
-j ETH2
iptables -t mangle -I OUTPUT -p tcp -m multiport --destination-ports 443 -j
ETH2
iptables -t nat -I POSTROUTING -p tcp -m multiport --destination-ports 25 -j
SPOOF_ETH2
iptables -t mangle -I PREROUTING -p tcp -m multiport --destination-ports 25 -
j ETH2
iptables -t mangle -I OUTPUT -p tcp -m multiport --destination-ports 25 -j
ETH2
iptables -t nat -I POSTROUTING -p tcp -m multiport --destination-ports 110 -j
SPOOF_ETH2
iptables -t mangle -I PREROUTING -p tcp -m multiport --destination-ports 110
-j ETH2
iptables -t mangle -I OUTPUT -p tcp -m multiport --destination-ports 110 -j
ETH2
RP_PATH=/proc/sys/net/ipv4/conf
for IFACE in `ls $RP_PATH`; do
echo 0 > $RP_PATH/$IFACE/rp_filter
done
Por último, estos aparatitos no tienen mucha capacidad de memoria, de hecho
tuve que pulir mucho la configuración de mi sistema ya que no puede haber más
de 36 kbytes en archivos de configuración, actualmente tengo menos de 2 kbytes
libres.
Saludos y Gracias
Adrián
------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <http://www.lugro.org.ar/pipermail/lugro-mix/attachments/20090520/c6cfa4ea/attachment.htm>
Más información sobre la lista de distribución Lugro-mix