#!/bin/sh

ipv6_enable_nat() {
  uci -q delete firewall.nat6
  uci -q set firewall.nat6="include"
  uci -q set firewall.nat6.path="/usr/share/systools/firewall.nat6"
  uci -q set firewall.nat6.reload="1"
  uci -q set firewall.@zone[1].masq6="1"
  uci -q set firewall.@zone[1].masq6_privacy="0"
}

ipv6_disable_lan_server() {
	uci -q batch <<-EOF >/dev/null
		del dhcp.lan.ra
		del dhcp.lan.ra_slaac
		del dhcp.lan.ra_flags
		del dhcp.lan.dhcpv6
		del dhcp.lan.ndp
EOF
}

ipv6_dns_on() {
	uci -q delete 'dhcp.@dnsmasq[0].filter_aaaa'
}

ipv6_pppoe_mode() {
	ipv6_disable_lan_server
	ipv6_dns_on
}

if opkg list-installed | grep -q "kmod-ipt-nat6"; then
  echo "kmod-ipt-nat6 found"
else
  echo "kmod-ipt-nat6 not found, installing"
  opkg update
  opkg install kmod-ipt-nat6
fi

echo "IPv6 NAT mode"
ipv6_enable_nat
ipv6_pppoe_mode

uci set network.globals.ula_prefix="$(uci get network.globals.ula_prefix | sed 's/^./d/')"

uci -q set network.wan6.reqaddress='try'
uci -q set network.wan6.reqprefix='auto'
uci -q set network.wan6.sourcefilter='0'

uci -q set dhcp.lan.ra='server'
uci -q add_list dhcp.lan.ra_flags='managed-config'
uci -q add_list dhcp.lan.ra_flags='other-config'
uci -q set dhcp.lan.dhcpv6='server'

uci -q batch <<-EOF >/dev/null
  commit firewall
	commit dhcp
	commit network
EOF

/etc/init.d/odhcpd reload
/etc/init.d/dnsmasq reload
/etc/init.d/network reload
/etc/init.d/firewall restart

echo "Done"
