$etcdir="/private/etc";
require "$etcdir/6to4.conf";
use Getopt::Std;
sub do_6to4_setup
{
if (`ifconfig -a | grep fe80: | wc -l` <= 0 or
`ifconfig -a | grep stf | wc -l` <= 0) {
die "$0: It seems your kernel does not support IPv6 or 6to4 (stf).\n";
}
$localadr4 = `ifconfig $ARGV[1] inet | grep inet | grep -v "10.*.*.*"| \
grep -v "172.[^16-31].*.*" | grep -v "192.168.*.*" | \
grep -v "169.254.*.*" | grep -v alias`;
$localadr4 =~ s/^.*inet\s*//;
$localadr4 =~ s/\s.*$//;
chomp($localadr4);
@l4c = split('\.', $localadr4);
$prefix = sprintf("2002:%02x%02x:%02x%02x", @l4c[0..3]);
$localadr6 = sprintf("$prefix:%04x", $v6_net);
if ($peer eq "6to4-anycast") {
$remoteadr4 = "192.88.99.1";
$remoteadr6 = "2002:c058:6301::";
}
else {
chomp($remoteadr4 = `host $peer`);
$remoteadr4 =~ s/^.*address //;
chomp($remoteadr6 = `host -t AAAA $peer`);
$remoteadr6 =~ s/^.*address //;
}
}
sub do_usage
{
print "Usage: $0 \n";
print " start-v6 all | stop-v6 all\n";
print " start-v6 [interface] | stop-v6 [interface]\n";
print " start-stf [interface] | stop-stf\n";
print " start-rtadvd | stop-rtadvd\n";
}
getopts('h');
if ($opt_h) {
do_usage;
exit 0;
}
if ($ARGV[0] eq "start-v6" or $ARGV[0] eq "v6-start") {
if ($ARGV[1] eq "all") {
print "Starting IPv6 on all interfaces.\n";
system "ip6 -a";
}
else {
print "Starting IPv6 on $ARGV[1].\n";
system "ip6 -u $ARGV[1]";
}
}
elsif ($ARGV[0] eq "stop-v6" or $ARGV[0] eq "v6-stop") {
if ($ARGV[1] eq "all") {
print "Stopping IPv6 on all interfaces.\n";
system "ip6 -x";
}
else {
print "Stopping IPv6 on $ARGV[1].\n";
system "ip6 -d $ARGV[1]";
}
}
elsif ($ARGV[0] eq "start-stf" or $ARGV[0] eq "stf-start") {
do_6to4_setup;
print "Starting 6to4 on $ARGV[1].\n";
system "ifconfig stf0 inet6 $localadr6:$hostbits6 prefixlen $v6_prefixlen alias";
system "route add -inet6 default $remoteadr6";
if ($in_if ne "") {
system "ifconfig $in_if inet6 $prefix:$v6_innernet:$hostbits6";
}
}
elsif ($ARGV[0] eq "stop-stf" or $ARGV[0] eq "stf-stop") {
print "Stopping 6to4.\n";
system "ifconfig stf0 down";
$cmd="ifconfig stf0 inet6 " .
"| grep inet6 " .
"| sed -e 's/inet6//' " .
"-e 's/prefix.*//g' " .
"-e 's/^[ ]*//' " .
"-e 's/[ ]*\$//'";
foreach $ip ( split('\s+', `$cmd`)) {
system "ifconfig stf0 inet6 -alias $ip";
}
system "route delete -inet6 default";
}
elsif ($ARGV[0] eq "rtadvd-start" or $ARGV[0] eq "start-rtadvd") {
print "WARNING: Setting up router advertisement should be done with great care\n";
print "because of a number of security issues. You should make sure this is\n";
print "allowed on your network and possibly fine-tune rtadvd.conf.\n";
print "\n";
print "Are you sure you want to start router advertisement (yes/no) ?: ";
while (<STDIN>) {
chomp;
if ($_ eq "yes" or $_ eq "y") {
if ( -f "/var/run/rtadvd.pid" ) {
print "rtadvd already running!\n";
} else {
print "Starting router advertisement.\n";
system "sysctl -w net.inet6.ip6.forwarding=1";
system "sysctl -w net.inet6.ip6.accept_rtadv=0";
shift @ARGV;
system "rtadvd @ARGV";
}
last;
}
elsif ($_ eq "no" or $_ eq "n") {
print "Router advertisement startup aborted.\n";
last;
}
else {
print "Invalid entry! Try again.\n";
print "Are you sure you want to start router advertisement? (yes/no): ";
}
}
}
elsif ($ARGV[0] eq "rtadvd-stop" or $ARGV[0] eq "stop-rtadvd") {
if ( -f "/var/run/rtadvd.pid" ) {
print "Stopping router advertisement.\n";
$pid = `cat /var/run/rtadvd.pid`;
system "kill -TERM $pid";
system "rm -f /var/run/rtadvd.pid";
system "rm -f /var/run/6to4-rtadvd.conf.$pid";
} else {
print "no rtadvd running!\n";
}
}
else {
do_usage;
}