#!/bin/sh
###############################################################################
#                                                                             #
# IPFire - An Open Source Firewall Solution                                   #
# Copyright (C) 2011 IPFire development team                                  #
#                                                                             #
# This program is free software: you can redistribute it and/or modify        #
# it under the terms of the GNU General Public License as published by        #
# the Free Software Foundation, either version 3 of the License, or           #
# (at your option) any later version.                                         #
#                                                                             #
# This program is distributed in the hope that it will be useful,             #
# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
# GNU General Public License for more details.                                #
#                                                                             #
# You should have received a copy of the GNU General Public License           #
# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
#                                                                             #
###############################################################################

EXIT_USERSPACE_STP=0
EXIT_KERNEL_STP=1

# Change LOG_FACILITY that we will find our messages in syslog.
LOG_FACILITY=$(basename ${0})

. /usr/lib/network/functions

zone=${1}
assert isset zone

action=${2}
assert isset action

# Exit immediately, if zone configuration does not exist.
# This is for manually created bridges.
if ! zone_exists ${zone}; then
	exit ${EXIT_KERNEL_STP}
fi

# Read zone configuration.
zone_config_read ${zone}

# Make sure STP is enabled for this zone.
assert enabled STP

log DEBUG "Configured STP mode is '${STP_MODE}'"

case "${STP_MODE}" in
	rstp)
		# Check if mstpd is running. If not, try to start it.
		if ! service_is_active mstpd; then
			service_start "mstpd.service"

			if ! service_is_active "mstpd.service"; then
				log ERROR "mstpd is not running. STP might not work."
				exit 1
			fi
		fi

		# Set the right protocol that should be used.
		# Do this after the bridge has been added.
		(
			sleep 2
			stp_bridge_set_protocol ${bridge} ${STP_MODE}
		) &

		# Tell mstpd that STP has to be enabled/disabled.
		case "${action}" in
			start)
				log DEBUG "Enabling userspace STP for zone '${zone}'"
				exec mstpctl addbridge ${zone}
				;;
			stop)
				log DEBUG "Disabling userspace STP for zone '${zone}'"
				exec mstpctl delbridge ${zone}
				;;
		esac

		log ERROR "Could not properly exec mstpctl."
		;;
	stp)
		case "${action}" in
			start)
				log DEBUG "Enabling kernel STP for zone '${zone}'"
				exit ${EXIT_KERNEL_STP}
				;;
			stop)
				log DEBUG "Disabling kernel STP for zone '${zone}'"
				exit ${EXIT_OK}
				;;
		esac
		;;
esac

# Fall back to kernel STP.
exit ${EXIT_KERNEL_STP}
