Skip to content

Instantly share code, notes, and snippets.

@jackyyf
Created November 9, 2017 03:10
Show Gist options
  • Save jackyyf/997882e1c51c2bc9247af91a604e9144 to your computer and use it in GitHub Desktop.
Save jackyyf/997882e1c51c2bc9247af91a604e9144 to your computer and use it in GitHub Desktop.
#! /bin/bash
### BEGIN INIT INFO
# Provides: ipv6
# Required-Start: $local_fs $all
# Required-Stop: $local_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the ipv6 tunnel
# Description: ipv6 tunnel start-stop-daemon
### END INIT INFO
PATH=/usr/bin:/usr/local/bin:/usr/sbin:/usr/local/sbin:/bin:/sbin
DAEMON=/usr/local/sbin/tb_userspace
NAME=v6tun
DEV_NAME=v6tun0
TUN_SINK=66.220.18.42
TUN_SOURCE=107.161.94.156
V6_ADDRS="
2001:470:c:dad::2/64
2001:470:c:dad:dead:beef:bad:cafe/64
2001:470:f548:face:dead:beef:bad:cafe/48
2001:470:f548::9/48
"
V6_PARTS="
2001:470:c:dad::/64
2001:470:d:dad::/64
2001:470:f548::/48
"
V6_ROUTES="
"
V6_GW=2001:470:c:dad::1
V6_DEFAULT=0
PID_FILE=/tmp/$NAME.pid
ROUTE_TABLE=233
if [ -r /etc/default/v6tun ]; then
. /etc/default/v6tun
fi
test -x $DAEMON || exit 0
. /lib/init/vars.sh
. /lib/lsb/init-functions
init_device() {
ip link set dev "$DEV_NAME" up mtu 1480 txqueuelen 256 >/dev/null 2>&1 || return 1
return 0
}
destroy_device() {
ip link set dev "$DEV_NAME" down >/dev/null 2>&1 || return 1
return 0
}
add_addresses() {
PREFERED="forever"
for address in $V6_ADDRS; do
ip -6 addr add "$address" dev "$DEV_NAME" scope global valid_lft forever preferred_lft "$PREFERED" >/dev/null 2>&1 || return 1
PREFERED="0"
done
return 0
}
add_routes() {
if [[ $V6_DEFAULT -ne 0 ]]; then
ip -6 route del default >/dev/null 2>&1 || true
ip -6 route add default via "$V6_GW" dev "$DEV_NAME" >/dev/null 2>&1 || return 1
fi
ip -6 route flush table $ROUTE_TABLE
ip -6 route add default via "$V6_GW" dev "$DEV_NAME" table $ROUTE_TABLE
for route in $V6_ROUTES; do
ip -6 route add "$route" dev "$DEV_NAME" via "$V6_GW" >/dev/null 2>&1 || return 1
done
for part in $V6_PARTS; do
ip -6 rule del from "$part" table $ROUTE_TABLE
ip -6 rule add from "$part" table $ROUTE_TABLE
done
return 0;
}
flush_routes() {
ip -6 route flush dev "$DEV_NAME" >/dev/null 2>&1 || true
return 0
}
case "$1" in
start)
log_daemon_msg "Starting userspace tunnel driver" "$NAME"
setsid "$DAEMON" v6tun0 "$TUN_SINK" "$TUN_SOURCE" sit >/dev/null 2>&1 &
PID=$!
echo $PID > $PID_FILE
for i in `seq 1 5`
do
sleep 5
log_end_msg 0
log_daemon_msg "Initialize virtual device"
if init_device; then
log_end_msg 0
INIT_OK=yes
break
fi
done
if [ "x$INIT_OK" != "xyes" ]
then
log_end_msg 1
exit 1
fi
log_daemon_msg "Add assigned addresses"
if ! add_addresses; then
log_end_msg 1
exit 1
fi
log_end_msg 0
log_daemon_msg "Add routes"
if ! add_routes; then
log_end_msg 1
exit 1
fi
log_end_msg 0
;;
stop)
log_daemon_msg "Flushing routes"
flush_routes
log_end_msg 0
log_daemon_msg "Destroying devices"
destroy_device
log_end_msg 0
log_daemon_msg "Stopping userspace tunnel driver" "$NAME"
kill `cat $PID_FILE` >/dev/null 2>&1 || true
rm -f `cat $PID_FILE` >/dev/null 2>&1 || true
log_end_msg 0
;;
*)
echo "Usage: /etc/init.d/v6tun {start|stop}"
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment