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