Last active
September 17, 2024 08:12
-
-
Save pklaus/960672 to your computer and use it in GitHub Desktop.
tunnelbroker.net automatic tunnel IP update and tunnel setup (on Mac OS X)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| #### This script is published by Philipp Klaus <philipp.l.klaus@web.de> | |
| #### on <http://blog.philippklaus.de/2011/05/ipv6-6in4-tunnel-via-hurricane-electric-tunnelbroker-net-automatic-ip-update-on-mac-os-x/> | |
| #### It is originally by freese60 and modified by limemonkey. | |
| #### Found on <http://www.tunnelbroker.net/forums/index.php?topic=287.0> | |
| ### Uncomment this line to debug the script: | |
| #set -x | |
| ####################################################################### | |
| #### Config start | |
| ### | |
| ### This configuration file must set the following variables: | |
| ### MYIF, DEVNAME, LOCAL_IPV4, EXTERNAL_IPV4, | |
| ### HEUSER, HEKEY, HETUNNEL, | |
| ### HESERVER4END, HESERVER6END and HECLIENT6END | |
| #MYIF="en1" # en1 = Airport, en0 = Ethernet | |
| MYIF=`netstat -f inet -r | grep default | tr -s ' ' | cut -d ' ' -f 6 | sed -n 1p` # autodetect | |
| DEVNAME='gif0' | |
| LOCAL_IPV4=`ifconfig $MYIF |grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}'` | |
| EXTERNAL_IPV4=`curl -s "http://ipv4.whatsmyip.reliable-ict.de/"` | |
| HEUSER='your.username' # The username you use to login at tunnelbroker.net | |
| HEKEY='32f325019357278d' # This 'Update Key' can be found on the 'Advanced' tab of the tunnel details page. | |
| HETUNNEL='12056' # The 'Tunnel ID' from the tab IPv6 tunnel on the tunnel details page. | |
| ### other settings from the website (the tunnel settings): | |
| HESERVER4END='216.66.80.30' | |
| HESERVER6END=2001:470:1f0a:3333::1 | |
| HECLIENT6END=2001:470:1f0a:3333::2 | |
| HE64PREFIX=2001:470:1f0b:3333:: | |
| MYCUSTOMADDRESS=${HE64PREFIX}1:1 | |
| ####################################################################### | |
| #### Starting the actual script | |
| echo "Please enter the 'sudo' password. This is password of your user account on this Mac. It is needed to set up the IPv6 tunnel." | |
| sudo echo "Gained superuser permissions" | |
| if [ $? == 1 ]; then echo "Sorry! You need to provide your password in order to set up the tunnel."; exit 1; fi | |
| echo "Remove previous tunnel (ignore any errors)" | |
| sudo ifconfig $DEVNAME down | |
| sudo ifconfig $DEVNAME inet6 $MYCUSTOMADDRESS prefixlen 128 delete | |
| sudo ifconfig $DEVNAME inet6 $HECLIENT6END $HESERVER6END prefixlen 128 delete | |
| sudo route delete -inet6 default -interface $DEVNAME | |
| sudo ifconfig $DEVNAME deletetunnel | |
| echo "Removed the previous tunnel. Will continue to set up the tunnel in 5 seconds..." | |
| for i in {5..1}; do echo "$i"; sleep 1; done | |
| echo "Updating your IPv4 tunnel endpoint setting on the Hurricane Electric Website." | |
| # And instead of determining the external IPv4 address on your own, you can also set the param ip to AUTO. | |
| curl -k -s "https://ipv4.tunnelbroker.net/nic/update?username=$HEUSER&password=$HEKEY&hostname=$HETUNNEL&myip=$EXTERNAL_IPV4" | |
| # One more API of the tunnelbroker.net site is: https://username:password@tunnelbroker.net/tunnelInfo.php[?tid=tunnel_id] which returns an XML output | |
| sleep 1 | |
| echo "Setting up the tunnel with the new settings now ." | |
| sudo ifconfig $DEVNAME create | |
| sudo ifconfig $DEVNAME tunnel $LOCAL_IPV4 $HESERVER4END | |
| sudo ifconfig $DEVNAME inet6 $MYCUSTOMADDRESS prefixlen 128 | |
| sudo ifconfig $DEVNAME inet6 $HECLIENT6END $HESERVER6END prefixlen 128 | |
| sudo route -n add -inet6 default $HECLIENT6END | |
| # We now provide the user with information if the tunnel has ben set up successfully: | |
| sleep 1 | |
| echo "The external IPv6 is now set to `curl -s 'http://ipv6.whatsmyip.reliable-ict.de/'`." | |
| echo "The external IP of your default connection is now set to `curl -s 'http://whatsmyip.reliable-ict.de/'`." | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Background
In order to check whether everything is correct we need to know what to check. Doing so requires knowing what each of the configuration commands does. Before getting into that, let's review some concepts we'll need...
What are we really doing?
We are creating a
6in4tunnel that will basically carryIPv6datagrams as the payloads of regularIPv4datagrams by means of a procedure called encapsulation. Check this diagram for more info. The previous link points to anRFCwhich can seen kind of daunting to read, but it's the most accurate information you can possibly get... In any case, we need a way of encapsulatingIPv6datagrams intoIPv4ones. One of the procedures we can use is leverage the interface model provided by tools likeifconfig(itself a part of thenet-toolssuite). We'll then configure an interface our tunnel will be supported on (namely, a tunnel interface if you want). This is exactly what the script automatizes.Why all the trouble?
If your
ISPdoesn't provide direct access to theIPv6internet we still need to go through a bit ofIPv4infrastructure before reaching theIPv6internet... When using the tunnel this is what's roughly going on behind the scenes:IPv6capable program (say,ping6) generates anIPv6datagram.IPv6datagram will be encapsulated into anIPv4datagram.IPv4datagram is sent to Hurricane Electric's tunnel endpoint over theISPs regularIPv4infrastructure.IPv6datagram contained within theIPv4datagram.IPv6datagram through theIPv6internet it has access to.All this process means that the original program really thinks it's communicating directly with the
IPv6internet: the path overIPv4is transparent to the application. If theISPprovided direct access to theIPv6internet we wouldn't need all these extra steps...Variable expansion
Note that a dollar sign (
$) prepended to a name expands that variable name, that is, it'll replace the variable name with its real value. Notice how on line22we defineDEVNAME='gif0'. This implies that wherever we find$DEVNAMEit'll be equivalent togif0. The same goes for the rest of variables such asLOCAL_IPV4andHERSERVER4END. Note that "outside the script" these variables might not be defined. You'll then need to manually substitute the variable name with its value yourself, i.e. writegif0where you find$DEVNAMEand so on.Command breakdown
ifconfig $DEVNAME create: This command creates the device we'll implement the6in4tunnel on. We can runifconfig $DEVNAMEto make sure a device named$DEVNAME exists. As$DEVNAMEis equivalent togif0we can also runifconfig gif0and we should get the exact same output. Ififconfigcomplains, then thegif0device might not be present...ifconfig gif0 tunnel $LOCAL_IPV4 $HESERVER4END: This command sets theIPv4addresses for each of the tunnel's endpoints. These are needed for the path traversed overIPv4through ourISP's network. We wouldn't know where to send theIPv4datagram containing the encapsulatedIPv6datagram otherwise... If you runifconfig $DEVNAMEagain you'll see a line resemblingtunnel inet $LOCAL_IPV4 --> $HESERVER4END. This shows that the addresses are correctly configured.ifconfig $DEVNAME inet6 $MYCUSTOMADDRESS prefixlen 128: This command adds anIPv6interface to the device at hand. We can also check whether it was correctly configured by runningifconfig $DEVNAME.ifconfig $DEVNAME inet6 $HECLIENT6END $HESERVER6END prefixlen 128: Just like withIPv4, this configures theIPv6addresses for both tunnel endpoints. This shows itself as a line likeinet6 $HECLIENT6END --> $HESERVER6END prefixlen 128in the output ofifconfig $DEVNAME.route -n add -inet6 default $HESERVER6END: This adds a default route to theIPv6routing table (which is, independent of theIPv4routing table) so that all theIPv6traffic is forced through the tunnel interface. We can check whether the rule was instantiated withnetstat -f inet6 -rn. This will show the routing table for theIPv6family. We need to look for a line resemblingdefault $HESERVER6END UGSc $DEVNAME(note some whitespace has been trimmed). having this line means that all the traffic is indeed being routed through the tunnel.Testing it out
You can then use any
IPv6capable tool to check your connectivity. An easy candidate isping6, which acts like good ol'pingexcept it usesIPv6at the network level. Executingping6 www.google.comshould begin showing replies right away.You can also try to check whether the Kame Project site displays a "dancing kame" (i.e. dancing turtle) when you manually introduce the
IPv6address.. If it does, it means yourIPv6tunnel is up and running! Please note that to manually navigate to anIPv6site you need to enclose the address in brackets ([]). The link for theIPv6kame site would then behttp://[2001:200:dff:fff1:216:3eff:feb1:44d7]. You can get this link yourself if you make a DNS lookup for a typeAAAArecord:dig -t AAAA www.kame.net, should you not trust our address 😉.Another thing to keep into account is that even though your tunnel might be up and running, your OS or browser might still be resolving hostnames to
IPv4addresses (i.e. typeADNS records instead ofAAAA). This is something you might need to look into, but if you introduceIPv6addresses manually you should be able to browse theIPv6internet unhindered.Hope the explanation helped 😼!