#!/bin/bash
###############################################################################
#                                                                             #
# IPFire.org - A linux based firewall                                         #
# Copyright (C) 2017  IPFire Network 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/>.       #
#                                                                             #
###############################################################################

LOG_DISABLE_STDOUT="true"

. /usr/lib/network/functions

# Read network settings
network_settings_read

# Make sure we are called by strongSwan
assert isset PLUTO_VERSION

if enabled DEBUG; then
	while read line; do
		[[ ${line} =~ ^PLUTO_ ]] || continue
		log DEBUG "  ${line}"
	done <<< "$(printenv | sort)"
fi

CONNECTION="${PLUTO_CONNECTION}"

if ! ipsec_connection_read_config "${CONNECTION}"; then
	log ERROR "Could not read configuration for ${CONNECTION}"
	exit ${EXIT_ERROR}
fi

log DEBUG "${0} called for ${CONNECTION}: ${PLUTO_VERB}"

case "${PLUTO_VERB}" in
	up-client|up-client-v6|up-host|up-host-v6)
		if isset ZONE && zone_exists "${ZONE}"; then
			# Bring up the zone if not done, yet
			if ! zone_is_up "${ZONE}"; then
				zone_up "${ZONE}"
			fi

			# Update peer and local address
			if ! ip_tunnel_change "${ZONE}" \
					--remote="${PLUTO_PEER}" --local="${PLUTO_ME}"; then
				return ${EXIT_ERROR}
			fi
		fi

		# Get source IP for routes
		SRC_IP=($(ip_get_assigned_addresses_from_net \
			"${PLUTO_MY_CLIENT}" "permanent"))

		# We take the lowest source IP we found,
		# which is ugly because the value is unpredictable.
		SRC_IP=${SRC_IP[0]}

		# Add routes to reach the remote subnet(s)
		if isset ZONE; then
			if ! cmd ip route add "${PLUTO_PEER_CLIENT}" proto static dev "${ZONE}" src "${SRC_IP}"; then
				log ERROR "Could not create route for ${PLUTO_PEER_CLIENT}"
			fi
		else
			if ! cmd ip route add "${PLUTO_PEER_CLIENT}" proto static via "${PLUTO_PEER}" src "${SRC_IP}"; then
				log ERROR "Could not create route for ${PLUTO_PEER_CLIENT} via ${PLUTO_PEER}"
			fi
		fi
		;;

	down-client|down-client-v6|down-host|down-host-v6)
		# Remove routes
		cmd ip route del "${PLUTO_PEER_CLIENT}"
		;;
esac

exit ${EXIT_OK}
