#!/bin/sh
#
# ovpn-linkup
#
# part of pfSense (https://www.pfsense.org)
# Copyright (c) 2004-2013 BSD Perimeter
# Copyright (c) 2013-2016 Electric Sheep Fencing
# Copyright (c) 2014-2026 Rubicon Communications, LLC (Netgate)
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

if [ "${dev_type}" = "tun" ]; then
	case "${dev}" in
		"ovpns"*)
			configuration_type="server"
			;;
		*)
			configuration_type="client"
			;;
	esac

	# Servers with a /30 IPv4 tunnel only have one peer and hence the
	# variable $ifconfig_remote can be used to set up the gateway.
	# Servers with > 30 (e.g. /24) IPv4 tunnel don't have a known peer and
	# hence the gateway must be set to a local address. In this case the
	# variable $ifconfig_remote is not set.
	if [ "${configuration_type}" = "server" ] && [ -n "${ifconfig_remote}" ]; then
		# IPv4 server with a /30 tunnel.
		echo "${ifconfig_remote}" > "/tmp/${1}_router"
	elif [ -n "${route_vpn_gateway}" ]; then
		echo "${route_vpn_gateway}" > "/tmp/${1}_router"
	elif [ -n "${ifconfig_remote}" ]; then
		echo "${ifconfig_remote}" > "/tmp/${1}_router"
	elif [ -n "${ifconfig_local}" ]; then
		# IPv4 server with a > /30 tunnel, or an IPv4 client.
		echo "${ifconfig_local}" > "/tmp/${1}_router"
	else
		echo "${5}" > "/tmp/${1}_router"
	fi

	# Servers with IPv6 tunnels always have $ifconfig_ipv6_remote and will
	# not start with e.g. a /126. The address from $ifconfig_ipv6_remote
	# may be incorrect and hence a local address is used for the gateway.
	if [ "${configuration_type}" = "server" ] && [ -n "${ifconfig_ipv6_local}" ]; then
		# Any server with an IPv6 tunnel.
		echo "${ifconfig_ipv6_local}" > "/tmp/${1}_routerv6"
	elif [ -n "${route_ipv6_gateway_1}" ]; then
		echo "${route_ipv6_gateway_1}" > "/tmp/${1}_routerv6"
	elif [ -n "${ifconfig_ipv6_remote}" ]; then
		echo "${ifconfig_ipv6_remote}" > "/tmp/${1}_routerv6"
	elif [ -n "${ifconfig_ipv6_local}" ]; then
		echo "${ifconfig_ipv6_local}" > "/tmp/${1}_routerv6"
	fi

else
	if [ -n "${route_vpn_gateway}" ]; then
		echo "${route_vpn_gateway}" > "/tmp/${1}_router"
	fi

	if [ -n "${route_ipv6_gateway_1}" ]; then
		echo "${route_ipv6_gateway_1}" > "/tmp/${1}_routerv6"
	fi
fi

if [ -f "/tmp/${1}_router" ]; then
	/bin/rm -f "/tmp/${1}_router.last"
fi
if [ -f "/tmp/${1}_routerv6" ]; then
	/bin/rm -f "/tmp/${1}_routerv6.last"
fi

if [ -n "${ifconfig_local}" ]; then
	/usr/bin/touch "/tmp/${1}_upstart4"
fi

if [ -n "${ifconfig_ipv6_local}" ]; then
	/usr/bin/touch "/tmp/${1}_upstart6"
fi

/usr/bin/touch "/tmp/${1}up"
# reload filter
/usr/local/sbin/pfSctl -c "interface newip ${1}"
exit 0
