#!/bin/sh
#
# Copyright (c) 1995, by Sun Microsystems, Inc.
# All Rights Reserved
#
#ident	"$Header: /net/projects/S2.6/cvs/os_net/src_ws/usr/src/tadcmd/locations/init.d/route,v 1.1 1997/10/09 10:37:04 ang Exp $"
#
#LocINFO Internet Routing
# 

killproc() {		# kill the named process(es)
	pid=`/usr/bin/ps -e |
	     /usr/bin/grep " ${1}$" |
	     /usr/bin/sed -e 's/^  *//' -e 's/ .*//'`
	[ "$pid" != "" ] && kill $pid
}

procexist() {
    pid=`/usr/bin/ps -e | /usr/bin/grep " ${1}$" |
	     /usr/bin/sed -e 's/^  *//' -e 's/ .*//'`
    if [ -z "$pid" ]
    then
	return 1
    fi
    return 0
}

# 
# This is the second phase of TCP/IP configuration and
# does all configuration that can be done before
# NIS or NIS+ is started.  This includes configuring IP routing
# and setting any tunable parameters.  The
# third part, run in a subsequent startup script, does all
# configuration that may be dependent on NIS/NIS+ maps.
# 
case "$1" in
'start')
    cp -p $LOCDB/etc/defaultrouter $LOCDB/etc/defaultrouter /etc 2>/dev/null

    #
    # Configure default routers using the local "/etc/defaultrouter"
    # configuration file.  The file can contain the hostnames or IP
    # addresses of one or more default routers.  If hostnames are used,
    # each hostname must also be listed in the local "/etc/hosts" file
    # because NIS and NIS+ are not running at the time that this script is
    # run.  Each router name or address is listed on a single line by
    # itself in the file.  Anything else on that line after the router's
    # name or address is ignored.  Lines that begin with "#" are
    # considered comments and ignored.
    #
    defrouters=`/sbin/dhcpinfo Router 2>/dev/null`
    
    if [ -z "$defrouters" -a -f /etc/defaultrouter ]
    then
	defrouters=`grep -v \^\# /etc/defaultrouter | awk '{print $1}' `
	if [ -n "$defrouters" ]
	then
	    #
	    # To support diskless operation with a "/usr"
	    #/filesystem NFS mounted from a server located on a
	    # remote subnet, we have to be very careful about
	    # replacing default routes.  We want the default
	    # routers listed in the "/etc/defaultrouter" file to
	    # replace the default router added by the bootparams
	    # protocol.  But we can't have a window of time when
	    # the system has no default routers in the process.
	    # That would cause a deadlock since the "route"
	    # command lives on the "/usr" filesystem.
	    #
	    pass=1
	    for router in $defrouters
	    do
	    	if [ $pass -eq 1 ]; then
	    	    /usr/sbin/route -f add default $router
	    	else
	    	    /usr/sbin/route add default $router
	    	fi
	    	pass=2
	    done
	else
	    /usr/sbin/route -f
	fi
    fi

    if [ -z "$defrouters" ]; then
	#
	# Determine how many active interfaces there are and how many pt-pt
	# interfaces. Act as a router if there are more than 2 interfaces 
	# (including the loopback interface) or one or more point-point 
	# interface. Also act as a router if /etc/gateways exists.
	#
	# Do NOT act as a router if /etc/notrouter exists.
	# Do NOT act as a router if DHCP was used to configure interface(s)
	#
	numifs=`ifconfig -au | grep inet | wc -l`
	numptptifs=`ifconfig -au | grep inet | egrep -e '-->' | wc -l`
	numdhcp=`ifconfig -a | grep DHCP | wc -l`
	if [ ! -f /etc/notrouter  -a  $numdhcp -eq 0 -a \
	    \( $numifs -gt 2 -o $numptptifs -gt 0 -o -f /etc/gateways \) ]
	then
	    # Machine is a router: turn on ip_forwarding, run routed,
	    # and advertise ourselves as a router using router discovery.
	    echo "machine is a router."
	    ndd -set /dev/ip ip_forwarding 1
	    if [ -f /usr/sbin/in.routed ]; then
	    	/usr/sbin/in.routed -s
	    fi
	    if [ -f /usr/sbin/in.rdisc ]; then
	    	/usr/sbin/in.rdisc -r
	    fi
	else
	    # Machine is a host: if router discovery finds a router then
	    # we rely on router discovery. If there are not routers
	    # advertising themselves through router discovery
	    # run routed in space-saving mode.
	    # Turn off ip_forwarding
	    forwarding=`/sbin/dhcpinfo IpFwdF 2>/dev/null`
	    if [ -z "$forwarding" ]
	    then
	    	forwarding=0
	    fi
	    ndd -set /dev/ip ip_forwarding $forwarding
	    if [ -f /usr/sbin/in.rdisc ] && /usr/sbin/in.rdisc -s; then
	    	echo "starting router discovery."
	    elif [  -f /usr/sbin/in.routed ]; then
	    	/usr/sbin/in.routed -q;
	    	echo "starting routing daemon."
	    fi
	fi
    else
	forwarding=`/sbin/dhcpinfo IpFwdF 2>/dev/null`
	if [ -z "$forwarding" ]
	then
	    forwarding=0
	fi
	ndd -set /dev/ip ip_forwarding $forwarding
    fi
    ;;

'stop')
    ndd -set /dev/ip ip_forwarding 0
    killproc in.route
    killproc in.rdisc
    route flush
    rm -f /etc/defaultrouter /etc/notrouter
    ;;

'create')
    cp -p /etc/defaultrouter /etc/notrouter $LOCDB/etc 2>/dev/null
    if procexist in.route || procexist in.rdisc
    then
	exit 0
    else
	exit 1
    fi
    ;;

*)
    echo "Usage: $0 { start | stop | create }" 1>&2
    ;;
esac

exit 0
