1#!/sbin/sh
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License, Version 1.0 only
7# (the "License").  You may not use this file except in compliance
8# with the License.
9#
10# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11# or http://www.opensolaris.org/os/licensing.
12# See the License for the specific language governing permissions
13# and limitations under the License.
14#
15# When distributing Covered Code, include this CDDL HEADER in each
16# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17# If applicable, add the following below this CDDL HEADER, with the
18# fields enclosed by brackets "[]" replaced with your own identifying
19# information: Portions Copyright [yyyy] [name of copyright owner]
20#
21# CDDL HEADER END
22#
23#
24# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27# Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T.
28# All rights reserved.
29#
30#
31# ident	"%Z%%M%	%I%	%E% SMI"
32
33#
34# In a zone we need this service to be up, but all of the work
35# it tries to do is irrelevant (and will actually lead to the service
36# failing if we try to do it), so just bail out.
37#
38if [ `/sbin/zonename` != "global" ]; then
39	exit 0
40fi
41
42. /lib/svc/share/smf_include.sh
43. /lib/svc/share/net_include.sh
44
45# Print warnings to console
46warn_failed_ifs() {
47	echo "Failed to $1 interface(s): $2" >/dev/msglog
48}
49
50# Make sure that the libraries essential to this stage of booting can be found.
51LD_LIBRARY_PATH=/lib; export LD_LIBRARY_PATH
52
53#
54# Cause ifconfig to not automatically start in.mpathd when IPMP groups are
55# configured.  This is not strictly necessary but makes it so that in.mpathd
56# will always be started explicitly from /etc/init.d/inetinit, when we're
57# sure that /usr is mounted.
58#
59SUNW_NO_MPATHD=; export SUNW_NO_MPATHD
60
61smf_netstrategy
62
63#
64# If the system was net booted by DHCP, hand DHCP management off to the
65# DHCP agent (ifconfig communicates to the DHCP agent through the
66# loopback interface).
67#
68if [ -n "$_INIT_NET_IF" -a "$_INIT_NET_STRATEGY" = "dhcp" ]; then
69	/sbin/dhcpagent -a
70fi
71
72#
73# The network initialization is done early to support diskless and
74# dataless configurations.  For IPv4 interfaces that were configured by
75# the kernel (e.g.  those on diskless machines) and not configured by
76# DHCP, reset the netmask using the local "/etc/netmasks" file if one
77# exists, and then reset the broadcast address based on the netmask.
78#
79/sbin/ifconfig -auD4 netmask + broadcast +
80
81#
82# All the IPv4 and IPv6 interfaces are plumbed before doing any
83# interface configuration.  This prevents errors from plumb failures
84# getting mixed in with the configured interface lists that the script
85# outputs.
86#
87
88#
89# Get the list of IPv4 interfaces to configure by breaking
90# /etc/hostname.* into separate args by using "." as a shell separator
91# character.
92#
93interface_names="`echo /etc/hostname.*[0-9] 2>/dev/null`"
94if [ "$interface_names" != "/etc/hostname.*[0-9]" ]; then
95	ORIGIFS="$IFS"
96	IFS="$IFS."
97	set -- $interface_names
98	IFS="$ORIGIFS"
99	while [ $# -ge 2 ]; do
100		shift
101		if [ "$1" = "xx0" ]; then
102			#
103			# For some unknown historical reason the xx0
104			# ifname is ignored.
105			#
106			shift
107			continue
108		fi
109		if [ $# -gt 1 -a "$2" != "/etc/hostname" ]; then
110			while [ $# -gt 1 -a "$1" != "/etc/hostname" ]; do
111				shift
112			done
113		else
114			inet_list="$inet_list $1"
115			shift
116		fi
117	done
118fi
119
120#
121# Get the list of IPv6 interfaces to configure by breaking
122# /etc/hostname6.* into separate args by using "." as a shell separator
123# character.
124#
125interface_names="`echo /etc/hostname6.*[0-9] 2>/dev/null`"
126if [ "$interface_names" != "/etc/hostname6.*[0-9]" ]; then
127	ORIGIFS="$IFS"
128	IFS="$IFS."
129	set -- $interface_names
130	IFS="$ORIGIFS"
131	while [ $# -ge 2 ]; do
132		shift
133		if [ $# -gt 1 -a "$2" != "/etc/hostname6" ]; then
134			while [ $# -gt 1 -a "$1" != "/etc/hostname6" ]; do
135				shift
136			done
137		else
138			inet6_list="$inet6_list $1"
139			shift
140		fi
141	done
142fi
143
144
145#
146# Step through the IPv4 interface list and try to plumb every interface.
147# Generate list of plumbed and failed IPv4 interfaces.
148#
149if [ -n "$inet_list" ]; then
150	set -- $inet_list
151	while [ $# -gt 0 ]; do
152		/sbin/ifconfig $1 plumb
153		if /sbin/ifconfig $1 inet >/dev/null 2>&1; then
154			inet_plumbed="$inet_plumbed $1"
155		else
156			inet_failed="$inet_failed $1"
157		fi
158		shift
159	done
160	[ -n "$inet_failed" ] && warn_failed_ifs "plumb IPv4" $inet_failed
161fi
162
163#
164# Step through the IPv6 interface list and plumb every interface.
165# Generate list of plumbed and failed IPv6 interfaces.  Each plumbed
166# interface will be brought up later, after processing any contents of
167# the /etc/hostname6.* file.
168#
169if [ -n "$inet6_list" ]; then
170	set -- $inet6_list
171	while [ $# -gt 0 ]; do
172		/sbin/ifconfig $1 inet6 plumb
173		if /sbin/ifconfig $1 inet6 >/dev/null 2>&1; then
174			inet6_plumbed="$inet6_plumbed $1"
175		else
176			inet6_failed="$inet6_failed $1"
177		fi
178		shift
179	done
180	[ -n "$inet6_failed" ] && warn_failed_ifs "plumb IPv6" $inet6_failed
181fi
182
183#
184# Process the /etc/hostname.* files of plumbed IPv4 interfaces.  If an
185# /etc/hostname file is not present or is empty, the ifconfig auto-dhcp
186# / auto-revarp command will attempt to set the address, later.
187#
188# If /etc/hostname.lo0 exists the loop below will do additional
189# configuration of lo0.
190#
191if [ -n "$inet_plumbed" ]; then
192	i4s_fail=
193	echo "configuring IPv4 interfaces:\c"
194	set -- $inet_plumbed
195	while [ $# -gt 0 ]; do
196		inet_process_hostname /sbin/ifconfig $1 inet \
197		    </etc/hostname.$1 >/dev/null
198		[ $? != 0 ] && i4s_fail="$i4s_fail $1"
199		echo " $1\c"
200		shift
201	done
202	echo "."
203	[ -n "$i4s_fail" ] && warn_failed_ifs "configure IPv4" $i4s_fail
204fi
205
206#
207# Process the /etc/hostname6.* files of plumbed IPv6 interfaces.  After
208# processing the hostname6 file, bring the interface up.  If
209# /etc/hostname6.lo0 exists the loop below will do additional
210# configuration of lo0.
211#
212if [ -n "$inet6_plumbed" ]; then
213	i6_fail=
214	echo "configuring IPv6 interfaces:\c"
215	set -- $inet6_plumbed
216	while [ $# -gt 0 ]; do
217		inet6_process_hostname /sbin/ifconfig $1 inet6 \
218		    </etc/hostname6.$1 >/dev/null &&
219		    /sbin/ifconfig $1 inet6 up
220		[ $? != 0 ] && i6_fail="$i6_fail $1"
221		echo " $1\c"
222		shift
223	done
224	echo "."
225	[ -n "$i6_fail" ] && warn_failed_ifs "configure IPv6" $i6_fail
226fi
227
228# Run DHCP if requested. Skip boot-configured interface.
229interface_names="`echo /etc/dhcp.*[0-9] 2>/dev/null`"
230if [ "$interface_names" != '/etc/dhcp.*[0-9]' ]; then
231	#
232	# First find the primary interface. Default to the first
233	# interface if not specified. First primary interface found
234	# "wins". Use care not to "reconfigure" a net-booted interface
235	# configured using DHCP. Run through the list of interfaces
236	# again, this time trying DHCP.
237	#
238	i4d_fail=
239	firstif=
240	primary=
241	ORIGIFS="$IFS"
242	IFS="${IFS}."
243	set -- $interface_names
244
245	while [ $# -ge 2 ]; do
246		shift
247		[ -z "$firstif" ] && firstif=$1
248
249		for i in `shcat /etc/dhcp\.$1`; do
250			if [ "$i" = primary ]; then
251				primary=$1
252				break
253			fi
254		done
255
256		[ -n "$primary" ] && break
257		shift
258	done
259
260	[ -z "$primary" ] && primary="$firstif"
261	cmdline=`shcat /etc/dhcp\.${primary}`
262
263	if [ "$_INIT_NET_IF" != "$primary" ]; then
264		echo "starting DHCP on primary interface $primary"
265		/sbin/ifconfig $primary auto-dhcp primary $cmdline
266		# Exit code 4 means ifconfig timed out waiting for dhcpagent
267		[ $? != 0 ]  && [ $? != 4 ] && i4d_fail="$i4d_fail $primary"
268	fi
269
270	set -- $interface_names
271
272	while [ $# -ge 2 ]; do
273		shift
274		cmdline=`shcat /etc/dhcp\.$1`
275		if [ "$1" != "$primary" -a \
276			"$1" != "$_INIT_NET_IF"  ]; then
277			echo "starting DHCP on interface $1"
278			/sbin/ifconfig $1 dhcp start wait 0 $cmdline
279			# Exit code can't be timeout when wait is 0
280			[ $? != 0 ] && i4d_fail="$i4d_fail $1"
281		fi
282		shift
283	done
284	IFS="$ORIGIFS"
285	unset ORIGIFS
286	[ -n "$i4d_fail" ] && warn_failed_ifs "configure IPv4 DHCP" $i4d_fail
287fi
288
289# Configure the rest of the IPv4 interfaces automatically, quietly.
290/sbin/ifconfig -adD4 auto-revarp netmask + broadcast + up
291
292#
293# Process IPv4 and IPv6 interfaces that failed to plumb.  Find an
294# alternative interface to host the addresses.
295#
296[ -n "$inet_failed" ] && move_addresses inet
297
298[ -n "$inet6_failed" ] && move_addresses inet6
299
300#
301# If the /etc/defaultrouter file exists, process it now so that the next
302# stage of booting will have access to NFS.
303#
304if [ -f /etc/defaultrouter ]; then
305	while read router rubbish; do
306		case "$router" in
307			'#'* | '') ;;	#  Ignore comments, empty lines
308			*)	/sbin/route -n add default -gateway $router ;;
309		esac
310	done </etc/defaultrouter
311fi
312
313#
314# We tell smf this service is online if any of the following is true:
315# - no interfaces were configured for plumbing and no DHCP failures
316# - any non-loopback IPv4 interfaces are up and have a non-zero address
317# - there are any DHCP interfaces started
318# - any non-loopback IPv6 interfaces are up
319#
320# If we weren't asked to configure any interfaces, exit
321if [ -z "$inet_list" ] && [ -z "$inet6_list" ]; then
322	# Config error if DHCP was attempted without plumbed interfaces
323	[ -n "$i4d_fail" ] && exit $SMF_EXIT_ERR_CONFIG
324	exit $SMF_EXIT_OK
325fi
326
327# Any non-loopback IPv4 interfaces with usable addresses up?
328if [ -n "`/sbin/ifconfig -a4u`" ]; then
329    	/sbin/ifconfig -a4u | while read intf addr rest; do
330		[ $intf = inet ] && [ $addr != 127.0.0.1 ] &&
331		[ $addr != 0.0.0.0 ] && exit 0
332	done && exit $SMF_EXIT_OK
333fi
334
335# Any DHCP interfaces started?
336[ -n "`/sbin/ifconfig -a4 dhcp status 2>/dev/null`" ] && exit $SMF_EXIT_OK
337
338# Any non-loopback IPv6 interfaces up?
339if [ -n "`/sbin/ifconfig -au6`" ]; then
340	/sbin/ifconfig -au6 | while read intf addr rest; do
341		[ $intf = inet6 ] && [ $addr != ::1/128 ] && exit 0
342	done && exit $SMF_EXIT_OK
343fi
344
345# This service was supposed to configure something yet didn't.  Exit
346# with config error.
347exit $SMF_EXIT_ERR_CONFIG
348