#!/bin/sh
#
#    Copyright (c) 1996-2010 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    Description:
#		Enabling  port forwarding on MM for gdb debugging on LC
#


# Source common library
. /etc/init.d/functions

PATH=${PATH}":/fabos/sbin:/fabos/bin:/fabos/libexec"

# Determine the system platform identifier.
SWBD=`sin | swbd 2> ${NULL}`
CPID=`sin | cpid 2> ${NULL}`
main_enable_lc_gdb_neptune() {
	echo "Enabling LC debugging on Neptune"
	# configure port forwarding rules for bp remote gdb debugging 
	# BP slots 1,2,7,8
	for i in 1 2 3 4 9 10 11 12
	do 
		port1="$((((i - 1) * 100) + 55000))"
		port2="$((((i - 1) * 100) +55099))"
		dest="127.1.$((CPID + 100)).$i"

		$ECHO "port1 $port1"
		$ECHO "port2 $port2"
		$ECHO "port2 $dest"
		/sbin/iptables -t nat -A PREROUTING -p tcp -i bond0 \
		--dport $port1:$port2 -j DNAT --to $dest > ${NULL} 2>&1
	done
	
}
main_enable_lc_gdb_M4() {
	echo "Enabling LC debugging on M4"
	# configure port forwarding rules for bp remote gdb debugging 
	# LC slots 1,2,7,8
	for i in 1 2 3 4
	do 
		port1="$((((i - 1) * 100) + 55000))"
		port2="$((((i - 1) * 100) +55099))"
		dest="127.1.1.$((i + 6))"

		$ECHO "port1 $port1"
		$ECHO "port2 $port2"
		$ECHO "port2 $dest"
		/sbin/iptables -t nat -A PREROUTING -p tcp -i eth0 \
		--dport $port1:$port2 -j DNAT --to $dest > ${NULL} 2>&1
	done
	
}
main_enable_lc_gdb_M8() {
	echo "Enabling LC debugging on M8"
	# configure port forwarding rules for bp remote gdb debugging 
	# LC slots 1,2,7,8
	for i in 1 2 3 4 5 6 7 8
	do 
		port1="$((((i - 1) * 100) + 55000))"
		port2="$((((i - 1) * 100) +55099))"
		case $i in
		'1'|'2'|'3'|'4')
			dest="127.1.1.$i"
			;;
		*)
			dest="127.1.1.$((i + 6))"
			;;
		esac

		$ECHO "port1 $port1"
		$ECHO "port2 $port2"
		$ECHO "port2 $dest"
		/sbin/iptables -t nat -A PREROUTING -p tcp -i eth0 \
		--dport $port1:$port2 -j DNAT --to $dest > ${NULL} 2>&1
	done
}
main_enable_lc_gdb_M16() {
	echo "Enabling LC debugging on M16"
	# configure port forwarding rules for bp remote gdb debugging 
	# LC slots 1,2,7,8
	for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
	do 
		port1="$((((i - 1) * 100) + 55000))"
		port2="$((((i - 1) * 100) +55099))"
		dest="127.1.1.$i"

		$ECHO "port1 $port1"
		$ECHO "port2 $port2"
		$ECHO "port2 $dest"
		/sbin/iptables -t nat -A PREROUTING -p tcp -i eth0 \
		--dport $port1:$port2 -j DNAT --to $dest > ${NULL} 2>&1
	done
}
main_disable_lc_gdb_pluto() {
	echo "Disabling LC debugging on Pluto"
	/sbin/iptables -t nat -F PREROUTING > ${NULL} 2>&1
}
main_disable_lc_gdb_neptune() {
	echo "Disabling LC debugging on Neptune"
	/sbin/iptables -t nat -F PREROUTING  > ${NULL} 2>&1
}
main_disable_lc_gdb_Mx() {
	echo "Disbling LC debugging on M4/M8/M16"
	/sbin/iptables -t nat -F PREROUTING  > ${NULL} 2>&1
}

#
# main
#
case "$1" in
'--enable')
    case ${SWBD##SWBD} in
    '62')
		main_enable_lc_gdb_neptune
    	;;
    '77')
		main_enable_lc_gdb_pluto
		;;
	'1000')
		main_enable_lc_gdb_M4
		;;
	'1001')
		main_enable_lc_gdb_M8
		;;
	'1002')
		main_enable_lc_gdb_M16
		;;
	*)
		echo "not a supported MM"
		;;
	esac
	#
	# Clear firewall forwarding reject rules
	#
	/sbin/iptables -F FORWARD
	/sbin/iptables -A FORWARD -p tcp -j ACCEPT
;;
'--disable')
    case ${SWBD##SWBD} in
    '62')
		main_disable_lc_gdb_neptune
    	;;
    '77')
		main_disable_lc_gdb_pluto
		;;
	'1000'|'1001'|'1002')
		main_disable_lc_gdb_Mx
		;;
	*)
		echo "not a supported MM"
		;;
	esac
	#
	#Reinstate the firewall rules TODO: Are these the correct rules?
	#
	/sbin/iptables -F FORWARD
	for k in bond0 eth0 eth1 eth2 
	do 
	/sbin/iptables -I FORWARD 1 -i $k -j REJECT \
				--reject-with icmp-port-unreachable > ${NULL} 2>&1
	done
;;
*)
	echo "Usage: $0 { --enable| --disable}"
;;
esac
