#!/bin/sh

################################################################################
##                                                                            ##
## Copyright (c) International Business Machines  Corp., 2006                 ##
##                                                                            ##
## This program is free software;  you can redistribute it and#or modify      ##
## it under the terms of the GNU General Public License as published by       ##
## the Free Software Foundation; either version 2 of the License, or          ##
## (at your option) any later version.                                        ##
##                                                                            ##
## This program is distributed in the hope that it will be useful, but        ##
## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
## for more details.                                                          ##
##                                                                            ##
## You should have received a copy of the GNU General Public License          ##
## along with this program;  if not, write to the Free Software               ##
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA    ##
##                                                                            ##
##                                                                            ##
################################################################################
#
# File:
#   route6-redirect
#
# Description:
#   Verify the kernel is not crashed when the route is modified by
#   ICMP Redirects frequently
#
# Setup:
#   See ltp-yyyymmdd/testcases/network/stress/README
#
# Author:
#   Mitsuru Chinen <mitch@jp.ibm.com>
#
# History:
#	Apr 07 2006 - Created (Mitsuru Chinen)
#
#-----------------------------------------------------------------------
# Uncomment line below for debug output.
#trace_logic=${trace_logic:-"set -x"}
$trace_logic

# The test case ID, the test case count and the total number of test case
TCID=route6-redirect01
TST_TOTAL=1
TST_COUNT=1
export TCID
export TST_COUNT
export TST_TOTAL

# Test description
tst_resm TINFO "Verify the kernel is not crashed when the IPv6 route is modified by  ICMP Redirects frequently"

# Make sure the value of LTPROOT
LTPROOT=${LTPROOT:-`(cd ../../../.. ; pwd)`}
export LTPROOT

# Check the environmanet variable
. check_envval || exit $TST_TOTAL

# The number of times where route is changed
NS_TIMES=${NS_TIMES:-10000}

# The number of the test link where tests run
LINK_NUM=${LINK_NUM:-0}

# Network portion of the IPv6 address
IPV6_NETWORK="fec0:1:1:1"

# Netmask of for the tested network
IPV6_NETMASK_NUM=64

# Host portion of the IPv6 address
LHOST_IPV6_HOST=":1"	# src
RHOST_IPV6_HOST=":2"	# gateway

# The destination network
DST_NETWORK="fec0:100:100:100"   # destination network
DST_HOST=":5"
DST_PORT="7"


#-----------------------------------------------------------------------
#
# NAME:
#   do_cleanup
#
# DESCRIPTION:
#   Recover the tested interfaces
#
#-----------------------------------------------------------------------
do_cleanup()
{
    # Kill the redirector utility
    $LTP_RSH $RHOST killall -SIGHUP ns-icmp_redirector >/dev/null 2>&1

    # Initialize the interfaces
    initialize_if lhost ${LINK_NUM}
    initialize_if rhost ${LINK_NUM}
}


#-----------------------------------------------------------------------
#
# NAME:
#   do_setup
#
# DESCRIPTION:
#   Set the initial route and start icmp redirect on the remote host
#
# SET VALUES:
#   rhost_ipv6addr      - IPv6 Address of the remote host
#   lhost_ifname        - Interface name of the local host
#   rhost_ifname        - Interface name of the remote host
#
#-----------------------------------------------------------------------
do_setup()
{
    # Make sure to cleanup the test environment
    do_cleanup
    
    # Get the Interface name of local host
    lhost_ifname=`get_ifname lhost ${LINK_NUM}`
    if [ $? -ne 0 ]; then
	tst_resm TBROK "Failed to get the interface name at the local host"
	exit $TST_TOTAL
    fi

    # Get the Interface name of remote host
    rhost_ifname=`get_ifname rhost ${LINK_NUM}`
    if [ $? -ne 0 ]; then
	tst_resm TBROK "Failed to get the interface name at the remote host"
	exit $TST_TOTAL
    fi

    # Remove the link-local address of the remote host
    sleep 5
    $LTP_RSH $RHOST "ip addr flush dev $rhost_ifname" > /dev/null

    # Assign IPv6 address to the interface of the local host
    add_ipv6addr lhost ${LINK_NUM} ${IPV6_NETWORK} ${LHOST_IPV6_HOST}
    if [ $? -ne 0 ]; then
	tst_resm TBROK "Failed to assign an IPv6 address at the local host"
	return 1
    fi
    
    # Add route to the initial gateway
    route -A inet6 add ${DST_NETWORK}::/64 gw fe80:${RHOST_IPV6_HOST} dev $lhost_ifname

    # Make sure the sysctl value is set for accepting the redirect
    sysctl -w net.ipv6.conf.${lhost_ifname}.accept_redirects=1 >/dev/null

    # Run the redirector utility at the remote host
    ret=`$LTP_RSH $RHOST "${LTPROOT}/testcases/bin/ns-icmp_redirector -I $rhost_ifname -b ; "'echo $?'`
    if [ $ret -ne 0 ]; then
	tst_resm TBROK "Failed to run icmp redirector at the remote host"
	exit $TST_TOTAL
    fi
}


#-----------------------------------------------------------------------
#
# FUNCTION:
#   test_body
#
# DESCRIPTION:
#   main code of the test
#
# Arguments:
#   None
#
#-----------------------------------------------------------------------
test_body()
{
    # Loop for changing the route
    cnt=0
    while [ $cnt -lt $NS_TIMES ]; do
	ns-udpsender -f 6 -D ${DST_NETWORK}:${DST_HOST} -p $DST_PORT -o -s 8
	if [ $? -ne 0 ]; then
	    tst_resm TBROK "Failed to run udp packet sender"
	    return 1
	fi
	cnt=`expr $cnt + 1`
    done

    tst_resm TPASS "Test is finished correctly."
    return 0
}
    

#-----------------------------------------------------------------------
#
# Main
#
# Exit Value:
#   The number of the failure
#
#-----------------------------------------------------------------------
RC=0
do_setup
test_body || RC=`expr $RC + 1`
do_cleanup

exit $RC
