#!/bin/sh /etc/rc.common
# Copyright (C) 2009-2013 OpenWrt.org
# Lantronix: after reload/restart, fully flush conntrack so DNAT dest_ip
# changes (port-forward after LAN subnet change) apply without reboot.

START=19
USE_PROCD=1
QUIET=""

validate_firewall_redirect()
{
	uci_validate_section firewall redirect "${1}" \
		'proto:or(uinteger, string)' \
		'src:string' \
		'src_ip:cidr' \
		'src_dport:or(port, portrange)' \
		'dest:string' \
		'dest_ip:cidr' \
		'dest_port:or(port, portrange)' \
		'target:or("SNAT", "DNAT")'
}

validate_firewall_rule()
{
	uci_validate_section firewall rule "${1}" \
		'proto:or(uinteger, string)' \
		'src:string' \
		'dest:string' \
		'src_port:or(port, portrange)' \
		'dest_port:or(port, portrange)' \
		'target:string'
}

service_triggers() {
	procd_add_reload_trigger firewall

	procd_open_validate
	validate_firewall_redirect
	validate_firewall_rule
	procd_close_validate
}

ltrx_flush_nat_state() {
	if [ -x /usr/sbin/conntrack ]; then
		conntrack -F >/dev/null 2>&1
	elif [ -e /proc/net/nf_conntrack ]; then
		echo f > /proc/net/nf_conntrack 2>/dev/null
	fi
}

restart() {
	fw3 restart
	ltrx_flush_nat_state
}

start_service() {
	fw3 ${QUIET} start
}

stop_service() {
	fw3 flush
	ltrx_flush_nat_state
}

reload_service() {
	fw3 reload
	ltrx_flush_nat_state
}

boot() {
	# Be silent on boot, firewall might be started by hotplug already,
	# so don't complain in syslog.
	QUIET=-q
	start
}
