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# Bring up link aggregations
65#
66/sbin/dladm up-aggr
67
68#
69# If the system was net booted by DHCP, hand DHCP management off to the
70# DHCP agent (ifconfig communicates to the DHCP agent through the
71# loopback interface).
72#
73if [ -n "$_INIT_NET_IF" -a "$_INIT_NET_STRATEGY" = "dhcp" ]; then
74	/sbin/dhcpagent -a
75fi
76
77#
78# The network initialization is done early to support diskless and
79# dataless configurations.  For IPv4 interfaces that were configured by
80# the kernel (e.g.  those on diskless machines) and not configured by
81# DHCP, reset the netmask using the local "/etc/netmasks" file if one
82# exists, and then reset the broadcast address based on the netmask.
83#
84/sbin/ifconfig -auD4 netmask + broadcast +
85
86#
87# All the IPv4 and IPv6 interfaces are plumbed before doing any
88# interface configuration.  This prevents errors from plumb failures
89# getting mixed in with the configured interface lists that the script
90# outputs.
91#
92
93#
94# Get the list of IPv4 interfaces to configure by breaking
95# /etc/hostname.* into separate args by using "." as a shell separator
96# character.
97#
98interface_names="`echo /etc/hostname.*[0-9] 2>/dev/null`"
99if [ "$interface_names" != "/etc/hostname.*[0-9]" ]; then
100	ORIGIFS="$IFS"
101	IFS="$IFS."
102	set -- $interface_names
103	IFS="$ORIGIFS"
104	while [ $# -ge 2 ]; do
105		shift
106		if [ "$1" = "xx0" ]; then
107			#
108			# For some unknown historical reason the xx0
109			# ifname is ignored.
110			#
111			shift
112			continue
113		fi
114		if [ $# -gt 1 -a "$2" != "/etc/hostname" ]; then
115			while [ $# -gt 1 -a "$1" != "/etc/hostname" ]; do
116				shift
117			done
118		else
119			inet_list="$inet_list $1"
120			shift
121		fi
122	done
123fi
124
125#
126# Get the list of IPv6 interfaces to configure by breaking
127# /etc/hostname6.* into separate args by using "." as a shell separator
128# character.
129#
130interface_names="`echo /etc/hostname6.*[0-9] 2>/dev/null`"
131if [ "$interface_names" != "/etc/hostname6.*[0-9]" ]; then
132	ORIGIFS="$IFS"
133	IFS="$IFS."
134	set -- $interface_names
135	IFS="$ORIGIFS"
136	while [ $# -ge 2 ]; do
137		shift
138		if [ $# -gt 1 -a "$2" != "/etc/hostname6" ]; then
139			while [ $# -gt 1 -a "$1" != "/etc/hostname6" ]; do
140				shift
141			done
142		else
143			inet6_list="$inet6_list $1"
144			shift
145		fi
146	done
147fi
148
149
150#
151# Step through the IPv4 interface list and try to plumb every interface.
152# Generate list of plumbed and failed IPv4 interfaces.
153#
154if [ -n "$inet_list" ]; then
155	set -- $inet_list
156	while [ $# -gt 0 ]; do
157		/sbin/ifconfig $1 plumb
158		if /sbin/ifconfig $1 inet >/dev/null 2>&1; then
159			inet_plumbed="$inet_plumbed $1"
160		else
161			inet_failed="$inet_failed $1"
162		fi
163		shift
164	done
165	[ -n "$inet_failed" ] && warn_failed_ifs "plumb IPv4" $inet_failed
166fi
167
168#
169# Step through the IPv6 interface list and plumb every interface.
170# Generate list of plumbed and failed IPv6 interfaces.  Each plumbed
171# interface will be brought up later, after processing any contents of
172# the /etc/hostname6.* file.
173#
174if [ -n "$inet6_list" ]; then
175	set -- $inet6_list
176	while [ $# -gt 0 ]; do
177		/sbin/ifconfig $1 inet6 plumb
178		if /sbin/ifconfig $1 inet6 >/dev/null 2>&1; then
179			inet6_plumbed="$inet6_plumbed $1"
180		else
181			inet6_failed="$inet6_failed $1"
182		fi
183		shift
184	done
185	[ -n "$inet6_failed" ] && warn_failed_ifs "plumb IPv6" $inet6_failed
186fi
187
188#
189# Process the /etc/hostname.* files of plumbed IPv4 interfaces.  If an
190# /etc/hostname file is not present or is empty, the ifconfig auto-dhcp
191# / auto-revarp command will attempt to set the address, later.
192#
193# If /etc/hostname.lo0 exists the loop below will do additional
194# configuration of lo0.
195#
196if [ -n "$inet_plumbed" ]; then
197	i4s_fail=
198	echo "configuring IPv4 interfaces:\c"
199	set -- $inet_plumbed
200	while [ $# -gt 0 ]; do
201		inet_process_hostname /sbin/ifconfig $1 inet \
202		    </etc/hostname.$1 >/dev/null
203		[ $? != 0 ] && i4s_fail="$i4s_fail $1"
204		echo " $1\c"
205		shift
206	done
207	echo "."
208	[ -n "$i4s_fail" ] && warn_failed_ifs "configure IPv4" $i4s_fail
209fi
210
211#
212# Process the /etc/hostname6.* files of plumbed IPv6 interfaces.  After
213# processing the hostname6 file, bring the interface up.  If
214# /etc/hostname6.lo0 exists the loop below will do additional
215# configuration of lo0.
216#
217if [ -n "$inet6_plumbed" ]; then
218	i6_fail=
219	echo "configuring IPv6 interfaces:\c"
220	set -- $inet6_plumbed
221	while [ $# -gt 0 ]; do
222		inet6_process_hostname /sbin/ifconfig $1 inet6 \
223		    </etc/hostname6.$1 >/dev/null &&
224		    /sbin/ifconfig $1 inet6 up
225		[ $? != 0 ] && i6_fail="$i6_fail $1"
226		echo " $1\c"
227		shift
228	done
229	echo "."
230	[ -n "$i6_fail" ] && warn_failed_ifs "configure IPv6" $i6_fail
231fi
232
233# Run DHCP if requested. Skip boot-configured interface.
234interface_names="`echo /etc/dhcp.*[0-9] 2>/dev/null`"
235if [ "$interface_names" != '/etc/dhcp.*[0-9]' ]; then
236	#
237	# First find the primary interface. Default to the first
238	# interface if not specified. First primary interface found
239	# "wins". Use care not to "reconfigure" a net-booted interface
240	# configured using DHCP. Run through the list of interfaces
241	# again, this time trying DHCP.
242	#
243	i4d_fail=
244	firstif=
245	primary=
246	ORIGIFS="$IFS"
247	IFS="${IFS}."
248	set -- $interface_names
249
250	while [ $# -ge 2 ]; do
251		shift
252		[ -z "$firstif" ] && firstif=$1
253
254		for i in `shcat /etc/dhcp\.$1`; do
255			if [ "$i" = primary ]; then
256				primary=$1
257				break
258			fi
259		done
260
261		[ -n "$primary" ] && break
262		shift
263	done
264
265	[ -z "$primary" ] && primary="$firstif"
266	cmdline=`shcat /etc/dhcp\.${primary}`
267
268	if [ "$_INIT_NET_IF" != "$primary" ]; then
269		echo "starting DHCP on primary interface $primary"
270		/sbin/ifconfig $primary auto-dhcp primary $cmdline
271		# Exit code 4 means ifconfig timed out waiting for dhcpagent
272		[ $? != 0 ]  && [ $? != 4 ] && i4d_fail="$i4d_fail $primary"
273	fi
274
275	set -- $interface_names
276
277	while [ $# -ge 2 ]; do
278		shift
279		cmdline=`shcat /etc/dhcp\.$1`
280		if [ "$1" != "$primary" -a \
281			"$1" != "$_INIT_NET_IF"  ]; then
282			echo "starting DHCP on interface $1"
283			/sbin/ifconfig $1 dhcp start wait 0 $cmdline
284			# Exit code can't be timeout when wait is 0
285			[ $? != 0 ] && i4d_fail="$i4d_fail $1"
286		fi
287		shift
288	done
289	IFS="$ORIGIFS"
290	unset ORIGIFS
291	[ -n "$i4d_fail" ] && warn_failed_ifs "configure IPv4 DHCP" $i4d_fail
292fi
293
294# In order to avoid bringing up the interfaces that have
295# intentionally been left down, perform RARP only if the system
296# has no configured hostname in /etc/nodename
297hostname="`shcat /etc/nodename 2>/dev/null`"
298if [ "$_INIT_NET_STRATEGY" = "rarp" -o -z "$hostname" ]; then
299	/sbin/ifconfig -adD4 auto-revarp netmask + broadcast + up
300fi
301
302#
303# Process IPv4 and IPv6 interfaces that failed to plumb.  Find an
304# alternative interface to host the addresses.
305#
306[ -n "$inet_failed" ] && move_addresses inet
307
308[ -n "$inet6_failed" ] && move_addresses inet6
309
310#
311# If the /etc/defaultrouter file exists, process it now so that the next
312# stage of booting will have access to NFS.
313#
314if [ -f /etc/defaultrouter ]; then
315	while read router rubbish; do
316		case "$router" in
317			'#'* | '') ;;	#  Ignore comments, empty lines
318			*)	/sbin/route -n add default -gateway $router ;;
319		esac
320	done </etc/defaultrouter
321fi
322
323#
324# We tell smf this service is online if any of the following is true:
325# - no interfaces were configured for plumbing and no DHCP failures
326# - any non-loopback IPv4 interfaces are up and have a non-zero address
327# - there are any DHCP interfaces started
328# - any non-loopback IPv6 interfaces are up
329#
330# If we weren't asked to configure any interfaces, exit
331if [ -z "$inet_list" ] && [ -z "$inet6_list" ]; then
332	# Config error if DHCP was attempted without plumbed interfaces
333	[ -n "$i4d_fail" ] && exit $SMF_EXIT_ERR_CONFIG
334	exit $SMF_EXIT_OK
335fi
336
337# Any non-loopback IPv4 interfaces with usable addresses up?
338if [ -n "`/sbin/ifconfig -a4u`" ]; then
339    	/sbin/ifconfig -a4u | while read intf addr rest; do
340		[ $intf = inet ] && [ $addr != 127.0.0.1 ] &&
341		[ $addr != 0.0.0.0 ] && exit 0
342	done && exit $SMF_EXIT_OK
343fi
344
345# Any DHCP interfaces started?
346[ -n "`/sbin/ifconfig -a4 dhcp status 2>/dev/null`" ] && exit $SMF_EXIT_OK
347
348# Any non-loopback IPv6 interfaces up?
349if [ -n "`/sbin/ifconfig -au6`" ]; then
350	/sbin/ifconfig -au6 | while read intf addr rest; do
351		[ $intf = inet6 ] && [ $addr != ::1/128 ] && exit 0
352	done && exit $SMF_EXIT_OK
353fi
354
355# This service was supposed to configure something yet didn't.  Exit
356# with config error.
357exit $SMF_EXIT_ERR_CONFIG
358