#!/bin/sh
#
#    tnlup - tunnel configuration script
#    Copyright (C) 1999, 2000 Arkadiusz Mi¶kiewicz <misiek@pld-linux.org>
#
#    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 2 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, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#    $Id: tnlup 6491 2005-10-26 16:33:29Z baggins $
#

. /etc/sysconfig/network
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network-scripts/functions.network

DEV=$1

[ -z "$DEV" ] && {
	nls "Usage: %s <device name>" "tnlup" >&2
	exit 1
}

TNLCONFIGS=$(ls /etc/sysconfig/interfaces/tnlcfg-* 2>/dev/null | egrep -v '~$')
TNLCONFIGS=$(egrep -L '^#!' $TNLCONFIGS)
CONFIG=$(egrep -l "^DEVICE=[\"\']*$DEV[\"\']*\$" $TNLCONFIGS)

if [ -z "$CONFIG" ]; then
	CONFIG="$DEV"
fi

if false; then
	[ -f "/etc/sysconfig/interfaces/$CONFIG" ] || CONFIG=tnlcfg-$CONFIG
	[ -f "/etc/sysconfig/interfaces/$CONFIG" ] || {
		echo "usage: tnlup <device name>" >&2
		exit 1
	}
fi

source_config

if [ "foo$2" = "fooboot" -a -n "${ONBOOT}" ] && is_no "${ONBOOT}"; then
	exit
fi

case "${MODE}" in
  sit|four)
	is_no "${IPV6_NETWORKING}" && exit 0
	is_no "${IPV6_TUNNELCONFIG}" && exit 0
	[ "${MODE}" = "four" ] && is_module "fourtun" && modprobe -s -k fourtun
	;;
  ipip)
	is_no "${IPV4_NETWORKING}" && exit 0
	is_module "ipip" && modprobe -s -k ipip
	;;
  gre)
	is_no "${IPV4_NETWORKING}" && exit 0
	is_module "ip_gre" && modprobe -s -k ip_gre
	;;
  ipxip|ipipx)
	is_no "${IPX}" && exit 0
	;;
esac

RESULT=0
[ -z "${LOCALADDR}" ] && LOCALADDR=any
[ -z "${REMOTEADDR}" ] && REMOTEADDR=any
[ -n "${TTL}" ] && TTL="ttl ${TTL}"
[ -n "${TOS}" ] && TOS="tos ${TOS}"
[ -n "${BIND_DEV}" ] && BIND_DEV="dev ${BIND_DEV}"
[ -n "${PRIORITY}" ] && PRIORITY="priority ${PRIORITY}"
[ -n "${HOPLIMIT}" ] && HOPLIMIT="hoplimit ${HOPLIMIT}"
[ -n "${ENCAPLIMIT}" ] && ENCAPLIMIT="encaplimit ${ENCAPLIMIT}"

if is_yes "${SEQ}"; then
	SEQ=seq
else
	is_yes "${ISEQ}" && SEQ=iseq
	is_yes "${OSEQ}" && SEQ="${SEQ} oseq"
fi

if is_yes "${CSUM}"; then
	CSUM=csum
else
	is_yes "${ICSUM}" && CSUM=icsum
	is_yes "${ICSUM}" && CSUM="${SEQ} ocsum"
fi

is_yes "${ISEQ}" && ISEQ=iseq

[ -z "${PMTUDISC}" -a "${TTL}" != "0" -a "${TTL}" != "inherit" ] && PMTUDISC=yes

if is_yes "${PMTUDISC}"; then
	PMTUDISC=pmtudisc
elif is_no "${PMTUDISC}"; then
	PMTUDISC=nopmtudisc
fi

if [ -n "${KEY}" ]; then
	IKEY=""
	OKEY=""
fi

case "${MODE}" in
  ipip|sit)
	ip tunnel add ${DEVICE} mode ${MODE} local ${LOCALADDR} \
		remote ${REMOTEADDR} ${TTL} ${TOS} ${PMTUDISC} ${BIND_DEV}
	RESULT=$?
	;;
  gre)
	ip tunnel add ${DEVICE} mode ${MODE} local ${LOCALADDR} \
		remote ${REMOTEADDR} ${TTL} ${TOS} ${PMTUDISC} ${BIND_DEV} \
		${CSUM} ${ISEQ} ${KEY} ${IKEY} ${OKEY}
	RESULT=$?
	;;
  fourtun)
	fourcfg add ${DEVICE} saddr ${LOCALADDR} daddr ${REMOTEADDR} \
		${PRIORITY} ${HOPLIMIT} ${ENCAPLIMIT}
	RESULT=$?
	;;
  ipxip|ipipx)
	;;
esac

exit $RESULT

# This must be last line !
# vi:syntax=sh
