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