#!/bin/sh
#
# ifdown-ipv6
#
#@-INF@# RedHat 6.2 + 7.x
#@-INF@#
#@-INF@# directory: /etc/sysconfig/network-scripts
#@-INF@#
#@-INF@# permissions: 755
#@-INF@#
#@-INF@# description: Brings down IPv6 for an interface
#
#@+RH7@# Taken from:
# (P) & (C) 2000-2001 by Peter Bieringer <pb@bieringer.de>
#
# RHL integration assistance by Pekka Savola <pekkas@netcore.fi>
#
# Version 2001-07-15
#
# 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:
#  IPV6INIT=yes|no: controls IPv6 configuration for this interface
#
# Optional for 6to4 tunneling:
#  IPV6TO4_RELAY=<ipv4address>: IPv4 address of the remote 6to4 relay
#  IPV6TO4_ROUTING="eth0-:f101::0/64 eth1-:f102::0/64": information to setup local subnetting
#  IPV6TO4_CONTROL_RADVD=yes|no: controls radvd triggering [optional]
#  IPV6TO4_RADVD_PIDFILE=file: PID file of radvd for sending signals, default is "/var/run/radvd/radvd.pid" [optional]
#
#  Requirements for 6to4 if using radvd:
#   radvd-0.6.2p3 or newer supporting option "Base6to4Interface"
#
#@-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@#  20000703 :initial for a new layout
#@-INF@#  20000704 :add test for static route file
#@-INF@#  20000723 :fix bug in static route selection
#@-INF@#  20000731 :fix misnamed function 'forwarding-ipv6-route' -> 'forwarding-ipv6'
#@-INF@#  20000816 :fix because "network-functions-ipv6" function names are changed
#@-INF@#  20001125 :explicitly GPL'ed
#@-INF@#  20001130 :remove not needed copyright reference to RedHat
#@-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@#            Enable support for IPV6ADDR_SECONDARIES (a list of IPv6 addresses)
#@-INF@#             remove IPV6ADDR_0 + IPV6ADDR_1 support 
#@-INF@#             prefixlength can be directly specified on IPv6 address
#@-INF@#  20010203 :Remove obsolete tunnel entries, add cleanup call at the end
#@-INF@#  20010208 :Remove backward compatibility for extra prefix length
#@-INF@#  20010304 :Add 6to4 setup support
#@-INF@#  20010309 :Review 6to4 setup support, takes IPv4 address from device first
#@-INF@#  20010310 :Cleanup for RH7 (line tagging)
#@-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@#  20010315 :Shutdown 6to4 config also regardless (take data from ifconfig and route)
#@-INF@#  20010418 :Remove IPV6TO4_RELAY_DEFAULT usage (not really used)
#@-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 :Add radvd control code from ip-up.6to4
#@-INF@#  20010501a: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@#  20010519 :Remove setting of *.router_solicitations in proc
#@-INF@#  20010520b:Cosmetic fixes
#@-INF@#  20010522 :Tagging fixes
#@-INF@#  20010522d:Cosmetic fixes
#@-INF@#  20010614 :Delete old 6to4 routes, even if IPV6TO4_RELAY was changed
#@-INF@#  20010614a:Add support IPV6TO4_RELAY can be also a IPv6to4 address (not working at the moment because of missing kernel implementation)
#@-INF@#  20010707 :Cosmetic fix
#@-INF@#  20010715 :Minor review, triggered by Pekka Savola

# 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

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

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

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

# Test version of /etc/sysconfig/network-scripts/network-functions-ipv6							#@-RH7@#
versioncurrent="`getversion_ipv6_functions`"										#@-RH7@#
versionneeded="20010317"												#@-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@#

# Switch some sysctls to secure mode
sysctl -w net.ipv6.conf.$DEVICE.forwarding=0 >/dev/null
sysctl -w net.ipv6.conf.$DEVICE.accept_ra=0 >/dev/null
sysctl -w net.ipv6.conf.$DEVICE.accept_redirects=0 >/dev/null

# Shutdown of 6to4, if configured
valid6to4config="yes"
if [ -z "$IPV6TO4_RELAY" ]; then
	valid6to4config="no"
fi
if [ "$valid6to4config" = "yes" ]; then
	if [ "$IPV6TO4_CONTROL_RADVD" = "yes" ]; then
		# stop RADVD from distributing no longer usable 6to4 prefixes
		if [ -z "$IPV6TO4_RADVD_PIDFILE" ]; then
			IPV6TO4_RADVD_PIDFILE="/var/run/radvd/radvd.pid"
		fi
		# Send SIGHUP to radvd
		if [ -f "$IPV6TO4_RADVD_PIDFILE" ]; then
			pid="`cat $IPV6TO4_RADVD_PIDFILE`"
			if [ ! -z "$pid" ]; then
				# echo $"Trigger RADVD for stopping distribution of IPv6to4 prefixes"			#@-RH7@#
				#  still waiting for feature enabling: stopping distribution of prefixes in RADVD....
				# kill -SOMETHING $pid
				false
			else
				echo $"RADVD not running, skip trigger for stopping distribution of IPv6to4 prefixes"	#@-RH7@#
				false
			fi
		fi
	fi

	if [ ! -z "$IPV6TO4_ROUTING" ]; then
		# Delete routes to local networks
		for devsuf in $IPV6TO4_ROUTING; do
			dev="`echo $devsuf | awk -F- '{ print $1 }'`"
			ifdown_ipv6_route_all $dev ::
		done
	fi

	# Detect type of address, whether it is IPv4 or IPv6
	if testipv6_valid $IPV6TO4_RELAY; then
		# IPv6 address is a 6to4										#@-RH7@#
		relay6to4type="ipv6"
	fi

	# Delete all static IPv6to4 routes
	if [ "$relay6to4type" = "ipv6" ]; then	
		ifdown_ipv6_route_all sit0 $IPV6TO4_RELAY
	else
		ifdown_ipv6_route_all sit0 ::$IPV6TO4_RELAY
	fi

	# Delete old 6to4 routes, even if IPV6TO4_RELAY was changed
	if [ -f /etc/sysconfig/static-routes-ipv6 ]; then
		grep -w "^sit0" /etc/sysconfig/static-routes-ipv6 | while read device network dummy; do
			if [ "$EXISTS_ipv6calc" = "yes" ]; then
				# Convert given network to compressed one
				network="`ipv6calc  --addr_to_compressed $network`"
			fi
			LC_ALL=C route -A inet6 | grep "^$network" | while read destination nexthop flags metric ref use iface dummy; do
				if ! [ "$device" = "$iface" -a "$network" = "$destination" ]; then
					continue
				fi
				# Look for routes to a compatible IPv4 address, delete them
				if echo $nexthop | egrep -q '^::[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$'; then
					ifdown_ipv6_route $destination $nexthop $iface
				fi
				# Look for routes to a IPv6to4 address, delete them
				if echo $nexthop | egrep -q '^2002::'; then
					ifdown_ipv6_route $destination $nexthop $iface
				fi
			done
		done
	fi

	# Delete 6to4 route
	ifdown_ipv6_route 2002::/16 :: sit0

	# Delete all configured 6to4 address
	ifdown_ipv6to4_all sit0 
fi

# Delete all current configured IPv6 addresses on this interface 
ifdown_ipv6_real_all $DEVICE
