#!/bin/sh /etc/rc.common

. /etc/net6conf/6data.conf

dhcp6c_write_config() {
	printf 'interface %s {\n' "$WAN"
	printf '\tsend ia-pd 11;\n'
	printf '\trequest domain-name;\n'
	printf '\trequest domain-name-servers;\n'
	printf '\trequest ntp-servers;\n'
	printf '\trequest sip-server-domain-name;\n'
	printf '\trequest sip-server-address;\n'
	printf '\tscript "%s";\n' "$DHCP6C_SCRIPT"
	printf '};\n'
	printf '\n'
	printf 'id-assoc pd 11 {\n'
	printf '};\n'
}

start_dhcp6c() {
	local U_CLADATA=`$CONFIG get ipv6_autoConfig_userClass`
	local U_DOMAIN=`$CONFIG get ipv6_autoConfig_domainName`
	local enable_orange_ipv6=`$CONFIG get enable_orange_ipv6`
	local wan_orange_username=`$CONFIG get wan_orange_username`
	local orange_stb_ver=`$CONFIG get LB_ver`
	# Using DHCP6 Client to get LAN's IPv6 prefix
	dhcp6c_write_config > /tmp/dhcp6c.conf
	if [ "x$enable_orange_ipv6" = "x1" ];then
		U_CLADATA="FSVDSL_livebox.Internet.softathome.Livebox3"
		[ "$orange_stb_ver" = "4" ] && U_CLADATA="FSVDSL_livebox.Internet.softathome.Livebox4"
		/usr/sbin/dhcp6c -c /tmp/dhcp6c.conf -3 ${U_CLADATA:+-u $U_CLADATA} ${U_DOMAIN:+-U $U_DOMAIN} -O -a $wan_orange_username -h $WAN $WAN
		
		#Add default IPv6 static route for Orange ISP
		ip -6 route add fe80::ba0:bab dev $WAN
		ip -6 route add default via fe80::ba0:bab dev $WAN
	else
		/usr/sbin/dhcp6c -c /tmp/dhcp6c.conf -3 ${U_CLADATA:+-u $U_CLADATA} ${U_DOMAIN:+-U $U_DOMAIN} $WAN
	fi

	#add the default route
#	$IP -6 route add default dev $WAN
}

stop_dhcp6c() {
	/usr/bin/killall dhcp6c
	#wait the dhcpv6 client send release package
	sleep 2
}

dhcpv6c_is_running() {
	if ps | grep dhcp6c | grep -v grep >&- 2>&- ; then
		return 0;
	else
		return 1;
	fi
}

autoconf_wan() {
	# Change the WAN(eth0) interface to send the IPv6 Router Solicitation
	if [ -f /proc/sys/net/ipv6/conf/${WAN}/autoconf ]; then
		$ECHO 1 > /proc/sys/net/ipv6/conf/${WAN}/autoconf
	fi
	if [ -f /proc/sys/net/ipv6/conf/${WAN}/accept_ra ]; then
		$ECHO 2 > /proc/sys/net/ipv6/conf/${WAN}/accept_ra
	fi
	if [ -f /proc/sys/net/ipv6/icmp/ra_sel_flag ]; then
		$ECHO 1 > /proc/sys/net/ipv6/icmp/ra_sel_flag
	fi
	rs_send -i $WAN -c 3 -t 4 &
#	ifconfig $WAN down && ifconfig $WAN up
}

start() {
	if ! [ -f /proc/net/if_inet6 ]; then
		$ECHO "IPv6 not enabled, install kmod-ipv6";
		unlock
		exit 1;
	fi
	if dhcpv6c_is_running ; then
		$ECHO "DHCPv6 Client Daemon is already running, kill it!!";
		local pid=`ps | grep dhcp6c | grep -v grep |awk '{print  $1}'`
		if [ "x$pid" != "x" ]; then
			/bin/kill -9 $pid
		fi
	fi
	rm -f $RA_DNS_FILE

	autoconf_wan

	start_dhcp6c

	if [ "x`which led_ring`" = "x" ] && [ "$wan6_type" = "dslite" -o "$wan6_type" = "v6plus" ]; then
		/etc/net6conf/check_ip_status.sh &
	fi
}

stop () {
	if [ -f /proc/sys/net/ipv6/conf/${WAN}/accept_ra ]; then
		$ECHO 1 > /proc/sys/net/ipv6/conf/${WAN}/accept_ra
	fi

	if [ "x`which led_ring`" = "x" ]; then 
		killall check_ip_status.sh
	fi
	
	if ! dhcpv6c_is_running ; then
		$ECHO "DHCPv6 client is not running! Return";
		return 1;
	fi

	# Stop the dhcp6c daemon
	stop_dhcp6c

	# Delete the config file
	rm -rf /tmp/dhcp6c.conf

	# Delete the prefix, prefix time, dns, ntp, sip file
	rm -f $DHCP6S_DSN
	rm -f $DHCP6S_PD
	rm -f $RA_DNS_FILE

	# Enable WAN(eth0) interface forwarding
#	$ECHO 1 > /proc/sys/net/ipv6/conf/$WAN/forwarding

	# Global addr and gw will be flushed in net6conf script
}

case "$1" in
	start)
	start
	;;
	stop)
    stop
    ;;
    restart)
	stop
	start
	;;
esac
