#!/bin/sh
#
# ifdown-sit
#
#@-INF@# RedHat 6.2 + 7.x
#@-INF@#
#@-INF@# directory: /etc/sysconfig/network-scripts
#@-INF@#
#@-INF@# permissions: 755
#@-INF@#
#@-INF@# description: Brings down IPv6 for tunnel interface (sitx)
#
#@+RH7@# Taken from:
# (P) & (C) 2000-2001 by Peter Bieringer <pb@bieringer.de>
#
# RHL integration assistance by Pekka Savola <pekkas@netcore.fi>
#
# Version 2001-05-22d
#
# Uses following information from /etc/sysconfig/network:
#  NETWORKING_IPV6=yes|no: controls IPv6 initialization (global setting)
#
# Uses following information from "/etc/sysconfig/network-scripts/ifcfg-$1":
#  For static tunnels
#  IPV6TUNNELIPV4="<ipv4 address of foreign tunnel endpoint>"
#  IPV6ADDR=<ipv6address>/<prefixlength> [OPTIONAL: numbered tunnels]
#
#@-INF@#  This script is now explicitly GPL'ed (20001125, Peter Bieringer)
#@-INF@#   http://www.gnu.org/copyleft/gpl.html
#@-INF@#
#@-INF@#  Suggestions, comments and improvements are welcome!
#@-INF@#
#@-INF@#  You will find more information in the IPv6-HowTo for Linux at
#@-INF@#   http://www.bieringer.de/linux/IPv6/
#@-INF@#
#@-INF@# Changes to:
#@-INF@#  20000704: initial for a new layout
#@-INF@#  20000722: add test for static route file
#@-INF@#  20000723: handle additional static routes for tunnel device in a right way
#@-INF@#  20000731: fix misnamed function 'forwarding-ipv6-route' -> 'forwarding-ipv6'
#@-INF@#  20000816: fix because "network-functions-ipv6" function names are changed
#@-INF@#  20001004: forget one to fix...
#@-INF@#  20001125: explicitly GPL'ed
#@-INF@#  20001130: remove not needed copyright reference to RedHat
#@-INF@#  20010201: add support for sit0 (automatic tunneling only) support
#@-INF@#             you have to use functions-ipv6 from version 20010201 or newer
#@-INF@#  20010202: Backpatch changes done in Rawhide/initscripts-5.60-1.src.rpm by others
#@-INF@#            Tag several lines for easier stripping of unnecessary lines    
#@-INF@#            Move forward switching to "ifdown_ipv6_autotunnel"
#@-INF@#  20010203: IPV6TUNNELROUTE is no longer used, all routes are now taken from
#@-INF@#             /etc/sysconfig/static-routes-ipv6
#@-INF@#  20010206: Add support for numbered tunnels
#@-INF@#  20010208: Remove backward compatibility for extra prefix length
#@-INF@#  20010303: Add ifdown_ipv6_tunnel_all at the end to cleanup all IPv6 tunnel configuration
#@-INF@#             on specified interface and shut down sit0, if no longer used
#@-INF@#            Delete first addresses on sit, cleanup route afterwards
#@-INF@#  20010309: Add support for new tunnel setup mode
#@-INF@#  20010310a:Shutdown IPv6 regardless of switches (to make sure the config is removed)
#@-INF@#  20010310b:Shutdown IPv6 really regardless (take data from ifconfig and route)
#@-INF@#  20010313 :Set "IP" as default tunnel mode
#@-INF@#  20010417 :Cleanup of historic code
#@-INF@#  20010429 :Unsuccessful "test_ipv6" skips IPv6 initialization
#@-INF@#  20010430 :Minor review
#@-INF@#  20010430a:Review "test_ipv6", use option "testonly", exit code set to 0
#@-INF@#  20010501 :Adapt forwarding control to kernel behavior
#@-INF@#  20010505 :Untag some lines on "network-functions-ipv6" exists check
#@-INF@#            Add some sysctls
#@-INF@#  20010507 :major cosmetic review (change tabsize to 8 and cleanups)
#@-INF@#  20010520 :Routes are flushed now using "ip"
#@-INF@#  20010522d:Cosmetic fixes

# Filter tags (for stripping, empty lines following if all is stripped) 					#@-INF@#
#  #@-DEB@#  : Additional debug code
#  #@-INF@#  : Additional information
#  #@-RH7@#  : Not necessary for RedHat 7.x
#  #@+RH7@#  : Necessary for RedHat 7.x

# Get global network configuration
[ -f /etc/sysconfig/network ] || exit 1											#@-RH7@#
. /etc/sysconfig/network 

# Source IPv4 helper functions
cd /etc/sysconfig/network-scripts
[ -f network-functions ] || exit 1											#@-RH7@#
. network-functions 

CONFIG=$1
[ -f "$CONFIG" ] || CONFIG=ifcfg-$CONFIG
source_config                                                                   

# Test if IPv6 is up
if [ ! "${NETWORKING_IPV6}" = "yes" ]; then
        # Global IPv6 switch not enabled, end now
        exit 0
fi

if [ ! -f /etc/sysconfig/network-scripts/network-functions-ipv6 ]; then
	# IPv6 setup isn't well
	echo $"You have enabled IPv6 in '/etc/sysconfig/network'"							#@-RH7@#
	echo $" by setting 'NETWORKING_IPV6' to 'yes', but"								#@-RH7@#
	echo $" file '/etc/sysconfig/network-scripts/network-functions-ipv6'"						#@-RH7@#
	echo $" is missing!"												#@-RH7@#
	exit 1
fi

# Source IPv6 helper functions
. /etc/sysconfig/network-scripts/network-functions-ipv6

# IPv6 test, no module loaded, exit if system is not IPv6-ready
test_ipv6 testonly || exit 0

# Delete IPv6-in-IPv4 tunnel(s)
if [ "$DEVICE" = "sit0" ]; then
	# Ok, bring down automatic tunneling										#@-RH7@# 
	ifdown_ipv6_autotunnel
	# Noting more to do...												#@-RH7@#
elif [ ! -z "$IPV6TUNNELIPV4" ]; then
	if [ "$IPV6_TUNNELMODE" = "NBMA" ]; then
        	if [ ! -z "$IPV6ADDR" ]; then
			# Numbered tunnel
			ifdown_ipv6_real sit0 $IPV6ADDR
		fi

	    	# Cleanup all IPv6 tunnel configuration on specified interface and shut down sit0, if no longer used
		ifdown_ipv6_tunnel_all $DEVICE $IPV6TUNNELIPV4

        elif [ -z "$IPV6_TUNNELMODE" -o "$IPV6_TUNNELMODE" = "IP" ]; then
		# Delete all IPv6 routes and addresses
		ifdown_ipv6_real_all $DEVICE 

		# Shut down tunnel device
		ifdown_ipv6_tunneldev $DEVICE 
        else
		echo $"Tunnel creation mode '$IPV6_TUNNELMODE' not supported - skip!"
        fi
fi
