3 # tinc tincd VPN setup script
5 # chkconfig: 2345 15 85
7 # author: Lubomir Bulej <pallas@kadan.cz>
8 # Modified for RPM by Mads Kiilerich <mads@kiilerich.com>
11 # description: this script takes care of starting and setting up of VPNs \
12 # provided by tincd daemon. It parses the configuration files \
13 # in all VPN directories and sets up the interfaces and routing
17 # Source function library.
18 . /etc/rc.d/init.d/functions
20 # Source networking configuration.
21 . /etc/sysconfig/network
23 # Check that networking is up.
24 [ ${NETWORKING} = "no" ] && exit 0
26 #############################################################################
34 # Check if ip-route is installed
35 if [ ! -f /sbin/ip ]; then
36 echo "**tinc: ip-route utilities not installed!"
41 ##############################################################################
42 # vpn_load () Loads VPN configuration
47 CFG="$TCONF/$1/tinc.conf"
48 [ -f $CFG ] || { echo "Error: $CFG does not exist" >&2 ; return 1 }
51 DEV=`grep -i -e '^TapDevice' $CFG | sed 's/[[:space:]]//g;s/^.*=//g'`
52 VPN=`grep -i -e '^(MyOwnVPNIP|MyVirtualIP)' -E $CFG | head -1 | sed 's/[[:space:]]//g;s/^.*=//g'`
54 # discourage empty and multiple entries
56 { echo "Error: TapDevice needed" >&2 ; return 2 }
57 echo $DEV | grep -q '^/dev/tap' ||
58 { echo "Error: TapDevice needs /dev/tapX" >&2 ; return 2 }
59 [ `echo $DEV | wc -l` -gt 1 ] && \
60 { echo "Error in TapDevice" >&2 ; return 3 }
62 { echo "Error: MyOwnVPNIP/MyVirtualIP needed" >&2 ; return 2 }
63 [ `echo $VPN | wc -l` -gt 1 ] && \
64 { echo "Error in MyOwnVPNIP/MyVirtualIP" >&2 ; return 3 }
65 echo $VPN | grep -q -x \
66 '\([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\{1,3\}/[[:digit:]]\{1,2\}' ||
67 { echo "Error in MyOwnVPNIP/MyVirtualIP address $VPN" ;
71 TAP=`echo $DEV | cut -d"/" -f3`
72 NUM=`echo $TAP | sed 's/tap//'`
74 # IP address, netmask length
75 ADR=`echo $VPN | cut -d"/" -f1`
76 LEN=`echo $VPN | cut -d"/" -f2`
78 # Expand bitlength to netmask
80 for cnt in 1 1 1 0; do
81 if [ $len -ge 8 ]; then
87 MSK="$MSK$((255 & (255 << (8 - msk))))"
88 [ $cnt -ne 0 ] && MSK="$MSK."
93 BRD=`ipcalc --broadcast $ADR $MSK | cut -d"=" -f2`
94 NET=`ipcalc --network $ADR $MSK | cut -d"=" -f2`
97 MAC=`printf "fe:fd:%0.2x:%0.2x:%0.2x:%0.2x" $(echo $ADR | sed 's/\./ /g')`
98 # echo "TAP $TAP NUM $NUM ADR $ADR LEN $LEN MSK $MSK BRD $BRD NET $NET MAC $MAC" >&2
103 ##############################################################################
104 # vpn_start () starts specified VPN
106 # $1 ... VPN to start
110 vpn_load $1 || { echo "Error: Could not vpn_load $1" >&2 ; return 1 }
113 if [ ! -c $DEV ]; then
114 [ -e $DEV ] && rm -f $DEV
115 mknod --mode=0600 $DEV c 36 $((16 + NUM))
119 { insmod ethertap --name="ethertap$NUM" unit="$NUM" 2>&1 || \
120 { echo "Error: cannot insmod ethertap$NUM" >&2 ; return 2 }
123 # configure the interface
124 ip link set $TAP address $MAC #&> /dev/null
125 ip link set $TAP up #&> /dev/null
126 ip addr flush dev $TAP 2>&1 | grep -v -x '^Nothing to flush.' #&> /dev/null
127 ip addr add $VPN brd $BRD dev $TAP #&> /dev/null
130 $TINCD --net="$1" $DEBUG_OPT || { echo "Error: Cannot start $TINCD" >&2;
133 # default interface route
134 ip route add $NET/$LEN dev $TAP #&> /dev/null
137 /etc/sysconfig/network-scripts/ifup-routes $TAP
143 ##############################################################################
144 # vpn_stop () Stops specified VPN
150 vpn_load $1 || return 1
152 # flush the routing table
153 ip route flush dev $TAP &> /dev/null
155 # kill the tincd daemon
156 PID="$TPIDS/tinc.$1.pid"
158 $TINCD --net="$1" --kill &> /dev/null
161 if [ $RET -eq 0 ]; then
163 while [ $dly -le 5 ]; do
165 sleep 1; dly=$((dly+1))
168 rm -f $PID &> /dev/null
172 # bring the interface down
173 ip link set $TAP down &> /dev/null
175 # remove kernel module
176 rmmod "ethertap$NUM" &> /dev/null
182 # See how we were called.
185 echo -n "Bringing up VPNs: "
186 for vpn in `ls -1 $TCONF`; do
187 vpn_start $vpn && echo -n "$vpn "
190 touch /var/lock/subsys/tinc
195 echo -n "Shutting down VPNs: "
196 for vpn in `ls -1 $TCONF`; do
197 vpn_stop $vpn && echo -n "$vpn "
200 rm -f /var/lock/susbsys/tinc
205 echo -n "Currently running VPNs: "
206 for vpn in `ls -1 $TCONF`; do
207 PID="$TPIDS/tinc.$vpn.pid"
209 if [ -f $PID -a `ps ax | grep "^ *$(cat $PID)" | wc -l` -eq 1 ]
225 echo "Usage: tinc {start|stop|status|restart}"