#!/bin/sh
# Script for setting CeroWrt (and OpenWrt) to create an IPv6 tunnel 
# to Hurricane Electric at http://www.tunnelbroker.net/
# There are two steps:
# 1) Go to the Tunnelbroker.net site to set up your free account
# 2) Run the script below, using the parameters supplied by Tunnelbroker
# This CeroWrt page gives detailed instructions for setting up an IPv6 tunnel: 
#    http://www.bufferbloat.net/projects/cerowrt/wiki/IPv6_Tunnel  
# 
# Once you've created your account and a tunnel, get the "Example
# Configurations" for OpenWRT Backfire, and paste them into this file
# named "tunnel.sh" Finally, ssh into the router and execute this script 
# with these steps:
# 
# ssh root@172.30.42.1
# cd /tmp
# cat > tunnel.sh 
# [paste in the contents of this file, then hit ^D]
# sh tunnel.sh
# [Restart your router. This seems to make a difference.]
#
# Presto! Your tunnel is set up. You should now be able 
#   communicate directly with IPv6 devices. 

# ==============================================
# Download and update all the interesting packages
# Some of these are pre-installed, but there is no 
# harm in updating/installing them a second time.
opkg update
opkg install 6in4

# Set the credentials for the tunnel login
# NB: These lines are automatically generated 
# specifically for *your* tunnel when you set it up
# Copy/paste them from the Example Configurations
# generated for the OpenWRT Backfire 10.03.1 dropdown
#
echo 'Setting up HE.net tunnel'
# ------- PASTE YOUR EXAMPLE CONFIGURATION LINES HERE --------
uci set network.henet=interface
uci set network.henet.proto=6in4
uci set network.henet.peeraddr=209.51.161.14
uci set network.henet.ip6addr='2001:470:1f06:64::2/64'
uci set network.henet.tunnelid=######
uci set network.henet.username=#####
uci set network.henet.password='###############'
# uci commit network

# ------ Set IPv6 subnets on the LAN interfaces...
# Addresses should be from the *routed* /48 address range assigned by HE.net
# Assigning se00 (secure wired LAN) and sw10 (secure wireless 5GHz - CEROwrt5)
echo 'Setting IPv6 addresses on local interfaces'
uci set network.se00.ip6addr='2001:470:8a63:100::1/64'
uci set network.sw10.ip6addr='2001:470:8a63:200::1/64'
#uci set network.sw00.ip6addr='2001:470:1f07:64::1/64'
#uci set network.gw00.ip6addr='2001:470:1f07:64::1/64'
#uci set network.gw01.ip6addr='2001:470:1f07:64::1/64' 
#uci set network.gw10.ip6addr='2001:470:1f07:64::1/64'
#uci set network.gw11.ip6addr='2001:470:1f07:64::1/64'
uci commit network

echo 'Restarting network...'
/etc/init.d/network restart

# CeroWrt puts WAN stuff in zone[0], not zone[1] as suggested by henet
uci set firewall.@zone[0].network='ge00 henet'
uci commit firewall

ifup henet
echo 'Restarting firewall...'
/etc/init.d/firewall restart

echo 'Done. You should restart the router now to make these take effect.'
# ------- END OF EXAMPLE CONFIGURATION LINES --------

# ==============================================
# Re-establishing the Tunnel
#
# NB: As of April 2012 (CeroWrt 3.3.1-4), the automatic re-establishment code 
# of the 6in4 module appears not to be working. You will need to re-establish 
# the tunnel manually when your external IP address changes.
#
# To re-establish the tunnel, say, because your external IP address changed,
# you can also use the following URL with these parameters. Note that the 
# USERNAME and PASSWORD are what you type to log into the Tunnelbroker site.
#
# USERNAME is the Account Name 
# PASSWORD is the current password
# TUNNELID is the Tunnel ID  
# https://USERNAME:PASSWORD@ipv4.tunnelbroker.net/ipv4_end.php?tid=TUNNELID
# 
# You can also use a non-HTTPS URL and parameters to re-establish the link.
# This form relies on hashed representations of the credentials since they're
# not carried on a secure connection. You can get more information about the
# parameters at https://ipv4.tunnelbroker.net/ipv4_end.php
#
# USERID is the "User ID" from the Tunnelbroker site's Main Page
# PWHASH is the MD5 hash of the password
# TUNNELID is the Tunnel ID
# http://ipv4.tunnelbroker.net/ipv4_end.php?ip=AUTO&apikey=USERID&pass=PWHASH&tid=TUNNELID
#
# --- end of script ---
