#!/usr/bin/ksh
#
#ident	"@(#)net6set.stf	1.11	05/06/08 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# by Bill.Yan
#
# Script name:  net6set
#########################################################################
# Its function is to set up IPv6 and IPv6 over IPv4 tunnel interfaces on the 
# machine.
#########################################################################
#
#########################################################################
# NOTES ON OPERATION AND ERROR HANDLING:
# This script is called by staf.eng, which is the main test harness.
# Standard output gets recorded in the test module log file by staf.eng.

#
# Variable maxwarn is checked by staf.eng in the "chk_err()" monitoring process,
# by grep'ing the log file of this test module.
# If maxwarn is exceeded, the staf.eng script will kill this test module.
# There are two formats for the error echos to the log files;
#
# 1.   echo "!ERROR: blah blah foo bar";
#      which will terminate the test.  If the test harness detects
#      a string match with "!ERROR", then all testing terminates.
#
# 2.   echo "!WARNING: blah blah foo bar";
#       The above format "!WARNING:" will make the log file entry,
#       but all testing will continue, no termination of tests,
#       UNLESS maxwarnings variable is exceeded.
#
# DANGER!!:  Even if you do not intend to use WARNING messages, variable
# maxwarn *MUST* be defined and the "maxwarnings=" line *MUST* be
# echo'ed to the logfile.  This syntax is mandatory.
#########################################################################


# Variables
maxwarn=80                      # max number of nonfatal warnings to tolerate.
remotemachine=$1			# Remote machine as a client
ipv6tunnelname="ip.tun0"		# IPv6 over IPv4 tunnel name
ipv6addrfile="/tmp/ipv6.addr.dat"	# This file stores IPv6 addresses
					# Configured by this script
multiportfile=/tmp/multiportfile
hostmachine=
#
#Check args
#
if [ -s $multiportfile ]
then
    	if [ $# -ne 2 ]
	then
            	echo "!ERROR: $0 called with bad arguments."
		exit 1
	else
		ipv6tunnelname=`echo $ipv6tunnelname | sed "s/0/$2/g"`
		ipv6addrfile=$ipv6addrfile'_'$2
		hostmachine=`head -$2 $multiportfile | tail -1 | cut -d';' -f1`
		#sleep for a while for multiport set up conflict.
		tmp_val=`expr $2 - 1`
		tmp_val=`expr $tmp_val \* 60`
		sleep $tmp_val
	fi
else
	if [ $# -ne 1 ]
	then
		echo "USAGE: $0 remote_hostname."
		echo "!ERROR: $0 called with bad arguments."
		exit 1
	else
		hostmachine=`hostname`
	fi
fi

#
# IPv4 interface is necessary for IPv6 set up, first find out the IPv4 
# interface ok or not
#
if [ -z `/usr/sbin/ifconfig -a4 | /usr/bin/grep -v lo0 | grep UP`  ]
then
	#no IPv4 interface exit
	echo "!ERROR:	Can't find IPv4 interface."
	exit 1
fi

#
# Check remotemachine connectivity
#
/usr/sbin/ping $remotemachine > /dev/null
if [ $? -ne 0 ]
then
	#No connectivity to $remotemachine
	echo "!ERROR:	No connectivity to $remotemachine."	
	exit 1
fi

#
# Get local machine IPv4 address and the network interface name.
#
lifc=`/usr/sbin/arp -a | grep $hostmachine | head -1 | cut -d' ' -f1`
if [ -z "$lifc" ]
then
	lifc=`/usr/sbin/arp -an | grep $hostmachine | head -1 | cut -d' ' -f1`
fi

laddr=`/usr/sbin/ifconfig $lifc | /usr/bin/grep inet | \
	/usr/bin/cut -d' ' -f2`

#
# Get remote machine IPv4 address and the network interface name.
#
rifc=`/usr/bin/rsh $remotemachine /usr/sbin/arp -a | \
	grep $remotemachine | head -1 | cut -d' ' -f1`
if [ -z "$rifc" ]
then
	rifc=`/usr/bin/rsh $remotemachine /usr/sbin/arp -an | \
		grep $remotemachine | head -1 | cut -d' ' -f1`
fi

raddr=`/usr/bin/rsh $remotemachine /usr/sbin/ifconfig $rifc \
	| /usr/bin/grep inet | /usr/bin/cut -d' ' -f2`

#
# Find out whether there is any IPv6 interface set up already
# on local machine and remote machine. if so, unplumb them
#
#if [ ! -z `/usr/sbin/ifconfig -a6` ]
#then
#	for i in `/usr/sbin/ifconfig -a6 | /usr/bin/grep flags \
#			| /usr/bin/cut -d':' -f1`
#	do
#		/usr/sbin/ifconfig $i inet6 unplumb
#		if [ $? -ne 0 ]
#		then
#			echo "!ERROR:	$i can't be unplumbed."
#			exit 1
#		fi
#	done
#fi
#
#if [ ! -z `/usr/bin/rsh $remotemachine /usr/sbin/ifconfig -a6` ]
#then
#	for i in `/usr/bin/rsh $remotemachine /usr/sbin/ifconfig -a6 \
#			| /usr/bin/grep flags | /usr/bin/cut -d':' -f1`
#	do
#		/usr/bin/rsh $remotemachine /usr/sbin/ifconfig $i inet6 unplumb
#		if [ $? -ne 0 ]
#		then
#			echo "!ERROR:	$i inet6 on $remotemachine can't be unplumbed."
#			exit 1
#		fi
#	done
#fi

#
# Set up Ipv6 and IPv6 over IPv4 tunnel on local machine and remote machine 
#
/usr/sbin/ifconfig lo0 inet6 plumb up
if [ $? -ne 0 ]
then
	echo "!ERROR:	Failed to bring lo0 inet6 up."
	exit 1
fi

/usr/sbin/ifconfig $lifc inet6 unplumb 1>/dev/null 2>&1
/usr/sbin/ifconfig $lifc inet6 plumb up
if [ $? -ne 0 ]
then
	echo "!ERROR:	Failed to bring $lifc inet6 up."
	exit 1
fi

/usr/bin/rsh $remotemachine /usr/sbin/ifconfig lo0 inet6 plumb up
if [ $? -ne 0 ]
then
	echo "!ERROR:	Failed to bring lo0 inet6 up on $remotemachine."
	exit 1
fi

/usr/bin/rsh $remotemachine /usr/sbin/ifconfig $rifc inet6 unplumb 1>/dev/null 2>&1
/usr/bin/rsh $remotemachine /usr/sbin/ifconfig $rifc inet6 plumb up
if [ $? -ne 0 ]
then
	echo "!ERROR:	Failed to bring $rifc inet6 up on $remotemachine."
	exit 1
fi

/usr/sbin/ifconfig $ipv6tunnelname inet6 unplumb 1>/dev/null 2>&1
/usr/sbin/ifconfig $ipv6tunnelname inet6 plumb
if [ $? -ne 0 ]
then
	echo "!ERROR:	Failed to plumb $ipv6tunnelname inet6."
	exit 1
fi

/usr/sbin/ifconfig $ipv6tunnelname inet6 tsrc $laddr \
		tdst $raddr up
if [ $? -ne 0 ]
then
	echo "!ERROR:	Failed to bring $ipv6tunnelname inet6 up."
	exit 1
fi

/usr/bin/rsh $remotemachine /usr/sbin/ifconfig $ipv6tunnelname inet6 unplumb 1>/dev/null 2>&1
/usr/bin/rsh $remotemachine /usr/sbin/ifconfig $ipv6tunnelname inet6 plumb
if [ $? -ne 0 ]
then
	echo "!ERROR:	Failed to plumb $ipv6tunnelname inet6 on $remotemachine."
	exit 1
fi

/usr/bin/rsh $remotemachine /usr/sbin/ifconfig $ipv6tunnelname inet6 tsrc $raddr \
		tdst $laddr up
if [ $? -ne 0 ]
then
	echo "!ERROR:	Failed to bring $ipv6tunnelname inet6 up on $remotemachine."
	exit 1
fi

#
#Bring the interface donw and up 
#

/usr/bin/rsh $remotemachine /usr/sbin/ifconfig $rifc inet6 down
if [ $? -ne 0 ]
then
	echo "!ERROR:	Failed to bring $rifc down inet6 on $remotemachine."
	exit 1
fi

/usr/bin/rsh $remotemachine /usr/sbin/ifconfig $rifc inet6 up
if [ $? -ne 0 ]
then
	echo "!ERROR:	Failed to bring $rifc inet6 up on $remotemachine."
	exit 1
fi

#
# Record the ipv6 addresses on local and remote machine into $ipv6addrfile for the referrenc of 
# other scripts
#
/usr/sbin/ifconfig $lifc inet6 | /usr/bin/grep inet6 | /usr/bin/cut -d' ' -f2 | \
	/usr/bin/cut -d'/' -f1 > $ipv6addrfile
if [ $? -ne 0 ]
then
	echo "!ERROR:	Failed to create $ipv6addrfile."
	exit 1
fi

/usr/bin/rsh $remotemachine /usr/sbin/ifconfig $rifc inet6 | /usr/bin/grep inet6 | \
	/usr/bin/cut -d' ' -f2 | /usr/bin/cut -d'/' -f1 >> $ipv6addrfile
if [ $? -ne 0 ]
then
	echo "!ERROR:	Failed to create $ipv6addrfile."
	exit 1
fi

/usr/sbin/ifconfig $ipv6tunnelname inet6 | /usr/bin/grep inet6 | /usr/bin/cut -d' ' -f2 | \
	/usr/bin/cut -d'/' -f1 >> $ipv6addrfile
if [ $? -ne 0 ]
then
	echo "!ERROR:	Failed to create $ipv6addrfile."
	exit 1
fi

/usr/sbin/ifconfig $ipv6tunnelname inet6 | /usr/bin/grep inet6 | \
	/usr/bin/cut -d' ' -f4 >> $ipv6addrfile
if [ $? -ne 0 ]
then
	echo "!ERROR:	Failed to create $ipv6addrfile."
	exit 1
fi

if [ `/usr/bin/cat $ipv6addrfile | /usr/bin/wc -l ` -ne 4 ]
then
	echo "!ERROR:	$ipv6addrfile is corrupted."
	exit 1
fi

#
#set the destination for ipv4 and ipv6 interfaces,in case they are in same network segment.
#only for multiport certification
#
if [ -s $multiportfile ]
then
	/usr/sbin/ifconfig $lifc inet6 destination `head -2 $ipv6addrfile | tail -1`
	if [ $? -ne 0 ]
	then
		echo "!ERROR: Failed to set destination."
		exit 1
	fi
fi

#
#the following is to set up environment for NFS mount and spray on IPv6
#

#get solaris version
version=`/usr/bin/uname -r|awk -F. '{print $2}'` 
c_version=`/usr/bin/rsh $remotemachine /usr/bin/uname -r|awk -F. '{print $2}'` 

#the following two lines are just to avoid warning message, so no need to exit when error occurs
/usr/bin/pkill in.ndpd
/usr/lib/inet/in.ndpd

if [  $version -le 9  ]
then 
	/etc/rc2.d/S71rpc stop
	if [ $? -ne 0 ]
	then
		echo "!ERROR:    Failed to stop rpc."
		exit 1
	fi
	/etc/rc2.d/S71rpc start
	if [ $? -ne 0 ]
	then
		echo "!ERROR:    Failed to start rpc."
		exit 1
	fi
else
	/usr/sbin/svcadm restart svc:/network/rpc/bind:default
	if [ $? -ne 0 ]
	then
		echo "!ERROR:    Failed to restart rpc."
		exit 1
	fi
	sleep 3
fi
if [  $c_version -le 9  ]
then
	/usr/bin/rsh $remotemachine /etc/rc2.d/S71rpc stop
	if [ $? -ne 0 ]
	then
		echo "!ERROR:    Failed to stop rpc on $remotemachine."
		exit 1
	fi
	/usr/bin/rsh $remotemachine /etc/rc2.d/S71rpc start
	if [ $? -ne 0 ]
	then
		echo "!ERROR:    Failed to start rpc on $remotemachine."
		exit 1
	fi
else
	/usr/bin/rsh $remotemachine /usr/sbin/svcadm restart svc:/network/rpc/bind:default
	if [ $? -ne 0 ]
	then
		echo "!ERROR:    Failed to restart rpc."
		exit 1
	fi
	sleep 3
fi


if [  $version -le 9  ]
then
	/etc/init.d/nfs.server stop
	if [ $? -ne 0 ]
	then
		echo "!ERROR:    Failed to stop nfs.server."
		exit 1
	fi
	/etc/init.d/nfs.server start
	if [ $? -ne 0 ]
	then
		echo "!ERROR:    Failed to start nfs.server."
		exit 1
	fi
else
	/usr/sbin/svcadm restart svc:/network/nfs/client:default
	if [ $? -ne 0 ]
	then
		echo "!ERROR:    Failed to restart nfs.client."
		exit 1
	fi
	sleep 3
	/usr/sbin/svcadm restart svc:/network/nfs/server:default
	if [ $? -ne 0 ]
	then
		echo "!ERROR:    Failed to restart nfs.server."
		exit 1
	fi
	sleep 3
fi
if [  $c_version -le 9  ]
then
	/usr/bin/rsh $remotemachine /etc/init.d/nfs.server stop
	if [ $? -ne 0 ]
	then
		echo "!ERROR:    Failed to stop nfs.server on $remotemachine."
		exit 1
	fi
	/usr/bin/rsh $remotemachine /etc/init.d/nfs.server start
	if [ $? -ne 0 ]
	then
		echo "!ERROR:    Failed to start nfs.server on $remotemachine."
		exit 1
	fi
	/usr/bin/rsh $remotemachine "/usr/bin/pkill rpc.sprayd;/usr/lib/netsvc/spray/rpc.sprayd"
	if [ $? -ne 0 ]
	then
		echo "!ERROR:    Failed to start rpc.sprayd on $remotemachine."
		exit 1
	fi
else
	/usr/bin/rsh $remotemachine /usr/sbin/svcadm restart svc:/network/nfs/client:default
	if [ $? -ne 0 ]
	then
		echo "!ERROR:    Failed to restart nfs.client on $remotemachine."
		exit 1
	fi
	sleep 3
	/usr/bin/rsh $remotemachine /usr/sbin/svcadm restart svc:/network/nfs/server:default
	if [ $? -ne 0 ]
	then
		echo "!ERROR:    Failed to restart nfs.server on $remotemachine."
		exit 1
	fi
	sleep 3
	/usr/bin/rsh $remotemachine /usr/sbin/svcadm clear svc:/network/rpc/spray:default >/dev/null 2>/dev/null
	/usr/bin/rsh $remotemachine /usr/sbin/svcadm enable svc:/network/rpc/spray:default
	if [ $? -ne 0 ]
	then
		echo "!ERROR:    Failed to enable spray:default on $remotemachine."
		exit 1
	fi
	sleep 3
	/usr/bin/rsh $remotemachine "/usr/bin/pkill rpc.sprayd;/usr/lib/netsvc/spray/rpc.sprayd"
	if [ $? -ne 0 ]
	then
		echo "!ERROR:    Failed to start rpc.sprayd on $remotemachine."
		exit 1
	fi
fi


# SET LOGFILE FLAG FOR MAX WARNING COUNT
# This is checked by staf.eng, and if exceeded, staf.eng will kill this module.
echo "maxwarnings=${maxwarn}"
echo "!PASS: Completed IPv6 setup successfully"
