Traffic splitting su due linee

From RVM Wiki
Revision as of 16:38, 29 April 2008 by Gabriele.vivinetto (talk | contribs) (New page: Si ha un firewall collegato a due router su due linee diverse. Si vogliono utilizzare le due linee in ingresso ed in uscita, selezionando il tipo di traffico da instradare su una linea o ...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Si ha un firewall collegato a due router su due linee diverse.

Si vogliono utilizzare le due linee in ingresso ed in uscita, selezionando il tipo di traffico da instradare su una linea o sull'altra.

Schema:


                                                                 ________
                                          +----------------+    /
                                          |                |   |
                            +-------------+ Router 1 ADSL  +---|
        __                  |             | (192.168.1.254)|  /
    ___/  \_         +------+---------+   +----------------+ |
  _/        \__      |     eth1       |                      /
 /             \     | 192.168.1.253  |                     /
| 192.168.0.0/24-----+ Linux router   |                     |     Internet
 \_           __/    |                |                     |
   \__     __/       |    eth1:1      |                     |
      |    |         | 192.168.11.253 |                     |
      \___/          +------+---------+   +----------------+ \  
                            |             |                |  \ 
                            +-------------+ Provider 2 VDSL+---\
                                          | 192.168.11.254 |    \   
                                          +----------------+     \___________



Le interfacce ethernet possono essere fisicamente distinte, oppure fisicamente una sola, con due indirizzi ip (eth1 e eth1:1 vedi man interfaces)

I router fanno nat per conto loro.

Scopi:

  • Default route tramite Router 2 via VDSL
  • Solo traffico SMTP attraverso Router 1 via ADSL
  • Consentire il traffico in entrata da entrambi i router

Installazione pacchetti e verifica configurazione di base

sudo apt-get install iproute

Disabilitare l'antispoof (potrebbe dare problemi in caso si usasse il nat fatto da iptables)

sudoedit /etc/sysctl.conf

...
net.ipv4.conf.default.rp_filter=0
net.ipv4.conf.all.rp_filter=0
...
sudo sysctl -p

Verificare

cat /proc/sys/net/ipv4/conf/all/rp_filter 
0

Verificare lo stato dell'ip_forward:

cat /proc/sys/net/ipv4/ip_forward         
1

Creare le due Tabelle di routing aggiungendo alla fine:

sudoedit /etc/iproute2/rt_tables
...
1       VDSL
2       ADSL

Assicurarsi che la default route settata in /etc/network/interfaces sia UNICA e corrisponda a Router_2

route -n
...
0.0.0.0         192.168.11.254  0.0.0.0         UG    0      0        0 eth1

Configurazione del routing in ingresso

Bisogna fare in modo che i pacchetti che entrano attraverso un'interfaccia, vengano rispediti indietro attraverso la stessa interfaccia.

Questo viene fatto creando due tabelle di routing diverse.

Nel caso la connessione arrivi tramite Router_1 si userà la tabella di routing chiamata ADSL:

  • si utilizzerà come ip sorgente di uscita il .1.253
ip route add 0.0.0.0 dev eth1 src 192.168.1.253  table ADSL
  • si utilizzerà come gateway il .1.254.
ip route add default via 192.168.1.254  table ADSL

Nel caso la connessione arrivi tramite Router_2 si userà la tabella di routing chiamata VDSL:

  • si utilizzerà come ip sorgente di uscita il .11.253
ip route add 0.0.0.0 dev eth1 src 192.168.11.253  table VDSL
  • si utilizzerà come gateway il .11.254.
ip route add default via 192.168.11.254  table VDSL

(nel caso di una sola interfaccia fisica ed alias, si può utilizzare sempre eth1. In caso di interfacce fisiche diverse, usare i relativi nomi.)

Ora si deve decidere in quali casi utilizzare le due tabelle di routing.

Se la connessione arriva dall'ip .1.253 (cioè dal router ADSL), usa la tabella ADSL:

ip rule add from 192.168.1.253  table ADSL

Se la connessione arriva dall'ip .11.253 (cioè dal router VDSL), usa la tabella VDSL:

ip rule add from 192.168.11.253  table VDSL

In questo modo, ci si potrà, ad esempio, connettere in ssh da fuori su entrambe le linee.

Instradamento di particolare traffico attraverso l'altra linea

Tutto il traffico in uscita passa normalmente dalla linea VDSL.

Tutto il traffico forwardati in uscita passa normalmente dalla linea VDSL.

Possiamo far USCIRE ad esempio il solo traffico SMTP FORWARDATO tramite la linea ADSL.

Per far questo dobbiamo prima marcare i pacchetti smtp da forwardare tramite iptables:

iptables -t mangle -A PREROUTING \
         -p tcp \
         -i eth0 \
         --dport \
         -j MARK \
         --set-mark 1

Una volta marcato il traffico, dobbiamo solo inserire uan regola che selezioni questi pacchetti per applicargli la tabella di routing ADSL:

ip rule add fwmark 1 table ADSL

Gestione con script iptables-init.d

  • Creare il file /etc/iptables-initd/routes:
#!/bin/bash
if [ "$1" = "on" ]
then
        echo "Setting up multiple routes ..."
        # Be sure to disable rp_filter in /etc/sysctl.conf
        # net.ipv4.conf.default.rp_filter=0
        # net.ipv4.conf.all.rp_filter=0

        # --------------------------------------------------------------------- 
        # Mark packets for forwarded smtp connections to be routed via ADSL
        # Exempt VPN traffic from marking
        # In any case, there is no smtp traffic to crosrv02, because postfix
        # is forced to forward mail for men users via transport_map to the 
        # public ip fire.mendrisio.croalliance.com
        # We need smtp to crosrv02 via VPN for notebook users
        # ------------------------------ 

        #$IPTABLES -t mangle -A PREROUTING \
        #          -p tcp \
        #          -i $LAN0_IF --dport 25 -d ! 192.168.10.0/24 \
        #          -j LOG \
        #         --log-level debug \
        #         --log-prefix "Routed via ADSL: "

       $IPTABLES -t mangle -A PREROUTING \
                  -p tcp \
                  -i $LAN0_IF --dport 25 -d ! 192.168.10.0/24 \
                  -j MARK --set-mark 1

        # This does not work, because local processes bind always to the
        # default interface. Try to use the "bind" option in the software
        # you use.
        #$IPTABLES -t mangle -A OUTPUT \
        #          -p tcp \
        #          --dport 25 \
        #          -j MARK --set-mark 1

        # Be sure that ADSL and VDSL tables are inside /etc/iproute2/rt_tables

        # Build the VDSL routing table
        # When using this table, use .11.253 as source address
        ip route add 0.0.0.0 dev eth1 src 192.168.11.253 table VDSL
        # When using this table, your default gw is .11.254
        ip route add default via 192.168.11.254 table VDSL

        # Build the ADSL routing table
        # When using this table, use .1.253 as source address
        ip route add 0.0.0.0 dev eth1 src 192.168.1.253  table ADSL
        # When using this table, your default gw is .1.254
        ip route add default via 192.168.1.254  table ADSL

        # This is the unconditional default route, that has already been set
        # in /etc/network/interfaces
        #ip route add 0.0.0.0 dev eth1 src 192.168.11.253
        #ip route add default via 192.168.11.254

        # Now create the rules for selecting the roting tables to use
        # If the packet is marked "1" use the ADSL routing table
        ip rule add fwmark 1            table ADSL
        # If the packet come from .11.253 use the VDSL routing table
        ip rule add from 192.168.11.253 table VDSL
        # If the packet come from .1.253 use the ADSL routing table
        ip rule add from 192.168.1.253  table ADSL
        # For other packets, use the default route

        echo "Setting up multiple routes done."

elif  [ "$1" = "off" ]
then
        echo "Deleting multiple routes..."
        ip route del 0.0.0.0 dev eth1 src 192.168.11.253 table VDSL
        ip route del default via 192.168.11.254 table VDSL

        ip route del 0.0.0.0 dev eth1 src 192.168.1.253  table ADSL
        ip route del default via 192.168.1.254  table ADSL

        #ip route del 0.0.0.0 dev eth1 src 192.168.1.253
        #ip route del default via 192.168.1.1

        ip rule del fwmark 1            table ADSL
        ip rule del from 192.168.11.253 table VDSL
        ip rule del from 192.168.1.253  table ADSL

        # --------------------------------------------------------------------- 
        # Unmark packets for outgoing mail 
        # ------------------------------ 

        # this is done in stop.firewall

        #$IPTABLES -t mangle -D PREROUTING \
        #          -p tcp \
        #          -i $LAN0_IF --dport 25 \
        #          -j MARK --set-mark 1

        echo "Deleting multiple routes done."

else
        echo "Usage: $(basename) [on|off]"
        exit 1
fi

Inserire in start.firewall, dopo il setup delle default policies:

if [ -e /etc/iptables-initd/routes ]
then
     source /etc/iptables-initd/routes on
fi

Inserire in stop.firewall, alla fine:

if [ -e /etc/iptables-initd/routes ]
then
     source /etc/iptables-initd/routes off
fi

Assicurarsi che in stop.firewall siano definiti:

LAN0_IF="eth0"
WAN0_IF="eth1"

Todo

Trovare un modo per gestire anche il traffico generato in locale dal firewall stesso.

Riferimenti