#!/bin/sh
#
# Copyright (c) 2004  The KAME Project
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

# PROVIDE: network_ipv6_mobile
# REQUIRE: network_ipv6
# REQUIRE: SERVERS

PATH=/usr/local/v6/bin:/usr/local/v6/sbin:${PATH}
. /etc/rc.subr
. /etc/network.subr

name="network_ipv6_mobile"
rcvar=`set_rcvar ipv6_mobile`
start_cmd="network_ipv6_mobile_start"

eval ipv6_mobile_enable=${ipv6_mobile_enable:=NO}
eval ipv6_mobile_config_dir=${ipv6_mobile_config_dir:=/usr/local/v6/etc/mobileip6}

eval ipv6_mobile_mobile_node_interfaces=${ipv6_mobile_mobile_node_interfaces:=mip0}
eval ipv6_mobile_mnd_flags=${ipv6_mobile_mnd_flags:='"-n -c /usr/local/v6/etc/mnd.conf"'}
eval ipv6_mobile_mrd_flags=${ipv6_mobile_mrd_flags:='"-n -c /usr/local/v6/etc/mrd.conf"'}

eval ipv6_mobile_mdd_program=${ipv6_mobile_mdd_program:=babymdd}

eval ipv6_mobile_home_agent_mode=${ipv6_mobile_home_agent_mode:=mobile_ipv6}
eval ipv6_mobile_had_flags=${ipv6_mobile_had_flags:='"-n -c /usr/local/v6/etc/had.conf"'}

eval ipv6_mobile_cnd_flags=${ipv6_mobile_cnd_flags:='"-n -c /usr/local/v6/etc/cnd.conf"'}

network_ipv6_mobile_start()
{
	echo 'Doing Mobile IPv6 network setup:'

	if ! ${SYSCTL_N} net.inet6.mip6.debug > /dev/null; then
		echo 'No MIP6 kernel'
		return
	fi

	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_migrate_enable} in
	[Yy][Ee][Ss])
		${SYSCTL_W} net.inet6.mip6.use_migrate=1
		;;
	[Nn][Oo])
		${SYSCTL_W} net.inet6.mip6.use_migrate=0
		;;
	esac

	case ${ipv6_mobile_nodetype} in
	[Mm][Oo][Bb][Ii][Ll][Ee]_[Nn][Oo][Dd][Ee])
		${SYSCTL_W} net.inet6.ip6.accept_rtadv=1 > /dev/null
		# 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}/spdadd_mobile_node ]; then
					continue;
				fi
				setkey -f ${node_dir}/add
				setkey -f ${node_dir}/spdadd_mobile_node
			done
		fi

		echo -n ' mnd'
		for mip_interface in ${ipv6_mobile_mobile_node_interfaces}
		do
			mnd ${ipv6_mobile_mnd_flags} ${mip_interface}
		done

		echo -n " ${ipv6_mobile_mdd_program}"
		for mip_interface in ${ipv6_mobile_mobile_node_interfaces}
		do
			${ipv6_mobile_mdd_program} -h ${mip_interface}
		done

		echo -n ' cnd'
		cnd ${ipv6_mobile_cnd_flags}

		echo -n ' rtsold'
		rtsold -a -m
		;;
	[Mm][Oo][Bb][Ii][Ll][Ee]_[Rr][Oo][Uu][Tt][Ee][Rr])
		${SYSCTL_W} net.inet6.ip6.accept_rtadv=1 > /dev/null
		${SYSCTL_W} net.inet6.ip6.forwarding=1 > /dev/null
		# 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}/spdadd_mobile_node ]; then
					continue;
				fi
				setkey -f ${node_dir}/add
				setkey -f ${node_dir}/spdadd_mobile_node
			done
		fi

		# do not autoconfigure addresses on ingress i/fs
		if [ "X${ipv6_mobile_ingress_interfaces}" != 'X' ]; then
			for iinterface in ${ipv6_mobile_ingress_interfaces}
			do
				ndp -i ${iinterface} -- -accept_rtadv > /dev/null 2>&1
			done
		fi

		echo -n ' mrd'
		for mip_interface in ${ipv6_mobile_mobile_node_interfaces}
		do
			mrd ${ipv6_mobile_mrd_flags} ${mip_interface}
		done

		echo -n " ${ipv6_mobile_mdd_program}"
		einterfaces=`ipv6_mobile_get_egress_interfaces ${ipv6_mobile_ingress_interfaces}`
		for mip_interface in ${ipv6_mobile_mobile_node_interfaces}
		do
			${ipv6_mobile_mdd_program} -h ${mip_interface} ${einterfaces}
		done

		echo -n ' nemonetd'
		ipv6_mobile_nemo_down
		nemonetd_flags=${ipv6_mobile_nemonetd_flags:="-n -m -c /usr/local/v6/etc/mrd.conf"}
		for mip_interface in ${ipv6_mobile_mobile_node_interfaces}
		do
			nemonetd ${nemonetd_flags} ${mip_interface}
		done

		echo -n ' rtsold'
		rtsold -a -m
		;;
	[Hh][Oo][Mm][Ee]_[Aa][Gg][Ee][Nn][Tt])
		case ${ipv6_mobile_home_interface} 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}/spdadd_home_agent ]; then
						continue;
					fi
					setkey -f ${node_dir}/add
					setkey -f ${node_dir}/spdadd_home_agent
				done
			fi

			home_prefixes=`mobileip6_getprefixes ${ipv6_mobile_home_interface}`
			for home_prefix in ${home_prefixes}
			do
				# XXX describe the case when plen != 64
				ifconfig ${ipv6_mobile_home_interface} inet6 ${home_prefix}:fdff:ffff:ffff:fffe prefixlen 64 anycast alias
			done

			echo -n ' had'
			had ${ipv6_mobile_had_flags} ${ipv6_mobile_home_interface}

			if [ ${ipv6_mobile_home_agent_mode} = "nemo" ]; then
				echo -n ' nemonetd'
				ipv6_mobile_nemo_down
				nemonetd_flags=${ipv6_mobile_nemonetd_flags:="-n -h -c /usr/local/v6/etc/had.conf"}
				nemonetd ${nemonetd_flags} ${ipv6_mobile_home_interface}
			fi

#			echo -n ' cnd'
#			cnd ${ipv6_mobile_cnd_flags}

			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 ' cnd'
		cnd ${ipv6_mobile_cnd_flags}
		;;
	esac

	echo ''
}

mobileip6_getprefixes()
{
	ndp -p 2>/dev/null | while read prefix interface rest; do
		if [ X$interface = X"if="$1 ]; then
			case ${prefix} in
			fe80:*)
				;;
			fec0:*)
				;;
			[0-9a-f][0-9a-f][0-9a-f][0-9a-f]:*)
				prefix64=`expr "${prefix}" : '\(.*\)::/64'`
				echo ${prefix64}
			esac
		fi
	done
}

ipv6_mobile_nemo_down()
{
	for ifname in `ifconfig -l`
	do
		case ${ifname} in
		nemo[0-9]*)
			ifconfig ${ifname} down
			;;
		esac
	done
}

ipv6_mobile_get_egress_interfaces()
{
	eifnames=''
	for ifname in `ifconfig -l`
	do
		case ${ifname} in
		mip[0-9]*|nemo[0-9]*|faith[0-9]*|pflog[0-9]*|pfsync[0-9]*)
			continue
			;;
		esac
		eifname=${ifname}
		for iifname in $@
		do
			if [ X${ifname} =  X${iifname} ]; then
				eifname=''
				break;
			fi
		done
		eifnames="${eifnames} ${eifname}"
	done
	echo ${eifnames}
}

load_rc_config $name
run_rc_command "$1"
