NOTA: Script a comprovar....
#!/bin/sh export ETH1=$1 export IP_GW1=$2 export ETH2=$3 export IP_GW2=$4 export GW1=$5 export GW2=$6 export ESTADO_1=0; export ESTADO_2=0; ip ro del scope global nexthop via 192.168.1.235 dev eth1 weight 1 nexthop via 192.168.2.236 dev eth2 weight 1; ip ro del scope global nexthop via $IP_GW1 dev $ETH1 weight 1; ip ro del scope global nexthop via $IP_GW2 dev $ETH2 weight 1; echo -n `date`": " >> /var/log/routed.log ; echo "START: Limpiada tabla de rutas inicial y variables de estado" >> /var/log/routed.log; while test 1 -eq 1;do #echo "Calculo SUCCESS 1"; export SUCCESS1=`ping -c 10 $IP_GW1 | grep icmp_seq | grep -v Unreachable | wc -l | awk ' { print $1 } '`; #echo "Fin calculo SUCCESS 1"; #echo "Calculo SUCCESS 2"; export SUCCESS2=`ping -c 10 $IP_GW2 | grep icmp_seq | grep -v Unreachable | wc -l | awk ' { print $1 } '`; #echo "Fin calculo SUCCESS 2"; if ( test $SUCCESS1 -eq 0 ) && ( test $ESTADO_1 -eq 1 ); then ip ro del scope global nexthop via 192.168.1.235 dev eth1 weight 1 nexthop via 192.168.2.236 dev eth2 weight 1; ip ro del scope global nexthop via $GW1 dev $ETH1 weight 1; ip ro append scope global nexthop via $GW2 dev $ETH2 weight 1; echo 1 > /proc/sys/net/ipv4/route/flush; echo -n `date`": " >> /var/log/routed.log ; echo "CAMBIO TOPOLOGICO: nodo1 caido" >> /var/log/routed.log; export ESTADO_1=0; fi; if ( test $SUCCESS2 -eq 0 ) && ( test $ESTADO_2 -eq 1 ) ; then ip ro del scope global nexthop via 192.168.1.235 dev eth1 weight 1 nexthop via 192.168.2.236 dev eth2 weight 1; ip ro del scope global nexthop via $GW2 dev $ETH2 weight 1; ip ro append scope global nexthop via $GW1 dev $ETH1 weight 1; echo 1 > /proc/sys/net/ipv4/route/flush; echo -n `date`": " >> /var/log/routed.log ; echo "CAMBIO TOPOLOGICO: nodo2 caido" >> /var/log/routed.log; export ESTADO_2=0; fi; if ( test $SUCCESS1 -ne 0 ) && ( test $SUCCESS2 -ne 0 ) && ( ( test $ESTADO_1 -eq 0 ) || ( test $ESTADO_2 -eq 0 ) ) ; then ip ro del scope global nexthop via $GW1 dev $ETH1 weight 1; ip ro del scope global nexthop via $GW2 dev $ETH2 weight 1; ip ro append scope global nexthop via 192.168.1.235 dev eth1 weight 1 nexthop via 192.168.2.236 dev eth2 weight 1; echo 1 > /proc/sys/net/ipv4/route/flush; echo -n `date`": " >> /var/log/routed.log ; echo "CAMBIO TOPOLOGICO nodo1 y nodo2 arriba" >> /var/log/routed.log; export ESTADO_1=1; export ESTADO_2=1; fi; if ( test $SUCCESS1 -ne 0 ) && ( test $SUCCESS2 -eq 0 ) && ( ( test $ESTADO_1 -eq 0 ) || ( test $ESTADO_2 -eq 1 ) ) ; then ip ro del scope global nexthop via 192.168.1.235 dev eth1 weight 1 nexthop via 192.168.2.236 dev eth2 weight 1; ip ro append scope global nexthop via $GW1 dev $ETH1 weight 1; ip ro del scope global nexthop via $GW2 dev $ETH2 weight 1; echo 1 > /proc/sys/net/ipv4/route/flush; echo -n `date`": " >> /var/log/routed.log ; echo "CAMBIO TOPOLOGICO: nodo1 arriba y nodo2 caido" >> /var/log/routed.log; export ESTADO_1=1; export ESTADO_2=0; fi; if ( test $SUCCESS1 -eq 0 ) && ( test $SUCCESS2 -ne 0 ) && ( ( test $ESTADO_1 -eq 1 ) || ( test $ESTADO_2 -eq 0 ) ); then ip ro del scope global nexthop via 192.168.1.235 dev eth1 weight 1 nexthop via 192.168.2.236 dev eth2 weight 1; ip ro append scope global nexthop via $GW2 dev $ETH2 weight 1; ip ro del scope global nexthop via $GW1 dev $ETH1 weight 1; echo 1 > /proc/sys/net/ipv4/route/flush; echo -n `date`": " >> /var/log/routed.log ; echo "CAMBIO TOPOLOGICO: nodo1 caido y nodo2 arriba"; export ESTADO_1=0; export ESTADO_2=1; fi; # echo "Iniciando SLEEP"; sleep 5; # echo "Reiniciando bucle tras SLEEP"; done
Utilitzant iptables + iproute:
Utilitzem iptables per marcar els paquets depenent de l'origen (o qualsevol altre criteri). Després és creant les taules de rutes per a cada marca amb iproute.
Un exemple podria ser el següent. Tenim dos routers adsl:
Cada un connectat a una xarxa diferent amb la seva corresponent interfície de xarxa:
Tenim dos xarxes més, cadascuna també amb la seva corresponent interfície:
El que volem fer és que les màquines de l'aula 1 es connectin a internet a través del router adsl1 i les màquines de l'aula 2 a través del router adsl2.
Afegim dues taules rt al fitxer /etc/iproute2/rt_tables:
$ sudo nano /etc/iproute2/rt_tables
I afegim al final:
201 adsl1 202 adsl2
El fitxer ha de quedar quelcom similar a:
# # reserved values # 255 local 254 main 253 default 0 unspec # # local # #1 inr.ruhep 201 adsl1 202 adsl2
Ara afegim les rutes per defecte als adsl amb iptables:
$ ip route add default via 192.168.11.1 dev eth11 table adsl1 $ ip route add default via 192.168.12.1 dev eth12 table adsl2
Ara es tracta de marcar els paquets abans d'enrutar amb una marca, segons l'origen del paquet
$ sudo iptables -A PREROUTING -i eth1 -s 192.168.1.0/24 -t mangle -m state --state NEW,ESTABLISHED,RELATED -j MARK --set-mark 1 $ sudo iptables -A PREROUTING -i eth2 -s 192.168.2.0/24 -t mangle -m state --state NEW,ESTABLISHED,RELATED -j MARK --set-mark 2
Ara només ens queda unir les marques amb les taules de rutes creades:
$ sudo ip rule add fwmark 1 table adsl1 $ sudo ip rule add fwmark 2 table adsl2
Borrem el cache de les taules i ja hauria de funcionar:
$ sudo ip route flush cache
NOTA:
Les taules afegides no són mostrades per la comanda route. Per consultar-les hem d'utilitzar:
$ sudo ip route list table all
o
$ sudo ip route list table adsl1
Recursos:
Podem crear un script a la carpeta /etc/network/if-up.d:
$ sudo nano /etc/network/if-up.d/forward_departament #!/bin/sh ip route add default via 192.168.0.10 dev eth0 table adsl1 iptables -A PREROUTING -i eth12 -s 192.168.12.0/24 -t mangle -m state --state NEW,ESTABLISHED,RELATED -j MARK --set-mark 1 ip rule add fwmark 1 table adsl1 ip route flush cache
$ sudo chmod +x /etc/network/if-up.d
I ara al reiniciar la targeta de xarxa s'hauria d'aplicar l'script.
#!/bin/bash #IPs of the def gateways IP1=192.168.1.1 IP2=192.168.2.1 REDLOCAL=10.1.1.0/24 IPLOCAL=10.1.1.1 # NICs IF1=eth0 IF2=eth1 ip rule add from $IP1 lookup T1 ip route add $REDLOCAL via $IPLOCAL table T1 ip route add 0/0 via $IP1 table T1 ip route add from $IP2 lookup T2 ip route add $REDLOCAL via $IPLOCAL table T2 ip route add 0/0 via $IP1 table T1 ip route default equalize nexthop via $IP1 dev $IF1 nexthop via $IP2 dev $IF2