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

START=10
STOP=99

get_hnat_status(){
	status=0
	hwnat_en=0

	config_load hwnat

	config_get_bool hwnat_en global enabled
	if [ "$hwnat_en" == "0" ] ; then
		echo $status
		return
	fi

	hnat_sens_table=`uci -c /etc/profile.d show profile.@hwnat[0] | sed '1d' | cut -d "." -f 3 | cut -d "=" -f 1 `
	if [ -n "$hnat_sens_table" ]; then
		for sens_module in ${hnat_sens_table}
		do
			config_get_bool module_stat module ${sens_module}
			if [ "$module_stat" == "1" ] ; then
				echo $status
				return
			fi
		done
	fi

	status=1
	echo $status
}



hnat_type=`/bin/hwnat -t`

get_current_hnat_status(){
	status=0
	if [ "$hnat_type" == "mtk" ] ; then
		[ ! -d /sys/kernel/debug/hnat ] && /bin/mount -o noatime -t debugfs debugfs /sys/kernel/debug
		status_value=`cat /sys/kernel/debug/hnat/hook_toggle`
	elif [ "$hnat_type" == "qca" ] ; then
		if [ -f "/proc/qca_switch/nf_athrs17_hnat" ] ; then
			status_value=`cat /proc/qca_switch/nf_athrs17_hnat`
		fi
	else
		echo "get current hwnat status failed, invalid hwnat type[$hnat_type]!" > /dev/console
		return
	fi
	if [ "$status_value" == "1" ] ; then
		status=1
	fi
	echo $status
}

start() {
	echo 1 > /tmp/hnat_start_complete
	local version=`uname -r`

	if [ "$hnat_type" == "mtk" ] ; then
		insmod /lib/modules/$version/mtkhnat.ko
		[ ! -d /sys/kernel/debug/hnat ] && /bin/mount -o noatime -t debugfs debugfs /sys/kernel/debug

		if [ `get_hnat_status` == "1" ]; then
			[ -d /sys/kernel/debug/hnat ] && echo 1 > /sys/kernel/debug/hnat/hook_toggle && echo 1 > /sys/kernel/debug/hnat/mape_toggle
			echo "1 5" > /sys/kernel/debug/hnat/hnat_setting

			qdma_enable=`uci -c /etc/profile.d  get profile.@qdma[0].enable`
			if [ -n "$qdma_enable" ] && [ "$qdma_enable" == "1" ] ; then
				echo 1 1 0 1 1000000 4 4 > /sys/kernel/debug/hnat/qdma_txq9
				echo 1 1 0 1 1000000 4 4 > /sys/kernel/debug/hnat/qdma_txq10
				echo 1 1 0 1 1000000 4 4 > /sys/kernel/debug/hnat/qdma_txq11
				echo 1 1 0 1 1000000 4 4 > /sys/kernel/debug/hnat/qdma_txq12
				echo 1 1 0 1 1000000 4 4 > /sys/kernel/debug/hnat/qdma_txq13
				echo 1 1 0 1 1000000 4 4 > /sys/kernel/debug/hnat/qdma_txq14
				echo 0 wrr 0 > /sys/kernel/debug/hnat/qdma_sch1
			fi
		else
			[ -d /sys/kernel/debug/hnat ] && echo 0 > /sys/kernel/debug/hnat/hook_toggle
			echo "hwnat status is off" > /dev/console
		fi
	elif [ "$hnat_type" == "qca" ] ; then
		if [ `get_hnat_status` == "1" ] ; then
			qca-hnat start
		else
			echo "hwnat status is off" > /dev/console
		fi
	else
		echo "start hwnat failed, invalid hwnat type[$hnat_type]!" > /dev/console
	fi
}

stop() {
	if [ "$hnat_type" == "mtk" ] ; then
		[ ! -d /sys/kernel/debug/hnat ] && /bin/mount -o noatime -t debugfs debugfs /sys/kernel/debug
		[ -d /sys/kernel/debug/hnat ] && echo 0 > /sys/kernel/debug/hnat/hook_toggle
	elif [ "$hnat_type" == "qca" ] ; then
		qca-hnat stop
	else
		echo "stop hwnat failed, invalid hwnat type[$hnat_type]!" > /dev/console
	fi
}

reload() {
	if [ -f /tmp/hnat_start_complete ] ; then
		current_hnat_status=`get_current_hnat_status`
		expect_hnat_status=`get_hnat_status`
		if [[ "$current_hnat_status" == "1" && "$expect_hnat_status" == "1" ]] ; then
			if [ "$hnat_type" == "mtk" ] ; then
				return 
			elif [ "$hnat_type" == "qca" ] ; then
				stop
				start
			else 
				return
			fi
		elif [[ "$current_hnat_status" == "1" && "$expect_hnat_status" == "0" ]] ; then
			stop
		elif [[ "$current_hnat_status" == "0" && "$expect_hnat_status" == "1" ]] ; then
			start
		fi
	fi
}
