#!/bin/bash
###############################################################################
#                                                                             #
# IPFire.org - A linux based firewall                                         #
# Copyright (C) 2010  Michael Tremer & Christian Schmidt                      #
#                                                                             #
# 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/>.       #
#                                                                             #
###############################################################################

. /usr/lib/network/header-port

HOOK_SETTINGS="HOOK ADDRESS MIIMON MODE SLAVES"

ADDRESS=$(mac_generate)
SLAVES=""
MIIMON=100
MODE="balance-rr"

hook_check_settings() {
	assert isset ADDRESS
	assert ismac ADDRESS

	#assert isset SLAVES
	assert isinteger MIIMON
}

hook_new() {
	hook_edit $@
}

hook_edit() {
	local port=${1}
	assert isset port
	shift

	while [ $# -gt 0 ]; do
		case "${1}" in
			--address=*)
				ADDRESS=$(cli_get_val ${1})
				;;
			--miimon=*)
				MIIMON=$(cli_get_val ${1})
				;;
			--mode=*)
				MODE=$(cli_get_val ${1})
				;;
			--slave=*)
				slave=$(cli_get_val ${1})
				SLAVES="${SLAVES} ${slave}"
				;;
			*)
				warning "Unknown argument '${1}'"
				;;
		esac
		shift
	done

	DEVICE=${port}

	# XXX think this must move to _check()
	if ! isset DEVICE; then
		error "You must set a device name."
		exit ${EXIT_ERROR}
	fi

	if ! isset SLAVES; then
		error "You need to specify at least one slave port (e.g. --slave=port0)."
		exit ${EXIT_ERROR}
	fi

	local slave
	for slave in $(unquote ${SLAVES}); do
		if ! device_is_ethernet_compatible ${slave}; then
			error "The slave device '${slave}' is not able to transport ethernet frames"
			exit ${EXIT_ERROR}
		fi
	done

	# Remove any whitespace
	SLAVES=$(echo ${SLAVES})

	port_settings_write "${port}" ${HOOK_SETTINGS}

	exit ${EXIT_OK}
}

hook_create() {
	local port="${1}"
	assert isset port

	# Exit silently if the device already exists
	device_exists "${port}" && exit ${EXIT_OK}

	port_settings_read "${port}" ${HOOK_SETTINGS}

	# Create the bonding devices
	bonding_create "${port}" \
		--address="${ADDRESS}" \
		--mode="${MODE}" || exit ${EXIT_ERROR}

	bonding_set_miimon "${port}" "${MIIMON}"

	exit ${EXIT_OK}
}

hook_remove() {
	local port="${1}"
	assert isset port

	port_settings_read "${port}" ${HOOK_SETTINGS}

	# Remove the bonding device
	if device_exists "${port}"; then
		bonding_remove "${port}"
	fi
}

hook_up() {
	local port="${1}"
	assert isset port

	port_settings_read "${port}" ${HOOK_SETTINGS}

	# Execute the default action
	hook_default_up "${port}"

	# Bring up all slaves
	local slave
	for slave in $(unquote ${SLAVES}); do
		port_up "${slave}"
	done
}

hook_down() {
	local port="${1}"
	assert isset port

	port_settings_read "${port}" ${HOOK_SETTINGS}

	# Bring down all slaves
	local slave
	for slave in $(unquote ${SLAVES}); do
		port_down "${slave}"
	done

	# Execute the default action
	hook_default_down "${port}"
}

hook_hotplug() {
	local port="${1}"
	assert isset port

	case "$(hotplug_action)" in
		add)
			# Handle events of the same interface
			if hotplug_event_port_is_interface "${port}"; then
				# Read configuration
				port_settings_read "${port}" ${HOOK_SETTINGS}

				# Bring up all slaves
				# Attach those which already exist and try to create
				# those which don't exist yet. They will be attached
				# in their own hotplug event.
				local slave
				for slave in $(unquote ${SLAVES}); do
					if device_exists "${slave}"; then
						bonding_enslave_device "${port}" "${slave}"
					else
						port_create "${slave}"
					fi
				done

				exit ${EXIT_OK}

			# Handle slave devices that have just been created and
			# attach them.
			elif hotplug_event_interface_is_slave_of_port "${port}"; then
				bonding_enslave_device "${port}" "${INTERFACE}"

				# If the parent device has been set up, we will
				# bring up the slave device as well.
				if device_is_up "${port}"; then
					port_up "${INTERFACE}"
				fi
			fi

			exit ${EXIT_OK}
			;;

		remove)
			if hotplug_event_port_is_interface "${port}"; then
				# Bring down all slaves after the parent device went away
				local slave
				for slave in $(port_get_slaves "${port}"); do
					port_remove "${slave}"
				done

				exit ${EXIT_OK}
			fi
			;;
	esac

	exit ${EXIT_NOT_HANDLED}
}
