#!/bin/sh
#
# network-functions-ipv6
#
#@-INF@# RedHat 6.2 + 7.x
#@-INF@#
#@-INF@# directory: /etc/sysconfig/network-scripts
#@-INF@#
#@-INF@# permissions: 644
#@-INF@#
#@-INF@# description: Several IPv6 related functions
#@-INF@#	
#@-INF@# Contents:
#@-INF@#	
#@-INF@# Wrapper functions:
#@-INF@#  ipv6_exec_ifconfig               : Wrapper for "ifconfig"
#@-INF@#  ipv6_exec_route                  : Wrapper for "route"
#@-INF@#  ipv6_exec_ip                     : Wrapper for "ip"
#@-INF@#  ipv6_exec_sysctl                 : Wrapper for "sysctl"
#@-INF@#	
#@-INF@# Worker functions:
#@-INF@#  ipv6_add_addr_on_device          : Configure an IPv6 address for a specified interface
#@-INF@#  ipv6_del_addr_on_device          : Remove an IPv6 address on a specified interface
#@-INF@#  ipv6_cleanup_device              : Remove any IPv6 addresses assigned on a specified interface and related routes
#@-INF@#	
#@-INF@#  ipv6_enable_autotunnel           : Enabling automatic tunneling
#@-INF@#  ipv6_disable_autotunnel          : Disable automatic tunneling
#@-INF@#	
#@-INF@#  ipv6_add_nbma_tunnel             : Configure a NBMA-styled IPv6 tunnel			#@-RH7@#
#@-INF@#  ipv6_del_nbma_tunnel             : Remove a NBMA-styled IPv6 tunnel				#@-RH7@#
#@-INF@#  ipv6_cleanup_nbma_tunnels_to_dest: Remove all IPv6 tunnels to a remote IPv4-tunnel address	#@-RH7@#
#@-INF@#												#@-RH7@#
#@-INF@#  ipv6_add_tunnel_device           : Create a dedicated tunnel device	
#@-INF@#  ipv6_del_tunnel_device           : Delete a dedicated tunnel device
#@-INF@#  ipv6_cleanup_tunnel_devices      : Remove all dedicated tunnel devices
#@-INF@#	
#@-INF@#  ipv6_add_6to4_tunnel             : Configure a 6to4 tunnel
#@-INF@#  ipv6_del_6to4_tunnel             : Remove a 6to4 tunnel
#@-INF@#  ipv6_cleanup_6to4_tunnels        : Remove all 6to4 tunnels
#@-INF@#  ipv6_create_6to4_prefix          : Create a 6to4 prefix
#@-INF@#  ipv6_create_6to4_relay_address   : Check and create a 6to4 relay address
#@-INF@# 	
#@-INF@#  ipv6_add_route                   : Add an IPv6 route
#@-INF@#  ipv6_del_route                   : Remove an IPv6 route
#@-INF@#  ipv6_cleanup_routes              : Remove all IPv6 gateway routes through a device
#@-INF@#	
#@-INF@#  ipv6_control_forwarding          : Control IPv6 forwarding globally (no longer in use)
#@-INF@#  
#@-INF@#  ipv6_set_mtu                     : Set IPv6 MTU for an interface
#@-INF@#  ipv6_set_default_route           : Set default route
#@-INF@#
#@-INF@#  ipv6_trigger_radvd               : Trigger radvd
#@-INF@#
#@-INF@#  ipv6_get_ipv4addr_of_tunnel      : Get IPv4 address of a dedicate tunnel (local|remote)
#@-INF@#  ipv6_get_ipv4addr_of_device      : Get IPv4 address of a device
#@-INF@#
#@-INF@# Test functions:
#@-INF@#  ipv6_test_addr_exists_on_device  : Test existing of IPv6 address on an interface
#@-INF@#  ipv6_test_addrprefix_exists_on_device: Test existing of an IPv6 address prefix on an interface
#@-INF@#  ipv6_test_ipv4_addr_valid        : Test an IPv4 address for validity
#@-INF@#  ipv6_test_ipv4_addr_global_usable: Test an IPv4 address for globally usable (no private or others)
#@-INF@#  ipv6_test_ipv6_addr_valid        : Test an IPv6 address for validity
#@-INF@#  ipv6_test                        : Test whether system is prepared for IPv6
#@-INF@#  ipv6_test_device_status          : Test given device for exists,up, not up
#@-INF@#  ipv6_test_route_requires_next_hop: Test whether interface needs explicit next hop for route setup
#@-INF@#  
#@-INF@# Informationals: 
#@-INF@#  getversion_ipv6_functions: Get version of file /etc/sysconfig/network-scripts/network-functions-ipv6
#@-INF@#
#@+RH7@## Taken from: network-functions-ipv6
# (P) & (C) 1997-2002 by Peter Bieringer <pb@bieringer.de>
#
# Version: 2002-01-10
#
# Extended address detection is enabled, if 'ipv6calc' is installed
#  Available here: http://www.bieringer.de/linux/IPv6/tools/index.html#ipv6calc
#
# DEBUG_IPV6=<bitwise orientated value> : debug value, see below for bitwise description				#@-DEB@#
#
#@-RH7@# Known bugs:
#@-RH7@#  sit0 will not be automatically shutdowned, if an additional IPv6 address was manually added to this device
#@-RH7@#   To solve this issue: add hooks in "/etc/[rc.d/]init.d/network" to call "init.ipv6-global"
#@-RH7@#
#@-INF@#  This function libary is now explicitly GPL'ed 
#@-INF@#   http://www.gnu.org/copyleft/gpl.html (20001125, Peter Bieringer)
#@-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 review for new layout, based on 'functions-ip6' 2.37
#@-INF@#             only ifup/ifdown works
#@-INF@#  20000704: review ifup-ipv6-tunnel, ifdown-ipv6-tunnel
#@-INF@#  20000723: minor review
#@-INF@#  20000731: fix misnamed function "forwarding-ipv6-route" -> "forwarding-ipv6"
#@-INF@#  20000816: rename all functions names: "-" to "_"
#@-INF@#             Mauro Tortonesi <mauro@ferrara.linux.it> reports incompatiblity
#@-INF@#             problems
#@-INF@#  20001125: explicitly GPL'ed
#@-INF@#  20001215: fix a bug regarding variable name length->prefix length in setting
#@-INF@#             up interfaces, thanks to Marcus Windisch <marcus.windisch@gmx.net>
#@-INF@#  20010201: if a tunnel interface named sit0 is requested to set up, only bring
#@-INF@#             sit0 up to life (automatic tunneling support only)
#@-INF@#            begin to mark all major debug code for fast removing via grep
#@-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 ifup_ipv6_real/ifdown_ipv6_real to extract prefix length from
#@-INF@#             given address, if not extra specified
#@-INF@#            Network route setup on interface initialization disabled (no longer needed?)
#@-INF@#  20010203: Remove obsolete kernel 2.1.x (x < 90) code, remove some information output
#@-INF@#            Add function ifdown_ipv6_real_all, usage prevents from kernel crashing
#@-INF@#             if on shutdown one address was forgotten
#@-INF@#            Add some "prevent of kernel crashing" airbags and duplicate configurations
#@-INF@#  20010205: Start interface, if down (for an IPv6 only configuration)
#@-INF@#             hint by Robert.Wachinger@icn.siemens.de
#@-INF@#  20010207: Change from direct writing to /proc to sysctl
#@-INF@#             Implement some rawhide fixes
#@-INF@#  20010208: Remove backward compatiblity for extra given prefix length
#@-INF@#            Remove no longer used routing setup on interfaces
#@-INF@#            Implement additional rawhide fixes
#@-INF@#  20010219: Cosmetic fix (remove a "!" on an echo output)
#@-INF@#  20010222: Fix variable names to prevent overwriting on nested function calls
#@-INF@#             in ifdown_ipv6_real_all, forwarding_ipv6
#@-INF@#  20010225: Extend address detection with external tool "ipv6addrshorter", if available
#@-INF@#  20010227: Because grepping in ifconfig output is not nice and perhaps depending on
#@-INF@#             international settings, the address detection now uses a new tool called
#@-INF@#             'ipv6calc', available here: http://www.bieringer.de/linux/IPv6/tools/index.html#ipv6calc
#@-INF@#             and grep /proc/net/if_inet6
#@-INF@#            On fallback mode (no 'ipv6calc' available) now using LC_ALL=C
#@-INF@#             credits for this hint to Pekka Savola <pekkas@netcore.fi>
#@-INF@#  20010302: Cleanup some cat/grep combinations
#@-INF@#            fix all net-tools commands for used with grep to LC_ALL=C
#@-INF@#            Clean up some "'" on echo
#@-INF@#  20010303: Address grepping use now "-i"
#@-INF@#            Looking for "ipv6calc" does no longer display "which" output of stderr
#@-INF@#            Add minor debugging code
#@-INF@#  20010303a:Detect routing setup issues (SIOCADDRT, SIOCDELRT) and handle them
#@-INF@#  20010303b:Add ifdown_ipv6_tunnel_all to shutdown all tunnels per device
#@-INF@#            Fix ifdown_ipv6_autotunnel (improved detection of existing tunnels)
#@-INF@#  20010303c:Run some checks on ipv6calc to prove expected working
#@-INF@#  20010304 :Add on ifdown_ipv6_tunnel_all a call to ifdown_ipv6_autotunnel
#@-INF@#  20010304a:Add 6to4 support
#@-INF@#  20010309 :Review 6to4 support, fix minor bugs
#@-INF@#            Add support for "ip" instead of NBMA-style for creating tunnels
#@-INF@#  20010310 :Cleanup for RH7 (line tagging)
#@-INF@#  20010310a:Cleanup for RH7 (line tagging)
#@-INF@#  20010310b:Add "ifdown_ipv6_route_all", cosmetic cleanups
#@-INF@#  20010312 :Add loopback address test on "testipv4_globalusable", add TTL for "ip tunnel" (really needed)
#@-INF@#  20010312a:Cosmetic fixes
#@-INF@#  20010313 :Cosmetic cleanup (remove "function" labels - not needed), add "-n" on "route" if output is grepped
#@-INF@#  20010315 :Add "ifdown_ipv6to4_all" to remove all applied 6to4 addresses
#@-INF@#  20010317 :Add "getversion_ipv6_functions" to get a version control capability for the future
#@-INF@#           :Review return code handling, add tests of addresses and configure results to prevent troubles
#@-INF@#           :More use of sub-function calls instead of direct command execution
#@-INF@#  20010317a:Change "create6to4address" to create6to4prefix
#@-INF@#  20010317a:"ifup_ipv6to4" can get an optional suffix (default: "1")
#@-INF@#  20010317b:Add a test whether "ip tunnel add" has worked
#@-INF@#  20010317c:Fix bad bug in low budget test in "testipv6_valid"
#@-INF@#  20010317d:Speedup duplicate remote IPv4 addresses on "ifup_ipv6_tunneldev"
#@-INF@#  20010417 :Spelling cleanups, remove usage information
#@-INF@#           :Remove "skip interface lo" code (perhaps dedicated configuration is needed sometimes)
#@-INF@#  20010417b:Fix comments
#@-INF@#  20010429 :Cosmetic review
#@-INF@#  20010430 :Cosmetic review
#@-INF@#  20010430a:Add optional option "testonly" for "test_ipv6"
#@-INF@#           :Change from "ifconfig" to "ip" if parsing output
#@-INF@#  20010501 :Adapt forwarding control to kernel behavior (only global switch is used)
#@-INF@#  20010505 :Add "exists IPv6 in proc" test
#@-INF@#            Add some sysctls for tunnel interfaces
#@-INF@#  20010507 :Remove double sysctls for 6to4 tunnel
#@-INF@#  20010511 :"forwarding_ipv6" should be no longer used (because of different behavior 'per interface'/'all')
#@-INF@#  20010519 :Remove setting of *.router_solicitations in proc 
#@-INF@#  20010520 :Cleanup some code by using "ip", adjust code for tabsize 8 (still not all)
#@-INF@#  20010520a:Add addtional tests on tunnel shutdown (not sit0)
#@-INF@#  20010520b:Cosmetic and minor fixes
#@-INF@#  20010522 :Add some forgotten debug code, pipe some output to /dev/null
#@-INF@#  20010522d:Cosmetic fixes
#@-INF@#  20010614a:Minor cosmetic fix
#@-INF@#  20010615 :Cosmetic fixes
#@-INF@#  20010707 :Change 6to4 setup prefix length from 48 (wrong) to 16 (well)
#@-INF@#  20010715 :Minor review, triggered by Pekka Savola
#@-INF@#  20010715a:Minor review, triggered by Pekka Savola
#@-INF@#  20010717 :Add function "ipv6_set_mtu"
#@-INF@#  20010717a:Change mechanism of mtu change to sysctl
#@-INF@#  20010718 :Minor RHL tagging
#@-INF@#  20010730 :Typo fixes, triggered by RHL
#@-INF@#            enable use of local IPv4 address for "ifup_ipv6_tunneldev", triggered by <kad # blackcatlinux.com>
#@-INF@#  20010731 :Another cosmetic fix, triggered by Pekka Savola, add some LC_ALL=C
#@-INF@#  20010806 :Direct stderr output of "ip tunnel" to /dev/null
#@-INF@#  20010815 :Add new function "ipv6_set_default_gateway", "ipv6_test_route_requires_next_hop"
#@-INF@#           :Began to rename some functions (wrappers for old names will be still there) to clear up names
#@-INF@#  20010818 :Fix call parameters in wrapper functions, some minor fixes regarding the default route functions
#@-INF@#  20010828 :Cosmetic fix (triggered by RH)
#@-INF@#  20010828a:Change default route from ::/0 to 2000::/3 until kernel supports it
#@-INF@#  20011124 :Catch error if device doesn't exist, minor renaming, minor cleanups
#@-INF@#  20011124c:Rename all functions to start with "ipv6_"
#@-INF@#  20011220 :Add "ipv6_create_6to4_prefix" in contents, remove RHL tags from IPv4 test functions (needed now really by 6to4 setup)
#@-INF@#  20020101 :Add some wrapper functions and add logging capability for each network configuration step, reorder debug code
#@-INF@#           :Add support for dedicated 6to4 tunnel device
#@-INF@#  20020102 :"ipv6_add_addr_on_device" uses prefix length '64' as default if missing
#@-INF@#  20020103a:Some RHL7 tagging and minor review
#@-INF@#  20020104 :Rename radvd message on missing pidfile, finish debug message format redesign
#@-INF@#  20020104a:Add functions "ipv6_get_ipv4addr_of_tunnel"+ "ipv6_test_addrprefix_exists_on_device", rename IPV6_TRIGGER_RADVD_ACTION_* IPV6_RADVD_TRIGGER_ACTION_*
#@-INF@#  20020105 :Add option "inet6" at "ifconfig $dev add|del $ipv6addr", add #@-DEB@# tag on some blank lines
#@-INF@#           :Cleanup inconsistent return codes, remove "set -x" debug mode, new func "ipv6_get_ipv4addr_of_device" + "ipv6_cleanup_tunnel_devices", fix stupid bug in "ipv6_get_ipv4addr_of_tunnel"
#@-INF@#  20020106 :Fix stupid bug in "ipv6_get_ipv4addr_of_device"
#@-INF@#  20020106a:Add func "ipv6_log" and reorganize all logging code (hopefully RHL translation friendly now)
#@-INF@#  20020107 :Next (hopefully last) redesign of logging mechanism, minor 6to4 review, new func "ipv6_create_6to4_relay_address"
#@-INF@#  20020107b:Minor review
#@-INF@#  20020108a:Tag by current scripts not used code for -RH7, "ipv6_set_default_route" checks now scope device againt given one and report error, if not match
#@-INF@#           :Redesign "ipv6_trigger_radvd"
#@-INF@#  20020109 :Set default for "ipv6_trigger_radvd" to SIGHUP, replace "route" with "ip -6 route" (proper syntax also, use metric 1)
#@-INF@#  20020109a:Pipe debug value information to stderr, review default route setup
#@-INF@#  20020110 :Minor review

# Filter tags (for stripping, empty lines following here if all is stripped) #@-INF@#
#  #@-DEB@#  : Additional debug code
#  #@-INF@#  : Additional information
#  #@-RH7@#  : Not necessary for RedHat 7.x
#  #@-RH7start@#: Not necessary for RedHat 7.x start of a block tag		#@-RH7@#
#  #@-RH7end@#  : Not necessary for RedHat 7.x end of a block tag		#@-RH7@#
#  #@+RH7@#  : Necessary for RedHat 7.x

#@-DEB@# This libary can be debugged easily, see following example
#@-DEB@#   export DEBUG_IPV6=256; ./ip-up.ipv6to4 ppp0
#@-DEB@#  output can be filtered, e.g. all executed commands will be show using
#@-DEB@#   export DEBUG_IPV6=256; ./ip-up.ipv6to4 ppp0 | grep "^EXE:"
#@-DEB@#
#@-DEB@# DEBUG_IPV6 &   1: unused now, ex "set -x" mode
#@-DEB@# DEBUG_IPV6 &   2: prevents from executing any network configuration
#@-DEB@# DEBUG_IPV6 &   4: shows messages in the test section
#@-DEB@# DEBUG_IPV6 &   8: address exists test
#@-DEB@# DEBUG_IPV6 &  16: ipv6_del_any_addr_on_device
#@-DEB@# DEBUG_IPV6 &  32: skip "ip*calc" exists test (simulate low budget tests)
#@-DEB@# DEBUG_IPV6 &  64: address tests
#@-DEB@# DEBUG_IPV6 & 128: 6to4
#@-DEB@# DEBUG_IPV6 & 256: display what will be executed
#@-DEB@# DEBUG_IPV6 & 512: ipv6_log
#@-DEB@# Hint: you can set the debug value for one command only like "export DEBUG_IPV6=1; command parameters"

#DEBUG_IPV6=$[ 65535 - 1 -2  -4 -8 -16 -32 -64 -128]								#@-DEB@#
#DEBUG_IPV6=0													#@-DEB@#
#DEBUG_IPV6=32 													#@-DEB@#
#DEBUG_IPV6=33 													#@-DEB@#
#DEBUG_IPV6=$[ 8	+ 32 ]											#@-DEB@#
#DEBUG_IPV6=2													#@-DEB@#
#DEBUG_IPV6=64													#@-DEB@#
#DEBUG_IPV6=1													#@-DEB@#
#DEBUG_IPV6=256													#@-DEB@#

[ -z "$DEBUG_IPV6" ]  && DEBUG_IPV6=0										#@-DEB@#


##### Logging function
#  $1: <message> : message string
#  $2: [stdout|stderr].[err|warn[ing]|inf[o]|notice] : log level with optional channel, default is "stdout.notice"
#      [syslog.[facility.].err|warn[ing]|inf[o]|notice : syslog channel, default is "syslog.user.notice"
#  $3: <function name> : name of function which calls this log function, can be empty using ""
# return code: 0=ok 1=argument error  3=major problem
ipv6_log() {
	#echo "DEBUG: [ipv6_log] Arguments: '$*'"								#@-DEB@#
														#@-DEB@#
	local message="$1"
	local level="$2"
	local name="$3"

	if [ -z "$message" ]; then
		echo $"ERROR: [ipv6_log] Missing 'message' (arg 1)" >/dev/stderr
		return 1
	fi
	if [ -z "$level" ]; then
		local level="stdout.notice"
	fi

	[ $[ $DEBUG_IPV6 & 512 ] = 0 ] || echo "DEBUG: [ipv6_log] Check loglevel '$level'"			#@-DEB@#

	# Map loglevel now
	local fn=1
	local fnawk="print \$$fn"
	local t="`echo $level | awk -F. "{ $fnawk }"`"

	# Check channel, if given
	[ $[ $DEBUG_IPV6 & 512 ] = 0 ] || echo "DEBUG: [ipv6_log] Check channel '$t'"				#@-DEB@#
	case $t in
	    'stdout'|'stderr'|'syslog')
		local channel="$t"
		local fn=$[ $fn + 1 ]
		[ $[ $DEBUG_IPV6 & 512 ] = 0 ] || echo "DEBUG: [ipv6_log] Channel '$channel' is ok"			#@-DEB@#
		;;
	    *)
		local channel="stdout"
		[ $[ $DEBUG_IPV6 & 512 ] = 0 ] || echo "DEBUG: [ipv6_log] No channel given, take default '$channel'"	#@-DEB@#
		;;
	esac

	# Check syslog facilty, if given
	if [ "$channel" = "syslog" ]; then
		local fnawk="print \$$fn"
		local t="`echo $level | awk -F. "{ $fnawk }"`"
		[ $[ $DEBUG_IPV6 & 512 ] = 0 ] || echo "DEBUG: [ipv6_log] Check syslog facility '$t'"		#@-DEB@#
		case $t in
		    'local0'|'local1'|'local2'|'local3'|'local4'|'local5'|'local6'|'local7'|'daemon')
			local facility="$t"
			local fn=$[ $fn + 1 ]
			[ $[ $DEBUG_IPV6 & 512 ] = 0 ] || echo "DEBUG: [ipv6_log] Facility '$facility' is ok"	#@-DEB@#
			;;
		    *)
			local facility="user"
			[ $[ $DEBUG_IPV6 & 512 ] = 0 ] || echo "DEBUG: [ipv6_log] No facility given, take default '$facility'"	#@-DEB@#
			;;
		esac
	fi

	local fnawk="print \$$fn"
	local t="`echo $level | awk -F. "{ $fnawk }"`"

	# Map priority
	[ "$t" = "inf"      ] && local t="info"
	[ "$t" = "deb"      ] && local t="debug"
	[ "$t" = "warning"  ] && local t="warn"
	[ "$t" = "error"    ] && local t="err"
	[ "$t" = "critical" ] && local t="crit"

	# Check priority, if given
	[ $[ $DEBUG_IPV6 & 512 ] = 0 ] || echo "DEBUG: [ipv6_log] Check priority '$t'"		#@-DEB@#
	case $t in
	    'info'|'debug'|'notice'|'warn'|'err'|'crit')
			local priority="$t"
			local fn=$[ $fn + 1 ]
			[ $[ $DEBUG_IPV6 & 512 ] = 0 ] || echo "DEBUG: [ipv6_log] Priority '$t' is ok"		#@-DEB@#
			;;
		    *)
			local priority="notice"
			[ $[ $DEBUG_IPV6 & 512 ] = 0 ] || echo "DEBUG: [ipv6_log] No priority given, take default '$priority'"	#@-DEB@#
			;;
	esac
	
	local fnawk="print \$$fn"
	local t="`echo $level | awk -F. "{ $fnawk }"`"
	if [ ! -z "$t" ]; then
		echo $"ERROR: [ipv6_log] Loglevel isn't valid '$level' (arg 2)" >/dev/stderr
		return 1
	fi
	
	# Generate function text
	if [ -z "$name" ]; then
		local txt_name=""
	else
		local txt_name="[$name]"
	fi

	# Log message
	case $channel in
	    'stdout'|'stderr')
		[ $[ $DEBUG_IPV6 & 512 ] = 0 ] || echo "DEBUG: [ipv6_log] Log to stdout|stderr"		#@-DEB@#
		# Generate level text
		case $priority in
		    'debug')
			local txt_level=$"DEBUG    "
			;;
		    'err')
			local txt_level=$"ERROR    "
			;;
		    'warn')
			local txt_level=$"WARN     "
			;;
		    'crit')
			local txt_level=$"CRITICAL "
			;;
		    'info')
			local txt_level=$"INFO     "
			;;
		    'notice')
			local txt_level=$"NOTICE   "
			;;
		esac
		
		[ ! -z "$txt_name" ] && local txt_name="$txt_name "		

		if [ "$channel" = "stderr" ]; then
			echo "$txt_level: ${txt_name}${message}" >/dev/stderr
		elif [ "$channel" = "stdout" ]; then
			echo "$txt_level: ${txt_name}${message}"
		fi
		;;
	    'syslog')
		[ $[ $DEBUG_IPV6 & 512 ] = 0 ] || echo "DEBUG: [ipv6_log] Log to syslog"		#@-DEB@#

		if [ ! -x logger ]; then
			echo $"ERROR: [ipv6_log] Syslog is chosen, but binary 'logger' doesn't exist or isn't executable" >/dev/stderr
			return 3
		fi
		if [ -z "$txt_name" ]; then
			logger -p $facility.$priority $message
		else
			logger -p $facility.$priority -t "$txt_name" "$message"
		fi
		;;
	    *)
		echo $"ERROR: [ipv6_log] Cannot log to channel '$channel'" >/dev/stderr
		return 3
		;;
	esac

	return 0
}


###### Beginning of main code here, always executed on "source|. network-functions-ipv6"

##### Test for "ipv6calc" (used for better existing address detection)
EXISTS_ipv6calc=no	
if [ $[ $DEBUG_IPV6 & 32 ] = 0 ]; then										#@-DEB@#
	if which ipv6calc >/dev/null 2>&1; then
		# do checks, whether ipv6calc does what was expected							#@-RH7@#
		if ipv6calc --if_inet62addr 3ffe04000100f1010000000000000001 40 | LC_ALL=C grep -q -v '3ffe:400:100:f101::1/64'; then
			false
			ipv6_log $"Binary 'ipv6calc' works not like expected, please check installed version (check 1)" warn 	#@-RH7@#
		elif ipv6calc --addr2if_inet6 3ffe:400:100::1/64 | LC_ALL=C grep -q -v '3ffe0400010000000000000000000001 00 40'; then
			false
			ipv6_log $"Binary 'ipv6calc' works not like expected, please check installed version (check 2)" warn	#@-RH7@#
		else
			EXISTS_ipv6calc=yes
		fi
	else
		ipv6_log $"Binary 'ipv6calc' doesn't exist, existing address detection is not extended" warn		#@-RH7@#
		false
	fi
else														#@-DEB@#
	ipv6_log $"'ipv6calc' exists test is skipped for debugging purposes" deb				#@-DEB@#
fi														#@-DEB@#

##### Test for "ipcalc" (used for private IPv4 address detection in 6to4 setup)						#@-RH7@#
EXISTS_ipcalc=no													#@-RH7@#	
if [ $[ $DEBUG_IPV6 & 32 ] = 0 ]; then										#@-DEB@##@-RH7@#
	if which ipcalc >/dev/null 2>&1; then										#@-RH7@#
		# do checks, whether ipv6calc does what was expected							#@-RH7@#
		if ipcalc --network 192.168.1.1 255.255.0.0 | LC_ALL=C grep -q -v 'NETWORK=192.168.0.0'; then		#@-RH7@#
			false												#@-RH7@#
			ipv6_log $"Binary 'ipcalc' works not like expected, please check installed version" warn 	#@-RH7@#
		else													#@-RH7@#
			EXISTS_ipcalc=yes										#@-RH7@#
		fi													#@-RH7@#
	else														#@-RH7@#
		false													#@-RH7@#
		ipv6_log $"Binary 'ipcalc' doesn't exist, private IPv4 address detection is not extended" warn 		#@-RH7@#
	fi														#@-RH7@#
else														#@-DEB@##@-RH7@#
	ipv6_log $"'ipcalc' exists test is skipped for debugging purposes" deb					#@-DEB@##@-RH7@#
fi														#@-DEB@##@-RH7@#


###### End of main code here


##### Test for IPv6 capabilites
# $1: (optional) testflag: currently supported: "testonly" (do not load a module)
# return code: 0=ok 2=IPv6 test fails
ipv6_test() {
	local fn="ipv6_test"

	local testflag=$1

	[ $[ $DEBUG_IPV6 & 4 ] = 0 ] || ipv6_log $"Tests for IPv6" deb $fn 					#@-DEB@# 
														#@-DEB@#
	# Test for IPv6 enabled kernel											#@-RH7@#
	[ $[ $DEBUG_IPV6 & 4 ] = 0 ] || ipv6_log $"Test kernel for IPv6..." deb $fn				#@-DEB@#
														#@-DEB@#
	if ! [ -f /proc/net/if_inet6 ]; then
		if [ "$testflag" = "testonly" ]; then
			return 2
		else
			ipv6_log $"Did not find IPv6 in kernel, trying to load module" inf $fn				#@-RH7@#
			modprobe ipv6
	
			if ! [ -f /proc/net/if_inet6 ]; then
				if ! [ $[ $DEBUG_IPV6 & 4 ] = 0 ] ; then					#@-DEB@#
					ipv6_log $"Not compiled for IPv6 - stop" crit $fn			#@-DEB@#
				else										#@-DEB@#
					ipv6_log $"Kernel is not compiled with IPv6 support" crit $fn
				fi										#@-DEB@#
				return 2
			fi
		fi
	else													#@-DEB@#
		[ $[ $DEBUG_IPV6 & 4 ] = 0 ] || ipv6_log $"Ok" deb $fn						#@-DEB@#
	fi

	if [ ! -d /proc/sys/net/ipv6/conf/ ]; then
		# IPv6 related proc directory doesn't exist 								#@-RH7@#
		return 2
	fi

	# Test for IPv6 enabled needed binaries										#@-RH7@#
	[ $[ $DEBUG_IPV6 & 4 ] = 0 ] || ipv6_log $"Test binaries for IPv6 capability..." deb $fn		#@-DEB@##@-RH7@#
															#@-RH7@#
	if ! LC_ALL=C ifconfig -? 2>&1 | LC_ALL=C grep -q "(IPv6)"; then						#@-RH7@#
		ipv6_log $"'`which ifconfig`' (net-tools) not compiled for IPv6 - stop" crit $fn			#@-RH7@#
		return 2												#@-RH7@#
	fi														#@-RH7@#
															#@-RH7@#
	if ! LC_ALL=C route -? 2>&1 | LC_ALL=C grep -q "(IPv6)"; then							#@-RH7@#
		ipv6_log $"'`which route`' (net-tools) not compiled for IPv6 - stop" crit $fn				#@-RH7@#
		return 2												#@-RH7@#
	fi														#@-RH7@#
															#@-RH7@#
	if ! which ip 2>&1 >/dev/null; then
		ipv6_log $"Utility 'ip' (from iproute-package) doesn't exist or isn't executable - stop" crit $fn
		return 2													
	else														#@-RH7@#
		if ! LC_ALL=C ip 2>&1 | LC_ALL=C grep -q "inet6"; then							#@-RH7@#
			ipv6_log $"'`which ip`' (iproute) not compiled for IPv6 - stop" crit $fn			#@-RH7@#
			return 2											#@-RH7@#
		fi													#@-RH7@#
	fi

	[ $[ $DEBUG_IPV6 & 4 ] = 0 ] || ipv6_log $"Ok" deb $fn							#@-DEB@#
														#@-DEB@#
	# Info about executing											#@-DEB@#
	[ $DEBUG_IPV6 -gt 0 ]  && echo $"Libary 'network-functions-ipv6' is executed in DEBUG_IPV6 mode: $DEBUG_IPV6" >&2 #@-DEB@#
														#@-DEB@#
	return 0
}


##### Get version of this function libary
# stdout: <version number YYYYMMDD>
getversion_ipv6_functions() {
	local version_ipv6_functions="`cat /etc/sysconfig/network-scripts/network-functions-ipv6 | LC_ALL=C grep "^# Version:" | awk '{ print $3 }' | sed 's/-//g' | sed 's/[A-Za-z]*$//g'`"
	echo $version_ipv6_functions
}


##### Wrapper for used binaries
## ifconfig
# $*: <arguments...>
# return code: result of execution
ipv6_exec_ifconfig() {
	local options=$*

	if [ $[ $DEBUG_IPV6 & 256 ] != 0 ]; then								#@-DEB@#
		echo "EXE:  ifconfig $options" >/dev/stderr							#@-DEB@#
	fi													#@-DEB@#
														#@-DEB@#
	if [ $[ $DEBUG_IPV6 & 2 ] != 0 ] ; then									#@-DEB@#
		echo "DRY: ifconfig $options" >/dev/stderr							#@-DEB@#
		return 0											#@-DEB@#
	fi													#@-DEB@#
														#@-DEB@#
	LC_ALL=C ifconfig $options 2>&1

	return $?
}


## route
#  $*: <arguments...>
# return code: result of execution
ipv6_exec_route() {
	local options=$*

	if [ $[ $DEBUG_IPV6 & 256 ] != 0 ]; then								#@-DEB@#
		echo "EXE:  route $options" >/dev/stderr							#@-DEB@#
	fi													#@-DEB@#
														#@-DEB@#
	if [ $[ $DEBUG_IPV6 & 2 ] != 0 ] ; then									#@-DEB@#
		echo "DRY: route $options" >/dev/stderr								#@-DEB@#
		return 0											#@-DEB@#
	fi													#@-DEB@#
														#@-DEB@#
	LC_ALL=C route $options 2>&1

	return $?
}


## ip
#  $*: <arguments...>
# return code: result of execution
ipv6_exec_ip() {
	local options=$*

	if [ $[ $DEBUG_IPV6 & 256 ] != 0 ]; then								#@-DEB@#
		echo "EXE:  ip $options" >/dev/stderr								#@-DEB@#
	fi													#@-DEB@#
														#@-DEB@#
	if [ $[ $DEBUG_IPV6 & 2 ] != 0 ] ; then									#@-DEB@#
		echo "DRY: ip $options" >/dev/stderr								#@-DEB@#
		return 0											#@-DEB@#
	fi													#@-DEB@#
														#@-DEB@#
	LC_ALL=C ip $options 2>&1

	return $?
}


## sysctl
#  $*: <arguments...>
# return code: result of execution
ipv6_exec_sysctl() {
	local options=$*

	if [ $[ $DEBUG_IPV6 & 256 ] != 0 ]; then								#@-DEB@#
		echo "EXE:  sysctl $options" >/dev/stderr							#@-DEB@#
	fi													#@-DEB@#
														#@-DEB@#
	if [ $[ $DEBUG_IPV6 & 2 ] != 0 ] ; then									#@-DEB@#
		echo "DRY: sysctl $options" >/dev/stderr							#@-DEB@#
		return 0											#@-DEB@#
	fi													#@-DEB@#
														#@-DEB@#
	LC_ALL=C sysctl "$options" 2>&1

	return $?
}


##### Control IPv6 forwarding

# Control IPv6 forwarding
#  $1: yes|no|on|off : control value
#  $2: [<interface>] : (optional), if not given, global IPv6 forwarding is set [OBSOLETE]
# return code: 0=ok 1=argument error 2=IPv6 test fails
ipv6_control_forwarding() {
	local fn="ipv6_control_forwarding"

	local fw_control=$1
	local fw_device=$2		# maybe empty

	if [ -z "$fw_control" ]; then
		ipv6_log $"Missing parameter 'forwarding control' (arg 1)" err $fn
		return 1
	fi

	if ! [ "$fw_control" = "yes" -o "$fw_control" = "no" -o "$fw_control" = "on" -o "$fw_control" = "off" ]; then
		ipv6_log $"Forwarding control parameter isn't valid '$fw_control' (arg 1)" err $fn
		return 1
	fi
	
	# Run IPv6 test													#@-RH7@#
	ipv6_test || return 2   

	if [ "$fw_control" = "yes" -o "$fw_control" = "on" ]; then
		local status=1
		local string="enabled"											#@-RH7@#
	else
		local status=0
		local string="disabled"											#@-RH7@#
	fi

	# Global control? (if no device is given)
	if [ -z "$fw_device" ]; then
		ipv6_log $"IPv6 forwarding is now globally '$string'" inf $fn						#@-RH7@#
		ipv6_exec_sysctl -w net.ipv6.conf.all.forwarding=$status >/dev/null
	fi
	
	# Per device control (not implemented in kernel)
	if [ ! -z "$fw_device" ]; then
		ipv6_log $"IPv6 forwarding per device cannot be controlled via sysctl - use netfilter6 instead" warn $fn
	fi
	
	return 0
}


##### Static IPv6 route configuration

# Set static IPv6 route
#  $1: <IPv6 network> : to route
#  $2: <IPv6 gateway> : over which $1 should be routed (if "::", gw will be skipped)
#  $3: [<Interface>] : (optional)
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem adding route
ipv6_add_route() {
	local fn="ipv6_add_route"

	local networkipv6=$1
	local gatewayipv6=$2
	local device=$3		# maybe empty

	if [ -z "$networkipv6" ]; then
		ipv6_log $"Missing parameter 'IPv6-network' (arg 1)" err $fn
		return 1
	fi

	if [ -z "$gatewayipv6" ]; then
		ipv6_log $"Missing parameter 'IPv6-gateway' (arg 2)" err $fn
		return 1
	fi

	# Run IPv6 test													#@-RH7@#
	ipv6_test || return 2   

	# Test, whether given IPv6 address is valid									#@-RH7@#
	ipv6_test_ipv6_addr_valid $networkipv6 || return 2
	ipv6_test_ipv6_addr_valid $gatewayipv6 || return 2

	if [ -z "$device" ]; then
		ipv6_log $"Add IPv6 route '$networkipv6' gateway '$gatewayipv6'" inf $fn				#@-RH7@#
		local returntxt="`ipv6_exec_ip -6 route add $networkipv6 via $gatewayipv6 metric 1`"
	else
		if [ "$gatewayipv6" = "::" ]; then
			ipv6_log $"Add IPv6 route '$networkipv6' through device '$device'" inf $fn			#@-RH7@#
			local returntxt="`ipv6_exec_ip -6 route add $networkipv6 dev $device metric 1`"
		else
			ipv6_log $"Add IPv6 route '$networkipv6' via gateway '$gatewayipv6' through device '$device'" inf $fn	#@-RH7@#
			local returntxt="`ipv6_exec_ip -6 route add $networkipv6 via $gatewayipv6 dev $device metric 1`"
		fi
	fi

	if [ ! -z "$returntxt" ]; then
		if echo $returntxt | grep -q "File exists"; then
			# Netlink: "File exists"
			ipv6_log $"Route already exists - skip" inf $fn							#@-RH7@#
			true
		elif echo $returntxt | grep -q "No route to host"; then
			# Netlink: "No route to host"
			ipv6_log $"'No route to host' adding route '$networkipv6' via gateway '$gatewayipv6' through device '$device'" warn $fn
			return 3
		else		
			ipv6_log $"Unknown error" warn $fn
			return 3
		fi
	fi

	return 0
}


# Delete a static IPv6 route
#  $1: <IPv6 network> : to route
#  $2: <IPv6 gateway> : over which $1 should be routed (if "::", gw will be skipped)
#  $3: [<Interface>] : (optional)
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem adding route
ipv6_del_route() {
	local fn="ipv6_del_route"

	local networkipv6=$1
	local gatewayipv6=$2
	local device=$3		# maybe empty

	if [ -z "$networkipv6" ]; then
		ipv6_log $"Missing parameter 'IPv6-network' (arg 1)" err $fn
		return 1
	fi

	if [ -z "$gatewayipv6" ]; then
		ipv6_log $"Missing parameter 'IPv6-gateway' (arg 2)" err $fn
		return 1
	fi

	# Run IPv6 test													#@-RH7@#
	ipv6_test testonly || return 2

	# Test, whether given IPv6 address is valid	
	ipv6_test_ipv6_addr_valid $networkipv6 || return 1
	ipv6_test_ipv6_addr_valid $gatewayipv6 || return 1

	if [ -z "$device" ]; then
		ipv6_log $"Delete IPv6 route '$networkipv' via gateway '$gatewayipv6'" inf $fn				#@-RH7@#
		ipv6_exec_ip -6 route del $networkipv6 via $gatewayipv6
		local result=$?
	else
		if [ "$gatewayipv6" = "::" ]; then
		    	ipv6_log $"Delete IPv6 route '$networkipv6' through device '$device'" inf $fn			#@-RH7@#
			ipv6_exec_ip -6 route del $networkipv6 dev $device
			local result=$?
		else
		    	ipv6_log $"Delete IPv6 route '$networkipv6' via gateway '$gatewayipv6' through device '$device'" inf $fn	#@-RH7@#
			ipv6_exec_ip -6 route del $networkipv6 via $gatewayipv6 dev $device
			local result=$?
		fi
	fi

	if [ $result -eq 2 ]; then
		# Netlink: "No such process"
		true
		ipv6_log $"Route '$networkipv6 gw $gatewayipv6 dev $device' doesn't exist" inf $fn			#@-RH7@#
	elif [ $result -ne 0 ]; then
		return 3
	fi

	return 0
}


# Delete all static IPv6 routes through a given interface
#  $1: <Interface>
#  $2: [<Gateway>] : to match (optional)
# return code: 0=ok 1=argument error 2=IPv6 test fails
ipv6_cleanup_routes() {
	local fn="ipv6_cleanup_routes"

	local device=$1
	local gatewaymatch=$2

	if [ -z "$device" ]; then
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
		return 1
	fi

	# Run IPv6 test													#@-RH7@#
	ipv6_test testonly || return 2   

	# Get all IPv6 routes through given interface and remove them
	ipv6_exec_route -A inet6 -n | LC_ALL=C grep "$device\W*$" | while read ipv6net nexthop flags metric ref use iface args; do
		if [ "$iface" = "$device" ]; then
			if [ ! -z "$gatewaymatch" ]; then
				# Test if given gateway matches
				if [ "$gatewaymatch" != "$nexthop" ]; then
					# No match, take next								#@-RH7@#
					continue
				fi
			fi
			# Only non addrconf (automatic installed) routes should be removed
			if echo $flags | LC_ALL=C grep -v -q "A"; then
    				ipv6_log $"Delete IPv6 route '$ipv6net' gateway '$nexthop' device '$iface'" inf $fn	#@-RH7@#
				ipv6_exec_route -A inet6 del $ipv6net gw $nexthop dev $iface
			fi
		fi
	done

	return 0
}


##### automatic tunneling configuration

## Configure automatic tunneling up
# return code: 0=ok 2=IPv6 test fails 3=major problem
ipv6_enable_autotunnel() {
	local fn="ipv6_enable_autotunnel"

	# Run IPv6 test													#@-RH7@#
	ipv6_test || return 2 

	# enable IPv6-over-IPv4 tunnels
	if ipv6_test_device_status sit0; then
		# already up, do nothing										#@-RH7@#
		true
	else
		# bring up basic tunnel device
	    	ipv6_log $"Bring up basic tunnel device 'sit0'" inf $fn							#@-RH7@#
		ipv6_exec_ifconfig sit0 up

		if [ $[ $DEBUG_IPV6 & 2 ] != 0 ] ; then								#@-DEB@#
			# Test, whether "up" has worked									#@-RH7@#
			if ! ipv6_test_device_status sit0; then
				ipv6_log $"Tunnel device 'sit0' enabling didn't work" err $fn
				return 3
			fi
		fi												#@-DEB@#

		# Set sysctls proper (regardless "default")
		ipv6_exec_sysctl -w net.ipv6.conf.sit0.forwarding=1 >/dev/null
		ipv6_exec_sysctl -w net.ipv6.conf.sit0.accept_ra=0 >/dev/null
		ipv6_exec_sysctl -w net.ipv6.conf.sit0.accept_redirects=0 >/dev/null
	fi
	
	return 0
}


## Configure automatic tunneling down
# return code: 0=ok 2=IPv6 test fails 3=major problem
ipv6_disable_autotunnel() {
	local fn="ipv6_disable_autotunnel"

	# Run IPv6 test													#@-RH7@#
	ipv6_test testonly || return 2

	if ipv6_test_device_status sit0; then
		# still up?												#@-RH7@#

		# disable IPv6-over-IPv4 tunnels (if a tunnel is no longer up)
		if ipv6_exec_route -A inet6 -n | LC_ALL=C grep "sit0\W*$" | awk '{ print $2 }' | LC_ALL=C grep -v -q "^::$"; then
			# still existing routes, skip shutdown of sit0
			true
		elif ipv6_exec_ip addr show dev sit0 | LC_ALL=C grep inet6 | awk '{ print $2 }' | LC_ALL=C grep -v -q '^::'; then
			# still existing IPv6 addresses, skip shutdown of sit0
			true
		else
			# take down basic tunnel device
			# Set sysctls proper 										#@-RH7@#
			ipv6_exec_sysctl -w net.ipv6.conf.sit0.forwarding=0 >/dev/null
			ipv6_exec_sysctl -w net.ipv6.conf.sit0.accept_ra=0 >/dev/null
			ipv6_exec_sysctl -w net.ipv6.conf.sit0.accept_redirects=0 >/dev/null
			
			ipv6_log $"Bring down basic tunnel device 'sit0'" inf $fn					#@-RH7@#
			ipv6_exec_ifconfig sit0 down

			if [ $[ $DEBUG_IPV6 & 2 ] = 0 ] ; then							#@-DEB@#
				# Test, whether "down" has worked							#@-RH7@#
				if ipv6_test_device_status sit0; then
					ipv6_log $"Tunnel device 'sit0' is still up" err $fn
					return 3
				fi
			fi											#@-DEB@#
		fi
	fi

	return 0
}	

#@-RH7@start#
##### static NBMA-styled tunnel configuration

## Configure a static tunnel in NBMA style up
#  $1: <Interface> : not needed - dummy
#  $2: <IPv4 address> : of foreign tunnel
#  $3: <IPv6 route> : through this tunnel
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
ipv6_add_nbma_tunnel() {
	local fn="ipv6_add_nbma_tunnel"
	
	local device=$1
	local addressipv4tunnel=$2
	local routeipv6=$3

	if [ -z "$device" ]; then
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
		return 1
	fi

	if [ -z "$addressipv4tunnel" ]; then
		ipv6_log $"Missing parameter 'IPv4-tunnel address' (arg 2)" err $fn
		return 1
	fi

	if [ -z "$routeipv6" ]; then
		ipv6_log $"Missing parameter 'IPv6-route' (arg 3)" err $fn
		return 1
	fi

	# Run IPv6 test													#@-RH7@#
	ipv6_test || return 2

	# Test, whether given IPv4 address is valid									#@-RH7@#
	ipv6_test_ipv4_addr_valid $addressipv4tunnel || return 1							#@-RH7@#
															#@-RH7@#
	# Test, whether given IPv6 address is valid	
	ipv6_test_ipv6_addr_valid $routeipv6 || return 1
	
	# enable general IPv6-over-IPv4 tunneling
	ipv6_enable_autotunnel
	if [ $? -ne 0 ]; then
		return 3
	fi
	
	ipv6_log $"Add NBMA-styled tunnel for '$routeipv6' to remote endpoint '$addressipv4tunnel' (virtual tunnel '$device')" inf $fn	#@-RH7@#
 	# Set up a tunnel
	ipv6_add_route $routeipv6 ::$addressipv4tunnel sit0
	if [ $? -ne 0 ]; then
		local retval=3
	else
		local retval=0
	fi

	return $retval
}	


## Configure a static tunnels in NBMA style down
#  $1: <Interface> : not used - dummy
#  $2: <IPv4 address> : of foreign tunnel
#  $3: <IPv6 route> : through this tunnel
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
ipv6_del_nbma_tunnel() {
	local fn="ipv6_del_nbma_tunnel"

	local device=$1
	local addressipv4tunnel=$2
	local routeipv6=$3

	if [ -z "$device" ]; then
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
		return 1
	fi

	if [ -z "$addressipv4tunnel" ]; then
		ipv6_log $"Missing parameter 'IPv4-tunnel address' (arg 2)" err $fn
		return 1
	fi

	if [ -z "$routeipv6" ]; then
		ipv6_log $"Missing parameter 'IPv6-route' (arg 3)" err $fn
		return 1
	fi

	# Run IPv6 test													#@-RH7@#
	ipv6_test testonly || return 2

	# Test, whether given IPv4 address is valid									#@-RH7@#
	ipv6_test_ipv4_addr_valid $addressipv4tunnel || return 1							#@-RH7@#
															#@-RH7@#
	ipv6_log $"Delete NBMA-styled tunnel for '$routeipv6' to remote endpoint '$addressipv4tunnel' (virtual tunnel '$device')" inf $fn #@-RH7@#
															#@-RH7@#
	# Delete a NBMA-styled tunnel
	ipv6_del_route $routeipv6 ::$addressipv4tunnel sit0
	if [ $? -ne 0 ]; then
		local retval=3
	else
		local retval=0
	fi

	# disable IPv6-over-IPv4 tunneling (if this was the last tunnel)
	ipv6_disable_autotunnel

	return $retval
}	


## Remove all NBMA-styled IPv6 tunnels for a given tunnel endpoint
#  $1: <Interface> : not used - dummy
#  $2: <IPv4-tunnel address>
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
ipv6_cleanup_nbma_tunnels_to_dest() {
	local fn="ipv6_cleanup_nbma_tunnels_to_dest"

	local device=$1
	local tunnelv4=$2

	if [ -z "$device" ]; then
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
		return 1
	fi

	if [ -z "$tunnelv4" ]; then
		ipv6_log $"Missing parameter 'IPv4-tunnel address' (arg 2)" err $fn
		return 1
	fi

	# Run IPv6 test													#@-RH7@#
	ipv6_test testonly || return 2

	# Test, whether given IPv4 address is valid									#@-RH7@#
	ipv6_test_ipv4_addr_valid $tunnelv4 || return 1									#@-RH7@#
															#@-RH7@#
	# Get all IPv6 routes through given interface and remove them
	ipv6_exec_route -A inet6 -n | LC_ALL=C grep "::$tunnelv4" | while read ipv6net nexthop flags metric ref use iface args; do
		if [ "::$tunnelv4" = "$nexthop" ]; then
			if echo $flags | LC_ALL=C grep -v -q "A"; then
				# Only non addrconf (automatic installed) routes should be removed
				 ipv6_del_nbma_tunnel $device $tunnelv4 $ipv6net
			fi
		fi
	done

	# disable IPv6-over-IPv4 tunneling (if this was the last tunnel)
	ipv6_disable_autotunnel

	return 0
}
#@-RH7@end#


##### Test, whether an IPv6 address exists on an interface
# $1: <Interface> : to testing
# $2: <IPv6 Address> : to test (without prefix length)
# $3: <Prefix length> : of address $2
# return values: 0=ok (exists) 1=argument error 3=major problem 10=not exists
ipv6_test_addr_exists_on_device() {
	local fn="ipv6_test_addr_exists_on_device"

	local testdevice=$1
	local testaddr=$2
	local testprefix=$3

	if [ -z "$testdevice" ]; then
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
		return 1
	fi
	if [ -z "$testaddr" ]; then
		ipv6_log $"Missing parameter 'IPv6 address to test' (arg 2)" err $fn
		return 1
	fi
	if [ -z "$testprefix" ]; then
		ipv6_log $"Missing parameter 'IPv6 address prefix length' (arg 3)" err $fn
		return 1
	fi

	# Run IPv6 test													#@-RH7@#
	ipv6_test testonly || return 2

	[ $[ $DEBUG_IPV6 & 8 ] = 0 ] || ipv6_log $"Called with device '$testdevice', addr '$testaddr', prefix length '$testprefix'" deb $fn	#@-DEB@#
														#@-DEB@#
	if [ "$EXISTS_ipv6calc" = "yes" ]; then
		# Using ipv6calc and compare against /proc/net/if_inet6

		[ $[ $DEBUG_IPV6 & 8 ] = 0 ] || ipv6_log $"Convert address '$testaddr/$testprefix'" deb $fn	#@-DEB@#
		local convertresult="`ipv6calc --addr2if_inet6 $testaddr/$testprefix`"
		[ $[ $DEBUG_IPV6 & 8 ] = 0 ] || ipv6_log $"Result '$convertresult'" deb $fn			#@-DEB@#

		# Split in address, scope and prefix length
		local test_addr="`echo $convertresult | awk '{ print $1 }'`"
		local test_scope="`echo $convertresult | awk '{ print $2 }'`"
		local test_prefixlength="`echo $convertresult | awk '{ print $3 }'`"
		[ $[ $DEBUG_IPV6 & 8 ] = 0 ] || ipv6_log $"Splitted into (all hex values) address '$test_addr', scope '$test_scope', prefix length '$test_prefixlength'" deb $fn					#@-DEB@#

		if [ -z "$test_prefixlength" ]; then
			local testresult="`LC_ALL=C grep "$test_addr .. .. $test_scope .." /proc/net/if_inet6 | LC_ALL=C grep $testdevice$`"
		else
			local testresult="`LC_ALL=C grep "$test_addr .. $test_prefixlength $test_scope .." /proc/net/if_inet6 | LC_ALL=C grep $testdevice$`"
		fi
		[ $[ $DEBUG_IPV6 & 8 ] = 0 ] || ipv6_log $"Exist test: '$testresult'" deb $fn				#@-DEB@#
		if [ ! -z "$testresult" ]; then
			# exists
			return 0
		else
			# not exits
			return 10
		fi
	else
		# low budget version, only works if given address is in equal form like "ip" displays
		local testresult="`ipv6_exec_ip addr show dev $testdevice | LC_ALL=C grep inet6 | awk '{ print $2 }' | LC_ALL=C grep -i "^$testaddr/$testprefix$"`"
		[ $[ $DEBUG_IPV6 & 8 ] = 0 ] || ipv6_log $"Exist test: '$testresult'" deb $fn				#@-DEB@#
		if [ ! -z "$testresult" ]; then
			# exits
			return 0
		else
			# not exits
			return 10
		fi
	fi

	# Normally this lines not reached
	return 3
}


##### Test, whether an IPv6 address with given prefix exists on an interface
# $1: <Interface> : to test
# $2: <Prefix>
# return values: 0=ok (exists) 1=argument error 3=major problem 10=not exists
ipv6_test_addrprefix_exists_on_device() {
	local fn="ipv6_test_addr_prefix_exists_on_device"

	local testdevice=$1
	local testaddrprefix=$2

	if [ -z "$testdevice" ]; then
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
		return 1
	fi
	if [ -z "$testaddrprefix" ]; then
		ipv6_log $"Missing parameter 'IPv6 address prefix' (arg 2)" err $fn
		return 1
	fi

	# Run IPv6 test													#@-RH7@#
	ipv6_test testonly || return 2

	[ $[ $DEBUG_IPV6 & 8 ] = 0 ] || ipv6_log $"Called with device '$testdevice', addrprefix '$testaddrprefix'" deb $fn	#@-DEB@#
														#@-DEB@#
	# For the moment: low budget version, only works if given address is in equal form like "ip" displays
	local testresult="`ipv6_exec_ip addr show dev $testdevice | LC_ALL=C grep inet6 | awk '{ print $2 }' | LC_ALL=C grep -i "^$testaddrprefix:"`"
	[ $[ $DEBUG_IPV6 & 8 ] = 0 ] || ipv6_log $"Exist test: '$testresult'" deb $fn				#@-DEB@#
	if [ ! -z "$testresult" ]; then
		# exists
		return 0
	else
		# not exists
		return 10
	fi

	# Normally this lines not reached
	return 3
}


##### Interface configuration

## Add an IPv6 address for given interface
#  $1: <Interface>
#  $2: <IPv6 address>
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
ipv6_add_addr_on_device() {
	local fn="ipv6_add_addr_on_device"

	local device=$1
	local address=$2

	if [ -z "$device" ]; then
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
		return 1
	fi

	if [ -z "$address" ]; then
		ipv6_log $"Missing parameter 'IPv6-address' (arg 2)" err $fn
		return 1
	fi

	# Run IPv6 test													#@-RH7@#
	ipv6_test || return 2

	# Test, whether given IPv6 address is valid									#@-RH7@#	
	ipv6_test_ipv6_addr_valid $address || return 1

	# Test status of interface											#@-RH7@#
	ipv6_test_device_status $device
	local result=$?

	if [ "$result" = "0" ]; then
		# Interface is already up										#@-RH7@#
		true
	elif [ "$result" != "11" ]; then
		# Interface doesn't exist										#@-RH7@#
		ipv6_log $"Device '$device' doesn't exist" err $fn
		return 3
	else
		# no IPv4 for this interface, interface is still down, bring it up ...					#@-RH7@#
		ipv6_exec_ifconfig $device up

		if [ $[ $DEBUG_IPV6 & 2 ] = 0 ] ; then								#@-DEB@#
			# Test, whether "up" has worked									#@-RH7@#
			if ! test_interface_status $device; then
				ipv6_log $"Device '$device' enabling didn't work" err $fn
				return 3
			fi
		fi												#@-DEB@#
	fi

	# Extract address parts
	local prefixlength_implicit="`echo $address | awk -F/ '{ print $2 }'`"
	local address_implicit="`echo $address | awk -F/ '{ print $1 }'`"

	# Check prefix length and using '64' as default
	if [ -z "$prefixlength_implicit" ]; then
		ipv6_log $"No prefix length specified, using '64' as default" inf $fn					#@-RH7@#
		local prefixlength_implicit="64"
		local address="$address_implicit/$prefixlength_implicit"
	fi

	# Only add if address does not already exist
	ipv6_test_addr_exists_on_device $device $address_implicit $prefixlength_implicit
	local result=$?

	if [ $result -ne 0 -a $result -ne 10 ]; then
		return 3
	fi

	if [ $result -eq 0 ]; then
		ipv6_log $"Given IPv6 address '$address' for dev '$device' is already configured - skip" inf $fn	#@-RH7@#
		true
	else
		ipv6_log $"Add for dev '$device' IPv6 address '$address'" inf $fn					#@-RH7@#
		ipv6_exec_ifconfig $device inet6 add $address || return 3
	fi

	return 0
}


## Remove all IPv6 routes and addresses on given interface (cleanup to prevent kernel crashes)
#  $1: <Interface>
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
ipv6_cleanup_device() {
	local fn="ipv6_cleanup_device"

	local device=$1

	if [ -z "$device" ]; then
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
		return 1
	fi

	# Run IPv6 test													#@-RH7@#
	ipv6_test testonly || return 2

	# Remove all IPv6 routes through this device (but not "lo")
	if [ "$device" != "lo" ]; then
		ipv6_log $"Flush all IPv6 routes on device '$device'" inf $fn						#@-RH7@#
		ipv6_exec_ip -6 route flush dev $device >/dev/null 2>&1
	fi

	# Remove all IPv6 addresses on this interface
	ipv6_log $"Flush all IPv6 addresses on device '$device'" inf $fn						#@-RH7@#
	ipv6_exec_ip -6 addr flush dev $device >/dev/null 2>&1

	return 0
}


## Remove an IPv6 address on given interface
#  $1: <Interface>
#  $2: <IPv6 address>
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
ipv6_del_addr_on_device() {
	local fn="ipv6_del_addr_on_device"

	local device=$1
	local address=$2

	if [ -z "$device" ]; then
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
		return 1
	fi

	if [ -z "$address" ]; then
		ipv6_log $"Missing parameter 'IPv6 address' (arg 2)" err $fn
		return 1
	fi

	# Run IPv6 test													#@-RH7@#
	ipv6_test testonly || return 2

	# Test, whether given IPv6 address is valid									#@-RH7@#
	ipv6_test_ipv6_addr_valid $address || return 1

	# Extract address parts
	local prefixlength_implicit="`echo $address | awk -F/ '{ print $2 }'`"
	local address_implicit="`echo $address | awk -F/ '{ print $1 }'`"

	# Check prefix length and using '64' as default
	if [ -z "$prefixlength_implicit" ]; then
		ipv6_log $"No prefix length specified, using '64' as default" inf $fn					#@-RH7@#
		local prefixlength_implicit="64"
		local address="$address_implicit/$prefixlength_implicit"
	fi

	# Only remove, if address exists and is not link-local (prevents from kernel crashing)
	ipv6_test_addr_exists_on_device $device $address_implicit $prefixlength_implicit
	local result=$?

	if [ $result -ne 0 -a $result -ne 10 ]; then
		return 3
	fi

	if [ $result -eq 0 ]; then
		ipv6_log $"Delete on device '$device' IPv6 address '$address'" inf $fn					#@-RH7@#
		ipv6_exec_ifconfig $device inet6 del $address || return 3
	else
		ipv6_log $"Given IPv6 address '$address' doesn't exist on device '$device'" inf $fn			#@-RH7@#
		true
	fi
    
	return 0
}


##### Some address test functions

## Test a given IPv6 address for validity
#  $1: <IPv6 address>
#  $2: [quiet] : (optional) don't display error message
# return code: 0=ok 1=argument error 10=not valid
ipv6_test_ipv6_addr_valid() {
	local fn="ipv6_test_ipv6_addr_valid"

	local testipv6addr_valid=$1
	local modequiet=$2

	[ $[ $DEBUG_IPV6 & 64 ] = 0 ] || ipv6_log $"Test IPv6 for validity: '$testipv6addr_valid'" deb $fn	#@-DEB@#
														#@-DEB@#
	if [ -z "$testipv6addr_valid" ]; then
		# nothing to test											#@-RH7@#
		return 1
	fi
	if [ ! -z "$modequiet" ]; then
		if [ "$modequiet" != "quiet" ]; then
			ipv6_log $"Parameter '$modequiet' for 'quiet' mode is not valid (arg 2)" err $fn
			return 1
		fi
	fi

	# Extract parts
	local prefixlength_implicit="`echo $testipv6addr_valid | awk -F/ '{ print $2 }'`"
	local address_implicit="`echo $testipv6addr_valid | awk -F/ '{ print $1 }'`"

	if [ "$EXISTS_ipv6calc" = "yes" ]; then
		if ! ipv6calc --addr2uncompaddr $testipv6addr_valid >/dev/null 2>&1; then
			if [ "$modequiet" != "quiet" ]; then
				ipv6_log $"Given IPv6 address '$testipv6addr_valid' is not valid" err $fn
			fi
			return 10
		fi
	else
		# Test for a valid format
		if ! echo "$address_implicit" | LC_ALL=C egrep -q '^[:xdigit:]|[:\.]*$'; then
			if [ "$modequiet" != "quiet" ]; then
				ipv6_log $"Given IPv6 address '$testipv6addr_valid' is not valid" err $fn
			fi
			return 10
		fi
	fi

	# Test for prefix length
	if [ -z "$prefixlength_implicit" ]; then
		if echo "$testipv6addr_valid" | LC_ALL=C grep "/$"; then
			# Trailing "/", but no value
			if [ "$modequiet" != "quiet" ]; then
				ipv6_log $"Missing prefix length for given address '$testipv6addr_valid'" err $fn
			fi
			return 10
		else
			return 0
		fi
	elif [ $prefixlength_implicit -lt 0 -o $prefixlength_implicit -gt 128 ]; then
		if [ "$modequiet" != "quiet" ]; then
			ipv6_log $"On given address '$testipv6addr_valid' the prefix length is out of range (valid: 0-128)" err $fn
		fi
		return 10
	fi

	[ $[ $DEBUG_IPV6 & 64 ] = 0 ] || ipv6_log $"IPv6 addres is valid" deb $fn				#@-DEB@#
														#@-DEB@#
	return 0
}


## Test a given IPv4 address for validity
#  $1: <IPv4 address>
#  $2: [quiet] : (optional) don't display error message
# return code: 0=ok 1=argument error 10=not valid
ipv6_test_ipv4_addr_valid() {
	local fn="ipv6_test_ipv4_addr_valid"

	local testipv4addr_valid=$1
	local modequiet=$2

	[ $[ $DEBUG_IPV6 & 64 ] = 0 ] || ipv6_log $"Test IPv4 for validity: '$testipv4addr_valid'" deb $fn	#@-DEB@#
														#@-DEB@#
	if [ -z "$testipv4addr_valid" ]; then
		# nothing to test											#@-RH7@#
		return 1
	fi
	if [ ! -z "$modequiet" ]; then
		if [ "$modequiet" != "quiet" ]; then
			ipv6_log $"Parameter '$modequiet' for 'quiet' mode is not valid (arg 2)" err $fn
			return 1
		fi
	fi

	# Test for a valid format
	if echo "$testipv4addr_valid" | LC_ALL=C egrep -q -v '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$'; then
		if [ "$modequiet" != "quiet" ]; then
			ipv6_log $"Given IPv4 address '$testipv4addr_valid' has no proper format" err $fn
		fi
		return 10
	fi

	# Test for valid IPv4 address parts
	local number1="`echo $testipv4addr_valid | awk -F. '{ print $1 }'`"
	local number2="`echo $testipv4addr_valid | awk -F. '{ print $2 }'`"
	local number3="`echo $testipv4addr_valid | awk -F. '{ print $3 }'`"
	local number4="`echo $testipv4addr_valid | awk -F. '{ print $4 }'`"
	local c=1
	for number in "$number1" "$number2" "$number3" "$number4"; do
		if [ $number -lt 0 -o $number -gt 255 ]; then
			if [ "$modequiet" != "quiet" ]; then
				ipv6_log $"Part $c of given IPv4 address '$testipv4addr_valid' is out of range" err $fn
			fi
			return 10
		fi
		local c=$[ $c + 1 ]
	done

	[ $[ $DEBUG_IPV6 & 64 ] = 0 ] || ipv6_log $"IPv4 address is valid" deb $fn				#@-DEB@#
														#@-DEB@#
	return 0
}


## Test a given IPv4 address for not a private but unicast one
#  $1: <IPv4 address>
# return code: 0=ok 1=argument error 10=private or not unicast
ipv6_test_ipv4_addr_global_usable() {
	local fn="ipv6_test_ipv4_addr_global_usable"

	local testipv4addr_globalusable=$1

	[ $[ $DEBUG_IPV6 & 64 ] = 0 ] || ipv6_log $"Test whether IPv4 address is globally usable: '$testipv4addr_globalusable'" deb $fn	#@-DEB@#

	if [ -z "$testipv4addr_globalusable" ]; then
		# nothing to test											#@-RH7@#
		return 1
	fi

	# Test for a valid IPv4 address											#@-RH7@#
	ipv6_test_ipv4_addr_valid $testipv4addr_globalusable || return 1						#@-RH7@#
															#@-RH7@#
	# Test for a globally usable IPv4 address now
	if [ "$EXISTS_ipcalc" = "yes" ]; then										#@-RH7@#
		# use ipcalc, easier for testing									#@-RH7@#
		# test 0.0.0.0/8
		ipcalc --network $testipv4addr_globalusable 255.0.0.0   | LC_ALL=C grep -q "NETWORK=0\.0\.0\.0"     && return 10
		# test 10.0.0.0/8     (RFC 1918 / private)
		ipcalc --network $testipv4addr_globalusable 255.0.0.0   | LC_ALL=C grep -q "NETWORK=10\.0\.0\.0"    && return 10
		# test 127.0.0.0/8    (loopback)
		ipcalc --network $testipv4addr_globalusable 255.0.0.0   | LC_ALL=C grep -q "NETWORK=127\.0\.0\.0"   && return 10
		# test 169.254.0.0/16 (APIPA / DHCP link local)
		ipcalc --network $testipv4addr_globalusable 255.255.0.0 | LC_ALL=C grep -q "NETWORK=169\.254\.0\.0" && return 10
		# test 172.16.0.0/12  (RFC 1918 / private)
		ipcalc --network $testipv4addr_globalusable 255.240.0.0 | LC_ALL=C grep -q "NETWORK=172\.16\.0\.0"  && return 10
		# test 192.168.0.0/16 (RFC 1918 / private)
		ipcalc --network $testipv4addr_globalusable 255.255.0.0 | LC_ALL=C grep -q "NETWORK=192\.168\.0\.0" && return 10
		# test 224.0.0.0/3    (multicast and reserved, broadcast)
		ipcalc --network $testipv4addr_globalusable 224.0.0.0   | LC_ALL=C grep -q "NETWORK=224\.0\.0\.0"   && return 10
	else														#@-RH7@#
		# low budget tester, perhaps not always correct working							#@-RH7@#
		local number1="`echo $testipv4addr_globalusable | awk -F. '{ print $1 }'`"				#@-RH7@#
		local number2="`echo $testipv4addr_globalusable | awk -F. '{ print $2 }'`"				#@-RH7@#
		if [ -z "$number1" -o -z "$number2" ]; then								#@-RH7@#
			# hmm, not an IPv4 address									#@-RH7@#
			return 1											#@-RH7@#
		fi													#@-RH7@#
		# test 0.0.0.0/8 											#@-RH7@#
		[ $number1 -eq 0 ]   && return 10									#@-RH7@#
		# test 10.0.0.0/8     (RFC 1918 / private)								#@-RH7@#
		[ $number1 -eq 10 ]  && return 10									#@-RH7@#
		# test 127.0.0.0/8    (loopback)									#@-RH7@#
		[ $number1 -eq 127 ] && return 10									#@-RH7@#
		# test 169.254.0.0/16 (APIPA / DHCP link local)								#@-RH7@#
		[ $number1 -eq 169 -a $number2 -eq 254 ] && return 10							#@-RH7@#
		# test 172.16.0.0/12  (RFC 1918 / private)								#@-RH7@#
		[ $number1 -eq 172 -a $number2 -ge 16 -a $number2 -le 31 ] && return 10					#@-RH7@#
		# test 192.168.0.0/16 (RFC 1918 / private)								#@-RH7@#
		[ $number1 -eq 192 -a $number2 -eq 168 ] && return 10							#@-RH7@#
		# test 224.0.0.0/3 (multicast and reserved, broadast)							#@-RH7@#
		[ $number1 -ge 224 ] && return 10									#@-RH7@#
	fi														#@-RH7@#
	
	[ $[ $DEBUG_IPV6 & 64 ] = 0 ] || ipv6_log $"IPv4 address is globally usable" deb $fn			#@-DEB@#
	return 0
}


## Test a given device for status
#  $1: <Interface>
# return code: 0=ok 1=argument error 10=not exists 11=down
ipv6_test_device_status() {
	local fn="ipv6_test_device_status"

	local device=$1

	if [ -z "$device" ]; then
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
		return 1
	fi

	# Test if device exists
	if ! ipv6_exec_ifconfig $device >/dev/null; then
		# not exists
		return 10
	fi

	# Test if device is up
	if ipv6_exec_ifconfig $device | LC_ALL=C grep -q "UP "; then
		# up
		return 0
	else
		# down
		return 11
	fi
}


## Create 6to4 prefix
#  $1: <IPv4 address>
# stdout: <6to4address>
# return code: 0=ok 1=argument error
ipv6_create_6to4_prefix() {
	local fn="ipv6_create_6to4_prefix"

	local ipv4addr=$1

	if [ -z "$ipv4addr" ]; then
		ipv6_log $"Missing parameter 'IPv4 address' (arg 1)" stderr.err $fn
	fi

	# Test addresses												#@-RH7@#
	ipv6_test_ipv4_addr_valid $ipv4addr || return 1									#@-RH7@#
															#@-RH7@#
	local major1="`echo $ipv4addr | awk -F. '{ print $1 }'`"
	local minor1="`echo $ipv4addr | awk -F. '{ print $2 }'`"
	local major2="`echo $ipv4addr | awk -F. '{ print $3 }'`"
	local minor2="`echo $ipv4addr | awk -F. '{ print $4 }'`"

	if [ -z "$major1" -o -z "$minor1" -o -z "$major2" -o -z "$minor2" ]; then
		return 1
	fi

	if [ $major1 -eq 0 ]; then
		local block1="`printf "%x" $minor1`"	
	else 
		local block1="`printf "%x%02x" $major1 $minor1`"
	fi 
	if [ $major2 -eq 0 ]; then
		local block2="`printf "%x" $minor2`"	
	else 
		local block2="`printf "%x%02x" $major2 $minor2`"
	fi 

	local prefix6to4="2002:$block1:$block2"

	echo "$prefix6to4"
	return 0
}


## Check and create 6to4 tunnel relay address
#  $1: <IPv4 address|IPv6to4 address>
# stdout: <tunnel relay address>
# return code: 0=ok 1=argument error
ipv6_create_6to4_relay_address() {
	local fn="ipv6_create_6to4_relay_address"

	local addr=$1

	if [ -z "$addr" ]; then
		ipv6_log $"Missing parameter 'address' (arg 1)" stderr.err $fn
		return 1
	fi

	# Check
	if ipv6_test_ipv4_addr_valid $addr quiet; then
		# ok, a IPv4 one
		if ipv6_test_ipv4_addr_global_usable $addr; then
			# IPv4 globally usable
			local ipv6to4_relay="::$addr"
		else
			ipv6_log $"Given address '$addr' is not a global IPv4 one (arg 1)" stderr.err $fn
			return 1
		fi
	elif ipv6_test_ipv6_addr_valid $addr quiet; then								#@-RH7@#
		# ok, IPv6 one												#@-RH7@#
		if echo $addr | LC_ALL=C grep -q "^2002:"; then								#@-RH7@#
			# IPv6 address is a 6to4 (further tests not be implemented at the moment)			#@-RH7@#
			local ipv6to4_relay="$addr"									#@-RH7@#
		else													#@-RH7@#
			ipv6_log $"Given address '$addr' is not a IPv6to4 one (arg 1)" stderr.err $fn			#@-RH7@#
			return 1											#@-RH7@#
		fi													#@-RH7@#
	else
		ipv6_log $"Given address '$addr' is not either a valid IPv4 or an IPv6to4 one (arg 1)" stderr.err $fn	#@-RH7@#
#@+RH7@#		ipv6_log $"Given address '$addr' is not a valid IPv4 one (arg 1)" stderr.err $fn
		return 1
	fi

	echo "$ipv6to4_relay"

	return 0
}


##### 6to4 tunneling setup

## Configure 6to4 tunneling up
#  $1: <Interface> : (sit0|tun6to4 implicit select the method using sit0 or a dedicated device)				#@-RH7@#
#@+RH7@##  $1: <Interface> : only "tun6to4" is supported
#  $2: <IPv4 address> : global address of local interface
#  $3: [<IPv6 suffix>] : for 6to4 prefix (optional, default is "::1")
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
ipv6_add_6to4_tunnel() {
	local fn="ipv6_add_6to4_tunnel"

	local device=$1
	local localipv4=$2
	local localipv6to4suffix=$3

	if [ -z "$device" ]; then
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
		return 1
	fi

	if [ -z "$localipv4" ]; then
		ipv6_log $"Missing parameter 'local IPv4 address' (arg 2)" err $fn
		return 1
	fi

	# Check device
	if [ "$device" != "sit0" -a "$device" != "tun6to4" ]; then							#@-RH7@#
#@+RH7@#	if [ "$device" != "tun6to4" ]; then
		ipv6_log $"Given device '$device' is not supported (arg 1)" err $fn
		return 1
	fi

	# Run IPv6 test													#@-RH7@#
	ipv6_test || return 2

	# Generate 6to4 address
	local prefix6to4="`ipv6_create_6to4_prefix $localipv4`"
	ipv6_log $"Generated 6to4 prefix '$prefix6to4' from '$localipv4'" inf $fn					#@-RH7@#
	if [ $? -ne 0 -o -z "$prefix6to4" ]; then
		return 3
	fi

	if [ -z "$localipv6to4suffix" ]; then
		local address6to4="${prefix6to4}::1/16"
	else
		local address6to4="${prefix6to4}::${localipv6to4suffix}/16"
	fi

#@-RH7@start#
	if [ "$device" = "sit0" ]; then
		# Enable generic IPv6-over-IPv4 tunneling
		ipv6_enable_autotunnel

		# Add 6to4 address on generic device sit0
		ipv6_add_addr_on_device sit0 $address6to4	
		if [ $? -ne 0 ]; then
			local retval=3
		else
			local retval=0
		fi
	elif [ "$device" = "tun6to4" ]; then
#@-RH7@end#
		ipv6_add_tunnel_device tun6to4 0.0.0.0 $address6to4 $ipv4addr
		if [ $? -ne 0 ]; then
			local retval=3
		else
			local retval=0
		fi
#@-RH7@start#
	fi
#@-RH7@end#

	[ $[ $DEBUG_IPV6 & 1 ] = 0 ] || set +x									#@-DEB@#
														#@-DEB@#
	return $retval
}	


## Configure all 6to4 tunneling down
#  $1: <Interface> : (sit0|tun6to4 implicit select the method)								#@-RH7@#
#@+RH7@##  $1: <Interface> : only "tun6to4" is supported
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
ipv6_cleanup_6to4_tunnels() {
	local fn="ipv6_cleanup_6to4_tunnels"

	local device=$1

	if [ -z "$device" ]; then
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
		return 1
	fi

	# Check device
	if [ "$device" != "sit0" -a "$device" != "tun6to4" ]; then							#@-RH7@#
#@+RH7@#	if [ "$device" != "tun6to4" ]; then
		ipv6_log $"Given device '$device' is not supported (arg 1)" err $fn
		return 1
	fi

	# Run IPv6 test													#@-RH7@#
	ipv6_test testonly || return 2

#@-RH7@start#
	if [ "$device" = "sit0" ]; then
		# Get all configured 6to4 addresses and delete them
		ipv6_exec_ip addr show dev sit0 | LC_ALL=C grep inet6 | awk '{ print $2 }' | LC_ALL=C grep "^2002:" | while read ipv6to4addr; do
			ipv6_del_addr_on_device sit0 $ipv6to4addr
		done

		# Try to disable general IPv6-over-IPv4 tunneling
		ipv6_disable_autotunnel
	elif [ "$device" = "tun6to4" ]; then
#@-RH7@end#
		ipv6_del_tunnel_device tun6to4
#@-RH7@start#
	fi
#@-RH7@end#

	[ $[ $DEBUG_IPV6 & 1 ] = 0 ] || set +x									#@-DEB@#
														#@-DEB@#
	return 0
}


## Configure 6to4 tunneling down
#  $1: <Interface> : (sit0|tun6to4 implicit select the method)								#@-RH7@#
#@+RH7@##  $1: <Interface> : only "tun6to4" is supported
#  $2: <IPv4 address> : global address of local interface
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
ipv6_del_6to4_tunnel() {
	local fn="ipv6_del_6to4_tunnel"

	local device=$1
	local localipv4=$2

	if [ -z "$device" ]; then
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
		return 1
	fi

	if [ -z "$localipv4" ]; then
		ipv6_log $"Missing parameter 'local IPv4 address' (arg 2)" err $fn
		return 1
	fi

	# Check device
	if [ "$device" != "sit0" -a "$device" != "tun6to4" ]; then							#@-RH7@#
#@+RH7@#	if [ "$device" != "tun6to4" ]; then
		ipv6_log $"Given device '$device' is not supported (arg 1)" err $fn
		return 1
	fi

	# Run IPv6 test													#@-RH7@# 
	ipv6_test || return 2

	# generate 6to4 address
	local prefix6to4="`ipv6_create_6to4_prefix $localipv4`"
	ipv6_log $"Generated 6to4 prefix '$prefix6to4' from '$localipv4'" inf $fn					#@-RH7@#
	if [ $? -ne 0 -o -z "$prefix6to4" ]; then
		return 3
	fi

	if [ -z "$localipv6to4suffix" ]; then
		local address6to4="$prefix6to4::1/16"
	else
		local address6to4="${prefix6to4}::${localipv6to4suffix}/16"
	fi

#@-RH7@start#
	if [ "$device" = "sit0" ]; then
		ipv6_del_addr_on_device sit0 $address6to4	
		if [ $? -ne 0 ]; then
			return 3
		fi

		# Try to disable general IPv6-over-IPv4 tunneling
		ipv6_disable_autotunnel
		if [ $? -ne 0 ]; then
			local retval=3
		else
			local retval=0
		fi
	elif [ "$device" = "tun6to4" ]; then
#@-RH7@end#
		ipv6_del_tunnel_device tun6to4
		local retval=$?
#@-RH7@start#
	fi
#@-RH7@end#

	[ $[ $DEBUG_IPV6 & 1 ] = 0 ] || set +x									#@-DEB@#
														#@-DEB@#
	return $retval
}	


## Configure a static tunnel device up
#  $1: <Interface>
#  $2: <IPv4 address> : of foreign tunnel
#  $3: [<IPv6 address>] : local one of a P-t-P tunnel (optional)
#  $4: [<IPv4 address>] : local one of tunnel (optional)
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
ipv6_add_tunnel_device() {
	local fn="ipv6_add_tunnel_device"

	local device=$1
	local addressipv4tunnel=$2
	local addressipv6local=$3
	local addressipv4tunnellocal=$4
	
	if [ -z "$device" ]; then
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
		return 1
	fi

	if [ -z "$addressipv4tunnel" ]; then
		ipv6_log $"Missing parameter 'IPv4-tunnel address' (arg 2)" err $fn 
		return 1
	fi

	if [ -z "$addressipv4tunnellocal" ]; then
		local addressipv4tunnellocal="any"
	fi
 
	# Run IPv6 test													#@-RH7@# 
	ipv6_test || return 2   

	# Test, whether given IPv4 address is valid									#@-RH7@#
	ipv6_test_ipv4_addr_valid $addressipv4tunnel || return 1							#@-RH7@#
															#@-RH7@#
	ipv6_log $"Create a new tunnel device '$device' to remote endpoint '$addressipv4tunnel', local IPv4 is '$addressipv4tunnellocal'" inf $fn	#@-RH7@#
	if ! ipv6_test_device_status $device; then
		# Get default TTL											#@-RH7@#
		local ttldefault="`ipv6_exec_sysctl net.ipv4.ip_default_ttl | awk '{ print $3 }'`"
		if [ -z "$ttldefault" ]; then
			local ttldefault=64
		fi

		# Test whether remote IPv4 address was already applied to another tunnel (does not catch IPv4 addresses with leading 0's)
		ipv6_exec_ip tunnel show 2>/dev/null | LC_ALL=C grep $addressipv4tunnel | while read dev type tag remote tag local tag ttl rest; do
			local devnew="`echo $dev | sed 's/:$//g'`"
			if [ "$remote" = "$addressipv4tunnel" ]; then
				ipv6_log $"Given remote address '$addressipv4tunnel' on tunnel device '$device' is already configured on device '$devnew'" err $fn
				return 3
			fi
		done
		if [ $? -ne 0 ]; then
			return 3
		fi

		ipv6_exec_ip tunnel add $device mode sit ttl $ttldefault remote $addressipv4tunnel local $addressipv4tunnellocal

		# Test, whether "ip tunnel show" works without error
		ipv6_exec_ip tunnel show $device >/dev/null 2>&1
		if [ $? -ne 0 ]; then
			ipv6_log $"Tunnel device '$device' creation didn't work" err $fn
			return 3
		fi

		# Test, whether "ip tunnel show" reports valid content
		if ! ipv6_exec_ip tunnel show $device | LC_ALL=C grep -q "remote"; then
			ipv6_log $"Tunnel device '$device' creation didn't work" err $fn
			return 3
		fi

		ipv6_exec_ifconfig $device up

		# Test, whether creation was successful									#@-RH7@#
		if ! ipv6_test_device_status $device; then
			ipv6_log $"Tunnel device '$device' bringing up didn't work" err $fn
			return 3
		fi

		# Set sysctls proper (regardless "default")
		ipv6_exec_sysctl -w net.ipv6.conf.$device.forwarding=1 >/dev/null
		ipv6_exec_sysctl -w net.ipv6.conf.$device.accept_ra=0 >/dev/null
		ipv6_exec_sysctl -w net.ipv6.conf.$device.accept_redirects=0 >/dev/null

		if [ ! -z "$addressipv6local" ]; then
			# Setup P-t-P address
			ipv6_add_addr_on_device $device $addressipv6local
			if [ $? -ne 0 ]; then
				return 3
			fi
		fi
	else
		ipv6_log $"Tunnel device '$device' is already up" warn $fn						#@-RH7@#
		false
	fi

	return 0
}	


## Configure a static tunnel device down
#  $1: <Interface>
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
ipv6_del_tunnel_device() {
	local fn="ipv6_del_tunnel_device"

	local device=$1

	if [ -z "$device" ]; then
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
		return 1
	fi

	# Run IPv6 test													#@-RH7@#
	ipv6_test testonly || return 2   

	if ipv6_test_device_status $device; then
		# Shut down tunnel											#@-RH7@#
		ipv6_cleanup_device $device
	else
		if [ "$device" != "sit0" ]; then
			ipv6_log $"Tunnel device '$device' is already down" warn $fn 					#@-RH7@#
			false
		fi
	fi

	if [ "$device" != "sit0" ]; then
		ipv6_log $"Delete tunnel device '$device'" inf $fn							#@-RH7@#
		if ipv6_exec_ip tunnel 2>/dev/null | LC_ALL=C grep -q "^$device:" ; then
			ipv6_exec_ip tunnel del $device

			if [ $[ $DEBUG_IPV6 & 2 ] = 0 ]; then							#@-DEB@#
				# Test, whether remove was succesfull							#@-RH7@#
				if ipv6_test_device_status $device; then
					ipv6_log $"Tunnel device '$device' removing didn't work" warn $fn		#@-RH7@#
					return 3
				fi
			fi											#@-DEB@#
		else												
			ipv6_log $"Tunnel device '$device' is already removed" warn $fn					#@-RH7@#
			false
		fi
	fi

	return 0
}


## Cleanup all dedicated tunnel devices
ipv6_cleanup_tunnel_devices() {
	local fn="ipv6_cleanup_tunnel_devices"

	# Run IPv6 test													#@-RH7@#
	ipv6_test testonly || return 2   

	# Find still existing tunnel devices and shutdown and delete them
	ipv6_log $"Cleanup still existing tunnels" inf $fn								#@-RH7@#

	ipv6_exec_ip tunnel | grep "ipv6/ip" | awk -F: '{ print $1 }' | while read device; do
		ipv6_del_tunnel_device $device
	done

	return 0
}


## Get address of a dedicated tunnel
#  $1: <Interface>
#  $2: local|remote : local or remote address
# stdout: <IPv4 address> if available
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
ipv6_get_ipv4addr_of_tunnel() {
	local fn="ipv6_get_local_ipv4_of_tunnel"

	local device=$1
	local selection=$2

	if [ -z "$device" ]; then
		ipv6_log $"Missing parameter 'device' (arg 1)" stderr.err $fn
		return 1
	fi

	if [ -z "$selection" ]; then
		ipv6_log $"Missing parameter 'selection' (arg 2)" stderr.err $fn
		return 1
	fi
	if [ "$selection" != "local" -a "$selection" != "remote" ]; then
		ipv6_log $"Unsupported selection '$selection' specified (arg 2)" stderr.err $fn
		return 1
	fi

	# Run IPv6 test													#@-RH7@#
	ipv6_test testonly || return 2   
	
	ipv6_test_device_status $device

	if [ $? != 0 -a $? != 11 ]; then
		# Device doesn't exist
		return 3
	fi

	# Device exists, retrieve address
	if [ "$selection" = "local" ]; then
		local tunnel_local_ipv4addr="`ipv6_exec_ip tunnel show $device | awk '{ print $6 }'`"
	elif [ "$selection" = "remote" ]; then
		local tunnel_local_ipv4addr="`ipv6_exec_ip tunnel show $device | awk '{ print $4 }'`"
	fi

	if [ $? != 0 ]; then
		return 3
	fi

	if [ "$tunnel_local_ipv4addr" = "any" ]; then
		local tunnel_local_ipv4addr="0.0.0.0"
	fi

	echo "$tunnel_local_ipv4addr"

	return 0
}	


## Get IPv4 address of a device
#  $1: <Interface>
# stdout: <IPv4 address> if available
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
ipv6_get_ipv4addr_of_device() {
	local fn="ipv6_get_ipv4addr_of_device"

	local device=$1

	if [ -z "$device" ]; then
		ipv6_log $"Missing parameter 'device' (arg 1)" stderr.err $fn
		return 1
	fi

	ipv6_test_device_status $device

	if [ $? != 0 -a $? != 11 ]; then
		# Device doesn't exist
		return 3
	fi

	# Device exists, retrieve address
	local ipv4addr="`ipv6_exec_ip -4 addr show dev $device | grep -w "inet" | awk '{ print $2 }' | awk -F/ '{ print $1 }'`"

	if [ $? != 0 ]; then
		return 3
	fi

	if [ "$ipv4addr" = "any" ]; then
		local ipv4addr="0.0.0.0"
	fi

	echo "$ipv4addr"

	return 0
}	


## Set IPv6 MTU for a device
#  $1: <Interface>
#  $2: <IPv6 MTU>
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
ipv6_set_mtu() {
	local fn="ipv6_set_mtu"

	local device=$1
	local ipv6_mtu=$2

	if [ -z "$device" ]; then
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
		return 1
	fi

	if [ -z "$ipv6_mtu" ]; then
		ipv6_log $"Missing parameter 'IPv6 MTU' (arg 2)" err $fn
		return 1
	fi

	# Check range
	if [ $ipv6_mtu -lt 1280 -o $ipv6_mtu -gt 65535 ]; then
		ipv6_log $"Given IPv6 MTU '$ipv6_mtu' is out of range" err $fn
		return 1
	fi

	# Run IPv6 test													#@-RH7@#
	ipv6_test testonly || return 2   

	# Check whether key exists
	ipv6_exec_sysctl net.ipv6.conf.$device.mtu=$ipv6_mtu >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		return 3
	fi

	# Set value
	ipv6_log $"Set IPv6 MTU on '$device' to '$ipv6_mtu'" inf $fn							#@-RH7@#
	ipv6_exec_sysctl -w net.ipv6.conf.$device.mtu=$ipv6_mtu >/dev/null
	
	return 0
}


## Set a default gateway
#  $1: <IPv6 Address> : gateway, can also contain scope suffix (device name), cause a warning if not matching with $2 (but will have precedence)
#  $2: <gateway device>: specified gateway device (has 
#  $3: <check device>: (optional) device to check scope and gateway device against (setup is skipped, if not matching)
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
ipv6_set_default_route() {
	local fn="ipv6_set_default_route"

	local address=$1
	local device=$2
	local device_check=$3

	# Run IPv6 test													#@-RH7@#
	ipv6_test testonly || return 2

	# Map the unspecified address to nothing
	if [ "$address" = "::" ]; then
		local address=""
	fi

	if [ ! -z "$address" ]; then
		# Split away scope, if available									#@-RH7@#
		local addressgw=`echo $address | awk -F% '{ print $1 }'`
		local device_scope=`echo $address | awk -F% '{ print $2 }'`
 
		if [ -z "$addressgw" ]; then
			ipv6_log $"Given IPv6 default gateway '$address' is not in proper format" err $fn
			return 3
		fi

		# Scope device has precedence
		if [ ! -z "$device_scope" -a ! -z "$device" -a "$device_scope" != "$device" ]; then
			ipv6_log $"Given IPv6 default gateway '$address' has scope '$device_scope' defined, given default gateway device '$device' will be not used" inf $fn
			local device=""
		fi

		# Link local addresses require a device
		if echo $addressgw | LC_ALL=C grep -qi "^fe80:"; then
			if [ -z "$device_scope" ]; then
				if [ -z "$device" ]; then
					ipv6_log $"Given IPv6 default gateway '$address' is link-local, but no scope or gateway device is specified" err $fn
					return 3
				fi
			fi
		fi

		# Check whether the route belongs to the specific given interface
		if [ ! -z "$device_check" ]; then
			# Check whether scope device matches given check device
			if [ ! -z "$device_scope" ]; then
				if [ "$device_check" != "$device_scope" ]; then
					# scope device != check -> skip
					ipv6_log $"Given IPv6 default gateway '$address' has scope '$device_scope' defined but this doesn't match given check device '$device_check', skip setup" deb $fn
					true
					return 0
				fi
				# Ok, scope matches check device, 
			elif [ ! -z "$device" ]; then
				if [ "$device_check" != "$device" ]; then
					# gateway device != check -> skip
					ipv6_log $"Given IPv6 default gateway device '$device' is defined but this doesn't match given check device '$device_check', skip setup" deb $fn
					true
					return 0
				fi
			fi
		fi

		# Set device now, if given
		if [ -z "$device" ]; then
			local device="$device_scope"
		fi

		# Install route												#@-RH7@#
		if [ -z "$device" ]; then
			ipv6_log $"Set default route to '$addressgw' (no device defined)" inf $fn			#@-RH7@#
			#ipv6_add_route ::/0 $addressgw
			ipv6_add_route 2000::/3 $addressgw
		else	
			ipv6_log $"Set default route to '$addressgw' via '$device'" inf $fn				#@-RH7@#
			#ipv6_add_route ::/0 $addressgw $device
			ipv6_add_route 2000::/3 $addressgw $device
		fi
	elif [ ! -z "$device" ]; then
		# Check whether the route belongs to the specific given interface
		if [ ! -z "$device_check" ]; then
			if [ "$device_check" != "$device" ]; then
				# gateway device != check -> skip
				ipv6_log $"Given IPv6 default gateway device '$device' is defined but this doesn't match given check device '$device_check', skip setup" deb $fn
				return 0
			fi
		fi
	
		# Test, whether interface needs explicit next hop							#@-RH7@#
		ipv6_test_route_requires_next_hop $device
		local result=$?

		if [ $result = 0 ]; then
			ipv6_log $"Given IPv6 default device '$device' requires an explicit nexthop" err $fn
			return 3
		elif [ $result != 10 ]; then
			ipv6_log $"Given IPv6 default device '$device' doesn't exist or isn't up" err $fn
			return 3
		fi

		ipv6_log $"Set default route via '$device'" inf $fn							#@-RH7@#
		#ipv6_add_route ::/0 :: $device
		ipv6_add_route 2000::/3 :: $device
	else
		ipv6_log $"No parameters given to setup a default route" err $fn
		return 3
	fi

	return 0
}


## Resolve need of explicit next hop for an interface
#  $1: <Interface>
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem 10=needs no explicit hop
ipv6_test_route_requires_next_hop() {
	local fn="ipv6_test_route_requires_next_hop"

	local device=$1

	if [ -z "$device" ]; then
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
		return 1
	fi

	# Run IPv6 test													#@-RH7@#
	ipv6_test testonly || return 2   

	ipv6_test_device_status $device
	
	if [ $? != 0 ]; then
		# Interface is not up or doesn't exist									#@-RH7@#
		return 3
	fi

	if [ "$device" = "sit0" ]; then		
		# Interface is basic tunnel interface, needs no explicit next hop					#@-RH7@#
		return 10
	fi

	if ipv6_exec_ip link show $device |  LC_ALL=C grep -q "POINTOPOINT"; then
		# Interface is P-to-P, needs no explicit next hop							#@-RH7@#
		return 10
	fi

	return 0	
}


## Trigger radvd
#  $1: up|down : device reason for triggering (coming up or going down)
#  $2: [startstop|restart|reload|SIGHUP] : triger mechanism (default is "SIGHUP")
#        "startstop" : reason=up -> start, reason=down -> stop
#  $3: [<filename>] : alternative pid file  [optional]
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem 10=need no explicit hop
ipv6_trigger_radvd() {
	local fn="ipv6_trigger_radvd"

	local reason=$1
	local mechanism=$2
	local pidfile=$3

	if [ -z "$reason" ]; then
		ipv6_log $"No reason given for sending trigger to radvd" err $fn 
		return 1
	fi

	if [ "$reason" != "up" -a "$reason" != "down" ]; then
		ipv6_log $"Unsupported reason '$reason' for sending trigger to radvd" err $fn
		return 1
	fi

	if [ -z "$mechanism" ]; then
		# Take default
		local mechanism="SIGHUP"
	fi
		
	if [ -z "$pidfile" ]; then
		# Take default												#@-RH7@#
		local pidfile="/var/run/radvd/radvd.pid"
	fi

	# Print message and select action
	case $mechanism in
	    'startstop')
		case $reason in
		    up)
			local action="start"
			;;
		    down)
			local action="stop"
			;;
		esac
		;;
	    'reload'|'restart'|'SIGHUP')
		local action="$mechanism"
		;;
	    *)
		ipv6_log $"Unsupported mechanism '$mechanism' for sending trigger to radvd" err $fn
		return 3
		;;
	esac
		
	# PID file needed?
	if [ "$action" = "SIGHUP" ]; then
		if [ ! -f "$pidfile" ]; then
			ipv6_log $"Given pidfile '$pidfile' doesn't exist, cannot send trigger to radvd" err $fn 
			return 3
		fi

		# Get PID
		local pid="`cat $pidfile`"
		if [ -z "$pid" ]; then
			# pidfile empty - strange
			ipv6_log $"Pidfile '$pidfile' is empty, cannot send trigger to radvd" err $fn
			return 3
		fi
	fi

	ipv6_log $"Trigger radvd using action '$action'" inf $fn		 				#@-RH7@#

	# Do action
	case $action in
	    'SIGHUP')
		kill -HUP $pid
		;;
	    'reload'|'restart'|'stop'|'start')
		service radvd $action >/dev/null 2>&1
		;;
	    *)
		# Normally not reached, "action" is set above to proper value
		;;
	esac

	return 0
}
