Posts mit dem Label DD-WRT werden angezeigt. Alle Posts anzeigen
Posts mit dem Label DD-WRT werden angezeigt. Alle Posts anzeigen

2008-10-02

VPN-Tunnel mittels VPNC und DD-WRT

Auf der folgenden Seite aus dem offiziellen Wiki zur Open-Source-Firmware DD-WRT wird beschrieben, wie man auf einem mit DD-WRT v24 (oder höher) gepatchten Router einen VPN-Tunnel einrichten kann: http://www.dd-wrt.com/wiki/index.php/VPNC.
Der Clou: Mittels Ping auf zwei unterschiedliche Rechner innerhalb des VPNs wird festgestellt, ob die Verbindung noch aktiv ist und im Fehlerfall wird der Tunnel wieder hergestellt.
Da es ausserdem sinnvoll ist, nicht alles durch den Tunnel zu schicken, sondern nur bestimmte Netze, kann man genau das konfigurieren. Mal abgesehen davon: Die Bandbreite, die durch den Tunnel geht ist auf 200kb/s beschränkt.
Es hat etwas gedauert das zum Laufen zu bringen, weil die Anleitung nur die Bruchstücke hergibt. Darum nun das komplette Skript.
Die fett gedruckten Teile sind natürlich anzupassen und die kursiv gedruckten kann man anpassen, z.B. wenn die Verbindung sehr oft wegbricht, das Gateway ein anderes ist, ...
Das ganze als Startup-Skript über den Administration/Diagnose-Reiter der Web-Oberfläche des Routers einspielen und den Router neu starten.
mkdir /tmp/etc/vpnc
rm -f /tmp/etc/vpnc/vpnc-script
echo '
#!/bin/sh
# This is a wrapper for the vpnc-script overriding some variables needed
# for setting up split-tunneling
# this effectively disables changes to /etc/resolv.conf
INTERNAL_IP4_DNS=

# This sets up split networking regardless of the concentrators specifications.
# You can add as many routes as you want, but you must set the counter
# CISCO_SPLIT_INC accordingly. All requests to IP ranges NOT listed
# in the code below will NOT go though the VPN tunnel.

CISCO_SPLIT_INC=2
CISCO_SPLIT_INC_0_ADDR=192.0.0.0 #IP range to go into first tunnel
CISCO_SPLIT_INC_0_MASK=255.0.0.0 #Subnet Mask for first tunnel
CISCO_SPLIT_INC_0_MASKLEN=8 #Mask length
CISCO_SPLIT_INC_0_PROTOCOL=0
CISCO_SPLIT_INC_0_SPORT=0
CISCO_SPLIT_INC_0_DPORT=0
CISCO_SPLIT_INC_1_ADDR=172.0.0.0 #IP range to go into 2nd tunnel
CISCO_SPLIT_INC_1_MASK=255.0.0.0 #Subnet Mask for 2nd tunnel
CISCO_SPLIT_INC_1_MASKLEN=8 #Mask length
CISCO_SPLIT_INC_1_PROTOCOL=0
CISCO_SPLIT_INC_1_SPORT=0
CISCO_SPLIT_INC_1_DPORT=0

# run the original script
. /etc/vpnc/vpnc-script
' >> /tmp/etc/vpnc/vpnc-script
chmod 700 /tmp/etc/vpnc/vpnc-script
rm -f /tmp/etc/vpnc/vpnc.sh
echo '
#!/bin/sh
vpn_concentrator="xxx.xxx.xxx.xxx" ##enter ip or hostname of your Ipsec vpn concentrator
vpn_keepalive_host1="192.168.0.1" ##enter the ip or hostname of a computer that is only reachable if vpn connection is established.
vpn_keepalive_host2="192.168.1.1" ##enter the ip or hostname of a computer that is only reachable if vpn connection is established.
vpn_groupname="xxx" ##enter the group name here
vpn_grouppasswd="xxx" ##enter the group password here
vpn_username="xxx" ##enter your username here
vpn_password="xxx" ##enter your password here

#--do not edit this--
#Written by Alain R. (alainr [AT] gmx.de) 28.Sep.2007
vpnc-disconnect
rm -f /tmp/etc/vpnc/vpn.conf
echo "
IPSec gateway $vpn_concentrator
IPSec ID $vpn_groupname
IPSec secret $vpn_grouppasswd
Xauth username $vpn_username
Xauth password $vpn_password
Script /tmp/etc/vpnc/vpnc-script
" >> /tmp/etc/vpnc/vpn.conf

pingtest1 () {
ping -q -c1 $param1 >> /dev/null
if [ "$?" == "0" ]; then
echo 0 #reachable
else
echo 1 #not reachable
fi
}

pingtest2 () {
ping -q -c2 $param2 >> /dev/null
if [ "$?" == "0" ]; then
echo 0 #reachable
else
echo 1 #not reachable
fi
}

while [ true ]; do
param1=$vpn_concentrator;
if [ "`pingtest1`" == "0" ]; then #Vpn concentrator reachable
doloop=1;
while [ $doloop -gt 0 ]; do
param1=$vpn_keepalive_host1;
if [ "`pingtest1`" == "0" ]; then
sleep 300
else
param2=$vpn_keepalive_host2;
if [ "`pingtest2`" == "0" ]; then
sleep 300
else
doloop=0;
vpnc-disconnect
vpnc /tmp/etc/vpnc/vpn.conf --dpd-idle 0
sleep 1
if [ "`pingtest1`" != "0" ]; then
sleep 10
fi
tundev="`ifconfig |grep tun |cut -b 1-4`"
iptables -A FORWARD -o $tundev -j ACCEPT
iptables -A FORWARD -i $tundev -j ACCEPT
iptables -t nat -A POSTROUTING -o $tundev -j MASQUERADE
sleep 9
fi
fi
done
else
sleep 10;
fi
done

return 0;
' >> /tmp/etc/vpnc/vpnc.sh
chmod 700 /tmp/etc/vpnc/vpnc.sh
/tmp/etc/vpnc/vpnc.sh&