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