3 # skeleton example file to build /etc/init.d/ scripts.
4 # This file should be used to construct scripts for /etc/init.d.
6 # Written by Miquel van Smoorenburg <miquels@cistron.nl>.
7 # Modified for Debian GNU/Linux
8 # by Ian Murdock <imurdock@gnu.ai.mit.edu>.
10 # Version: @(#)skeleton 1.8 03-Mar-1998 miquels@cistron.nl
12 # This file was automatically customized by dh-make on Fri, 21 Apr 2000 17:07:50 +0200
14 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
15 DAEMON=/usr/sbin/tincd
22 test -f $DAEMON || exit 0
27 if [ ! -x $TINCD ]; then
28 echo "**tinc: daemon $TINCD does not exist or is not executable!"
32 # Check the configuration directory
33 if [ ! -d $TCONF ]; then
34 echo "**tinc: configuration directory ($TCONF) not found!"
39 ##############################################################################
40 # vpn_load () Loads VPN configuration
46 CFG="$TCONF/$1/tinc.conf"
47 [ -f $CFG ] || { echo "tinc: $CFG does not exist" >&2 ; return 1; }
50 DEV=`grep -i -e '^[[:space:]]*TapDevice' $CFG | sed 's/[[:space:]]//g; s/^.*=//g'`
51 VPN=`grep -i -e '^[[:space:]]*(MyOwnVPNIP|MyVirtualIP)' -E $CFG | head -1 | sed 's/[[:space:]]//g; s/^.*=//g'`
53 # discourage empty and multiple entries
55 { echo "tinc: TapDevice required" >&2 ; return 2; }
56 echo $DEV | grep -q '^/dev/tap' ||
57 { echo "tinc: TapDevice should be in form /dev/tapX" >&2 ; return 2; }
58 [ `echo $DEV | wc -l` -gt 1 ] &&
59 { echo "tinc: multiple TapDevice entries not allowed" >&2 ; return 3; }
61 { echo "tinc: MyOwnVPNIP/MyVirtualIP required" >&2 ; return 2; }
62 [ `echo $VPN | wc -l` -gt 1 ] &&
63 { echo "tinc: multiple MyOwnVPNIP/MyVirtualIP entries not allowed" >&2 ; return 3; }
64 echo $VPN | grep -q -x \
65 '\([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\{1,3\}/[[:digit:]]\{1,2\}' || \
66 { echo "tinc: badly formed MyOwnVPNIP/MyVirtualIP address $VPN" ; return 3; }
69 TAP=`echo $DEV | cut -d"/" -f3`
70 NUM=`echo $TAP | sed 's/tap//'`
72 # IP address, netmask length
73 ADR=`echo $VPN | cut -d"/" -f1`
74 LEN=`echo $VPN | cut -d"/" -f2`
76 # Expand bitlength to netmask
78 for cnt in 1 1 1 0 ; do
79 if [ $len -ge 8 ]; then
85 MSK="$MSK$((255 & (255 << (8 - msk))))"
86 [ $cnt -ne 0 ] && MSK="$MSK."
90 # Network & broadcast addresses
91 # BRD=`ipcalc --broadcast $ADR $MSK | cut -d"=" -f2`
92 # NET=`ipcalc --network $ADR $MSK | cut -d"=" -f2`
95 MAC=`printf "fe:fd:%0.2x:%0.2x:%0.2x:%0.2x" $(echo $ADR | sed 's/\./ /g')`
97 echo "TAP $TAP NUM $NUM ADR $ADR LEN $LEN MSK $MSK BRD $BRD NET $NET MAC $MAC" >&2
102 ##############################################################################
103 # vpn_start () starts specified VPN
105 # $1 ... VPN to start
109 vpn_load $1 || { echo "**tinc: could not vpn_load $1" >&2 ; return 1; }
112 if [ ! -c $DEV ]; then
113 [ -e $DEV ] && rm -f $DEV
114 mknod --mode=0600 $DEV c 36 $((16 + NUM))
118 { insmod ethertap --name="ethertap$NUM" unit="$NUM" 2>&1 || \
119 { echo "**tinc: cannot insmod ethertap$NUM" >&2 ; return 2; } ;
122 # configure the interface
123 ip link set $TAP address $MAC
125 ip addr flush dev $TAP 2>&1 | grep -v -x '^Nothing to flush.'
126 ip addr add $VPN brd $BRD dev $TAP
129 $TINCD --net="$1" $DEBUG || \
130 { echo "**tinc: could not start $TINCD" >&2; return 3; }
132 # default interface route
133 # ip route add $NET/$LEN dev $TAP
136 /etc/sysconfig/network-scripts/ifup-routes $TAP
142 ##############################################################################
143 # vpn_stop () Stops specified VPN
149 vpn_load $1 || return 1
151 # flush the routing table
152 # ip route flush dev $TAP &> /dev/null
154 # kill the tincd daemon
155 PID="$TPIDS/tinc.$1.pid"
157 $TINCD --net="$1" --kill &> /dev/null
160 if [ $RET -eq 0 ]; then
162 while [ $dly -le 5 ]; do
164 sleep 1; dly=$((dly+1))
168 [ -f $PID ] && rm -f $PID
171 # bring the interface down
172 ip link set $TAP down &> /dev/null
174 # remove ethertap module
175 rmmod "ethertap$NUM" &> /dev/null
188 echo -n "Starting $DESC:"
189 for net in $NETS ; do
192 start-stop-daemon --start --quiet --pidfile /var/run/$NAME.$net.pid \
193 --exec $DAEMON -- -n $net
198 echo -n "Stopping $DESC:"
199 for net in $NETS ; do
201 start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.$net.pid \
202 --exec $DAEMON -- -n $net -k
208 # If the daemon can reload its config files on the fly
209 # for example by sending it SIGHUP, do it here.
211 # If the daemon responds to changes in its config file
212 # directly anyway, make this a do-nothing entry.
214 # echo "Reloading $DESC configuration files."
215 # start-stop-daemon --stop --signal 1 --quiet --pidfile \
216 # /var/run/$NAME.pid --exec $DAEMON
218 restart|force-reload)
220 # If the "reload" option is implemented, move the "force-reload"
221 # option to the "reload" entry above. If not, "force-reload" is
222 # just the same as "restart".
224 echo -n "Restarting $DESC:"
225 for net in $NETS ; do
226 start-stop-daemon --stop --quiet --pidfile \
227 /var/run/$NAME.$net.pid --exec $DAEMON -- -n $net -k
229 start-stop-daemon --start --quiet --pidfile \
230 /var/run/$NAME.$net.pid --exec $DAEMON -- -n $net
237 # echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
238 echo "Usage: $N {start|stop|restart|force-reload}" >&2