#!/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:
#   route4-ifdown
#
# Description:
#   Verify the kernel is not crashed when IPv4 route is add then it is deleted
#   by the interface down
#
# Setup:
#   See ltp-yyyymmdd/testcases/network/stress/README
#
# Author:
#   Mitsuru Chinen <mitch@jp.ibm.com>
#
# History:
#	Apr 8 2006 - Created (Mitsuru Chinen)
#
#-----------------------------------------------------------------------
# Uncomment line below for debug output.
#trace_logic=${trace_logic:-"set -x"}
$trace_logic

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

# Total number of the test case
TST_TOTAL=2
export TST_TOTAL

# Default of the test case ID and the test case count
TCID=route4-ifdown
TST_COUNT=0
export TCID
export TST_COUNT

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

# The number of times where IPv4 route is add/delete
NS_TIMES=${NS_TIMES:-10000}

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

# Network portion of the IPv4 address
IPV4_NETWORK="10.0.0"

# Netmask of for the tested network
IPV4_NETMASK="255.255.255.0"
IPV4_NETMASK_NUM=24

# Broadcast address of the tested network
IPV4_BROADCAST=${IPV4_NETWORK}.255

# Host portion of the IPv4 address
LHOST_IPV4_HOST="2"	# src
RHOST_IPV4_HOST="1"	# gateway

# The destination network
DST_NETWORK="10.10.10"	# destination network would be 10.10.10.0/24
DST_HOST="5"
DST_PORT="7"


#-----------------------------------------------------------------------
#
# NAME:
#   do_cleanup
#
# DESCRIPTION:
#   Recover the tested interfaces
#
#-----------------------------------------------------------------------
do_cleanup()
{
    # Initialize the interfaces
    initialize_if lhost ${LINK_NUM}
    initialize_if rhost ${LINK_NUM}
}


#-----------------------------------------------------------------------
#
# NAME:
#   do_setup
#
# DESCRIPTION:
#   Make a IPv4 connectivity
#
# SET VALUES:
#   rhost_ipv4addr	- IPv4 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 clean up
    do_cleanup

    # Set IPv4 address to the interfaces of the remote host
    set_ipv4addr rhost ${LINK_NUM} ${IPV4_NETWORK} ${RHOST_IPV4_HOST}
    if [ $? -ne 0 ]; then
	tst_resm TBROK "Failed to add an IPv4 address the remote host"
	exit $TST_TOTAL
    fi
    rhost_ipv4addr="${IPV4_NETWORK}.${RHOST_IPV4_HOST}"

    # Assign IPv4 address to the interface of the local host
    set_ipv4addr lhost ${LINK_NUM} ${IPV4_NETWORK} ${LHOST_IPV4_HOST}
    if [ $? -ne 0 ]; then
	tst_resm TBROK "Failed to assign an IPv4 address at the local host"
	return 1
    fi
    lhost_ipv4addr="${IPV4_NETWORK}.${LHOST_IPV4_HOST}"

    # Get the Interface names
    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

    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

    # Set the variables for destination network
    dst_addr=${DST_NETWORK}.${DST_HOST}
    dst_network=${DST_NETWORK}.0
}


#-----------------------------------------------------------------------
#
# FUNCTION:
#   test_body
#
# DESCRIPTION:
#   main code of the test
#
# Arguments:
#   $1: define the test type
#	1 - route command case
#	ay="${IPV4_NETWORK_PRE}.${link_num}.${RHOST_IPV4_HOST}"
    case $test_type in
	1)
	route add -net $dst_network netmask 255.255.255.0 gw $gatew#
# DESCRIPTION:
#   main code of the test
#
# Arguments:
#   $1: define the test type
#	1 - route command case
#	ay="${IPV4_NETWORK_PRE}.${link_num}.${RHOST_IPV4_HOST}"
    case $test_type in
	1)
	route add -net $dst_network netmask 255.255.255.0 gw $gatew#
# DESCRIPTION:
#   main code of the test
#
# Arguments:
#   $1: define the test type
#	1 - route command case
#	ay="${IPV4_NETWORK_PRE}.${link_num}.${RHOST_IPV4_HOST}"
    case $test_type in
	1)
	route add -net $dst_network netmask 255.255.255.0 gw $gatew#
# DESCRIPTION:
#   main code of the test
#
# Arguments:
#   $1: define the test type
#	1 - route command case
#	ay="${IPV4_NETWORK_PRE}.${link_num}.${RHOST_IPV4_HOST}"
    case $test_type in
	1)
	route add -net $dst_network netmask 255.255.255.0 gw $gatew#
# DESCRIPTION:
#   main code of the test
#
# Arguments:
#   $1: define the test type
#	1 - route command case
#	ay="${IPV4_NETWORK_PRE}.${link_num}.${RHOST_IPV4_HOST}"
    case $test_type in
	1)
	route add -net $dst_network netmask 255.255.255.0 gw $gatew#
# DESCRIPTION:
#   main code of the test
#
# Arguments:
#   $1: define the test type
#	1 - route command case
#	ay="${IPV4_NETWORK_PRE}.${link_num}.${RHOST_IPV4_HOST}"
    case $test_type in
	1)
	route add -net $dst_network netmask 255.255.255.0 gw $gatew#
# DESCRIPTION:
#   main code of the test
#
# Arguments:
#   $1: define the test type
#	1 - route command case
#	ay="${IPV4_NETWORK_PRE}.${link_num}.${RHOST_IPV4_HOST}"
    case $test_type in
	1)
	route add -net $dst_network netmask 255.255.255.0 gw $gatew#
# DESCRIPTION:
#   main code of the test
#
# Arguments:
#   $1: define the test type
#	1 - route command case
#	ay="${IPV4_NETWORK_PRE}.${link_num}.${RHOST_IPV4_HOST}"
    case $test_type in
	1)
	route add -net $dst_network netmask 255.255.255.0 gw $gatew#
# DESCRIPTION:
#   main code of the test
#
# Arguments:
#   $1: define the test type
#	1 - route command case
#	ay="${IPV4_NETWORK_PRE}.${link_num}.${RHOST_IPV4_HOST}"
    case $test_type in
	1)
	route add -net $dst_network netmask 255.255.255.0 gw $gatew#
# DESCRIPTION:
#  