#!/bin/sh
findCurrentTunnel() { gifs=`ifconfig | grep 'gif[0-9]:' | awk '{print $1}' | awk 'gsub("[:]", "")'` for gif in $gifs; do echo -n "Searching $gif... " >> /var/log/dhcp6c.log aftr=`ifconfig $gif | grep inet6 | grep '\-\->' | awk '{print $5}'` echo "$aftr" >> /var/log/dhcp6c.log if [ "$aftr" == "$1" ]; then echo $gif return 0 fi done return 1
}
findAvailableGif() { gifs=`ifconfig | grep 'gif[0-9]:' | awk '{print $1}' | awk 'gsub("gif|:", "")'` for i in `seq 0 100 1`; do for o in $gifs; do if [ $i -eq $o ]; then continue fi echo "gif$i" return 0 done done echo "gif0" return 0
}
AFTR="2a02:908::13:4000"
WANIP6=`ifconfig $1 | grep inet6 | grep 'prefixlen 128' | awk '{print $2}'`
if [ -f /tmp/dslite.dat ]; then OLDWANIP6=`cat /tmp/dslite.dat` echo "Found old WAN IPv6: $OLDWANIP6" >> /var/log/dhcp6c.log
fi
echo "Current WAN IPv6: $WANIP6" >> /var/log/dhcp6c.log
gif=$(findCurrentTunnel "$AFTR")
if [ $? -eq 0 ]; then if [ "$OLDWANIP6" == "$WANIP6" ]; then # just received new DHCP6 lease, IPv6 and probably prefix stayed the same, preventing tunnel from recreation echo "No new IPv6 received, skipping tunnel recreation." >> /var/log/dhcp6c.log return 0 fi echo -n "Found old DS-Lite tunnel $gif, destroying... " >> /var/log/dhcp6c.log ifconfig $gif destroy echo "done." >> /var/log/dhcp6c.log
fi
gif=$(findAvailableGif)
if [ ! $? -eq 0 ]; then echo "No available GIF interface number found..." >> /var/log/dhcp6c.log return 1
fi
echo -n "Creating DS-Lite tunnel with $gif... " >> /var/log/dhcp6c.log
ifconfig $gif create
ifconfig $gif inet6 tunnel $WANIP6 $AFTR mtu 1460 -accept_rtadv ifdisabled
ifconfig $gif inet 192.0.0.2 192.0.0.1 netmask 255.255.255.248
echo "done." >> /var/log/dhcp6c.log
echo -n "Setting up default route for IPv4 to tunnel... " >> /var/log/dhcp6c.log
#route add default -interface $gif > /dev/null
route add default 192.0.0.1
echo "done." >> /var/log/dhcp6c.log
echo "$WANIP6" > /tmp/dslite.dat