Creazione di una VPN Ipsec Lan2Lan con Debian Lenny

From RVM Wiki
Jump to navigation Jump to search

Si utilizza racoon, che differenza di openswan non crea un'interfaccia ipsec0, ma gestisce tutto a livello di kernel.

Installazione pacchetti

sudo apt-get install racoon ipsec-tools


"Please select the configuration mode for racoon IKE daemon."

                                       "direct"

Overview

La configurazione si artiola nei seguenti step:

  • file configurazione racoon: definisce gli estremi di connessione ed i parametri usati per la stessa
  • file di configurazione setkey: definisce le politiche di pseudo-routing per cui attivare la vpn
  • file delle psk: contiene le password di collegamento
  • file di startup (sostituibile con "script phase1_up / phase1_down" vedi man racoon.conf): setta il routing e lancia il file setkey
  • disabilitazione source nat interfaccia pubblica
  • sblocco regole firewall

Da notare che una volta che si lancia racoon, il tunnel non si attiva: è necessario pingare o generare traffico tra le due subnet per attivarlo.

Inoltre occorre disabilitare il natting in uscita sull'interfaccia pubblica verso la subnet di destinazione, altrimenti il tunnel non funziona.

Questo perchè il traffico verso la subnet remota è instradato tramite la eth1, che natta tutto il traffico con l'ip pubblico.

VPN Lan2Lan tra due host con eth1 con IP Pubblico

Schema:


Subnet_A
192.168.254.0/24
|
| eth0 192.168.254.254
Firewall_A
| eth1 xx.xx.xx.xx
|
|
Internet
|
|
| eth1 yy.yy.yy.yy
Firewall_B
| eth0 192.168.3.254
|
192.168.3.0/24
Subnet_B

Configurazione Firewall_A

Tutta la configurazione è contenuta in

cd /etc/racoon

Creazione configurazione racoon

sudoedit rvmgal.conf 
listen {
        isakmp xx.xx.xx.xx [500];
        # IP pubblico Firewall_A su cui bindare
}              

remote yy.yy.yy.yy {
       # IP Pubblico Firewall_B a cui collegarsi
        exchange_mode main;
        my_identifier address xx.xx.xx.xx;
        # identificatore per trovare la riga della password nel file psk remoto
        initial_contact off;
        proposal {
                encryption_algorithm 3des;
                hash_algorithm md5;
                authentication_method pre_shared_key;
                dh_group 2;
        }
}

sainfo address 192.168.254.0/24 any address 192.168.3.0/24 any {
        # plitiche di connessione Subnet_B-Subnet_A
        pfs_group 2;
        encryption_algorithm 3des;
        authentication_algorithm hmac_md5;
        compression_algorithm deflate;
        lifetime time 28800 sec;
}


File di configurazioen setkey

Questo file imposta le politiche di autorizzazione al traffico una volta che il tunnel è stato creato:

sudoedit setkey.conf 
#!/usr/sbin/setkey -f
#
# Flush SAD and SPD
flush;
spdflush;

# Create policies for racoon
spdadd 192.168.3.0/24 192.168.254.0/24 any -P in ipsec esp/tunnel/yy.yy.yy.yy-xx.xx.xx.xx/require;
spdadd 192.168.254.0/24 192.168.3.0/24 any -P out ipsec esp/tunnel/xx.xx.xx.xx-yy.yy.yy.yy/require;

File delle PSK

In questo file si mette la password che deve usare il peer remoto per collegarsi. La password viene matchata con l'identificatore usato nel file di cfg remoto:

sudoedit psk.txt 
...
yy.yy.yy.yy    lapassword
  • Impostare le permission ,altrimenti ci sono problemi:
sudo chmod 600 psk.txt

File di Startup

Questo file serve per far partire in foreground la VPN per debuggare il collegamento:

sudoedit startup 
#!/bin/bash
# Startup Script
setkey -f /etc/racoon/setkey.conf
route add -net 192.168.3.0/24 gw 192.168.254.254
racoon -d -F -f /etc/racoon/racoon.conf 
echo flush\; | /usr/sbin/setkey -c
echo spdflush\; | /usr/sbin/setkey -c
route del -net 192.168.3.0/24 gw 192.168.254.254

Disabilitazione del SNAT

Solitamente l'SNAT è composto dalla regola:

$IPT -t nat -A POSTROUTING \
        -o $WAN0_IF \
        -j SNAT --to-source $WAN0_IP

è possibile sostituirla con questa, che esclude le destinazioni di subnet private:

$IPT -t nat -A POSTROUTING \
        -o $WAN0_IF  -d ! 192.168.0.0/16 \
        -j SNAT --to-source $WAN0_IP

Sblocco regole firewall

  • TODO

Configurazione Firewall_B

Configurazione interfaccia dummy0

  • Se fosse necessario, si può creare un'interfaccia di rete fittizzia, ad esempio se si vuole nattare il traffico per farlo provenire da questo indirizzo ip:
sudoedit /etc/network/interfaces
auto dummy0
iface dummy0 inet static
# interfaccia dummy per nat con vpn consorzio triveneto
        address         192.168.3.254
        netmask         255.255.255.0
        post-up         route add -net 10.9.86.0/24 gw 192.168.3.25
        post-down       route del -net 10.9.86.0/24 gw 192.168.3.25

In questo modo si imposta automaticamente il routing per l'host di destinazione scelto.

Creazione configurazione racoon

cd /et/racoon
sudoedit galrvm.conf 
# racoon.conf
path pre_shared_key "/etc/racoon/psk.txt";
path certificate "/etc/racoon/certs";

listen {
        isakmp yy.yy.yy.yy [500];
}

remote xx.xx.xx.xx {
        exchange_mode main;
        #exchange_mode aggessive,main;
        my_identifier address yy.yy.yy.yy;
        initial_contact off;
        #nat_traversal on;
        proposal {
                encryption_algorithm 3des;
                hash_algorithm md5;
                authentication_method pre_shared_key;
                dh_group 2;
        }
}

sainfo address 192.168.3.0/24 any address 192.168.254.0/24 any {
        pfs_group 2;
        encryption_algorithm 3des;
        authentication_algorithm hmac_md5;
        compression_algorithm deflate;
        lifetime time 28800 sec;
} 

File di configurazioen setkey

sudoedit setkey.rvm 
#!/usr/sbin/setkey -f
#
# Flush SAD and SPD
flush;
spdflush;

# Create policies for racoon
spdadd 192.168.254.0/24 192.168.3.0/24 any -P in ipsec esp/tunnel/xx.xx.xx.xx-yy.yy.yy.yy/require;
spdadd 192.168.3.0/24 192.168.254.0/24 any -P out ipsec esp/tunnel/yy.yy.yy.yy-xx.xx.xx.xx/require;

File delle PSK

sudoedit psk.txt 
...
81.72.122.31    lapassword

File di Startup

sudoedit startup.rvm 
#!/bin/bash
# Startup Script
setkey -f /etc/racoon/setkey.rvm
route add -net 192.168.254.0/24 gw 192.168.3.254
racoon -d -F -f /etc/racoon/galrvm.conf 
echo flush\; | /usr/sbin/setkey -c
echo spdflush\; | /usr/sbin/setkey -c
route del -net 192.168.254.0/24 gw 192.168.3.254

Disabilitazione del SNAT

  • Idem

Sblocco regole firewall

  • Idem

Avvio del tunnel

  • RICORDARE CHE IL TUNNEL VIENE SOLO PREDISPOSTO, E VIENE ATTIVATO SOLO QUANDO SI GENERA DEL TRAFFICO !!!
  • Dal Firewall_A
cd /etc/racoon
sudo ./startup 
Foreground mode.
2009-12-04 19:04:57: INFO: @(#)ipsec-tools 0.7.1 (http://ipsec-tools.sourceforge.net)
2009-12-04 19:04:57: INFO: @(#)This product linked OpenSSL 0.9.8g 19 Oct 2007 (http://www.openssl.org/)
2009-12-04 19:04:57: INFO: Reading configuration from "/etc/racoon/racoon.conf"
2009-12-04 19:04:57: DEBUG: call pfkey_send_register for AH
2009-12-04 19:04:57: DEBUG: call pfkey_send_register for ESP
2009-12-04 19:04:57: DEBUG: call pfkey_send_register for IPCOMP
2009-12-04 19:04:57: INFO: Resize address pool from 0 to 255
2009-12-04 19:04:57: DEBUG: reading config file /etc/racoon/racoon.conf
2009-12-04 19:04:57: DEBUG: filename: /etc/racoon/rvmgal.conf
2009-12-04 19:04:57: DEBUG: reading config file /etc/racoon/rvmgal.conf
2009-12-04 19:04:57: DEBUG: compression algorithm can not be checked because sadb message doesn't support it.
2009-12-04 19:04:57: DEBUG: getsainfo params: loc='192.168.254.0/24', rmt='192.168.3.0/24', peer='NULL', id=0
2009-12-04 19:04:57: DEBUG: getsainfo pass #2
2009-12-04 19:04:57: DEBUG: open /var/run/racoon/racoon.sock as racoon management.
2009-12-04 19:04:57: INFO: xx.xx.xx.xx[500] used as isakmp port (fd=6)
2009-12-04 19:04:57: INFO: xx.xx.xx.xx[500] used for NAT-T
2009-12-04 19:04:57: DEBUG: pk_recv: retry[0] recv() 
2009-12-04 19:04:57: DEBUG: get pfkey X_SPDDUMP message
2009-12-04 19:04:57: DEBUG: pk_recv: retry[0] recv() 
2009-12-04 19:04:57: DEBUG: get pfkey X_SPDDUMP message
2009-12-04 19:04:57: DEBUG: sub:0xbf93d138: 192.168.3.0/24[0] 192.168.254.0/24[0] proto=any dir=fwd
2009-12-04 19:04:57: DEBUG: db :0x8591880: 192.168.3.0/24[0] 192.168.254.0/24[0] proto=any dir=in
2009-12-04 19:04:57: DEBUG: pk_recv: retry[0] recv() 
2009-12-04 19:04:57: DEBUG: get pfkey X_SPDDUMP message
2009-12-04 19:04:57: DEBUG: sub:0xbf93d138: 192.168.254.0/24[0] 192.168.3.0/24[0] proto=any dir=out
2009-12-04 19:04:57: DEBUG: db :0x8591880: 192.168.3.0/24[0] 192.168.254.0/24[0] proto=any dir=in
2009-12-04 19:04:57: DEBUG: sub:0xbf93d138: 192.168.254.0/24[0] 192.168.3.0/24[0] proto=any dir=out
2009-12-04 19:04:57: DEBUG: db :0x8591ac8: 192.168.3.0/24[0] 192.168.254.0/24[0] proto=any dir=fwd
  • Idem sul Firewall_B
  • Pingare ora dal Firewall_A selezionando come sorgente l'interfaccia interna, o meglio dalla Lan_A:
ping -I eth0 192.168.3.254

...
2009-12-04 19:24:52: DEBUG: pfkey UPDATE succeeded: ESP/Tunnel 83.211.75.50[0]->81.72.122.31[0] spi=142357427(0x87c33b3)
2009-12-04 19:24:52: INFO: IPsec-SA established: ESP/Tunnel 83.211.75.50[0]->81.72.122.31[0] spi=142357427(0x87c33b3)
2009-12-04 19:24:52: DEBUG: ===
2009-12-04 19:24:52: DEBUG: pk_recv: retry[0] recv()
2009-12-04 19:24:52: DEBUG: get pfkey ADD message
2009-12-04 19:24:52: INFO: IPsec-SA established: ESP/Tunnel 81.72.122.31[500]->83.211.75.50[500] spi=137500065(0x83215a1)
2009-12-04 19:24:52: DEBUG: ===

Controllo del tunnel

  • Lanciare startup, e controllare che il setkey abbia messo le regole:
sudo setkey -D
No SAD entries.
sudo setkey -D -P
192.168.3.0/24[any] 192.168.254.0/24[any] any
        in ipsec
        esp/tunnel/yy.yy.yy.yy-xx.xx.xx.xx/require
        created: Dec  4 19:37:05 2009  lastused:                     
        lifetime: 0(s) validtime: 0(s)
        spid=1512 seq=1 pid=2632
        refcnt=1
192.168.3.0/24[any] 192.168.254.0/24[any] any
        fwd ipsec
        esp/tunnel/yy.yy.yy.yy-xx.xx.xx.xx/require
        created: Dec  4 19:37:05 2009  lastused:                     
        lifetime: 0(s) validtime: 0(s)
        spid=1522 seq=2 pid=2632
        refcnt=1
192.168.254.0/24[any] 192.168.3.0/24[any] any
        out ipsec
        esp/tunnel/xx.xx.xx.xx-yy.yy.yy.yy/require
        created: Dec  4 19:37:05 2009  lastused:                     
        lifetime: 0(s) validtime: 0(s)
        spid=1529 seq=0 pid=2632
        refcnt=1
  • Una volta attivato il tunnel, lo si può verificare con:
sudo setkey -D
yy.yy.yy.yy xx.xx.xx.xx 
        esp mode=tunnel spi=33087045(0x01f8de45) reqid=0(0x00000000)
        E: 3des-cbc  5331ab5c fd398df6 61eefef7 911f8a48 70e951d1 b8b322a8
        A: hmac-md5  ba6f7322 f4b5abd5 043ef7c3 1820a510
        seq=0x00000000 replay=4 flags=0x00000000 state=mature 
        created: Dec  4 19:41:17 2009   current: Dec  4 19:41:22 2009
        diff: 5(s)      hard: 28800(s)  soft: 23040(s)
        last: Dec  4 19:41:18 2009      hard: 0(s)      soft: 0(s)
        current: 420(bytes)     hard: 0(bytes)  soft: 0(bytes)
        allocated: 5    hard: 0 soft: 0
        sadb_seq=1 pid=2896 refcnt=0
xx.xx.xx.xx yy.yy.yy.yy 
        esp mode=tunnel spi=173685099(0x0a5a396b) reqid=0(0x00000000)
        E: 3des-cbc  caae5f4d 88ea3646 b4927c2c 26fd3163 c35b2753 a1c1b84b
        A: hmac-md5  f63d761d 50f9e552 a798e46d 931de1d0
        seq=0x00000000 replay=4 flags=0x00000000 state=mature 
        created: Dec  4 19:41:17 2009   current: Dec  4 19:41:22 2009
        diff: 5(s)      hard: 28800(s)  soft: 23040(s)
        last: Dec  4 19:41:18 2009      hard: 0(s)      soft: 0(s)
        current: 420(bytes)     hard: 0(bytes)  soft: 0(bytes)
        allocated: 5    hard: 0 soft: 0
        sadb_seq=0 pid=2896 refcnt=0

Gestione della connessione con gli script init.d

Per automatizzare la cosa, vengono usati due script initd, uno per le Security Policy e l'altro per Racoon.

Inizializzazione delle Security Policy

  • Editare il file di configurazione
sudoedit /etc/ipsec-tools.conf 
# Flush SAD and SPD
flush;
spdflush;

# Create policies for racoon
spdadd 192.168.254.0/24 192.168.3.0/24 any -P in ipsec esp/tunnel/xx.xx.xx.xx-yy.yy.yy.yy/require;
spdadd 192.168.3.0/24 192.168.254.0/24 any -P out ipsec esp/tunnel/yy.yy.yy.yy-xx.xx.xx.xx/require;
  • Impostare l'avvio automatico:
sudo update-rc.d -f setkey remove
sudo update-rc.d setkey start 40 S . stop 89 1 . start 34 0 6 .
 Adding system startup for /etc/init.d/setkey ...
   /etc/rc1.d/K89setkey -> ../init.d/setkey
   /etc/rc0.d/S34setkey -> ../init.d/setkey
   /etc/rc6.d/S34setkey -> ../init.d/setkey
   /etc/rcS.d/S40setkey -> ../init.d/setkey

Verifica /etc/default/setkey

  • Impostare le policy
sudo invoke-rc.d setkey restart
Reloading IPsec SA/SP database: done.

Gestione Racoon con script

  • Impostare il livello di debug:
sudoedit /etc/default/racoon 
...
CONFIG_MODE="direct"
# Arguments to pass to racoon (ignored when config mode is racoon-tool)
RACOON_ARGS="-d -d"
...
  • Editare il file di configurazione di racoon, che contenga solo:
sudoedit /etc/racoon/racoon.conf 
include "/etc/racoon/galrvm.conf";
  • Impostare gli startup link per lo script:
sudo update-rc.d -f racoon remove
sudo update-rc.d racoon start 40 S . stop 89 1 . start 34 0 6 .
Adding system startup for /etc/init.d/racoon ...
  /etc/rc1.d/K89racoon -> ../init.d/racoon
  /etc/rc0.d/S34racoon -> ../init.d/racoon
  /etc/rc6.d/S34racoon -> ../init.d/racoon
  /etc/rcS.d/S40racoon -> ../init.d/racoon
  • Avviare la vpn
sudo invoke-rc.d racoon restart

Riferimenti