Skip to content

Instantly share code, notes, and snippets.

@vi
Created March 19, 2014 01:09
Show Gist options
  • Save vi/9633572 to your computer and use it in GitHub Desktop.
Save vi/9633572 to your computer and use it in GitHub Desktop.
A script to set up IPv6 router advertisment proxy for single peer
#!/bin/bash
set -e
if [ -z "$2" ]; then
echo "Script to set up router advertising proxy for one peer"
echo "Usage: setup_ipv6_hacky_router UPLINK DOWNLIK"
echo "Example: setup_ipv6_hacky_router eth0 wlan0"
exit 1
fi
UPLINK=$1
DOWNLINK=$2
echo "Trying to forward stateless-configured IPv6 from $UPLINK to $DOWNLINK"
echo "0. Checking prerequisites"
tcpdump -h 2>&1 | grep -q version
radvdump -h 2>&1 | grep -q usage
radvd -v 2>&1 | grep -q Version;
npd6 --version 2> /dev/null > /dev/null
echo "1. Turning on forwarding on $UPLINK and $DOWNLINK"
echo 2 > /proc/sys/net/ipv6/conf/$UPLINK/accept_ra
echo 1 > /proc/sys/net/ipv6/conf/$UPLINK/forwarding
echo 1 > /proc/sys/net/ipv6/conf/$DOWNLINK/forwarding
T=/tmp/hackyipv6
mkdir -p "$T"
cd "$T"
echo "2. Getting radvd configuration from $UPLINK and fixing it for $DOWNLINK"
# FIXME: exits on the second dump from radvdump, not the first
radvdump | perl -ne "
s@interface $UPLINK@interface $DOWNLINK@;
s@^.*RtrAdvInterval.*\$@ MaxRtrAdvInterval 5;@;
print;
exit 0 if /End of interface definition/;
" > radvd.conf
echo "3. Starting radvd"
radvd -p radvd.pid -n -C radvd.conf -m stderr &
RADVPID=$!
trap "kill $RADVPID" EXIT
echo "4. Determining peer's IPv6"
# 02:39:04.127632 IP6 2001:470:7bd6:e105:88af:83ff:fed9:c835 > 2600::: ICMP6, echo request, seq 1, length 64
PEER="$(tcpdump -i $DOWNLINK -c 1 -n 'ip6 and not net fe00::/7' 2> /dev/null | perl -ne '/IP6 ([^ .]{4,})[ .]/ and print "$1\n"')"
PREFIX=${PEER/:*/}:
echo "PEER=$PEER PREFIX=$PREFIX"
echo "5. Adding a route for the peer"
ip -6 route add $PEER/128 dev $DOWNLINK || true
echo "6. Starting npd for the peer"
cat > npd6.conf <<EOF
prefix=$PREFIX
interface = $UPLINK
listtype = white
addrlist = $PEER
collectTargets = 100
linkOption = false
ignoreLocal = true
routerNA = true
maxHops = 255
pollErrorLimit = 20
EOF
npd6 -f -c $T/npd6.conf -l - &
NPD6PID=$!
trap "kill $RADVPID $NPD6PID" EXIT
sleep 2
echo "Finished. Sleeping and terminating services on exit"
echo "(Note: $UPLINK's and $DOWNLINK's forwarding mode and a route to $PEER will not be cleaned up)"
sleep infinity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment