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 2007 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 shared-IP 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# In the global zone and exclusive-IP zones we proceed.
40#
41smf_configure_ip || exit $SMF_EXIT_OK
42
43# Print warnings to console
44warn_failed_ifs() {
45	echo "Failed to $1 interface(s): $2" >/dev/msglog
46}
47
48# Make sure that the libraries essential to this stage of booting can be found.
49LD_LIBRARY_PATH=/lib; export LD_LIBRARY_PATH
50
51#
52# Cause ifconfig to not automatically start in.mpathd when IPMP groups are
53# configured.  This is not strictly necessary but makes it so that in.mpathd
54# will always be started explicitly from /etc/init.d/inetinit, when we're
55# sure that /usr is mounted.
56#
57SUNW_NO_MPATHD=; export SUNW_NO_MPATHD
58
59smf_netstrategy
60
61if smf_is_globalzone; then
62	#
63	# Bring up link aggregations and initialize security objects.
64	# Note that link property initialization is deferred until after
65	# IP interfaces are plumbed to ensure that the links will not
66	# be unloaded (and the property settings lost).
67	#
68	/sbin/dladm up-aggr
69	/sbin/dladm init-secobj
70fi
71
72#
73# If the system was net booted by DHCP, hand DHCP management off to the
74# DHCP agent (ifconfig communicates to the DHCP agent through the
75# loopback interface).
76#
77if [ -n "$_INIT_NET_IF" -a "$_INIT_NET_STRATEGY" = "dhcp" ]; then
78	/sbin/dhcpagent -a
79fi
80
81#
82# The network initialization is done early to support diskless and
83# dataless configurations.  For IPv4 interfaces that were configured by
84# the kernel (e.g.  those on diskless machines) and not configured by
85# DHCP, reset the netmask using the local "/etc/netmasks" file if one
86# exists, and then reset the broadcast address based on the netmask.
87#
88/sbin/ifconfig -auD4 netmask + broadcast +
89
90#
91# All the IPv4 and IPv6 interfaces are plumbed before doing any
92# interface configuration.  This prevents errors from plumb failures
93# getting mixed in with the configured interface lists that the script
94# outputs.
95#
96
97#
98# Get the list of IPv4 interfaces to configure by breaking
99# /etc/hostname.* into separate args by using "." as a shell separator
100# character.
101#
102interface_names="`echo /etc/hostname.*[0-9] 2>/dev/null`"
103if [ "$interface_names" != "/etc/hostname.*[0-9]" ]; then
104	ORIGIFS="$IFS"
105	IFS="$IFS."
106	set -- $interface_names
107	IFS="$ORIGIFS"
108	while [ $# -ge 2 ]; do
109		shift
110		if [ $# -gt 1 -a "$2" != "/etc/hostname" ]; then
111			while [ $# -gt 1 -a "$1" != "/etc/hostname" ]; do
112				shift
113			done
114		else
115			inet_list="$inet_list $1"
116			shift
117		fi
118	done
119fi
120
121#
122# Get the list of IPv6 interfaces to configure by breaking
123# /etc/hostname6.* into separate args by using "." as a shell separator
124# character.
125#
126interface_names="`echo /etc/hostname6.*[0-9] 2>/dev/null`"
127if [ "$interface_names" != "/etc/hostname6.*[0-9]" ]; then
128	ORIGIFS="$IFS"
129	IFS="$IFS."
130	set -- $interface_names
131	IFS="$ORIGIFS"
132	while [ $# -ge 2 ]; do
133		shift
134		if [ $# -gt 1 -a "$2" != "/etc/hostname6" ]; then
135			while [ $# -gt 1 -a "$1" != "/etc/hostname6" ]; do
136				shift
137			done
138		else
139			inet6_list="$inet6_list $1"
140			shift
141		fi
142	done
143fi
144
145
146#
147# Step through the IPv4 interface list and try to plumb every interface.
148# Generate list of plumbed and failed IPv4 interfaces.
149#
150if [ -n "$inet_list" ]; then
151	set -- $inet_list
152	while [ $# -gt 0 ]; do
153		/sbin/ifconfig $1 plumb
154		if /sbin/ifconfig $1 inet >/dev/null 2>&1; then
155			inet_plumbed="$inet_plumbed $1"
156		else
157			inet_failed="$inet_failed $1"
158		fi
159		shift
160	done
161	[ -n "$inet_failed" ] && warn_failed_ifs "plumb IPv4" $inet_failed
162fi
163
164# Run autoconf to connect to a WLAN if the interface is a wireless one
165if [ -x /sbin/wificonfig -a -n "$inet_plumbed" ]; then
166	set -- $inet_plumbed
167	while [ $# -gt 0 ]; do
168			if [ -r /dev/wifi/$1 ]; then
169				/sbin/wificonfig -i $1 startconf >/dev/null
170			fi
171		shift
172	done
173fi
174
175#
176# Step through the IPv6 interface list and plumb every interface.
177# Generate list of plumbed and failed IPv6 interfaces.  Each plumbed
178# interface will be brought up later, after processing any contents of
179# the /etc/hostname6.* file.
180#
181if [ -n "$inet6_list" ]; then
182	set -- $inet6_list
183	while [ $# -gt 0 ]; do
184		/sbin/ifconfig $1 inet6 plumb
185		if /sbin/ifconfig $1 inet6 >/dev/null 2>&1; then
186			inet6_plumbed="$inet6_plumbed $1"
187		else
188			inet6_failed="$inet6_failed $1"
189		fi
190		shift
191	done
192	[ -n "$inet6_failed" ] && warn_failed_ifs "plumb IPv6" $inet6_failed
193fi
194
195if smf_is_globalzone; then
196	#
197	# Unfortunately, if a driver unloads and then is subsequently reloaded,
198	# no mechanism currently exists to restore the properties of its
199	# associated links.  Hence, we wait until after interfaces have been
200	# plumbed (above) to initialize link properties.
201	#
202	/sbin/dladm init-linkprop
203fi
204
205#
206# Process the /etc/hostname.* files of plumbed IPv4 interfaces.  If an
207# /etc/hostname file is not present or is empty, the ifconfig auto-dhcp
208# / auto-revarp command will attempt to set the address, later.
209#
210# If /etc/hostname.lo0 exists the loop below will do additional
211# configuration of lo0.
212#
213if [ -n "$inet_plumbed" ]; then
214	i4s_fail=
215	echo "configuring IPv4 interfaces:\c"
216	set -- $inet_plumbed
217	while [ $# -gt 0 ]; do
218		inet_process_hostname /sbin/ifconfig $1 inet \
219		    </etc/hostname.$1 >/dev/null
220		[ $? != 0 ] && i4s_fail="$i4s_fail $1"
221		echo " $1\c"
222		shift
223	done
224	echo "."
225	[ -n "$i4s_fail" ] && warn_failed_ifs "configure IPv4" $i4s_fail
226fi
227
228#
229# Process the /etc/hostname6.* files of plumbed IPv6 interfaces.  After
230# processing the hostname6 file, bring the interface up.  If
231# /etc/hostname6.lo0 exists the loop below will do additional
232# configuration of lo0.
233#
234if [ -n "$inet6_plumbed" ]; then
235	i6_fail=
236	echo "configuring IPv6 interfaces:\c"
237	set -- $inet6_plumbed
238	while [ $# -gt 0 ]; do
239		inet6_process_hostname /sbin/ifconfig $1 inet6 \
240		    </etc/hostname6.$1 >/dev/null &&
241		    /sbin/ifconfig $1 inet6 up
242		[ $? != 0 ] && i6_fail="$i6_fail $1"
243		echo " $1\c"
244		shift
245	done
246	echo "."
247	[ -n "$i6_fail" ] && warn_failed_ifs "configure IPv6" $i6_fail
248fi
249
250# Run DHCP if requested. Skip boot-configured interface.
251interface_names="`echo /etc/dhcp.*[0-9] 2>/dev/null`"
252if [ "$interface_names" != '/etc/dhcp.*[0-9]' ]; then
253	#
254	# First find the primary interface. Default to the first
255	# interface if not specified. First primary interface found
256	# "wins". Use care not to "reconfigure" a net-booted interface
257	# configured using DHCP. Run through the list of interfaces
258	# again, this time trying DHCP.
259	#
260	i4d_fail=
261	firstif=
262	primary=
263	ORIGIFS="$IFS"
264	IFS="${IFS}."
265	set -- $interface_names
266
267	while [ $# -ge 2 ]; do
268		shift
269		[ -z "$firstif" ] && firstif=$1
270
271		for i in `shcat /etc/dhcp\.$1`; do
272			if [ "$i" = primary ]; then
273				primary=$1
274				break
275			fi
276		done
277
278		[ -n "$primary" ] && break
279		shift
280	done
281
282	[ -z "$primary" ] && primary="$firstif"
283	cmdline=`shcat /etc/dhcp\.${primary}`
284
285	if [ "$_INIT_NET_IF" != "$primary" ]; then
286		echo "starting DHCP on primary interface $primary"
287		/sbin/ifconfig $primary auto-dhcp primary $cmdline
288		# Exit code 4 means ifconfig timed out waiting for dhcpagent
289		[ $? != 0 ] && [ $? != 4 ] && i4d_fail="$i4d_fail $primary"
290	fi
291
292	set -- $interface_names
293
294	while [ $# -ge 2 ]; do
295		shift
296		cmdline=`shcat /etc/dhcp\.$1`
297		if [ "$1" != "$primary" -a \
298			"$1" != "$_INIT_NET_IF"  ]; then
299			echo "starting DHCP on interface $1"
300			/sbin/ifconfig $1 dhcp start wait 0 $cmdline
301			# Exit code can't be timeout when wait is 0
302			[ $? != 0 ] && i4d_fail="$i4d_fail $1"
303		fi
304		shift
305	done
306	IFS="$ORIGIFS"
307	unset ORIGIFS
308	[ -n "$i4d_fail" ] && warn_failed_ifs "configure IPv4 DHCP" $i4d_fail
309fi
310
311# In order to avoid bringing up the interfaces that have
312# intentionally been left down, perform RARP only if the system
313# has no configured hostname in /etc/nodename
314hostname="`shcat /etc/nodename 2>/dev/null`"
315if [ "$_INIT_NET_STRATEGY" = "rarp" -o -z "$hostname" ]; then
316	/sbin/ifconfig -adD4 auto-revarp netmask + broadcast + up
317fi
318
319#
320# Process IPv4 and IPv6 interfaces that failed to plumb.  Find an
321# alternative interface to host the addresses.
322#
323[ -n "$inet_failed" ] && move_addresses inet
324
325[ -n "$inet6_failed" ] && move_addresses inet6
326
327#
328# If the /etc/defaultrouter file exists, process it now so that the next
329# stage of booting will have access to NFS.
330#
331if [ -f /etc/defaultrouter ]; then
332	while read router rubbish; do
333		case "$router" in
334			'#'* | '') ;;	#  Ignore comments, empty lines
335			*)	/sbin/route -n add default -gateway $router ;;
336		esac
337	done </etc/defaultrouter
338fi
339
340#
341# If we get here and were not asked to plumb any IPv4 interfaces, look
342# for boot properties that direct us.
343#
344# - The "network-interface" property is required and indicates the
345#   interface name.
346# - The "xpv-hcp" property, if present, is used by the hypervisor
347#   tools to indicate how the specified interface should be configured.
348#   Permitted values are "dhcp" and "off", where "off" indicates static
349#   IP configuration.
350#
351# In the case where "xpv-hcp" is set to "dhcp", no further properties
352# are required or examined.
353#
354# In the case where "xpv-hcp" is not present or set to "off", the
355# "host-ip" and "subnet-mask" properties are used to configure
356# the specified interface.  The "router-ip" property, if present,
357# is used to add a default route.
358#
359nic="`/sbin/devprop network-interface`"
360if smf_is_globalzone && [ -z "$inet_list" ] && [ -n "$nic" ]; then
361	hcp="`/sbin/devprop xpv-hcp`"
362	case "$hcp" in
363	"dhcp")
364		/sbin/ifconfig $nic plumb 2>/dev/null
365		[ -n "`/sbin/ifconfig $nic 2>/dev/null`" ] && (
366			# The interface is successfully plumbed, so
367			# modify "inet_list" to force the exit code
368			# checks to work.
369			inet_list=$nic;
370			# Given that this is the only IPv4 interface,
371			# we assert that it is primary.
372			echo "starting DHCP on primary interface $primary";
373			/sbin/ifconfig $nic auto-dhcp primary;
374			# Exit code 4 means ifconfig timed out waiting
375			# for dhcpagent
376			[ $? != 0 ] && [ $? != 4 ] && \
377			    i4d_fail="$i4d_fail $nic";
378		)
379		;;
380
381	"off"|"")
382		/sbin/devprop host-ip subnet-mask router-ip | (
383			read ip;
384			read mask;
385			read router;
386			[ -n "$ip" ] && [ -n "$mask" ] && \
387				/sbin/ifconfig $nic plumb 2>/dev/null
388			[ -n "`/sbin/ifconfig $nic 2>/dev/null`" ] && (
389				# The interface is successfully
390				# plumbed, so modify "inet_list" to
391				# force the exit code checks to work.
392				inet_list=$nic;
393				/sbin/ifconfig $nic  inet $ip \
394				    netmask $mask broadcast + up 2>/dev/null;
395				[ -n "$router" ] && route add \
396				    default $router 2>/dev/null;
397			)
398		)
399		;;
400	esac
401fi
402
403#
404# We tell smf this service is online if any of the following is true:
405# - no interfaces were configured for plumbing and no DHCP failures
406# - any non-loopback IPv4 interfaces are up and have a non-zero address
407# - there are any DHCP interfaces started
408# - any non-loopback IPv6 interfaces are up
409#
410# If we weren't asked to configure any interfaces, exit
411if [ -z "$inet_list" ] && [ -z "$inet6_list" ]; then
412	# Config error if DHCP was attempted without plumbed interfaces
413	[ -n "$i4d_fail" ] && exit $SMF_EXIT_ERR_CONFIG
414	exit $SMF_EXIT_OK
415fi
416
417# Any non-loopback IPv4 interfaces with usable addresses up?
418if [ -n "`/sbin/ifconfig -a4u`" ]; then
419    	/sbin/ifconfig -a4u | while read intf addr rest; do
420		[ $intf = inet ] && [ $addr != 127.0.0.1 ] &&
421		[ $addr != 0.0.0.0 ] && exit $SMF_EXIT_OK
422	done && exit $SMF_EXIT_OK
423fi
424
425# Any DHCP interfaces started?
426[ -n "`/sbin/ifconfig -a4 dhcp status 2>/dev/null`" ] && exit $SMF_EXIT_OK
427
428# Any non-loopback IPv6 interfaces up?
429if [ -n "`/sbin/ifconfig -au6`" ]; then
430	/sbin/ifconfig -au6 | while read intf addr rest; do
431		[ $intf = inet6 ] && [ $addr != ::1/128 ] && exit $SMF_EXIT_OK
432	done && exit $SMF_EXIT_OK
433fi
434
435# This service was supposed to configure something yet didn't.  Exit
436# with config error.
437exit $SMF_EXIT_ERR_CONFIG
438