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

START=19
STOP=90

lock=/var/lock/uci.network.lock

check_passthrough ()
{
	local ipv6_enable=`uci get ipv6.ipv6.enable`
	local wan_proto=`uci get network.wan.proto`
	local lan_mode=`uci get ipv6.ipv6.lan_mode`

	[ "$ipv6_enable" == "0" ] && return 0
	[ "$wan_proto" != "pppoe" ] && return 0
	[ "$lan_mode" != "passthrough" ] && return 0

	local wan6_proto=`uci get network.wan6.proto`
	[ "$wan6_proto" == "dhcpv6" ] && local reqprefix="network.wan6.reqprefix=auto"

	# change to route mode
	uci batch <<EOT
		set ipv6.ipv6.lan_mode='route'
		set ipv6.ipv6.reqaddress='slaac+iana'
		set ipv6.ipv6.protocol='dhcpv6'
		set ipv6.ipv6.ra=1
		set ipv6.ipv6.dhcpv6_server=1
		set ipv6.ipv6.reqprefix=auto
		commit ipv6
		set dhcp.lan.ra='server'
		set dhcp.lan.dhcpv6='server'
		commit dhcp
		set network.wan6.device='@wan'
		set network.wan6.proto='dhcpv6'
		set network.wan6.reqprefix='auto'
		set network.wan6.reqaddress='try'
		$reqprefix
		commit network
EOT
}

start ()
{
	#huangjie add for auto change role, re can not start ipv6
	current_role=`uci get role_manager.config.role`
	if [ "$current_role" = "re" ];then
		return 0
	fi

	local ipv6_enable=`uci get ipv6.ipv6.enable`
	local wan_proto=`uci get network.wan.proto`
	local lan_mode=`uci get ipv6.ipv6.lan_mode`

	[ "$ipv6_enable" == "1" ] || return 0
	[ "$wan_proto" != "pppoe" ] || return 0
	[ "$lan_mode" == "passthrough" ] || return 0

	insmod /lib/modules/*/passthrough.ko
	#pconfig add `uci get network.wan.ifname`
	pconfig add `uci get network.wan.device`
}

stop ()
{
	[ -d /sys/module/passthrough ] && rmmod passthrough
}

restart ()
{
	flock -nx 9 || exit 1
	check_passthrough
	stop
	start
	ubus call network reload
	/etc/init.d/odhcpd restart
} 9<> $lock

