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# Run autoconf to connect to a WLAN if the interface is a wireless one
169if [ -x /sbin/wificonfig -a -n "$inet_plumbed" ]; then
170	set -- $inet_plumbed
171	while [ $# -gt 0 ]; do
172			if [ -r /dev/wifi/$1 ]; then
173				/sbin/wificonfig -i $1 startconf >/dev/null
174			fi
175		shift
176	done
177fi
178
179#
180# Step through the IPv6 interface list and plumb every interface.
181# Generate list of plumbed and failed IPv6 interfaces.  Each plumbed
182# interface will be brought up later, after processing any contents of
183# the /etc/hostname6.* file.
184#
185if [ -n "$inet6_list" ]; then
186	set -- $inet6_list
187	while [ $# -gt 0 ]; do
188		/sbin/ifconfig $1 inet6 plumb
189		if /sbin/ifconfig $1 inet6 >/dev/null 2>&1; then
190			inet6_plumbed="$inet6_plumbed $1"
191		else
192			inet6_failed="$inet6_failed $1"
193		fi
194		shift
195	done
196	[ -n "$inet6_failed" ] && warn_failed_ifs "plumb IPv6" $inet6_failed
197fi
198
199#
200# Process the /etc/hostname.* files of plumbed IPv4 interfaces.  If an
201# /etc/hostname file is not present or is empty, the ifconfig auto-dhcp
202# / auto-revarp command will attempt to set the address, later.
203#
204# If /etc/hostname.lo0 exists the loop below will do additional
205# configuration of lo0.
206#
207if [ -n "$inet_plumbed" ]; then
208	i4s_fail=
209	echo "configuring IPv4 interfaces:\c"
210	set -- $inet_plumbed
211	while [ $# -gt 0 ]; do
212		inet_process_hostname /sbin/ifconfig $1 inet \
213		    </etc/hostname.$1 >/dev/null
214		[ $? != 0 ] && i4s_fail="$i4s_fail $1"
215		echo " $1\c"
216		shift
217	done
218	echo "."
219	[ -n "$i4s_fail" ] && warn_failed_ifs "configure IPv4" $i4s_fail
220fi
221
222#
223# Process the /etc/hostname6.* files of plumbed IPv6 interfaces.  After
224# processing the hostname6 file, bring the interface up.  If
225# /etc/hostname6.lo0 exists the loop below will do additional
226# configuration of lo0.
227#
228if [ -n "$inet6_plumbed" ]; then
229	i6_fail=
230	echo "configuring IPv6 interfaces:\c"
231	set -- $inet6_plumbed
232	while [ $# -gt 0 ]; do
233		inet6_process_hostname /sbin/ifconfig $1 inet6 \
234		    </etc/hostname6.$1 >/dev/null &&
235		    /sbin/ifconfig $1 inet6 up
236		[ $? != 0 ] && i6_fail="$i6_fail $1"
237		echo " $1\c"
238		shift
239	done
240	echo "."
241	[ -n "$i6_fail" ] && warn_failed_ifs "configure IPv6" $i6_fail
242fi
243
244# Run DHCP if requested. Skip boot-configured interface.
245interface_names="`echo /etc/dhcp.*[0-9] 2>/dev/null`"
246if [ "$interface_names" != '/etc/dhcp.*[0-9]' ]; then
247	#
248	# First find the primary interface. Default to the first
249	# interface if not specified. First primary interface found
250	# "wins". Use care not to "reconfigure" a net-booted interface
251	# configured using DHCP. Run through the list of interfaces
252	# again, this time trying DHCP.
253	#
254	i4d_fail=
255	firstif=
256	primary=
257	ORIGIFS="$IFS"
258	IFS="${IFS}."
259	set -- $interface_names
260
261	while [ $# -ge 2 ]; do
262		shift
263		[ -z "$firstif" ] && firstif=$1
264
265		for i in `shcat /etc/dhcp\.$1`; do
266			if [ "$i" = primary ]; then
267				primary=$1
268				break
269			fi
270		done
271
272		[ -n "$primary" ] && break
273		shift
274	done
275
276	[ -z "$primary" ] && primary="$firstif"
277	cmdline=`shcat /etc/dhcp\.${primary}`
278
279	if [ "$_INIT_NET_IF" != "$primary" ]; then
280		echo "starting DHCP on primary interface $primary"
281		/sbin/ifconfig $primary auto-dhcp primary $cmdline
282		# Exit code 4 means ifconfig timed out waiting for dhcpagent
283		[ $? != 0 ]  && [ $? != 4 ] && i4d_fail="$i4d_fail $primary"
284	fi
285
286	set -- $interface_names
287
288	while [ $# -ge 2 ]; do
289		shift
290		cmdline=`shcat /etc/dhcp\.$1`
291		if [ "$1" != "$primary" -a \
292			"$1" != "$_INIT_NET_IF"  ]; then
293			echo "starting DHCP on interface $1"
294			/sbin/ifconfig $1 dhcp start wait 0 $cmdline
295			# Exit code can't be timeout when wait is 0
296			[ $? != 0 ] && i4d_fail="$i4d_fail $1"
297		fi
298		shift
299	done
300	IFS="$ORIGIFS"
301	unset ORIGIFS
302	[ -n "$i4d_fail" ] && warn_failed_ifs "configure IPv4 DHCP" $i4d_fail
303fi
304
305# In order to avoid bringing up the interfaces that have
306# intentionally been left down, perform RARP only if the system
307# has no configured hostname in /etc/nodename
308hostname="`shcat /etc/nodename 2>/dev/null`"
309if [ "$_INIT_NET_STRATEGY" = "rarp" -o -z "$hostname" ]; then
310	/sbin/ifconfig -adD4 auto-revarp netmask + broadcast + up
311fi
312
313#
314# Process IPv4 and IPv6 interfaces that failed to plumb.  Find an
315# alternative interface to host the addresses.
316#
317[ -n "$inet_failed" ] && move_addresses inet
318
319[ -n "$inet6_failed" ] && move_addresses inet6
320
321#
322# If the /etc/defaultrouter file exists, process it now so that the next
323# stage of booting will have access to NFS.
324#
325if [ -f /etc/defaultrouter ]; then
326	while read router rubbish; do
327		case "$router" in
328			'#'* | '') ;;	#  Ignore comments, empty lines
329			*)	/sbin/route -n add default -gateway $router ;;
330		esac
331	done </etc/defaultrouter
332fi
333
334#
335# We tell smf this service is online if any of the following is true:
336# - no interfaces were configured for plumbing and no DHCP failures
337# - any non-loopback IPv4 interfaces are up and have a non-zero address
338# - there are any DHCP interfaces started
339# - any non-loopback IPv6 interfaces are up
340#
341# If we weren't asked to configure any interfaces, exit
342if [ -z "$inet_list" ] && [ -z "$inet6_list" ]; then
343	# Config error if DHCP was attempted without plumbed interfaces
344	[ -n "$i4d_fail" ] && exit $SMF_EXIT_ERR_CONFIG
345	exit $SMF_EXIT_OK
346fi
347
348# Any non-loopback IPv4 interfaces with usable addresses up?
349if [ -n "`/sbin/ifconfig -a4u`" ]; then
350    	/sbin/ifconfig -a4u | while read intf addr rest; do
351		[ $intf = inet ] && [ $addr != 127.0.0.1 ] &&
352		[ $addr != 0.0.0.0 ] && exit 0
353	done && exit $SMF_EXIT_OK
354fi
355
356# Any DHCP interfaces started?
357[ -n "`/sbin/ifconfig -a4 dhcp status 2>/dev/null`" ] && exit $SMF_EXIT_OK
358
359# Any non-loopback IPv6 interfaces up?
360if [ -n "`/sbin/ifconfig -au6`" ]; then
361	/sbin/ifconfig -au6 | while read intf addr rest; do
362		[ $intf = inet6 ] && [ $addr != ::1/128 ] && exit 0
363	done && exit $SMF_EXIT_OK
364fi
365
366# This service was supposed to configure something yet didn't.  Exit
367# with config error.
368exit $SMF_EXIT_ERR_CONFIG
369