Skip to content

Instantly share code, notes, and snippets.

@m0n5t3r
Created February 11, 2011 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m0n5t3r/822962 to your computer and use it in GitHub Desktop.
Save m0n5t3r/822962 to your computer and use it in GitHub Desktop.
dynamically set up ipv6 for a 2-router dmz with openwrt
#!/bin/sh
export PATH=/bin:/sbin:/usr/bin:/usr/sbin
radvdump | awk '/Router Advertisement/{router=$NF}; /interface eth0\.2/{good_if=1}; /interface br-lan/{good_if=0}; /prefix/{if(good_if && $1 == "prefix"){print router" "$2}}' | while read router prefix; do
old_prefix=$(uci -q get "radvd.@prefix[0].prefix")
new_prefix=$(echo $prefix | sed -e 's@1::1\?/64$@2::/64@g')
lan_addr=$(echo $prefix | sed -e 's@1::1/64$@2::1/64@g')
new_addr=$(echo $prefix | sed -e 's@1::1/64$@1::2/64@g')
if [ "$old_prefix" != "$new_prefix" ]; then
logger -t '6cfg' "Prefix changed from $old_prefix to $new_prefix, updating"
uci set "radvd.@prefix[0].prefix"="$new_prefix"
uci commit
#ip -6 ro add $prefix dev eth0.2
ip -6 ad sh | awk '/^[0-9]+:/{intf=gensub("(@.*)?:$", "", "g", $2)}; /inet6 2002:/{ system("ip -6 ad del "$2" dev "intf) }'
ip -6 ro flush dev eth0.2
ip -6 ad add $new_addr dev eth0.2
ip -6 ad add $lan_addr dev br-lan
ip -6 ro add 2000::/3 via $router dev eth0.2
/etc/init.d/radvd restart
fi
done
#!/bin/sh /etc/rc.common
START=55
start() {
uci set radvd.@prefix[0].prefix=''
pgrep -f /usr/bin/6cfg >/dev/null || /usr/bin/6cfg &
}
stop() {
killall 6cfg
killall radvdump
}

This allows using openwrt to dynamically set up an ipv6 network that looks like like

internet--<router1>--dmz--<router2>--internal 

I is useful for, say, 6to4 and DHCP-provided IP from the ISP; in order for routing to work scripts on the Internet facing openwrt must be modified to add an explicit route to the internal network.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment