#!/bin/sh
# $Id: mobileip6,v 1.5 2004/12/09 02:19:30 t-momose Exp $

# REQUIRE: network
# PROVIDE: mobileip6
# BEFORE: LOGIN

. /etc/rc.subr

name="mobileip6"
start_cmd="mobileip6_start"
stop_cmd="mobileip6_stop"


mobileip6_start() 
{
	echo -n 'Doing Mobile IPv6 network setup:'

	case ${ipv6_mobile_debug_enable} in
	[Yy][Ee][Ss])
		sysctl -w net.inet6.mip6.debug=1
		;;
	[Nn][Oo])
		sysctl -w net.inet6.mip6.debug=0
		;;
	esac

	case ${ipv6_mobile_security_enable} in
	[Yy][Ee][Ss])
		sysctl -w net.inet6.mip6.use_ipsec=1
		;;
	[Nn][Oo])
		sysctl -w net.inet6.mip6.use_ipsec=0
		;;
	esac

	case ${ipv6_mobile_nodetype} in
	[Mm][Oo][Bb][Ii][Ll][Ee]_[Nn][Oo][Dd][Ee])
		# setup SPD/SA
		if [ `sysctl -n net.inet6.mip6.use_ipsec` = '1' ]; then
			for node_dir in ${ipv6_mobile_config_dir}/*
			do
				if [ ! -e ${node_dir}/add ]; then
					continue;
				fi
				setkey -f ${node_dir}/add
				setkey -f ${node_dir}/spdadd_mobile_node
			done
		fi

		echo -n ' mnd'
		mnd -n -i mip0
		echo -n ' mdd'
		mdd -n
		echo -n ' rtsold'
		sysctl -w net.inet6.ip6.accept_rtadv=1
		rtsold -a -m
		cnd -n
		;;
	[Mm][Oo][Bb][Ii][Ll][Ee]_[Rr][Oo][Uu][Tt][Ee][Rr])
		# setup SPD/SA
		if [ `sysctl -n net.inet6.mip6.use_ipsec` = '1' ]; then
			for node_dir in ${ipv6_mobile_config_dir}/*
			do
				if [ ! -e ${node_dir}/add ]; then
					continue;
				fi
				setkey -f ${node_dir}/add
				setkey -f ${node_dir}/spdadd_mobile_node
			done
		fi

		echo -n ' mrd'
		mrd -n -f /etc/prefixtable.conf -i mip0 
		echo -n ' nemonetd'
		nemonetd -n -m -f /etc/prefixtable.conf
		echo -n ' mdd' 
		mdd -n 
		echo -n ' rtsold'
		sysctl -w net.inet6.ip6.accept_forwarding=1
		sysctl -w net.inet6.ip6.accept_rtadv=1
		rtsold -a -m 
		cnd -n 
		;;
	[Hh][Oo][Mm][Ee]_[Aa][Gg][Ee][Nn][Tt])
		case ${ipv6_mobile_home_link} in
		'')
			;;
		*)
			# setup SPD/SA
			if [ `sysctl -n net.inet6.mip6.use_ipsec` = '1' ]; then
				for node_dir in ${ipv6_mobile_config_dir}/*
				do
					if [ ! -e ${node_dir}/add ]; then
						continue;
					fi
					setkey -f ${node_dir}/add
					setkey -f ${node_dir}/spdadd_home_agent
				done
			fi

			echo -n ' had'
			had -i ${ipv6_mobile_home_interface}
			cnd -n
			echo -n ' rtadvd'
			rtadvd -m ${ipv6_mobile_home_interface}
			# take in Router Advertisement
			rtsol ${ipv6_mobile_home_interface} > /dev/null 2>&1
			;;
		esac
		;;
	*)
		echo -n ' Correspondent Node'
		cnd -n
		;;
	esac

	echo ''
}

mobileip6_stop() {

}

load_rc_config $name
run_rc_command "$1"
