Loading

6to4 IPv6 tunnel

  1. #!/bin/bash
  2. #
  3. # Configures the 6to4 address based on the ip address of the interface to which
  4. # the default route points.
  5. # Parameter 1: action (start, stop, restart)
  6. #           2: interface SLA (optional)
  7. #           3: interface ID (optional)
  8. #           4: interface prefix (optional)
  9. #           5: IPv6 relay address (optional)
  10. #
  11.  
  12. # Variable parameters. TUN_INTF used for start and stop, all others only start
  13. # No validation of input at all!!
  14. TUN_INTF=${2:-tun6to4}
  15. SLA_INTF=${3:-0}
  16. INTF_ID=${4:-1}
  17. INTF_PFX=${5:-48}
  18. TUN_RELY=${6:-192.88.99.1}
  19.  
  20. case "${1}" in
  21.     start)
  22.         # Clean up before configuring the tunnel
  23.         $0 stop ${TUN_INTF}
  24.  
  25.         # IPv6 ready?
  26.         [ -f /proc/net/if_inet6 ] || exit 0
  27.  
  28.         # Grab the device of the default route (may be
  29.         # multiple -> CUR_DV is an array)
  30.         CUR_DV=(`ip -4 route list | awk '/^default / { print $0 }' | \
  31.                                     sed 's/.* dev \([^ ]\+\).*$/\1/'`)
  32.  
  33.         # Is there any device? Or no default route at all??
  34.         if [ -z ${CUR_DV} ]; then
  35.             echo "No default route. Cannot determine IPv4 address to use"
  36.             exit 0
  37.         fi
  38.  
  39.         # We simply take the device from the first default route
  40.         CUR_IP=(`ip -4 addr show ${CUR_DV} | awk '/inet / { print $2 }' | \
  41.                sed -e 's/^\(\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*$/\1/'`)
  42.  
  43.         # Find the MTU on the interface used for the tunnel
  44.         DEV_MTU=`ip link show ${CUR_DV} | tr '\n' ' ' | \
  45.                                           sed 's/.* mtu \([0-9]\+\).*$/\1/'`
  46.  
  47.         # Multiple IP addresses may be defined on device. First one is ours.
  48.         # Add the tunnel. The local address is the one found from default route.
  49.         ip tunnel add ${TUN_INTF} mode sit remote any local ${CUR_IP}
  50.         # Bring the tunnel up.
  51.         ip link set dev ${TUN_INTF} mtu `expr ${DEV_MTU} - 20` up
  52.         # Add the interface address
  53.         IPV6_ADDR=$(printf "2002:%02x%02x:%02x%02x:%04x::%04x" \
  54.                   $(echo "${CUR_IP} ${SLA_INTF} ${INTF_ID}" | tr '.' ' '))
  55.         ip -6 addr add ${IPV6_ADDR}/${INTF_PFX} dev ${TUN_INTF}
  56.  
  57.         # Add the routes
  58.         ip -6 route add 2002::/16 dev ${TUN_INTF}
  59.         ip -6 route add ::/0 via ::${TUN_RELY} dev ${TUN_INTF} metric 1
  60.  
  61.         # Notify
  62.         echo "6to4 tunnel '${TUN_INTF}' created." \
  63.              "IPv6 address: ${IPV6_ADDR}/${INTF_PFX}, IPv6 relay: ${TUN_RELY}"
  64.         ;;
  65.     stop)
  66.         # Skip if device is not configured
  67.         ip link list ${TUN_INTF} > /dev/null 2>&1
  68.         if [ $? = 0 ]; then
  69.             # Remove all routes going through this device
  70.             ip -6 route flush dev ${TUN_INTF}
  71.             # Disable the link
  72.             ip link set dev ${TUN_INTF} down
  73.             # Delete the device
  74.             ip tunnel del ${TUN_INTF}
  75.             # Notify
  76.             echo "6to4 tunnel '${TUN_INTF}' removed."
  77.         fi
  78.         ;;
  79.     restart)
  80.         $0 stop ${2}
  81.         $0 start ${2} ${3} ${4} ${5}
  82.         ;;
  83.     *)
  84.         echo "Usage: `basename $0` (start|restart)" \
  85.              "[<tunnel-name> [<sla> [<int-id> [<prefix> [<relay-ip>]]]]]"
  86.         echo "       `basename $0` stop [<tunnel-name>]"
  87.         ;;
  88. esac

Comments