#!/bin/bash # # Script to automatically set up 6to4-tunnel # Usage: 6to4tunnel-control {start|stop} ipv4address # # Author: Roshan Revankar # http://www.geocities.com/rosh98 # http://roshweb.blogspot.com # # All the commands(including the comments accompanying them;)) for setting # up the ipv6 tunnel are from Peter Bieringer's # excellent Linux IPv6 HOWTO at http://www.tldp.org/HOWTO/Linux+IPv6-HOWTO/ # # This script can be called from /etc/ppp/ip-up to set up the tunnel and # from /etc/ppp/ip-down to bring down the tunnel # eg., The following can be used in /etc/ppp/ip-up to set up the tunnel # 6to4tunnel-control start $4 # The following in /etc/ppp/ip-down brings down the tunnel # 6to4tunnel-control stop LOGFILE=/etc/ppp/ipv6-log IS_IPV6_LOADED=0 IPV4_ADDR=$2 IS_SUCCESS=0 function check_cmd_status(){ if [ $IS_SUCCESS != 1 ]; then exit 0; fi IS_SUCCESS=0 } function setup_6to4_tunnel(){ echo Setting up 6to4 tunnel >> $LOGFILE # Create a new tunnel device /sbin/ip tunnel add tun6to4 mode sit remote any local $IPV4_ADDR >> $LOGFILE 2>&1 && IS_SUCCESS=1 check_cmd_status # Bring interface up /sbin/ip link set dev tun6to4 up >> $LOGFILE 2>&1 && IS_SUCCESS=1 check_cmd_status # Add local 6to4 address to interface (note: prefix length 16 is imporant!) /sbin/ip -6 addr add $IPV6_ADDR/16 dev tun6to4 >> $LOGFILE 2>&1 && IS_SUCCESS=1 check_cmd_status # Add (default) route to the global IPv6 network using the all-6to4-routers IPv4 anycast address /sbin/ip -6 route add 2000::/3 via ::192.88.99.1 dev tun6to4 metric 1 >> $LOGFILE 2>&1 && IS_SUCCESS=1 check_cmd_status } function remove_6to4_tunnel(){ echo Removing 6to4 tunnel >> $LOGFILE # Remove all routes through this dedicated tunnel device /sbin/ip -6 route flush dev tun6to4 # Shut down interface /sbin/ip link set dev tun6to4 down # Remove created tunnel device /sbin/ip tunnel del tun6to4 } function is_tunnel_up(){ IS_TUNNEL_UP=`/sbin/ip -6 tunnel show tun6to4` if [ "$IS_TUNNEL_UP" != "" ]; then echo "Check 6to4 tunnel status:6to4 tunnel is up" >> $LOGFILE echo IS_TUNNEL_UP >> $LOGFILE return 1 else echo "Check 6to4 tunnel status:6to4 tunnel is not up" >> $LOGFILE return 0 fi } function start() { # Setup IPv6 date > $LOGFILE echo Bringing up IPv6 >> $LOGFILE echo IPv4 address is $IPV4_ADDR >> $LOGFILE # Test if ipv6 was loaded into the kernel test -f /proc/net/if_inet6 && IS_IPV6_LOADED = 1 echo $IS_IPV6_LOADED >> $LOGFILE if [ $IS_IPV6_LOADED = 0 ]; then # its not loaded, so attempt to load it /sbin/modprobe ipv6 >> $LOGFILE 2>&1 && IS_SUCCESS=1 check_cmd_status fi # First generate the IPv6 address from IPv4 address TMP_ADDR=`echo $IPV4_ADDR | tr "." " "` IPV6_ADDR=`printf "2002:%02x%02x:%02x%02x::1" $TMP_ADDR` echo IPV6 address is $IPV6_ADDR >> $LOGFILE is_tunnel_up if [ $? = 1 ] ; then remove_6to4_tunnel fi setup_6to4_tunnel } function stop() { is_tunnel_up if [ $? = 1 ] ; then remove_6to4_tunnel fi } case "$1" in start) start ;; stop) stop ;; *) echo $"Usage: $0 {start|stop} ipv4address" exit 1 esac