Skip to content

Instantly share code, notes, and snippets.

@updateing
Last active February 14, 2019 15:20
Show Gist options
  • Save updateing/e14df3a992b6e125c02c to your computer and use it in GitHub Desktop.
Save updateing/e14df3a992b6e125c02c to your computer and use it in GitHub Desktop.
Automatically set up 6to4 tunnel on optical fiber modems
#!/bin/sh
#
# Use PPP interface to set up a 6to4 tunnel
#
# Author: Hamster Tian <haotia@gmail.com>
# Date: 2016/01/26
#
PPP_IFACE=ppp0
LAN_IFACE=br0
RADVD_TEMPLATE=/flash/radvd.conf.template
RADVD_CONF_OUT=/flash/radvd.conf
RADVD_BIN=radvd
# Overtrimmed shell does not support functions.
# function del_6to4_tunnel {
# ip -6 route flush dev tun6to4
# ip link set dev tun6to4 down
# ip tunnel del tun6to4
# }
# IP -> Obtain IP information from PPP interface
# -> Select the first "inet" line
# -> Trim the spaces
# -> Extract IPv4 address from the line
# IP_DELIM -> Replace dot with space to use with printf
IP=`ip addr show $PPP_IFACE | grep inet -m 1 | sed 's/^ *//g' | cut -d " " -f 2`
IP_DELIM=`echo $IP | sed 's/\./ /g'`
# Calculate the prefixes and addresses
IPv6_PREFIX=`printf 2002:%02x%02x:%02x%02x $IP_DELIM`
LAN_IPv6_ADDR=${IPv6_PREFIX}:1::1/64
TUN_IPv6_ADDR=${IPv6_PREFIX}::1/16
echo $LAN_IPv6_ADDR,$TUN_IPv6_ADDR,$IP,$IP_DELIM
# Remove existing global IPv6 addresses from LAN
ip -6 addr flush scope global dev $LAN_IFACE
# Add new address to LAN
ip -6 addr add $LAN_IPv6_ADDR dev $LAN_IFACE
# Set up the tunnel
ip tunnel add tun6to4 mode sit ttl 64 remote any local $IP
ip link set dev tun6to4 up
ip -6 addr add $TUN_IPv6_ADDR dev tun6to4
# Set up default route
ip -6 route add default via ::192.88.99.1 dev tun6to4
# Set up radvd, \\=\, /=/ in script
RADVD_PREFIX=`echo $LAN_IPv6_ADDR | sed 's/\\//\\\\\\//g'`
sed -e "s/!LAN_IFACE!/$LAN_IFACE/g" -e "s/!LAN_PREFIX!/$RADVD_PREFIX/g" $RADVD_TEMPLATE > $RADVD_CONF_OUT
killall radvd
$RADVD_BIN -C $RADVD_CONF_OUT
interface !LAN_IFACE!
{
AdvSendAdvert on;
AdvManagedFlag off;
MinRtrAdvInterval 3;
MaxRtrAdvInterval 10;
prefix !LAN_PREFIX!
{
AdvOnLink on;
AdvAutonomous on;
AdvRouterAddr on;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment