#!/bin/sh
#
# ifup-sit
#
#@-INF@# RedHat 6.2 + 7.x
#@-INF@#
#@-INF@# directory: /etc/sysconfig/network-scripts
#@-INF@#
#@-INF@# permissions: 755
#@-INF@#
#@-INF@# description: Brings up IPv6 for tunnel interfaces (sitx)
#
#@+RH7@## Taken from:
# (P) & (C) 2000-2002 by Peter Bieringer <pb@bieringer.de>
#
# RHL integration assistance by Pekka Savola <pekkas@netcore.fi>
#
# Version 2002-01-08a
#
# 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>
#  IPV6INIT=yes|no: controls IPv6 configuration for this interface
#  IPV6_MTU=<MTU for IPv6>: controls IPv6 MTU for this link [optional]
# 
#  For static tunnels 
#  IPV6TUNNELIPV4=<IPv4 address>: IPv4 address of remote tunnel endpoint
#  IPV6TUNNELIPV4LOCAL=<IPv4 address>: (optional) local IPv4 address of tunnel
#  IPV6ADDR=<IPv6 address>[/<prefix length>]: (optional) local IPv6 address of a numbered tunnel
#
#@-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 :test, if static route file exists
#@-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@#  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 "ifup_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 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@#  20010309 :Add support for new tunnel setup mode
#@-INF@#  20010310 :Enable global IPv6 forwarding also
#@-INF@#  20010313 :Set "IP" as default tunnel mode
#@-INF@#  20010417 :Add code for dedicated forwarding control of tunnel interface
#@-INF@#  20010428 :Unsuccessful "test_ipv6" skips IPv6 initialization
#@-INF@#  20010429 :Fix spelling, minor reviews
#@-INF@#  20010430 :Minor review
#@-INF@#  20010501 :Adapt forwarding control to kernel behavior
#@-INF@#  20010505 :Untag some lines on "network-functions-ipv6" exists check
#@-INF@#            Add some sysctls, remove use 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@#  20010507 :major cosmetic review (change tabsize to 8 and cleanups)
#@-INF@#  20010520b:Cosmetic fixes
#@-INF@#  20010522d:Cosmetic fixes 
#@-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@#  20010730 :Add option "IPV6TUNNELIPV4LOCAL", triggered by kad@blackcatlinux.com
#@-INF@#  20010731 :LC_ALL review
#@-INF@#  20010731a:Add a forgotten a tag
#@-INF@#  20011124c:Adapt function names after renaming in libary
#@-INF@#  20020106a:Review log text
#@-INF@#  20020106b:Revert log style to 20020106 one
#@-INF@#  20020107 :Minor review
#@-INF@#  20020108 :Remove support for NBMA-styled tunnels using sit0 (IPV6_TUNNELMODE is no longer used) and sit0 (use IPV6_AUTOTUNNEL instead)
#@-INF@#  20020108a:Review default route setup

# 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

# Generic tunnel device sit0 is not supported here
if [ "$DEVICE" = "sit0" ]; then
	echo $"Device '$DEVICE' isn't supported here, use IPV6_AUTOTUNNEL setting and restart (IPv6) networking"
	exit 1
fi

if [ -z "$IPV6TUNNELIPV4" ]; then
	echo $"Missing remote IPv4 address of tunnel, configuration is not valid"
	exit 1
fi

# Test device status
ipv6_test_device_status $DEVICE
if [ $? = 0 ]; then
	# device is already up
	echo $"Device '$DEVICE' is already up, please shutdown first"
	exit 1
fi

# Create tunnel
ipv6_add_tunnel_device $DEVICE $IPV6TUNNELIPV4 "" $IPV6TUNNELIPV4LOCAL || exit 1

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

# Apply local IPv6 address, if given (numbered tunnel)
if [ ! -z "$IPV6ADDR" ]; then
	ipv6_add_addr_on_device $DEVICE $IPV6ADDR
fi

# Setup default IPv6 route, check are done by function
ipv6_set_default_route "$IPV6_DEFAULTGW" "$IPV6_DEFAULTDEV" "$DEVICE"
	
# Setup additional static IPv6 routes on specified interface, if given		
if [ -f /etc/sysconfig/static-routes-ipv6 ]; then
	LC_ALL=C grep -w "^$DEVICE" /etc/sysconfig/static-routes-ipv6 | while read device ipv6route args; do
		ipv6_add_route $ipv6route :: $DEVICE
	done
fi
