3
# Copyright (c) 2007 Javier Fernandez-Sanguino <jfs@debian.org>
4
# Modified by Andrew Shadura <bugzilla@tut.by>
6
# This is free software; you may redistribute it and/or modify
7
# it under the terms of the GNU General Public License as
8
# published by the Free Software Foundation; either version 2,
9
# or (at your option) any later version.
11
# This is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License with
17
# the Debian operating system, in /usr/share/common-licenses/GPL; if
18
# not, write to the Free Software Foundation, Inc., 59 Temple Place,
19
# Suite 330, Boston, MA 02111-1307 USA
23
# Required-Start: $network $local_fs $remote_fs
24
# Required-Stop: $remote_fs
27
# Default-Start: 2 3 4 5
29
# Short-Description: userspace NAT64
30
# Description: TAYGA is a stateless NAT64 userspace daemon. Using the
31
# in-kernel TUN network driver, TAYGA receives IPv4 and
32
# IPv6 packets, translates them to the other protocol,
33
# and sends the translated packets back using the same
37
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
39
DAEMON=/usr/sbin/tayga # Introduce the server's location here
40
NAME=tayga # Introduce the short server's name here
41
DESC="userspace NAT64" # Introduce a short description here
43
PIDFILE=/var/run/$NAME.pid
45
test -x "$DAEMON" || exit 0
47
. /lib/lsb/init-functions
49
# Default options, these can be overriden by the information
50
# at /etc/default/$NAME
51
DAEMON_OPTS="--nodetach" # Additional options given to the server
53
DIETIME=10 # Time to wait for the server to die, in seconds
54
# If this value is set too low you might not
55
# let some servers to die gracefully and
56
# 'restart' will not work
58
STARTTIME=2 # Time to wait for the server to start, in seconds
59
# If this value is set each time the server is
60
# started (on start or restart) the script will
61
# stall to try to determine if it is running
62
# If it is not set and the server takes time
63
# to setup a pid file the log message might
64
# be a false positive (says it did not start
65
# when it actually did)
67
TUN_DEVICE=$(sed -rn "/^[ \t]*tun-device/s/^[ \t]*tun-device[ \t]+//p" /etc/tayga.conf)
68
IPV6_PREFIX=$(sed -rn "/^[ \t]*prefix/s/^[ \t]*prefix[ \t]+//p" /etc/tayga.conf)
69
DYNAMIC_POOL=$(sed -rn "/^[ \t]*dynamic-pool/s/^[ \t]*dynamic-pool[ \t]+//p" /etc/tayga.conf)
73
# Include defaults if available
74
if [ -f "/etc/default/$NAME" ] ; then
75
. "/etc/default/$NAME"
78
if [ "$RUN" != "yes" ] ; then
79
log_failure_msg "$NAME disabled, please adjust the configuration to your needs "
80
log_failure_msg "and then set RUN to 'yes' in /etc/default/$NAME to enable it."
87
# Check if a given process pid's cmdline matches a given name
90
[ -z "$pid" ] && return 1
91
[ ! -d "/proc/$pid" ] && return 1
96
# Check if the process is running looking at /proc
97
# (works for all users)
99
# No pidfile, probably no daemon present
100
[ ! -f "$PIDFILE" ] && return 1
102
running_pid "$pid" "$DAEMON" || return 1
107
if [ "$CONFIGURE_IFACE" = "yes" ] ; then
108
"$DAEMON" --mktun | logger -t "$NAME" -i
109
ip link set "$TUN_DEVICE" up
110
[ -n "$DYNAMIC_POOL" ] && ip route add "$DYNAMIC_POOL" dev "$TUN_DEVICE"
111
[ -n "$IPV6_PREFIX" ] && ip route add "$IPV6_PREFIX" dev "$TUN_DEVICE"
112
[ -n "$IPV4_TUN_ADDR" ] && ip addr add "$IPV4_TUN_ADDR" dev "$TUN_DEVICE"
113
[ -n "$IPV6_TUN_ADDR" ] && ip addr add "$IPV6_TUN_ADDR" dev "$TUN_DEVICE"
115
[ "$CONFIGURE_NAT44" = "yes" ] && [ -n "$DYNAMIC_POOL" ] && iptables -t nat -A POSTROUTING -s "$DYNAMIC_POOL" -j MASQUERADE || true
117
start-stop-daemon --start --quiet \
118
-b --exec "$DAEMON" -- --pidfile "$PIDFILE" $DAEMON_OPTS
122
start-stop-daemon --stop --quiet --pidfile "$PIDFILE"
124
if [ "$CONFIGURE_IFACE" = "yes" ] ; then
125
ip link set "$TUN_DEVICE" down
126
"$DAEMON" --rmtun | logger -t "$NAME" -i
128
[ "$CONFIGURE_NAT44" = "yes" ] && [ -n "$DYNAMIC_POOL" ] && iptables -t nat -D POSTROUTING -s "$DYNAMIC_POOL" -j MASQUERADE || true
135
# Force the process to die killing it manually
136
[ ! -e "$PIDFILE" ] && return
145
echo "Cannot kill $NAME (pid=$pid)!"
156
log_daemon_msg "Starting $DESC" "$NAME"
157
# Check if it's running first
159
log_progress_msg "apparently already running"
163
if start_server ; then
164
# NOTE: Some servers might die some time after they start,
165
# this code will detect this issue if STARTTIME is set
166
# to a reasonable value
167
[ -n "$STARTTIME" ] && sleep $STARTTIME # Wait some time
169
# It's ok, the server started and is running
172
# It is not running after we did start
176
# Either we could not start it
181
log_daemon_msg "Stopping $DESC" "$NAME"
183
# Only stop the server if we see it running
185
stop_server || errcode=$?
188
# If it's not running don't do anything
189
log_progress_msg "apparently not running"
195
# First try to stop gracefully the program
198
# If it's still running try to kill it more forcefully
199
log_daemon_msg "Stopping (force) $DESC" "$NAME"
201
force_stop || errcode=$?
205
restart|force-reload)
206
log_daemon_msg "Restarting $DESC" "$NAME"
208
stop_server || errcode=$?
209
# Wait some sensible amount, some server need this
210
[ -n "$DIETIME" ] && sleep $DIETIME
211
start_server || errcode=$?
212
[ -n "$STARTTIME" ] && sleep $STARTTIME
213
running || errcode=$?
218
log_daemon_msg "Checking status of $DESC" "$NAME"
220
log_progress_msg "running"
223
log_progress_msg "apparently not running"
228
# Use this if the daemon cannot reload
230
log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon"
231
log_warning_msg "cannot re-read the config file (use restart)."
235
echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2