#!/bin/bash
#
# ifup-ipv6
#
#@-INF@# RedHat 6.2 + 7.x
#@-INF@#
#@-INF@# directory: /etc/sysconfig/network-scripts
#@-INF@#
#@-INF@# permissions: 755
#@-INF@#
#@-INF@# description: Brings up IPv6 for an interface
#
#@+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":
#  IPV6INIT=yes|no: controls IPv6 configuration for this interface
#  IPV6ADDR=<ipv6address>/<prefixlength>: specify primary static IPv6 address here
#  IPV6ADDR_SECONDARIES="<list of ipv6 addresses>" [optional]
#  IPV6_ROUTER=yes|no: controls IPv6 autoconfiguration (no: multi-homed interface without routing)
#  IPV6_AUTOCONF=yes|no: controls IPv6 autoconfiguration
#   defaults:
#    IPV6FORWARDING=yes: IPV6_AUTOCONF=no, IPV6_ROUTER=yes
#    IPV6FORWARDING=no: IPV6_AUTOCONF=yes
#  IPV6_MTU=<MTU for IPv6>: controls IPv6 MTU for this link [optional]
#
# Optional for 6to4 tunneling:
#  IPV6TO4INIT=yes|no: controls 6to4 tunneling setup
#  IPV6TO4_RELAY=<ipv4address>: IPv4 address of the remote 6to4 relay [default: 192.88.99.1]
#  IPV6TO4_IPV4ADDR=<ipv6address>: overwrite local IPv4 address [optional]
#  IPV6TO4_ROUTING="eth0-:f101::0/64 eth1-:f102::0/64": information to setup local subnetting
#  IPV6TO4_SETUP_METHOD=sit0|tun6to4: generic tunnel or dedicated tunnel usage (default)
#
# Optional for 6to4 tunneling or ppp links to trigger radvd:
#  IPV6_CONTROL_RADVD=yes|no: controls radvd triggering [optional]
#  IPV6_RADVD_PIDFILE=<file>: PID file of radvd for sending signals, default is "/var/run/radvd/radvd.pid" [optional]
#
#  Required version of radvd to use 6to4 prefix recalculation
#   0.6.2p3 or newer supporting option "Base6to4Interface"
#  Required version of radvd to use dynamic ppp links
#   0.7.0 + fixes or newer 
#
#@-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@#  20000703 :initial for a new layout
#@-INF@#  20000704 :review
#@-INF@#  20000722 :test for static route file
#@-INF@#  20000723 :fix bug in static route selection
#@-INF@#  20000724 :add missing forwarding for "all", enabled only if "all" & "perdevice"
#@-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 configuration
#@-INF@#  20010208 :Remove backward compatibility for extra prefix length
#@-INF@#  20010303 :Add test_ipv6 at beginning to make sure that the
#@-INF@#             ipv6 module is loaded regardless of the existence of a
#@-INF@#             manual IPv6 address
#@-INF@#  20010304 :Add 6to4 setup support
#@-INF@#  20010309 :Review 6to4 setup support, take IPv4 address from device first
#@-INF@#  20010310 :Cleanup for RH7 (line tagging)
#@-INF@#  20010312 :Cosmetic fixes
#@-INF@#  20010417 :Modify forwarding enable part to a little bit more secure, spelling fixes
#@-INF@#  20010418 :Remove IPV6TO4_RELAY_DEFAULT usage (not really used)
#@-INF@#  20010428 :Unsuccessful "test_ipv6" skips IPv6 initialization
#@-INF@#  20010429 :Fix spelling, minor reviews
#@-INF@#  20010430 :Minor reviews
#@-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@#            Change usage of "IPV6FORWARDING", must be located now in "/etc/sysconfig/network"
#@-INF@#            In addition with the new hook in "/etc/[rc.d/]init.d/network"
#@-INF@#  20010505a:Cosmetic fixes 
#@-INF@#  20010507 :Major cosmetic review (change tabsize to 8 and cleanups)
#@-INF@#  20010511 :Do not enable global IPv6 forwarding, if yes but not set: display warning
#@-INF@#  20010519 :Remove setting of *.router_solicitations in proc 
#@-INF@#  20010520b:Cosmetic fixes
#@-INF@#  20010522d:Change AUTOCONF behavior (now also possible, if IPV6FORWARDING=yes)
#@-INF@#  20010614a:Add support IPV6TO4_RELAY can be also a IPv6to4 address (not working at the moment because of missing kernel implementation)
#@-INF@#  20010707 :6to4 remove additional route entry, no longer necessary because of prefix length change in network-functions-ipv6
#@-INF@#  20010715 :Minor review, triggered by Pekka Savola
#@-INF@#  20010715a:Minor review, triggered by Pekka Savola
#@-INF@#  20010716 :Add option IPV6_MTU
#@-INF@#  20010717 :Add function call for MTU change
#@-INF@#  20010731 :LC_ALL review
#@-INF@#  20011124 :Exit, if interface setup fails
#@-INF@#  20011124a:Add support for default route (e.g. for PPP devices)
#@-INF@#  20011124b:Generalize handling of radvd trigger to use it also for dynamic ppp links
#@-INF@#           :Important: options renamed: IPV6TO4_CONTROL_RADVD -> IPV6_CONTROL_RADVD, IPV6TO4_RADVD_PIDFILE -> IPV6_RADVD_PIDFILE
#@-INF@#  20011124c:Adapt function names after renaming in libary
#@-INF@#  20020101 :Add support for dedicated 6to4 tunnel device (default now)
#@-INF@#  20020103 :IPV6TO4_RELAY get a default now, if not specified
#@-INF@#  20020103a:Spell checking, add compatibility mode for *RADVD* to use old values also
#@-INF@#  20020104 :Review 6to4 setup
#@-INF@#  20020104a:Minor review
#@-INF@#  20020105 :Minor tag review, add default route support for 6to4, remove static route support for 6to4, use "ipv6_get_ipv4addr_of_device" now instead of "ifconfig", some fixes
#@-INF@#  20020106 :Minor cleanup
#@-INF@#  20020106a:6to4: don't setup default gateway, if method is "sit0" and auto tunneling is enabled, 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 configuration is enabled for this interface, else stop
[ "$IPV6INIT" = "yes" ] || exit 0

# 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="20020106"												#@-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, module loaded, exit if system is not IPv6-ready
ipv6_test || exit 1

# Test device status
ipv6_test_device_status $DEVICE
if [ "$?" = 2 ]; then
	# device doesn't exist
	exit 1
fi 

# Setup IPv6 address on specified interface
if ! [ -z "$IPV6ADDR" ]; then
	ipv6_add_addr_on_device $DEVICE $IPV6ADDR || exit 1
fi

# Get current global IPv6 forwarding
ipv6_global_forwarding_current="`sysctl -n net.ipv6.conf.all.forwarding`"

# Set some proc switches depending on defines
if [ "$IPV6FORWARDING" = "yes" ]; then
	# Global forwarding should be enabled

	# Check, if global IPv6 forwarding was already set by global script
	if [ $ipv6_global_forwarding_current -ne 1 ]; then
		# Forwarding enabled, but not set, display warning							#@-RH7@#
		echo $"Global IPv6 forwarding is enabled in configuration, but not currently enabled in kernel"
		echo $"Please use latest IPv6 enabled '/etc/[rc.d/]init.d/network' (patch it with available diff-file)"	#@-RH7@#
#@+RH7@#		echo $"Please restart network with '/sbin/service network restart'"
	fi

	ipv6_local_forwarding=1
	ipv6_local_auto=0
	if [ "$IPV6_ROUTER" = "no" ]; then
		# Interface should not act as a Router									#@-RH7@#
		ipv6_local_forwarding=0
	fi
	if [ "$IPV6_AUTOCONF" = "yes" ]; then
		# Interface should be autoconfigured									#@-RH7@#
		ipv6_local_auto=1
	fi
else
	# Global forwarding should be disabled

	# Check, if global IPv6 forwarding was already set by global script
	if [ $ipv6_global_forwarding_current -ne 0 ]; then
		# Forwarding disabled, but not set, display warning							#@-RH7@#
		echo $"Global IPv6 forwarding is disabled in configuration, but not currently disabled in kernel"
		echo $"Please use latest IPv6 enabled '/etc/[rc.d/]init.d/network' (patch it with available diff-file)"	#@-RH7@#
#@+RH7@#		echo $"Please restart network with '/sbin/service network restart'"
	fi

	ipv6_local_forwarding=0
	ipv6_local_auto=1
	if [ "$IPV6_AUTOCONF" = "no" ]; then
		# Interface should not be autoconfigured								#@-RH7@#
		ipv6_local_auto=0
	fi
fi
sysctl -w net.ipv6.conf.$DEVICE.forwarding=$ipv6_local_forwarding >/dev/null
sysctl -w net.ipv6.conf.$DEVICE.accept_ra=$ipv6_local_auto >/dev/null
sysctl -w net.ipv6.conf.$DEVICE.accept_redirects=$ipv6_local_auto >/dev/null

# Set IPv6 MTU, if given
if [ ! -z "$IPV6_MTU" ]; then
	ipv6_set_mtu $DEVICE $IPV6_MTU
fi

# Setup additional IPv6 addresses from list
if [ ! -z "$IPV6ADDR_SECONDARIES" ]; then
	for ipv6addr in $IPV6ADDR_SECONDARIES; do
		ipv6_add_addr_on_device $DEVICE $ipv6addr
	done
fi

# Setup additional static IPv6 routes on specified interface
if [ -f /etc/sysconfig/static-routes-ipv6 ]; then
	LC_ALL=C grep -w "^$DEVICE" /etc/sysconfig/static-routes-ipv6 | while read device args; do
		ipv6_add_route $args $DEVICE
	done
fi

# Setup default IPv6 route through device, if enabled
if [ "$IPV6_DEFAULTDEV" = "$DEVICE" ]; then
	ipv6_set_default_route "" $DEVICE
fi

# Setup of 6to4, if configured
if [ "$IPV6TO4INIT" = "yes" ]; then
	valid6to4config="yes"
	if [ ! -z "$IPV6TO4_IPV4ADDR" ]; then
		# Take special configured from config file (precedence 1)
		ipv4addr="$IPV6TO4_IPV4ADDR"
		echo $"Overwrite 6to4 address with '$ipv4addr'"								#@-RH7@#
        else
		# Get IPv4 address from interface first (has precedence 2)
		ipv4addr="`ipv6_get_ipv4addr_of_device $DEVICE`"
		if [ -z "$ipv4addr" ]; then
			# Take configured from config file (precedence 3)
			ipv4addr="$IPADDR"
		fi
        fi
	if [ ! -z "$ipv4addr" ]; then
		# Test for non-global IPv4 address									#@-RH7@#
		if ! ipv6_test_ipv4_addr_global_usable $ipv4addr; then
			echo $"Given IPv4 address '$ipv4addr' is not globally usable, 6to4 configuration is not valid"
	                valid6to4config="no"
        	fi
		if [ -z "$IPV6TO4_RELAY" ]; then
			IPV6TO4_RELAY="192.88.99.1"
			echo $"IPv6to4 has no 6to4 relay router address specified, using default: '$IPV6TO4_RELAY'"	#@-RH7@#
		fi
		if [ "$valid6to4config" = "yes" ]; then
			if ipv6_test_ipv4_addr_global_usable $IPV6TO4_RELAY 2>/dev/null; then
				# IPv4 global usable one								#@-RH7@#
				true
			elif ipv6_test_ipv6_addr_valid $IPV6TO4_RELAY quiet; then
				# IPv6 address										#@-RH7@#
				relay6to4type="ipv6"
				if echo $IPV6TO4_RELAY | LC_ALL=C grep -q "^2002:"; then
					# IPv6 address is a 6to4 (further tests not be implemented at the moment)
					true
				else
					echo $"Given IPv6 address of relay is not a 6to4 one, 6to4 configuration is not valid"
					valid6to4config="no"
				fi
			else
				echo $"Given address of relay is not globally usable, 6to4 configuration is not valid"
				valid6to4config="no"
			fi
		fi
	else
        	echo $"IPv6to4 configuration needs an IPv4 address on related interface or otherwise specified, 6to4 configuration is not valid"
		valid6to4config="no"
	fi

        # Check method
        if [ -z "$IPV6TO4_SETUP_METHOD" ]; then
                tundev="tun6to4"
        elif [ "$IPV6TO4_SETUP_METHOD" = "tun6to4" ]; then
                tundev="tun6to4"
        elif [ "$IPV6TO4_SETUP_METHOD" = "sit0" ]; then
                tundev="sit0"
        else
		echo $"Unsupported 6to4 tunneling setup method '$IPV6TO4_SETUP_METHOD'"
		valid6to4config="no"
        fi

        if [ "$valid6to4config" = "yes" ]; then
		ipv6_add_6to4_tunnel $tundev $ipv4addr

		# Add default route, if device matches
		if [ "$IPV6_DEFAULTDEV" = "$tundev" ]; then
			if [ "$IPV6_AUTOTUNNEL" = "yes" -a "$tundev" = "sit0" ]; then
				echo $"Default gateway device '$tundev' isn't supported if IPv6 auto tunneling is enabled"
			else
				if [ "$relay6to4type" = "ipv6" ]; then
					# Foreign 6to4 relay gateway as IPv6to4
					ipv6_set_default_route $IPV6TO4_RELAY $tundev
				else
					# Foreign 6to4 relay gateway as compatible IPv4
					ipv6_set_default_route ::$IPV6TO4_RELAY $tundev
				fi
			fi
		fi

	        if [ "$IPV6_CONTROL_RADVD" = "yes" -o "$IPV6TO4_CONTROL_RADVD" = "yes" ]; then
			# RADVD is in use, so forwarding of IPv6 packets should be enabled, display warning
			if [ $ipv6_global_forwarding_current -ne 1 ]; then
				# Forwarding not set, display warning							#@-RH7@#
				echo $"Using 6to4 and RADVD IPv6 forwarding usually should be enabled, but it isn't"
				echo $"Add/change 'IPV6FORWARDING=yes' in '/etc/sysconfig/network'"			#@-RH7@#
				echo $" and restart network with 'service network restart'"				#@-RH7@#
			fi

		        if [ ! -z "$IPV6TO4_ROUTING" ]; then
				# Generate 6to4 address									#@-RH7@#
				ipv6to4prefix="`ipv6_create_6to4_prefix $ipv4addr`"
				if [ ! -z "$ipv6to4prefix" ]; then
					# Add route to local networks
					for devsuf in $IPV6TO4_ROUTING; do
						dev="`echo $devsuf | awk -F- '{ print $1 }'`"
						suf="`echo $devsuf | awk -F- '{ print $2 }'`"
						ipv6_add_route ${ipv6to4prefix}$suf :: $dev
					done
				else
					echo $"Error occured while calculating the IPv6to4 prefix"
				fi
			else
				echo $"radvd control enabled, but config is not complete"
			fi
		fi
        fi
fi

# Control running radvd
if [ "$IPV6_CONTROL_RADVD" = "yes" -o "$IPV6TO4_CONTROL_RADVD" = "yes" ]; then
	radvdpidfile="$IPV6_RADVD_PIDFILE"
	if [ -z "$pidfile" ]; then
		radvdpidfile="$IPV6TO4_RADVD_PIDFILE"
	fi

	if [ "$IPV6TO4INIT" = "yes" ]; then
		ipv6_trigger_radvd 6to4 $radvdpidfile
	else
		ipv6_trigger_radvd ppp $radvdpidfile
	fi
fi
