#!/bin/bash
#
# 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-2002 by Peter Bieringer <pb@bieringer.de>
#
#  You will find more information in the IPv6-HowTo for Linux at
#   http://www.bieringer.de/linux/IPv6/
#
# RHL integration assistance by Pekka Savola <pekkas@netcore.fi>
#
# Version 2002-01-06b
#
# 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:
#  DEVICE=<device>
#  For static tunnels
#  IPV6TUNNELIPV4=<ipv4 address of foreign tunnel endpoint>
#  IPV6ADDR=<ipv6address>/<prefixlength> [OPTIONAL: numbered tunnels]
#
# Either from /etc/sysconfig/network or /etc/sysconfig/network-scripts/ifcfg-$1:
#  IPV6_TUNNELMODE=IP|NBMA: mode of tunnel creation [default: IP]
#
#@-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@# 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
#@-INF@#  20010715 :Minor review, triggered by Pekka Savola
#@-INF@#  20010715a:Minor review, triggered by Pekka Savola
#@-INF@#  20011124c:Adapt function names after renaming in libary
#@-INF@#  20020103a:Add a forgotten RHL tag
#@-INF@#  20020106a:Review log text
#@-INF@#  20020106b:Revert log style to 20020106 one

# 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 whether IPv6 should be configured, else stop
[ "${NETWORKING_IPV6}" = "yes" ] || exit 0

if [ ! -f /etc/sysconfig/network-scripts/network-functions-ipv6 ]; then
	# IPv6 setup isn't well												#@-RH7@#
	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

# Test version of /etc/sysconfig/network-scripts/network-functions-ipv6                                                 #@-RH7@#
versioncurrent="`getversion_ipv6_functions`"                                                                            #@-RH7@#
versionneeded="20011124"                                                                                                #@-RH7@#
if [ $versioncurrent -lt $versionneeded ]; then                                                                         #@-RH7@#
	echo $"'/etc/sysconfig/network-scripts/network-functions-ipv6' has version '$versioncurrent', but '$0' needs version '$versionneeded' or higher, so please update"   #@-RH7@#
        exit 1                                                                                                          #@-RH7@#
fi                                                                                                                      #@-RH7@#

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

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

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

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

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