#! /bin/sh
# IPv6 Tunnel Script using iproute package
# Indra Kusuma <Indra@Kusuma.OR.ID>
# Thanks to :
# Peter Bieringer <peter@bieringer.de> for his contribution
# Fabio Massimo Di Nitto <fabbione@fabbione.net> for his suggestion
# Version 1.3


ipbin=`which /sbin/ip`

if [ 0 != `id -u` ]; then echo "You must be root !" && exit 1 ; fi
test ! -f /proc/net/if_inet6  && echo "System is not IPv6-enabled (try to load 'ipv6' module)" && exit 1
test -z ${ipbin} && echo "Please install iproute packages" && exit 1

ask_user() {
	P=${1:-'Should I do this ? [yN] '}
	D=${2:-1}
	Y=${3:-'[yY]*'}
	N=${4:-'[nN]*'}
	E=${5:-'\nPlease enter either y)es or n)o, followed by <RETURN>\n'}

	while true ; do
		echo -ne "$P"
		read response
		case "$response" in
		${Y} ) return 0 ;;
		${N} ) return 1 ;;
		"" ) [ "$D" = 0 -o "$D" = 1 ] && return $D ;;
		esac
		echo -e $E
	done
}

case "$1" in
	list)
		case "$2" in
			tunnel)
				echo "List of existing IPv6 tunnel:"
				${ipbin} -6 tunnel show
				;;
			address)
				echo "List of existing IPv6 addresses :"
				${ipbin} -6 addr show
				;;
			route)
				echo "Routing table of IPv6 :"
				${ipbin} -6 route show
				;;
			*)
				echo "$0 list (tunnel | address | route)"
				;;
		esac
		;;
	add)
		case "$2" in
		device)
			dev=$3
			remote4=$4
			local6=$5
			route6=$6
			echo "[*] Building new tunnel device named ${dev} with remote address ${remote4}"
			${ipbin} -6 tunnel add ${dev} mode sit ttl 255 remote ${remote4} local any
			echo "[*] Bringing up the tunnel"
			${ipbin} -6 link set ${dev} up
			echo "[*] Adding the IPv6/prefix address to tunnel"
			${ipbin} -6 addr add ${local6} dev ${dev}
			test ! -z ${route6} && echo "[*] Adding default route ${route6} to device ${dev}" && ${ipbin} -6 route add ${route6} dev ${dev}
			;;
		*)
			echo -n "Device name 			: " && read dev
			echo -n "Remote IPv4 address 		: " && read remote4
			echo -n "Local IPv6/prefix address 	: " && read local6
			echo -n "Default route (enter for none)	: " && read route6
			echo
			echo "Building IPv6 tunnel"
			echo "Device name    	           	: ${dev}"
			echo "Remote IPv4 address 	      	: ${remote4}"
			echo "Local IPv6/prefix address 	: ${local6}"
			echo "Default route			: ${route6}"
			if ask_user "Do you like to setup the tunnel like this now ? [Yn] " 0
			then
				echo "[*] Building new tunnel device named ${dev} with remote address ${remote4}"
				${ipbin} -6 tunnel add ${dev} mode sit ttl 255 remote ${remote4} local any
				echo "[*] Bringing up the tunnel"
				${ipbin} -6 link set ${dev} up
				echo "[*] Adding the IPv6/prefix address ${local6} to tunnel"
				${ipbin} -6 addr add ${local6} dev ${dev}
				test ! -z ${route6} && echo "[*] Adding default route ${route6} to device ${dev}" && ${ipbin} -6 route add ${route6} dev ${dev}
			fi
			;;
		esac
		;;
	del)
		case "$2" in
		device)
			devdel=$3
			echo "[*] Deleting the tunnel ${devdel}"
			${ipbin} -6 tunnel del ${devdel}
			;;
		*)
			echo "Available IPv6 tunnel		:"
			${ipbin} -6 tunnel show
			echo
			echo -n "Device name to delete		: " && read devdel
			${ipbin} -6 tunnel show $devdel
			echo
			if ask_user "Are you sure want to delete this ${devdel} tunnel ? [yN] " 1
			then
				echo "[*] Deleting the tunnel"
				${ipbin} -6 tunnel del ${devdel}
			fi
			;;
		esac
		;;
	*)
		echo
		echo "$0 ( list (tunnel|address|route) | add | del )"
		echo
		echo -e "list\t: List of IPv6 tunnel/address/route, use $0 list, for more help"
		echo -e "add [device (devicename) (remoteipv4) (localipv6) [defaultroute]]"
		echo -e "\t: Build new tunnel device, use device option to oneline command"
		echo -e "\t  $0 add device mytunnel 1.2.3.4 3ffe:ffff:1f00:ffff::261/127 2000::/3"
		echo -e "\t  or just use $0 add"
		echo -e "del [device (devicename)]"
		echo -e "\t: Delete existing tunnel device, or just $0 del, to use menu"
		echo
		;;
esac
