#!/bin/sh /etc/rc.common
#
# Copyright (c) 2014-2015 The Linux Foundation. All rights reserved.
# Permission to use, copy, modify, and/or distribute this software for
# any purpose with or without fee is hereby granted, provided that the
# above copyright notice and this permission notice appear in all copies.
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

#SFE connection manager has a lower priority, it should be started after other connection manager
#to detect the existence of connection manager with higher priority

SERVICE_DAEMONIZE=1

START=72

start()
{
	echo 'stop sfe first ... ' > /dev/console
	stop

	local sfe_switch=`uci get sfe.setting.switch`
	# local qos_switch=`uci get sfe.module.qos`
	# local ipsec_switch=`uci get sfe.module.ipsec`
	# local portal_switch=`uci get sfe.module.portal`
	local version=`uname -r`
	local dynamic_statistical_switch=$(uci -c /etc/profile.d get profile.@global[0].sfe_ipstat_dynamic_support)
	local sfe_ipstat_max=$(uci -c /etc/profile.d get profile.@ipstats[0].sfe_ipstat_max)
	[ -z "$sfe_ipstat_max" ] && sfe_ipstat_max=4096
	# echo "sfe_switch is $sfe_switch,qos_switch is $qos_switch,ipsec_switch is $ipsec_switch,portal_switch is $portal_switch" > /dev/console

	# [ "$sfe_switch" == "on" -a "$qos_switch" == "off" -a "$ipsec_switch" == "off" -a "$portal_switch" == "off" ] && {	
	[ "$sfe_switch" == "on" ] && {	
		insmod /lib/modules/$version/shortcut-fe.ko
		insmod /lib/modules/$version/shortcut-fe-ipv6.ko
		insmod /lib/modules/$version/fast-classifier.ko
		echo 2 > /sys/module/shortcut_fe/parameters/sfe_debug
		echo $sfe_ipstat_max > /proc/net/ipstat_max

		#Set the value of sfe_fastpath_nat_switch according to the state of qos
		local qos_enable=`uci get qos.setting.qos_enable`
		if [ $qos_enable = "on" ]; then
			echo 1 >/proc/net/sfe_fastpath_nat_switch
		else
			echo 0 >/proc/net/sfe_fastpath_nat_switch
		fi
		
		#Config SFE ipstat
		if [ -n "$dynamic_statistical_switch" -a "$dynamic_statistical_switch" == "1" ];then
			echo "Dynamic adjust sfe_ipstat_switch, the initial state of sfe_ipstat_switch is 0, off" > /dev/console
			echo 0 > /proc/net/sfe_ipstat_switch
		else
			echo 1 > /proc/net/sfe_ipstat_switch
		fi
		if [ ! -e "/etc/init.d/hwnat_ipstat" ];then
			echo "Config SFE ipstat" > /dev/console
			ip=$(uci get ipstat.setting.ip)
			mask=$(uci get ipstat.setting.mask)
			[ -e /proc/net/sfe_ipstat ] && {
				echo -n  ${ip} ${mask} >  /proc/net/sfe_ipstat
			}
		else
			[ -e /proc/net/sfe_ipstat ] && {
				echo -n  0.0.0.0 0.0.0.0 >  /proc/net/sfe_ipstat
			}
		fi
	}
}

stop()
{
	[ -d /sys/module/fast_classifier ] && rmmod fast_classifier
	[ -d /sys/module/shortcut_fe_ipv6 ] && rmmod shortcut_fe_ipv6
	[ -d /sys/module/shortcut_fe ] && rmmod shortcut_fe
}

restart()
{
	stop
	start
}

reload()
{
	restart   
}
