#!/bin/sh
#
# frrctl
#
# part of pfSense (https://www.pfsense.org)
# Copyright (c) 2004-2021 Rubicon Communications, LLC (Netgate)
# All rights reserved.
#
# originally based on m0n0wall (http://neon1.net/m0n0wall)
# Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
# 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.

RC_SCRIPT=/usr/local/etc/rc.d/frr.sh
FRR_VTYSH_PATH=/usr/local/bin/vtysh

daemon_command() {
	VTYSH_COMMAND=""

	for var in "$@"; do
		VTYSH_COMMAND="${VTYSH_COMMAND}-c \"${var%% }\" "
	done

	eval $FRR_VTYSH_PATH "${VTYSH_COMMAND}"
}

case $1 in
stop)
	$RC_SCRIPT stop
	;;
start)
	$RC_SCRIPT start
	;;
restart)
	$RC_SCRIPT restart
	;;
zebra)
	if [ "`pgrep zebra`" = "" ]; then
		echo "zebra does not appear to be running"
		exit 1
	fi
	case $2 in
	cpu*)
		daemon_command "show thread cpu"
		;;
	mem*)
		shift; shift;
		daemon_command "show memory $*"
		;;
	int*)
		daemon_command "show interface $3"
		;;
	route6)
		daemon_command "show ipv6 route"
		;;
	route)
		daemon_command "show ip route"
		;;
	bgpr*)
		daemon_command "show ip route bgp"
		;;
	esac ;;
ospf6)
	if [ "`pgrep ospf6d`" = "" ]; then
		echo "ospf6d does not appear to be running"
		exit 1
	fi
	case $2 in
	cpu*)
		daemon_command "show thread cpu"
		;;
	mem*)
		shift; shift;
		daemon_command "show memory $*"
		;;
	gen*)
		daemon_command "show ipv6 ospf6"
		;;
	nei*)
		shift; shift;
		daemon_command "show ipv6 ospf6 neighbor $*"
		;;
	dat*)
		shift; shift;
		daemon_command "show ipv6 ospf6 database $*"
		;;
	int*)
		daemon_command "show ipv6 ospf6 interface $3"
		;;
	bor*)
		daemon_command "show ipv6 ospf6 border-routers"
		;;
	rou*)
		daemon_command "show ipv6 ospf6 route"
		;;
	esac ;;
ospf)
	if [ "`pgrep ospfd`" = "" ]; then
		echo "ospfd does not appear to be running"
		exit 1
	fi
	case $2 in
	cpu*)
		daemon_command "show thread cpu"
		;;
	mem*)
		shift; shift;
		daemon_command "show memory $*"
		;;
	gen*)
		daemon_command "show ip ospf"
		;;
	nei*)
		shift; shift;
		daemon_command "show ip ospf neighbor $*"
		;;
	dat*)
		shift; shift;
		daemon_command "show ip ospf database $*"
		;;
	int*)
		daemon_command "show ip ospf interface $3"
		;;
	bor*)
		daemon_command "show ip ospf border-routers"
		;;
	rou*)
		daemon_command "show ip ospf route"
		;;
	esac ;;
bgp6*)
	if [ "`pgrep bgpd`" = "" ]; then
		echo "bgpd does not appear to be running"
		exit 1
	fi
	case $2 in
	rou*)
		daemon_command "show bgp ipv6"
		;;
	esac ;;
bgp*)
	if [ "`pgrep bgpd`" = "" ]; then
		echo "bgpd does not appear to be running"
		exit 1
	fi
	case $2 in
	rou*)
		daemon_command "show bgp ipv4"
		;;
	nei*)
		shift; shift;
		daemon_command "show bgp neighbors $*"
		;;
	peer*)
		shift; shift;
		daemon_command "show bgp peer-group $*"
		;;
	sum*)
		shift; shift;
		daemon_command "show bgp summary $*"
		;;
	nexth*)
		shift; shift;
		daemon_command "show bgp nexthop detail $*"
		;;
	mem*)
		shift; shift;
		daemon_command "show bgp memory $*"
		;;
	cycleneighbor)
		daemon_command "config terminal" "router bgp $3"  "neighbor $4 shutdown"
		sleep 1;
		daemon_command "config terminal" "router bgp $3"  "no neighbor $4 shutdown"
		;;
	esac ;;
bfd*)
	if [ "`pgrep bfdd`" = "" ]; then
		echo "bfdd does not appear to be running"
		exit 1
	fi
	case $2 in
	peer_br*)
		daemon_command "show bfd peers brief"
		;;
	peer*)
		daemon_command "show bfd peers"
		;;
	count*)
		daemon_command "show bfd peers counters"
		;;
	esac ;;
cycleinterface)
	daemon_command "config terminal" "interface $2" "shutdown"
	sleep 1;
	daemon_command "config terminal" "interface $2" "no shutdown"
	;;
*)
	echo "Unknown command"
	exit 1
esac
