#!/sbin/sh
#
# Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T.
# All rights reserved.
#
# THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
# The copyright notice above does not evidence any
# actual or intended publication of such source code.
#
# Copyright (c) 1996-1999 by Sun Microsystems, Inc.
# All rights reserved.
#
#ident	"@(#)network	1.10	00/03/29 SMI"

# Make sure that the libraries essential to this stage of booting can be found.
LD_LIBRARY_PATH=/etc/lib; export LD_LIBRARY_PATH

#
# Configure the software loopback driver. The network initialization is
# done early to support diskless and dataless configurations.
#
/sbin/ifconfig lo0 plumb 127.0.0.1 up 2>&1 >/dev/null

# Read in the default configuration settings of in.mpathd
# and start the network multipathing daemon in.mpathd if all
# network interfaces must be tracked.
#
if [ -r /etc/default/mpathd ]; then
	. /etc/default/mpathd
	if [ "$TRACK_INTERFACES_ONLY_WITH_GROUPS" = "no" ]; then
		/sbin/in.mpathd
	fi
fi

#
# If the system was net booted by DHCP, hand DHCP management off to the
# DHCP agent (ifconfig communicates to the DHCP agent through the loopback
# interface).
#
if [ -n "$_INIT_NET_IF" -a "$_INIT_NET_STRATEGY" = "dhcp" ]; then
	/sbin/dhcpagent -a 
fi

#
# The network initialization is done early to support diskless and dataless
# configurations.  For IPv4 interfaces that were configured by the kernel (e.g.
# those on diskless machines) and not configured by DHCP, reset the netmask
# using the local "/etc/netmasks" file if one exists, and then reset the
# broadcast address based on the netmask.
#
/sbin/ifconfig -auD4 netmask + broadcast + 2>&1 >/dev/null

#
# Get the list of IPv4 interfaces to configure by breaking
# /etc/hostname.* into separate args by using "." as a shell separator
# character, then step through args and ifconfig every other arg.
# Set the netmask along the way using the local "/etc/netmasks" file,
# which also sets up the streams plumbing for the interface.
# With an empty /etc/hostname.* file this only sets up the streams plumbing,
# allowing the ifconfig auto-dhcp / auto-revarp command to attempt to
# set the address.
#
# If there is only line in an /etc/hostname.* file we assume it contains
# the old style address which results in the interface being brought up and the
# netmask and broadcast address being set.
#
# If there are multiple lines we assume the file contains a list of commands to
# ifconfig with neither the implied bringing up of the interface nor the setting
# of the default netmask and broadcast address.
#
interface_names="`echo /etc/hostname.*[0-9] 2>/dev/null`"
if [ "$interface_names" != "/etc/hostname.*[0-9]" ]; then
(
	#
	# If /etc/hostname.lo0 exists the loop below will do
	# additional configuration of lo0.
	#
	echo "configuring IPv4 interfaces:\c"
	ORIGIFS="$IFS"
	IFS="$IFS."
	set -- $interface_names
	IFS="$ORIGIFS"
	while [ $# -ge 2 ]; do
		shift
		if [ "$1" = "ip" -o "$1" = "ip6" ]; then
			#
			# Filenames of the form /etc/hostname.ip.* and
			# /etc/hostname.ip6.* represent tunneling interfaces
			# (see tun(7M));  these are handled later by
			# /etc/init.d/inetinit.
			#
			shift
			while [ $# -gt 1 -a "$1" != "/etc/hostname" ]; do
				shift
			done
			continue
		fi
		if [ "$1" = "xx0" ]; then
			#
			# For some unknown historical reason the xx0
			# ifname is ignored.
			#
			shift
			continue
		fi
		/sbin/ifconfig $1 plumb

		#
		# Redirecting hostname.$1 as input results in a sub-shell being
		# used, hence this outer loop surrounding the "multiple_lines"
		# and "ifcmds" variables.
		#
		while :; do
			multiple_lines=false
			ifcmds=""

			while read line; do
				if [ -n "$ifcmds" ]; then
					#
					# This ifconfig handles the first N-1
					# lines of a N-line hostname.$1 file.
					#
					/sbin/ifconfig $1 inet $ifcmds \
					    2>&1 >/dev/null
					multiple_lines=true
				fi
				ifcmds="$line"
			done

			#
			# If the hostname.$1 file is empty or consists of only
			# blank lines, break out of the outer loop without
			# configuring the newly plumbed interface.
			#
			[ -z "$ifcmds" ] && break
			if [ $multiple_lines = false ]; then
				# The traditional single-line hostname.$1 file.
				ifcmds="$ifcmds netmask + broadcast + up"
			fi

			#
			# This ifconfig handles either the single-line case or
			# the last line of the N-line case.
			#
			/sbin/ifconfig $1 inet $ifcmds 2>&1 >/dev/null
			break
		done </etc/hostname.$1
		echo " $1\c"
		shift
	done
	echo "."
)
fi

#
# Get the list of IPv6 interfaces to configure by breaking
# /etc/hostname6.* into separate args by using "." as a shell separator
# character, then step through args and ifconfig every other arg.
# This also sets up the streams plumbing for the interface.
# With an empty /etc/hostname6.* file this only sets up the streams plumbing
# and the link-local IPv6 address. in.ndpd will acquire additional addresses
# later.
#
# If there is at least one IPv6 interface configure the IPv6 loopback.
#
interface_names="`echo /etc/hostname6.*[0-9] 2>/dev/null`"
if [ "$interface_names" != "/etc/hostname6.*[0-9]" ]; then
(
	#
	# Configure the software loopback driver.
	#
	# If /etc/hostname6.lo0 exists the loop below will do
	# additional configuration of lo0.
	#
	/sbin/ifconfig lo0 inet6 plumb ::1 up 2>&1 >/dev/null

	echo "configuring IPv6 interfaces:\c"
	ORIGIFS="$IFS"
	IFS="$IFS."
	set -- $interface_names
	IFS="$ORIGIFS"
	while [ $# -ge 2 ]; do
		shift
		if [ "$1" = "ip" -o "$1" = "ip6" ]; then
			#
			# Filenames of the form /etc/hostname.ip.* and
			# /etc/hostname.ip6.* represent tunneling interfaces
			# (see tun(7M));  these are handled later by
			# /etc/init.d/inetinit.
			#
			shift
			while [ $# -gt 1 -a "$1" != "/etc/hostname6" ]; do
				shift
			done
			continue
		fi
		/sbin/ifconfig $1 inet6 plumb up
		while read ifcmds; do
			if [ -n "$ifcmds" ]; then
				/sbin/ifconfig $1 inet6 $ifcmds 2>&1 >/dev/null
			fi
		done </etc/hostname6\.$1
		echo " $1\c"
		shift
	done
	echo "."
)
fi

# Run DHCP if requested. Skip boot-configured interface.
interface_names="`echo /etc/dhcp.*[0-9] 2>/dev/null`"
if [ "$interface_names" != '/etc/dhcp.*[0-9]' ]; then
	#
	# First find the primary interface. Default to the first
	# interface if not specified. First primary interface found
	# "wins". Use care not to "reconfigure" a net-booted interface
	# configured using DHCP. Run through the list of interfaces
	# again, this time trying DHCP.
	#
	firstif=
	primary=
	ORIGIFS="$IFS"
	IFS="${IFS}."
	set -- $interface_names

	while [ $# -ge 2 ]; do
		shift
		[ -z "$firstif" ] && firstif=$1

		for i in `shcat /etc/dhcp\.$1`; do
			if [ "$i" = primary ]; then
				primary=$1
				break
			fi
		done

		[ -n "$primary" ] && break
		shift
	done

	[ -z "$primary" ] && primary="$firstif"
	cmdline=`shcat /etc/dhcp\.${primary}`

	if [ "$_INIT_NET_IF" != "$primary" ]; then
		echo "starting DHCP on primary interface $primary"
		/sbin/ifconfig $primary auto-dhcp primary $cmdline

		#
		# diskfull machine which uses dhcp. Set strategy for
		# the use of the rest of this run level.
		#
		_INIT_NET_STRATEGY="dhcp"
		export _INIT_NET_STRATEGY
	fi

	set -- $interface_names

	while [ $# -ge 2 ]; do
		shift
		cmdline=`shcat /etc/dhcp\.$1`
		if [ "$1" != "$primary" -a \
		    "$1" != "$_INIT_NET_IF"  ]; then
			echo "starting DHCP on interface $1"
			/sbin/ifconfig $1 dhcp start wait 0 $cmdline
		fi
		shift
	done
	IFS="$ORIGIFS"
	unset ORIGIFS
fi

# Configure the rest of the IPv4 interfaces automatically, quietly.
if [ "$_INIT_NET_STRATEGY" = "rarp" ]; then
	/sbin/ifconfig -adD4 auto-revarp netmask + broadcast + up \
	    2>&1 >/dev/null
fi

#
# If DHCP was used on a primary interface then set the hostname
# that was returned. If no hostname was returned, set the name
# to be "unknown". The hostname must be set to something, because
# tooltalk will hang unless the name can be locally resolved.
# Sendmail also requires the name to be resolvable locally.
# Later, in inetsvc, we create a name "unknown" and create a entry
# in the local /etc/inet/hosts file pairing "unknown" with the IP
# address assigned by DHCP.
#
case "$_INIT_NET_STRATEGY" in
	"dhcp") hostname=`/sbin/dhcpinfo Hostname` ;;
	"rarp") hostname=`/sbin/hostconfig -h -p bootparams`
		trap 'intr=1' 2 3
		while [ -z "$hostname" -a ! -f /etc/.UNCONFIGURED -a \
		    -z "$intr" ]; do
			echo "re-trying host configuration..."
			# Restrict this to IPv4 interfaces.
			/sbin/ifconfig -adD4 auto-revarp up 2>&1 >/dev/null
			hostname=`/sbin/hostconfig -h -p bootparams 2>&1 \
			    >/dev/null`
		done
		trap 2 3 ;;
	"none") hostname="`shcat /etc/nodename 2>/dev/null`" ;;
esac

#
# If the netstrategy was unsuccessful or we haven't got a locally configured
# name, default to "unknown"
#
if [ -z "$hostname" ]; then
	hostname="unknown"
fi

/sbin/uname -S $hostname
echo "Hostname: `/sbin/uname -n`" >&2

# Reset the library path now that we are past the critical stage
LD_LIBRARY_PATH=; export LD_LIBRARY_PATH
